-
Notifications
You must be signed in to change notification settings - Fork 1
/
Naive_bayes.py
48 lines (42 loc) · 1.17 KB
/
Naive_bayes.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
import os
import sys
#IOB model
class_dict = dict()
char_dict = dict()
classes = ['I','E','O','B','S']
#training
with open("out.csv") as f:
while True:
line = f.readline()
if line:
line = line.split(',')
if class_dict.get(line[1][:-1]) == None:
class_dict[line[1][:-1]] = 1
else:
class_dict[line[1][:-1]] += 1
if char_dict.get(line[0]+"_"+line[1][:-1]) == None:
char_dict[line[0]+"_"+line[1][:-1]] = 1
else:
char_dict[line[0]+"_"+line[1][:-1]] += 1
else:
break
#print([key for key in char_dict.keys()])
#testing
fil1 = open("entertainment_anime.txt")
text = fil1.readlines(10)
# print(text)
# print(char_dict['n_I'])
for line in text:
i = 0
while line[i] != "\n":
mx = 0
classified = 'X'
for clas in classes:
if char_dict.get(line[i]+"_"+clas) == None:
continue
else:
if char_dict[line[i]+"_"+clas]/class_dict[clas] > mx:
mx = char_dict[line[i]+"_"+clas]/class_dict[clas]
classified = clas
print(line[i],classified)
i += 1