-
Notifications
You must be signed in to change notification settings - Fork 21
/
getlang_test.go
501 lines (420 loc) · 9.55 KB
/
getlang_test.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package getlang
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestEmptyStringFromReader(t *testing.T) {
info, _ := FromReader(strings.NewReader(""))
assert.Equal(t, "und", info.LanguageCode())
}
func TestEnglishPhraseFromBigReader(t *testing.T) {
largeText := ""
for i := 0; i < 800; i++ {
largeText += "this is more language as you can see "
}
info, _ := FromReader(strings.NewReader(largeText))
assert.Equal(t, "en", info.LanguageCode())
assert.Equal(t, true, info.Confidence() > 0.999)
}
func TestEnglishPhraseFromReader(t *testing.T) {
info, _ := FromReader(strings.NewReader("this is the language"))
assert.Equal(t, "en", info.LanguageCode())
assert.Equal(t, true, info.Confidence() > 0.75)
}
func TestEnglishPhraseTag(t *testing.T) {
info, _ := FromReader(strings.NewReader("this is the language"))
tag := info.Tag()
assert.Equal(t, "en", tag.String())
assert.Equal(t, false, tag.IsRoot())
assert.Equal(t, true, tag.Parent().IsRoot())
}
func TestEnglishPhraseUSDI(t *testing.T) {
text := "We hold these truths to be self-evident, that all men are created equal"
ensureClassifiedWithConfidence(
t,
text,
"en",
0.95)
ensureClassifiedTextNamed(
t,
text,
"English",
"English")
}
func TestGermanPhraseUSDI(t *testing.T) {
text := "Wir halten diese Wahrheiten für ausgemacht, daß alle Menschen gleich erschaffen worden"
ensureClassifiedWithConfidence(
t,
text,
"de",
0.95)
ensureClassifiedTextNamed(
t,
text,
"German",
"Deutsch")
}
func TestEnglishMixedGerman(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"If you wanted to greet someone in this language, you'd say 'wie geht es'",
"en",
0.35)
}
func TestEnglishMixedUkrainian(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"the best thing to say is своїй гідності in my opinon of this.",
"en",
0.55)
}
func TestSpanishPhraseUSDI(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Sostenemos como evidentes estas verdades: que los hombres son creados iguales",
"es",
0.75)
}
func TestPortuguesePhraseUSDI(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Consideramos estas verdades como autoevidentes, que todos os homens são criados iguais",
"pt",
0.95)
}
func TestPolishPhraseUDHR(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Wszyscy ludzie rodzą się wolni i równi w swojej godności i prawach",
"pl",
0.95)
}
func TestPunjabiPhrase(t *testing.T) {
text := "ਮੇਰਾ ਨਾਮ ਭਰਤ ਹੈ."
lang := "ਪੰਜਾਬੀ"
ensureClassifiedWithConfidence(
t,
text,
"pa",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Punjabi",
lang)
}
func TestHungarianPhraseUDHR(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Minden emberi lény szabadon születik és egyenlő méltósága és joga van",
"hu",
0.95)
}
func TestItalianPhraseUDHR(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Tutti gli esseri umani nascono liberi ed eguali in dignità e diritti",
"it",
0.95)
}
func TestRussianPhraseUDHR(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Все люди рождаются свободными и равными в своем достоинстве и правах",
"ru",
0.55)
}
func TestUkrainianPhraseUDHR(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Всі люди народжуються вільними і рівними у своїй гідності та правах",
"uk",
0.80)
}
func TestFrenchPhraseUDHR(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"Tous les êtres humains naissent libres et égaux",
"fr",
0.95)
}
func TestKoreanPhrase(t *testing.T) {
ensureClassifiedWithConfidence(
t,
"원래 AB형 사람이 똑똑해",
"ko",
0.95)
}
func TestJapanesePhrase(t *testing.T) {
text := "何を食べますか"
ensureClassifiedWithConfidence(
t,
text,
"ja",
0.90)
ensureClassifiedTextNamed(
t,
text,
"Japanese",
"日本語")
}
func TestChinesePhrase(t *testing.T) {
text := "球的采编网络,记者遍布"
ensureClassifiedWithConfidence(
t,
text,
"zh",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Chinese",
"中文")
}
func TestArabicPhrase(t *testing.T) {
text := "اهتمامًا بذلك المشروع. المجموعة الوحيدة التي "
lang := "العربية"
ensureClassifiedWithConfidence(
t,
text,
"ar",
0.55)
ensureClassifiedTextNamed(
t,
text,
"Arabic",
lang)
}
func TestBanglaPhrase(t *testing.T) {
text := "এই গবেষণায় রত, তাঁদেরকে বলা হয় ভাষাবিজ্ঞানী।ভাষাবিজ্ঞানীরা নৈর্ব্যক্তিক"
lang := "বাংলা"
ensureClassifiedWithConfidence(
t,
text,
"bn",
0.85)
ensureClassifiedTextNamed(
t,
text,
"Bangla",
lang)
}
func TestHindiPhrase(t *testing.T) {
text := "ब तक लगातार चल रहा है। इसका प्रसारण प्रत्येक शनिवार और रविवार को रात 10 बजे होता है। इसका पुनः प्रसारण सोनी पल चैनल पर रात 9 बजे होता"
lang := "हिन्दी"
ensureClassifiedWithConfidence(
t,
text,
"hi",
0.75)
ensureClassifiedTextNamed(
t,
text,
"Hindi",
lang)
}
func TestGreekPhrase(t *testing.T) {
text := "Ολοι οι άνθρωποι γεννιούνται ελεύθεροι και ίσοι στην αξιοπρέπεια και στα δικαιώματα"
ensureClassifiedWithConfidence(
t,
text,
"el",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Greek",
"Ελληνικά")
}
func TestHebrewPhrase(t *testing.T) {
text := "כראוי. בִּדקו את כותרת הדף"
lang := "עברית"
ensureClassifiedWithConfidence(
t,
text,
"he",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Hebrew",
lang)
}
func TestGujaratiPhrase(t *testing.T) {
text := "ગુજરાતી"
lang := "ગુજરાતી"
ensureClassifiedWithConfidence(
t,
text,
"gu",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Gujarati",
lang)
}
func TestThaiPhrase(t *testing.T) {
text := "ไทย ไทยไทย"
lang := "ไทย"
ensureClassifiedWithConfidence(
t,
text,
"th",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Thai",
lang)
}
func TestArmenianPhrase(t *testing.T) {
text := "ըստ Գրիգորյան օրացույցի"
lang := "հայերեն"
ensureClassifiedWithConfidence(
t,
text,
"hy",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Armenian",
lang)
}
func TestSerbianLatinPhrase(t *testing.T) {
text := "ljudi ne znaju jer me uglavnom vide"
lang := "srpskohrvatski"
ensureClassifiedWithConfidence(
t,
text,
"sr",
0.85)
ensureClassifiedTextNamed(
t,
text,
"Serbo-Croatian",
lang)
}
func TestSerbianCyrillicPhrase(t *testing.T) {
text := "Код животиња су ове реакције посебно важне при зарастању рана"
lang := "српски"
ensureClassifiedWithConfidence(
t,
text,
"sr",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Serbian (Cyrillic)",
lang)
}
func TestVietnamesePhrase(t *testing.T) {
text := "Truyền thông Việt Nam vào dịp này đăng bài ký tên ông"
lang := "Tiếng Việt"
ensureClassifiedWithConfidence(
t,
text,
"vi",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Vietnamese",
lang)
}
func TestTeluguPhrase(t *testing.T) {
text := "భారతదేశంలోని దక్షిణ"
lang := "తెలుగు"
ensureClassifiedWithConfidence(
t,
text,
"te",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Telugu",
lang)
}
func TestTamilPhrase(t *testing.T) {
text := " நீளமான, கிளைக்காத"
lang := "தமிழ்"
ensureClassifiedWithConfidence(
t,
text,
"ta",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Tamil",
lang)
}
func TestTagalogPhrase(t *testing.T) {
text := "ano ang nangyayari sa iyo at ang mah-ina mo ay hindi mo"
lang := "Filipino"
ensureClassifiedWithConfidence(
t,
text,
"tl",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Filipino",
lang)
}
func TestDutchPhrase(t *testing.T) {
text := "Een ieder heeft, waar hij zich ook bevindt, het recht als persoon erkend te worden voor de wet"
lang := "Nederlands"
ensureClassifiedWithConfidence(
t,
text,
"nl",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Dutch",
lang)
}
func TestKannadaPhrase(t *testing.T) {
text := "ನನ್ನ ಹೆಸರು ಭಾರತ್."
lang := "ಕನ್ನಡ"
ensureClassifiedWithConfidence(
t,
text,
"kn",
0.95)
ensureClassifiedTextNamed(
t,
text,
"Kannada",
lang)
}
func TestNonsense(t *testing.T) {
text := "wep lvna eeii vl jkk azc nmn iuah ppl zccl c%l aa1z"
ensureClassifiedWithConfidence(
t,
text,
"und",
0.75)
ensureClassifiedTextNamed(
t,
text,
"Unknown language",
"")
}
func ensureClassifiedWithConfidence(t *testing.T, text string, expectedLang string, minConfidence float64) {
info := FromString(text)
assert.Equal(t, expectedLang, info.LanguageCode(), "Misclassified text: "+text)
assert.Equal(t, true, info.Confidence() > minConfidence)
}
func ensureClassifiedTextNamed(t *testing.T, text string, expectedEnglishName string, expectedSelfName string) {
info := FromString(text)
assert.Equal(t, expectedEnglishName, info.LanguageName(), "Wrong language name: "+text)
assert.Equal(t, expectedSelfName, info.SelfName(), "Wrong self lang name: "+text)
}