-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathguesslanguage.go
208 lines (174 loc) · 5.16 KB
/
guesslanguage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package guesslanguage
/* Guess the natural language (idiom) of a text.
2015, Jonathan Simon Prates.
Go library to identify the natural language of a text.
Based on guesslanguage.py by Kent S Johnson (https://pypi.python.org/pypi/guess-language)
Python version: 2008, Kent S Johnson
C++ version: 2006 Jacob R Rideout <kde@jacobrideout.net>
Perl version: 2004-6 Maciej Ceglowski
This library is free software; you can redistribute it and/or
modify it under the terms of the LGPL (http://www.gnu.org/licenses/lgpl-3.0.html).
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See LICENSE file for details.
*/
import (
"errors"
"math"
"sort"
"strings"
"unicode"
"unicode/utf8"
"github.com/jonathansp/guess-language/data"
"golang.org/x/text/unicode/norm"
)
const (
minLength = 20
maxGrams = 300
)
var (
//ErrUnknownLanguage happens when Parse() wasn't able to find an idiom.
ErrUnknownLanguage = errors.New("Unknown Language")
//ErrStringTooShort happens when given string has 3 chars or less.
ErrStringTooShort = errors.New("String too short")
)
func createModel(content string) []string {
trigrams := make(map[string]int)
content = strings.ToLower(content)
size := utf8.RuneCountInString(content)
for i := 0; i <= size-2; i++ {
end := i + 3
if end > size {
end = size - 1
}
slice := string([]rune(content)[i:end])
trigrams[slice]++
}
var values []string
for key := range trigrams {
values = append(values, key)
}
// possible overflow with big strings.
sort.Strings(values)
return values
}
func distance(foundModel []string, knownModel map[string]int) float64 {
var dist float64
for i, value := range foundModel {
// must ignore samples with 2 spaces in a row.
if !strings.Contains(value, " ") {
if _, ok := knownModel[value]; ok {
dist += math.Abs(float64(i - knownModel[value]))
} else {
dist += maxGrams
}
}
}
return dist
}
func check(sample string, languageSet []string) (string, error) {
model := createModel(sample)
minDistance := math.Inf(1)
var lastLang string
for _, lang := range languageSet {
if value, ok := data.Trigrams[lang]; ok {
distance := distance(model, value)
if distance < minDistance {
minDistance = distance
lastLang = lang
}
}
}
if lastLang == "" {
return "", ErrUnknownLanguage
}
return lastLang, nil
}
func hasOne(items []string, sortedList sort.StringSlice) bool {
// change to a map for O(1) ?
for _, item := range items {
i := sort.SearchStrings(sortedList, item)
if i < len(sortedList) && sortedList[i] == item {
return true
}
}
return false
}
func identify(sample string, scripts sort.StringSlice) (string, error) {
if len(sample) < minLength {
return "", ErrStringTooShort
}
sort.Strings(scripts)
switch {
case hasOne([]string{"Hangul Syllables", "Hangul Jamo",
"Hangul Compatibility Jamo", "Hangul"}, scripts):
return "ko", nil
case hasOne([]string{"Greek and Coptic"}, scripts):
return "el", nil
case hasOne([]string{"Katakana"}, scripts):
return "ja", nil
case hasOne([]string{"Cyrillic"}, scripts):
return check(sample, data.Alphabets["Cyrillic"])
case hasOne([]string{"Arabic", "Arabic Presentation Forms-A",
"Arabic Presentation Forms-B"}, scripts):
return check(sample, data.Alphabets["Arabic"])
case hasOne([]string{"Devanagari"}, scripts):
return check(sample, data.Alphabets["Devanagari"])
case hasOne([]string{"CJK Unified Ideographs", "Bopomofo",
"Bopomofo Extended", "KangXi Radicals"}, scripts):
return "zh", nil
}
// We need to check LocalLanguage (specific idioms) before checking Latin.
for block, language := range data.LocalLanguages {
if hasOne([]string{block}, scripts) {
return language, nil
}
}
switch {
case hasOne([]string{"Latin Extended Additional"}, scripts):
return "vi", nil
case hasOne([]string{"Extended Latin"}, scripts):
return check(sample, data.Alphabets["ExtendedLatin"])
case hasOne([]string{"Basic Latin"}, scripts):
return check(sample, data.Alphabets["Latin"])
}
return "", ErrUnknownLanguage
}
func normalize(text string) string {
buffer := []byte(text)
buffer = norm.NFKC.Bytes(buffer)
text = strings.Replace(string(buffer), " ", " ", -1)
return text
}
func findRuns(text string) []string {
var runTypes = make(map[string]int)
var relevantRuns sort.StringSlice
var totalCount = 0
for _, char := range text {
if unicode.IsLetter(char) {
block := data.GetBlockFromChar(char)
runTypes[block] = +1
totalCount = +1
}
}
for key, value := range runTypes {
percent := (value * 100) / totalCount
if percent >= 40 ||
(key == "Basic Latin" && percent >= 15) ||
(key == "Latin Extended Additional" && percent >= 10) {
relevantRuns = append(relevantRuns, key)
}
}
return relevantRuns
}
// Parse function tries to identify natural language from a given string.
func Parse(text string) (data.Language, error) {
text = normalize(text)
runs := findRuns(text)
if len(runs) == 0 {
return data.Languages[""], ErrUnknownLanguage
}
isocode, err := identify(text, runs)
return data.Languages[isocode], err
}