-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcheck_words_frq.py
214 lines (178 loc) · 4.64 KB
/
check_words_frq.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
import csv, sys, re
from nltk.corpus import stopwords
from textblob import TextBlob
csv.field_size_limit(sys.maxsize)
#use the same helper method to split papers
def split_into_lemmas(message):
try:
message = message.encode('utf-8').lower()
except:
print type(message)
sys.exit()
words = TextBlob(message).words
# for each word, take its "base form" = lemma
stopWords = set(stopwords.words('english'))
wordsRaw = [word.lemma for word in words]
wordsOut = []
for word in wordsRaw:
if len(word) == 1:
continue
if word in stopWords:
continue
p = re.compile(r'\W')
check_digit = p.split(word)
digit = True
for i in check_digit:
if not i.isdigit():
digit = False
if digit:
continue
wordsOut.append(word)
return wordsOut
#open file that contains all papers and labels
names = set()
data_papers = []
nonD_papers = []
with open('MLpapers_whole.csv','rb') as cf:
rd = csv.reader(cf, delimiter=',', quotechar='|')
header = rd.next()
for r in rd:
text = r[1]
words_set = set(split_into_lemmas(text))
if r[-1] == 'Data':
data_papers.append(words_set)
names.add(r[0])
elif r[-1] == 'Non-data':
nonD_papers.append(words_set)
names.add(r[0])
else:
print 'error:', r[-1]
print 'len data:', len(data_papers)
print 'len non-data:', len(nonD_papers)
# open file that contains tfidf words:
word_list = []
with open('tfidf2.csv','rb') as cf:
rd = csv.reader(cf, delimiter=',', quotechar='"')
header = rd.next()
stopWords = set(stopwords.words('english'))
for r in rd:
words = r[1]
p = re.compile(r'\W')
words = p.split(words)
for word in words:
if word in stopWords:
continue
word_list.append(word)
print 'total words:', len(word_list)
qualify0 = []
qualify1 = []
qualify2 = []
qualify3 = []
qualify4 = []
qualify5 = []
for word in word_list:
count = 0
for paper in nonD_papers:
if word in paper:
count += 1
if count <= 0:
qualify0.append(word)
print 'qualify0:', len(qualify0)
count = len(data_papers)
for paper in data_papers:
appear = False
for word in qualify0:
if word in paper:
appear = True
break
if appear:
count -=1
print 'not cover by qualify0:', count
fh = open('checkWords.txt', 'wb')
fh.write(' '.join(qualify0))
fh.close()
word_list = word_list[:len(word_list)/2]
print 'total words:', len(word_list)
#check appearance of these words in Non-data papers. If it is < 18, marked down
for word in word_list:
count = 0
for paper in nonD_papers:
if word in paper:
count += 1
if count <= 1:
qualify1.append(word)
if count <= 2:
qualify2.append(word)
if count <= 3:
qualify3.append(word)
if count <= 4:
qualify4.append(word)
if count <= 5:
qualify5.append(word)
print '1',len(qualify1)
print '2',len(qualify2)
print '3',len(qualify3)
print '4',len(qualify4)
print '5',len(qualify5)
count = len(data_papers)
for paper in data_papers:
appear = False
for word in qualify1:
if word in paper:
appear = True
break
if appear:
count -=1
print 'not cover by qualify1:', count
count = len(data_papers)
for paper in data_papers:
appear = False
for word in qualify2:
if word in paper:
appear = True
break
if appear:
count -=1
print 'not cover by qualify2:', count
count = len(data_papers)
for paper in data_papers:
appear = False
for word in qualify3:
if word in paper:
appear = True
break
if appear:
count -=1
print 'not cover by qualify3:', count
count = len(data_papers)
for paper in data_papers:
appear = False
for word in qualify4:
if word in paper:
appear = True
break
if appear:
count -=1
print 'not cover by qualify4:', count
count = len(data_papers)
for paper in data_papers:
appear = False
for word in qualify5:
if word in paper:
appear = True
break
if appear:
count -=1
print 'not cover by qualify5:', count
fh = open('checkWords1.txt', 'wb')
fh.write(' '.join(qualify1))
fh.close()
fh = open('checkWords2.txt', 'wb')
fh.write(' '.join(qualify2))
fh.close()
fh = open('checkWords3.txt', 'wb')
fh.write(' '.join(qualify3))
fh.close()
fh = open('checkWords4.txt', 'wb')
fh.write(' '.join(qualify4))
fh.close()