-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreprocess.py
More file actions
332 lines (274 loc) · 10.3 KB
/
preprocess.py
File metadata and controls
332 lines (274 loc) · 10.3 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
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
import string
import nltk
import sys
import numpy as np
from nltk.tokenize import StanfordTokenizer
from nltk.tokenize import TweetTokenizer
from stop_words import get_stop_words
from collections import OrderedDict
from nltk.stem.snowball import SnowballStemmer
from nltk.tag import StanfordNERTagger
from model_init.subjective_lexicon import mpqa_priors_to_dict
stopwords = get_stop_words("en")
#Stopwords typically occurring in debates
stopwords += [
"gentleman",
"hon",
"right",
"rose—",
"minister",
"member",
"members",
"mr",
"mrs",
"friend",
"mr_speaker",
"mr","speaker",
"chief_secretary",
"her_majesty",
"her","majesty",
"house_of_commons",
"house_of_lords",
]
"""
Remove documents that are either too long or too short
"""
def remove_texts(docs,high=60, low=10, strategy='zipfs'):
lengths = [len(doc) for doc_id, doc in docs.items()]
new_docs = OrderedDict({})
mean = np.mean(lengths)
std = np.std(lengths)
print("------------------------------",
"Printing some texts statistics",
"------------------------------")
print ("Mean:",mean,"Std: +/-",std)
for percentile in range(100,10,-5):
percentile_value = np.percentile(lengths, percentile)
print ("Percentile "+str(percentile)+" = "+str(percentile_value))
if strategy == 'mean-std': #not good, std desviation is too large
for doc_id, doc in docs.items():
if mean < len(doc) and len(doc) < mean+2*std:
new_docs[doc_id] = doc
elif strategy == 'zipfs':
for doc_id, doc in docs.items():
if low <= len(doc) and len(doc) <= high:
new_docs[doc_id] = doc
elif strategy == 'percentile':
for doc_id, doc in docs.items():
if np.percentile(lengths,15) <= len(doc) and len(doc) <= np.percentile(lengths,95):
new_docs[doc_id] = doc
return new_docs
"""
Removes the words that are too common or too rare
Each doc_i is a list of tuples (textid,[(word,tag)])
"""
def remove_words(docs, high = 120, low =0, strategy='mean-std'):
vocab = {}
for doc_id, doc in docs.items():
for word,postag in doc:
if word in vocab:
vocab[word] += 1
else:
vocab[word] = 1
lengths = np.array([vocab[word] for word in vocab])
sorted_lengths = sorted(lengths,reverse=True)
mean = np.mean(lengths)
std = np.std(lengths)
print("------------------------------",
"Printing some word statistics",
"------------------------------")
print ("Mean:",mean,"Std: +/-",std)
for percentile in range(100,10,-5):
percentile_value = np.percentile(lengths, percentile)
print ("Percentile "+str(percentile)+" = "+str(percentile_value))
new_docs = OrderedDict({})
if strategy == 'mean-std':
for doc_id, doc in docs.items():
new_words = [(word,postag) for word,postag in doc
if (mean-2*std) < vocab[word] and vocab[word] < (mean+2*std)]
if new_words != []:
new_docs[doc_id] = new_words
elif strategy == 'zipfs':
for doc_id, doc in docs.items():
new_docs[doc_id] = []
for word,postag in doc:
if vocab[word] <= high and vocab[word] >= low:
new_docs[doc_id].append((word,postag))
elif strategy == 'percentile':
for doc_id, doc in docs.items():
new_docs[doc_id] = []
for word,postag in doc:
if vocab[word] <= np.percentile(lengths,99) and vocab[word] >= np.percentile(lengths,65):
new_docs[doc_id].append((word,postag))
return new_docs
"""
Determines if a string s is a number
"""
def is_number(s):
try:
complex(s)
except ValueError:
return False
return True
def _is_content_word(self,postag):
return postag.startswith('n') or postag.startswith('jj') or postag.startswith('vb') or postag.startswith('rb')
"""
Lemmatizes a list of documents
Each doc_i is a list of tuples (textid,[(word,tag)])
"""
def lemmatize_words(docs):
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
new_docs = OrderedDict({})
for doc_id, doc in docs.items():
new_docs[doc_id] = []
for word,postag in doc:
try:
lemma = wordnet_lemmatizer.lemmatize(word,pos=postag.lower()[0])
new_docs[doc_id].append((lemma.lower(),postag))
except KeyError:
lemma = word
new_docs[doc_id].append((lemma.lower(),postag))
return new_docs
"""
Removes the stopwords from a list of documents
Each doc_i is a list of tuples (textid,[(word,tag)])
"""
def remove_stopwords(docs):
punctuation = string.punctuation + string.digits+"”"+"“"
new_docs = OrderedDict({})
for doc_id, doc in docs.items():
new_docs[doc_id] = []
for word,postag in doc:
if word not in stopwords and word not in punctuation and not is_number(word) and len(word) > 1: #To avoid rare web characters that might be not considered among the stopword lists
new_docs[doc_id].append((word,postag))
return new_docs
# """
# Returns a list o docs annotated with NER information
# Each doc_i is a list of tuples (textid,[(word,tag)])
# """
# def NER(docs):
#
# print ("NER... (it might take some seconds/minutes)")
# st = StanfordNERTagger('/home/david.vilares/Descargas/stanford-ner-2012-11-11-nodistsim/conll.closed.iob2.crf.ser.gz',
# '/home/david.vilares/Descargas/stanford-ner-2015-12-09/stanford-ner.jar',
# encoding='utf-8')
#
# new_docs = OrderedDict({})
# #We append all docs not to be calling ther NER jar for every single document
# aux_docs = []
# docs_id = []
# for doc_id, doc in docs.items():
# aux_docs.append(doc)
# docs_id.append(doc_id)
# ner_docs = st.tag_sents(aux_docs)
#
# if len(docs_id) != len(ner_docs): raise ValueError
# #We can do this zip because we assumed docs is an ordered dict!
# for doc_id,ner_doc in zip(docs_id,ner_docs):
# composed_ner = []
# aux = []
# for word, ner in ner_doc:
# if len(word) > 0:
# if ner == 'O':
# #If we finished computing a multiword NER
# #we needed to put it in the list first
# if composed_ner != []:
# aux.append('_'.join(composed_ner))
# composed_ner = []
# aux.append(word)
# else:
# if ner.startswith('B-') and composed_ner != []:
# aux.append('_'.join(composed_ner))
# composed_ner = [word]
# else:
# composed_ner.append(word)
# new_docs[doc_id] = aux
# return new_docs
"""
Each doc_i is a list of tuples (textid,[(word,tag)])
"""
def lowercase(docs):
new_docs = OrderedDict({})
for doc_id, doc in docs.items():
new_docs[doc_id] = [(w.lower(),p) for w,p in doc]
return new_docs
def tokenize(docs, punctuation = string.punctuation,
tokenizer = TweetTokenizer()):
new_docs = OrderedDict({})
for doc_id, doc in docs.items():
new_docs[doc_id] = tokenizer.tokenize(doc)
return new_docs
"""
Tags a collection of documents
Each doc_i is a list of tuples (textid, text)
"""
def postagging(docs):
new_docs = OrderedDict({})
for doc_id, doc in docs.items():
new_docs[doc_id] = nltk.pos_tag(docs[doc_id])
return new_docs
def _index_inside_negating_scope(index, indexes_negating, scope = 4):
aux = False
for index_negating in indexes_negating:
if index_negating < index and index_negating + scope > index:
return True
return aux
"""
Applies a simple negation heuristic to created 'negation bi-grams' (e.g.
transforms not good into not_good)
Each doc_i is a list of tuples (word,tag)
"""
def neg_subjective(docs, negating_terms=['not','no'], subjective_dict_priors={}):
new_docs = OrderedDict({})
for doc_id, doc in docs.items():
new_docs[doc_id] = []
indexes_negating = []
#identify negating terms
index = 0
for word,postag in doc:
if word in negating_terms:
indexes_negating.append(index)
index+=1
index = 0
for word,postag in doc:
if _index_inside_negating_scope(index, indexes_negating) and word in subjective_dict_priors:
new_docs[doc_id].append(("not_"+word,postag))
#reverse subjective priors
if (len(subjective_dict_priors[word]) == 2 or len(subjective_dict_priors[word]) % 2 !=0 ):
subjective_dict_priors["not_"+word] = subjective_dict_priors[word][::-1]
else:
raise NotImplementedError("List of subjectivity priors must be 2 or odd number to be able to apply an inversion of the subjective priors")
else:
new_docs[doc_id].append((word,postag))
index+=1
return new_docs
def remove_outlines(docs):
print ("Removing most common/rare words...")
non_outlinewords_docs = remove_words(docs, strategy='percentile')
print("Removing the largest/shortest texts...")
non_outline_docs = remove_texts(non_outlinewords_docs, strategy='percentile')
return non_outline_docs
"""
Each doc is a list of tuples (textid, text)
"""
def preprocess(ori_docs, subjective_dict_priors):
print ("Tokenizing...")
tokenized_docs = tokenize(ori_docs, tokenizer= TweetTokenizer())
print ("PoS tagging...")
postagged_docs = postagging(tokenized_docs)
# ner_docs = NER(tokenized_docs)
print ("Lemmatizing...")
lematized_docs = lemmatize_words(postagged_docs)
print ("Lowercasing...")
lowercase_docs = lowercase(lematized_docs)
if subjective_dict_priors is not None:
print ("Applying simple negation scope")
neg_subjective_docs= neg_subjective(lowercase_docs,
negating_terms = ['not'],
subjective_dict_priors=subjective_dict_priors)
else:
neg_subjective_docs = lowercase_docs
print ("Removing stopwords...")
non_stopwords_docs = remove_stopwords(neg_subjective_docs)
return non_stopwords_docs