-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaux.py
More file actions
197 lines (146 loc) · 5 KB
/
aux.py
File metadata and controls
197 lines (146 loc) · 5 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
def getAllWordsAndTags(filename):
with open(filename) as f_input:
tags = set()
words = set()
for line in f_input:
tokens = line.strip().split()
if(tokens[1] == 'WORDTAG'):
tag = tokens[2]
tags.add(tag)
word = tokens[3]
words.add(word)
return words, tags
def computeEmissionsCategorical(classifier):
#read the ner_rare.counts file
#compute e(x|y) for all x and y
words, tags = getAllWordsAndTags('ner_rare_categorical.counts')
#emissions[word][tag] -> e(word | tag)
emissions = dict()
for word in words:
emissions[word] = dict()
#count(y)
count_of_tags = dict()
for tag in tags:
count_of_tags[tag] = 0
with open('ner_rare_categorical.counts') as f:
for line in f:
tokens = line.strip().split()
if(tokens[1] == 'WORDTAG'):
word = tokens[3]
tag = tokens[2]
tag_count = int(tokens[0])
count_of_tags[tag] += tag_count
emissions[word][tag] = int(tokens[0])
#normalize the counts now
for word in words:
for tag in emissions[word]:
emissions[word][tag] = float(emissions[word][tag]) / float(count_of_tags[tag])
return emissions
def computeEmissions():
#read the ner_rare.counts file
#compute e(x|y) for all x and y
words, tags = getAllWordsAndTags('ner_rare.counts')
#emissions[word][tag] -> e(word | tag)
emissions = dict()
for word in words:
emissions[word] = dict()
#count(y)
count_of_tags = dict()
for tag in tags:
count_of_tags[tag] = 0
with open('ner_rare.counts') as f:
for line in f:
tokens = line.strip().split()
if(tokens[1] == 'WORDTAG'):
word = tokens[3]
tag = tokens[2]
tag_count = int(tokens[0])
count_of_tags[tag] += tag_count
emissions[word][tag] = int(tokens[0])
#normalize the counts now
for word in words:
for tag in emissions[word]:
emissions[word][tag] = emissions[word][tag] / count_of_tags[tag]
return emissions
def computeWC():
#read the ner_rare.counts file
#compute e(x|y) for all x and y
words = set()
tags = set()
#find out all the distinct words and tags
with open('ner_rare.counts') as f:
for line in f:
tokens = line.strip().split()
if(tokens[1] == 'WORDTAG'):
words.add(tokens[3])
tags.add(tokens[2])
#emissions[word][tag] -> e(word | tag)
emissions = dict()
for word in words:
emissions[word] = dict()
#by defult if a tag is not assigned to a word => count(x, y) = 0
for tag in tags:
emissions[word][tag] = 0
#count(y)
count_of_tags = dict()
for tag in tags:
count_of_tags[tag] = 0
with open('ner_rare.counts') as f:
for line in f:
tokens = line.strip().split()
if(tokens[1] == 'WORDTAG'):
word = tokens[3]
tag = tokens[2]
tag_count = int(tokens[0])
count_of_tags[tag] += tag_count
emissions[word][tag] = int(tokens[0])
return emissions
# In[ ]:
def isRare(emissions, x):
return (x not in emissions)
# In[ ]:
def getTrigramCount(w_x, w_y, w_z):
count_of_trigram = 0
with open('ner_rare.counts') as f:
for line in f:
tokens = line.strip().split()
if(tokens[1] == '3-GRAM'
and tokens[2] == w_x
and tokens[3] == w_y
and tokens[4] == w_z):
count_of_trigram = int(tokens[0])
break
return count_of_trigram
# In[ ]:
def getBigramCount(w_x, w_y):
count_of_bigram = 0
with open('ner_rare.counts') as f:
for line in f:
tokens = line.strip().split()
if(tokens[1] == '2-GRAM'
and tokens[2] == w_x
and tokens[3] == w_y):
count_of_bigram = int(tokens[0])
break
return count_of_bigram
def getWordCount():
words = set()
count_of_words = dict()
with open('ner.counts') as f:
for line in f:
tokens = line.strip().split()
if(tokens[1] == 'WORDTAG'):
word = tokens[3]
this_count = int(tokens[0])
if(word in count_of_words):
count_of_words[word] += this_count
else:
count_of_words[word] = this_count
return count_of_words
def getRareWords_(count_of_words):
rare_words = [word for word in count_of_words if count_of_words[word] < 5]
return rare_words
def getRareWords():
count_of_words = getWordCount()
rare_words = getRareWords_(count_of_words)
return rare_words