-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_proc.py
349 lines (260 loc) · 8.77 KB
/
pre_proc.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import re
import pickle
import operator
import random
def listTags(corCorpus):
'''generate a list with all tags. input is the already clean corpus
'''
l, tagList = [], open('tagList.txt', 'w')
for line in corCorpus:
splitSentence = line.split()
for s in splitSentence:
if s[0] == '⛬' and not(s in l):
l.append(s)
tagList.write(s+'\n')
def corpusToSentence(corpus):
'''clean the corpus. Output is a file named by the user.
each line is a tagged sentence in the following format:
word1 ⛬TAG1 word2 ⛬TAG2
'''
corpusName = input('Nome do arquivo do corpus de sentencas: ')
newCorpus = open(corpusName+'.txt', 'w')
regexWord = '^.*?\s'
regexTag = '\s(N|PROP|SPEC|DET|PERS|ADJ|ADV|V|NUM|PRP|KS|KC|IN|EC)\s'
newSentence = True
for line in corpus:
if line == '\n' and newSentence: #detecs sentence end
newCorpus.write('\n')
newSentence = False #avoid creating empty lines
elif line[0]=='$':
newSentence = True
word = line[1]
tag = 'PONT'
newCorpus.write(word+' ⛬'+tag+' ')
elif line[0]!='<':
newSentence = True
word = re.search(regexWord, line)
tag = re.search(regexTag, line)
try:
newCorpus.write(word.group()[:-1]+' ⛬'+tag.group()[1:-1]+' ')
except:
pass
newCorpus.close()
def numTag(corCorpus):
'''count no of tags
'''
total = 0
for line in corCorpus:
words = line.split()
for w in words:
if w[0] == '⛬':
total += 1
return total
def countUniqueWords(corCorpus):
'''return a dictionary with words as keys and no of
occurence as values
print number of unique words
five most common words
number of hapax'''
words, five, hapax, totalW = {}, {}, 0, 0
for line in corCorpus:
for word in line.split()[::2]:
totalW +=1
if word in words:
words[word] += 1
else:
words[word] = 1
for i in range(5):
key = max(words, key=words.get)
value = words[key]
five[key] = value
words[key]= 0
print('\nmost common words:')
for word in five: #restore and print
words[word] = five[word]
print(word,' : ', five[word])
for word in words:
if words[word] == 1:
hapax += 1
print('Number of unique words: ', len(words))
print('Number of hapax: ', hapax)
print('Number of tokens: ', totalW)
return words
def counTag(corCorpus):
'''count no of occurence for each tag.
Output into a txt file, named by the user.
'''
noTag = dict() #dictionary w tags as keys and no occurrence as value
fileName = input('Nome do arquivo para contagem de TAGs: ')
newFile = open(fileName+'.txt', 'w')
for line in corCorpus:
tags = re.findall('⛬\w*', line)
for t in tags:
if t in noTag:
noTag[t] += 1
else:
noTag[t] = 1
for tag in noTag:
newFile.write(tag)
newFile.write(' : '+str(noTag[tag]))
newFile.write('\n')
newFile.close()
def countTagWords(corCorpus):
'''count number of tags per word (i.e. some word appeared under 2 distinct tags)
'''
wordDict, noOcurrences = dict(), [0,0,0,0,0,0,0,0,0,0,0]
for line in corCorpus:
words = line.split()
for n in range(0,len(words),2):
if n+1 < len(words):
if words[n] in wordDict:
if words[n+1] in wordDict[words[n]]:
wordDict[words[n]][words[n+1]] += 1
else:
wordDict[words[n]][words[n+1]] = 1
else:
wordDict[words[n]] = {words[n+1] : 1}
for word in wordDict:
if len(wordDict[word])-1>6: #catch strange errors
print(len(wordDict[word])-1,wordDict[word])
else:
noOcurrences[len(wordDict[word])-1] += 1
print(noOcurrences)
def likelyTag(corCorpus):
'''generate a dictionary [words] ==> [likely tag].
'''
wordDict = dict()
for line in corCorpus:
words = line.split()
for n in range(0,len(words),2):
if n+1 < len(words):
if words[n] in wordDict:
if words[n+1] in wordDict[words[n]]:
wordDict[words[n]][words[n+1]] += 1
else:
wordDict[words[n]][words[n+1]] = 1
else:
wordDict[words[n]] = {words[n+1] : 1}
for word in wordDict:
wordDict[word] = max(wordDict[word].items(), key=operator.itemgetter(1))[0]
return (wordDict)
def cutCorpus(corCorpus, n=1):
'''cut the corpus into a small file for testing stuff.
saves it into a file named smallCorpus.
'''
newCorpus = open('smallCorpus.txt', 'w')
for line in corCorpus:
if random.randint(0,100) < n:
newCorpus.write(line)
#print('lenght = ', len(newCorpus))
newCorpus.close()
def maxTag(sentence, commonDictionary):
'''Tag a sentence with most likely tag method.
commonDictionary == dictionary [word] ==> most likely tag.
'''
newSentence = sentence.split()
tagged = []
for word in newSentence:
try:
tagged.append(commonDictionary[word])
except:
tagged.append('⛬DESCONHECIDO')
return(tagged)
def tagAccuracy(corCorpus, commonDictionary):
'''tag sentences with commonest tag. Calculate the percentage of correct tags.
commonDictionary == dictionary [word] ==> most likely tag.
'''
correct, total = 0,0
for line in corCorpus:
sentence, correctTags = '', []
wordlist = line.split()
for w in wordlist:
if w[0] != '⛬':
sentence += w+' '
else:
correctTags.append(w)
tagged = maxTag(sentence, commonDictionary) #calculate the tags
for w in range(0, len(tagged)): #count correct guesses
total += 1
if correctTags[w] == tagged[w]:
correct += 1
return (correct/total)
def tagAccuracySentence(corCorpus, commonDictionary):
'''tag sentences with commonest tag. Calculate the percentage of correct tagged sentences.
'''
correct, total = 0,0
for line in corCorpus:
total += 1
correctTagging = True
sentence, correctTags = '', []
wordlist = line.split()
for w in wordlist:
if w[0] != '⛬':
sentence += w+' '
else:
correctTags.append(w)
tagged = maxTag(sentence, commonDictionary) #calculate the tags
for w in range(0, len(tagged)): #count correct guesses
if correctTags[w] != tagged[w]:
correctTagging = False
if correctTagging:
correct += 1
return (correct/total)
def countWords(corCorpus):
wordic, size = dict(), 0
wordFreq = open('wordFreq', 'wb')
for sentence in corCorpus:
for word in sentence.split():
if word[0] != '⛬':
size += 1
if word in wordic:
wordic[word] += 1
else:
wordic[word] = 1
print(wordic)
print(wordic['a'])
print(size)
pickle.dump(wordic, wordFreq)
def countSentenceLenght(corpus):
'''extract sentence lenght
'''
totalLenght, noSentences = 0, len(corpus)
for sentence in corpus:
totalLenght += len(sentence.split())/2
return totalLenght/noSentences
def noEmptylines(corCorpus):
new_corpus = open(input('Nome do novo arquivo: '), 'w')
noEmptylines = 0
for i in corCorpus:
if i != '\n':
new_corpus.write(i+'\n')
else:
noEmptylines += 1
new_corpus.close()
print(noEmptylines)
def findProblems(corCorpus):
throw = 0
for sentence in corCorpus:
noTag, noWord = 0, 0
sliced = sentence.split()
for tw in sliced:
if tw[0] == '⛬':
noTag += 1
else:
noWord +=1
if noTag != noWord:
print(sliced)
throw += 1
print(throw)
'''testes'''
if __name__ == '__main__':
corpus = open('smallCorpus.txt', 'r').readlines()
common = likelyTag(corpus)
#cutCorpus(corpus, 10)
print(tagAccuracySentence(corpus, common))
#print(countSentenceLenght(corpus))
#wordFreq = open('wordFrequencePrePro', 'wb')
#words = countUniqueWords(corpus)
#pickle.dump(words, wordFreq)
#orderedWords = sorted(words.items(), key=lambda x: x[1])
#occurence =