-
Notifications
You must be signed in to change notification settings - Fork 1
/
language_svm.py
48 lines (38 loc) · 1.2 KB
/
language_svm.py
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
from sklearn import svm, metrics
import glob, os.path, re, json
def check_freq(fname):
name = os.path.basename(fname)
lang = re.match(r'^[a-z]{2,}', name).group()
with open(fname, "r", encoding="utf-8") as f:
text = f.read()
text = text.lower()
cnt = [0 for n in range(0, 26)]
code_a = ord("a")
code_z = ord("z")
for ch in text:
n = ord(ch)
if code_a <= n <= code_z:
cnt[n - code_a] = 1
total = sum(cnt)
freq = list(map(lambda n: n / total, cnt))
return (freq, lang)
def load_files(path):
freqs = []
labels = []
file_list = glob.glob(path)
for fname in file_list:
r = check_freq(fname)
freqs.append(r[0])
labels.append(r[1])
return {"freqs":freqs, "labels":labels}
data = load_files("./data/train/*.txt")
test = load_files("./data/test/*.txt")
with open("./data/freq.json", "w", encoding="utf-8") as fp:
json.dump([data, test], fp)
clf = svm.SVC()
clf.fit(data["freqs"], data["labels"])
predict = clf.predict(test["freqs"])
ac_score = metrics.accuracy_score(test["labels"], predict)
cl_report = metrics.classification_report(test["labels"], predict)
print(ac_score)
print(cl_report)