Skip to content

Commit

Permalink
Levenshtein
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffy-melli committed Jan 14, 2025
1 parent ed8827f commit 65bff56
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
20 changes: 10 additions & 10 deletions korcen.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func General(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand Down Expand Up @@ -532,7 +532,7 @@ func Minor(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand Down Expand Up @@ -723,7 +723,7 @@ func Sexual(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand Down Expand Up @@ -780,7 +780,7 @@ func Belittle(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand All @@ -805,7 +805,7 @@ func Race(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand Down Expand Up @@ -838,7 +838,7 @@ func Parent(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand Down Expand Up @@ -882,7 +882,7 @@ func Politics(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand All @@ -905,7 +905,7 @@ func Japanese(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand All @@ -929,7 +929,7 @@ func Chinese(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand All @@ -952,7 +952,7 @@ func Special(input string) (bool, string) {
if strings.Contains(input, item) {
return true, item
}
if Levenshtein(input, item) <= 4 {
if Levenshtein(input, item) <= 3 {
return true, item
}
}
Expand Down
32 changes: 30 additions & 2 deletions leven.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
package korcen

import (
"unicode"
)

func SplitHangul(s string) []rune {
var result []rune
for _, r := range s {
if unicode.Is(unicode.Hangul, r) {
code := r - 0xAC00
chun := code / (21 * 28)
jung := (code % (21 * 28)) / 28
jong := code % 28
if chun != 0 {
result = append(result, rune(chun+0x1100))
}
if jung != 0 {
result = append(result, rune(jung+0x1161))
}
if jong != 0 {
result = append(result, rune(jong+0x11A7))
}
} else {
result = append(result, r)
}
}
return result
}

func Levenshtein(a, b string) int {
m := len(a)
n := len(b)
m := len(SplitHangul(a))
n := len(SplitHangul(b))
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
Expand Down
5 changes: 5 additions & 0 deletions test/speed_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"fmt"
"testing"

"github.com/fluffy-melli/korcen-go"
Expand All @@ -12,3 +13,7 @@ func Benchmark0(b *testing.B) {
korcen.Check("")
}
}

func Test0(t *testing.T) {
fmt.Println(korcen.Levenshtein("안녕하세요", "안녕안해요"))
}

0 comments on commit 65bff56

Please sign in to comment.