diff --git a/korcen.go b/korcen.go index 4b9fb41..c2cb258 100644 --- a/korcen.go +++ b/korcen.go @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } diff --git a/leven.go b/leven.go index 15c96bc..096f41a 100644 --- a/leven.go +++ b/leven.go @@ -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) diff --git a/test/speed_test.go b/test/speed_test.go index 3898a71..bba4e6c 100644 --- a/test/speed_test.go +++ b/test/speed_test.go @@ -1,6 +1,7 @@ package test import ( + "fmt" "testing" "github.com/fluffy-melli/korcen-go" @@ -12,3 +13,7 @@ func Benchmark0(b *testing.B) { korcen.Check("") } } + +func Test0(t *testing.T) { + fmt.Println(korcen.Levenshtein("안녕하세요", "안녕안해요")) +}