-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_msg.py
45 lines (40 loc) · 1.19 KB
/
split_msg.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
from nltk.stem import WordNetLemmatizer
import nltk
def normalize_word(sentence):
lemmatizer = WordNetLemmatizer()
tokens = nltk.word_tokenize(sentence)
pos_tags = nltk.pos_tag(tokens)
res = []
for word, pos in pos_tags:
if pos.startswith('J'):
tmp = lemmatizer.lemmatize(word.lower(), 'a')
elif pos.startswith('V'):
tmp = lemmatizer.lemmatize(word.lower(), 'v')
elif pos.startswith('N'):
tmp = lemmatizer.lemmatize(word.lower(), 'n')
elif pos.startswith('R'):
tmp = lemmatizer.lemmatize(word.lower(), 'r')
else:
tmp = lemmatizer.lemmatize(word.lower())
res.append(tmp)
return ' '.join(res)
with open('generation.txt') as f, open('gen.txt','w') as f1, open('ref.txt','w') as f2:
while True:
line = f.readline()
t = 1
if line == '':
break
# if 'mmm' not in line or '. java ' not in line:
# t = 0
gen = f.readline()
gen = gen[11:]
ref = f.readline()
ref = ref[5:]
if t == 1:
gen = normalize_word(gen)
f1.write(gen)
f1.write('\n')
if t == 1:
ref = normalize_word(ref)
f2.write(ref)
f2.write('\n')