-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
62 lines (61 loc) · 2.06 KB
/
train.py
File metadata and controls
62 lines (61 loc) · 2.06 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
infile=open("data/input.txt",'r')
lines=infile.readlines()
infile.close()
s=[]
current=[]
results={}
buf=''
for line in lines:
for char in line:
#当char等于'('时,判断buf是否为空,下面会给buf赋值
if char=='(':
if len(buf):
current.append(buf)
buf=''
s.append(current)
current=[]
#当char等于')'时,判断buf是否为空,下面会给buf赋值
elif char==')':
if len(buf):
current.append(buf)
buf=''
#left表示词的标注
left=current[0]
if len(current) == 2:
#right表示词
right=current[1].lower()
else:
right=' '.join(current[1:])
if not left in results:
#如果left代表的内容不在results中,则以其内容创建一个对应的主键
results[left]={}
if not right in results[left]:
#如果right代表的内容不在results中,则以其内容创建一个对应的副键
#并且将副键right的值赋为0
results[left][right]=0
#若已存在,则值+1
results[left][right]+=1
tmp=current[0]
#弹出列表中最后一个元素
#这里的作用是当连续有多个')'时,从里到外求results的键以及值。
current=s.pop(-1)
current.append(tmp)
#当char等于' '时,判断buf是否为空,下面会给buf赋值
elif char==' ':
if len(buf):
current.append(buf)
buf=''
#这里会使buf不一直为空
else:
buf+=char
print("输出规则对应的概率:")
print(results)
outfile=open("data/model.txt", 'w')
for result in results:
total=0
rights=results[result]
for right in rights:
total+=rights[right]
for right in rights:
outfile.write(result+" # "+right+" # "+str(float(rights[right])/total)+'\n')
outfile.close()