-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemonstration.py
70 lines (50 loc) · 1.84 KB
/
demonstration.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
import os
def main():
print("---- Train file ----")
train = get_files_and_language("train/")
print("\n---- Test file ----")
test = get_files_and_language("test/", train.keys())
train = load_data(train)
test = load_data(test)
list_predict, score = prediction(train, test)
list_predict = list(list_predict)
print(f"\n ========= Prediction score : {str(score*100)}% ========= ")
for predict, real in zip(list_predict, list(test.keys())):
check_prediction = "good prediction" if predict == real else "bad prediction"
print(f"- Language predict: {predict} / real: {real} ====> {check_prediction}")
print("Process End")
def prediction(train, test):
clf = Pipeline([
('vec', CountVectorizer(analyzer='word')),
('clf', MultinomialNB()),
])
clf.fit(list(train.values()), list(train.keys()))
predict = clf.predict(test.values())
score = clf.score(list(test.values()), list(test.keys()))
return predict, score
def load_data(files):
data = {}
for lang, path in files.items():
with open(path, 'r') as f:
data[lang] = f.read()
f.close()
return data
def get_files_and_language(dir_path, possible_choice=""):
files = {}
for file in os.listdir(dir_path):
lang = prompt(f"What is the language of '{file}' file ? ({file}) ", file, possible_choice)
files[lang] = dir_path + file
return files
def prompt(text, default, possible_choice):
inp = input(text)
while possible_choice and inp not in possible_choice:
print("Unknown language")
inp = input(text)
if not inp:
inp = default
return inp
if __name__ == '__main__':
main()