Repository navigation
Expand file tree
/
Copy pathlanguage.go
More file actions
85 lines (78 loc) · 2.98 KB
/
Copy pathlanguage.go
File metadata and controls
85 lines (78 loc) · 2.98 KB
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
package main
// Which language a post is written in, from the small words.
//
// Only English and Spanish are read for facts, and that is a limit rather
// than a choice: every pattern in here is a sentence somebody would write
// in one of those two. A post in anything else is still counted towards
// the language of the account, and the report says it was not read.
import "strings"
var smallWords = map[string]map[string]bool{
"en": set("the and is are was were you he she it we they this that with for of " +
"to in on at but have has had do does did would could should my your our their " +
"him her them what which who when where why how there here just like so if or be " +
"been being an i'm don't it's can't won't about from"),
"es": set("el la los las de del que y en un una es son era fue por para con pero " +
"como más muy yo tú mi mis su sus te se lo le nos este esta estos estas eso esa " +
"ese hay ya también porque cuando donde qué está estoy tengo soy pues"),
"pt": set("o os as do da dos das em um uma é são não com mas como mais muito eu " +
"você meu minha seu sua isso essa esse tem estou sou então também"),
"fr": set("le les des du un une est sont avec mais comme plus très je tu il elle " +
"nous vous mon ma mes ce cette ces c'est pas pour dans sur qui que"),
"de": set("der die das den dem des und ist sind mit aber wie mehr sehr ich du er " +
"sie wir ihr mein meine nicht für auf ein eine zu auch noch schon"),
"it": set("il lo gli le della dello di che è sono con ma come più molto io tu lui " +
"lei noi voi mio mia non per una uno anche questo quella sto"),
}
func set(words string) map[string]bool {
m := map[string]bool{}
for _, w := range strings.Fields(words) {
m[w] = true
}
return m
}
// detect gives a language for a post, or nothing when the post is too short
// or too mixed to say. Four small words is the least that decides anything:
// "no" and "me" are English and Spanish both.
func detect(text string) string {
hits := map[string]int{}
for _, w := range strings.Fields(strings.ToLower(text)) {
w = strings.Trim(w, `.,;:!?()"¿¡-`)
for lang, words := range smallWords {
if words[w] {
hits[lang]++
}
}
}
// A fixed order, so a tie between two languages goes the same way on
// every run.
best, most, next := "", 0, 0
for _, lang := range []string{"en", "es", "pt", "fr", "de", "it"} {
switch n := hits[lang]; {
case n > most:
next = most
best, most = lang, n
case n > next:
next = n
}
}
if most < 4 || most < 2*next {
return ""
}
return best
}
// languageCode finds a language by the name somebody wrote it under, in
// English or Spanish: "Japanese", "japonés".
func languageCode(name string) string {
key := fold(name)
for code, l := range census().Languages {
for _, n := range l.Names {
if fold(n) == key {
return code
}
}
}
return ""
}
var languageNames = map[string]string{
"en": "English", "es": "Spanish", "pt": "Portuguese", "fr": "French", "de": "German", "it": "Italian",
}