-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathpreprocessing.py
More file actions
335 lines (306 loc) · 16.6 KB
/
preprocessing.py
File metadata and controls
335 lines (306 loc) · 16.6 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
332
333
334
335
import string
from typing import List, Union
import spacy
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from tqdm.contrib.concurrent import process_map # or thread_map
from tqdm import tqdm
from pathlib import Path
from octis.dataset.dataset import Dataset
from collections import Counter
"""
Maps the language to its corresponding spacy model
"""
spacy_model_mapping = {
'chinese': 'zh_core_web_sm', 'danish': 'nl_core_news_sm',
'dutch': 'nl_core_news_sm', 'english': 'en_core_web_sm',
'french': 'fr_core_news_sm', 'german': 'de_core_news_sm',
'greek': 'el_core_news_sm', 'italian': 'it_core_news_sm',
'japanese': 'ja_core_news_sm', 'lithuanian': 'lt_core_news_sm',
'norwegian': 'nb_core_news_sm', 'polish': 'pl_core_news_sm',
'portuguese': 'pt_core_news_sm', 'romanian': 'ro_core_news_sm',
'russian': 'ru_core_news_sm', 'spanish': 'es_core_news_sm'}
class Preprocessing:
def __init__(
self, lowercase: bool = True, vocabulary: List[str] = None,
max_features: int = None, min_df: float = 0.0, max_df: float = 1.0,
remove_punctuation: bool = True, punctuation: str = string.punctuation,
remove_numbers: bool = True, lemmatize: bool = True,
stopword_list: Union[str, List[str]] = None, min_chars: int = 1,
min_words_docs: int = 0, language: str = 'english', split: bool = True,
verbose: bool = False, num_processes: int = None,
save_original_indexes=True, remove_stopwords_spacy: bool = True):
"""
init Preprocessing
:param lowercase: if true, words in documents are reduced to
lowercase (default: true)
:type lowercase: boolean
:param vocabulary: the vocabulary of the corpus to preprocess
(default: None)
:type vocabulary: list
:param max_features: maximum number of words that the vocabulary must
contain. The less frequent words will be removed. If it's not None,
then max_df and min_df are ignored (default: None)
:type max_features: int
:param min_df: words below this minumum document frequency will be
removed (default: 0.0)
:type min_df: float
:param max_df: words above this maximum document frequency will be
removed (default: 1.0)
:type max_df: float
:param remove_punctuation: if true, punctuation will be removed
(default: true)
:type remove_punctuation: bool
:param punctuation: string containing all the punctuation chars that
need to be removed (default:
string.punctuation)
:type punctuation: str
:param remove_numbers: if true, numbers will be removed
:type remove_numbers: bool
:param remove_stopwords_spacy: bool , if true use spacy to remove
stopwords (default: true)
:param lemmatize: if true, words will be lemmatized using a spacy model
according to the language that has been set (default: true)
:type lemmatize: bool
:param stopword_list: if a list of strings is passed, the strings will
be removed from the texts. Otherwise, if a str is passed, it
represents the language of the stopwords that need to be removed.
The stopwords are spacy's stopwords (default: None)
:type stopword_list: str or list of str
:param min_chars: mininum number of characters that a token should have
(default: 1)
:type min_chars: int
:param min_words_docs: minimun number of words that a document should
contain (default: 0)
:type min_words_docs: int
:param language: language of the documents. It needs to be set for the
lemmatizer (default: english)
:type language: str
:param split: if true, the corpus will be split in train (85%),
testing (7.5%) and validation (7.5%) set (default: true)
:type split: bool
:param verbose: if true, some steps of the preprocessing will be
printed on screen (default: false)
:type verbose: bool
:param num_processes: number of processes to run the preprocessing
:type num_processes: int
:param save_original_indexes: if true, it keeps track of the original
indexes of the documents
"""
self.vocabulary = vocabulary
self.lowercase = lowercase
self.max_features = max_features
self.min_df = min_df
self.max_df = max_df
self.remove_punctuation = remove_punctuation
self.punctuation = punctuation
self.lemmatize = lemmatize
self.language = language
self.num_processes = num_processes
self.remove_numbers = remove_numbers
self.save_original_indexes = save_original_indexes
if self.lemmatize:
lang = spacy_model_mapping[self.language]
try:
self.spacy_model = spacy.load(lang)
except IOError:
raise IOError("Can't find model " + lang + ". Check the data directory or download it using the "
"following command:\npython -m spacy download " + lang)
self.split = split
self.verbose = verbose
self.remove_stopwords_spacy = remove_stopwords_spacy
stopwords = []
# if stopwords is None then stopwords are not removed
if stopword_list is None:
self.remove_stopwords_spacy = False
else:
# if custom list is specified, then we do not use spacy stopwords
if type(stopword_list) == list:
stopwords = set(stopword_list)
self.remove_stopwords_spacy = False
elif self.remove_stopwords_spacy:
assert stopword_list == language
else:
# if remove_stopwords_spacy is false, then use MALLET English stopwords
if 'english' in stopword_list:
stop_word_path = Path(__file__).parent.joinpath('stopwords', 'english.txt')
with open(stop_word_path) as fr:
stopwords = [line.strip() for line in fr.readlines()]
assert stopword_list == language
self.stopwords = stopwords
self.min_chars = min_chars
self.min_doc_words = min_words_docs
self.preprocessing_steps = []
def preprocess_dataset(self, documents_path, labels_path=None, multilabel=False):
"""
preprocess the input dataset
:param documents_path: path to the documents file. Each row of the file represents a document
:type documents_path: str
:param labels_path: path to the documents file. Each row of the file represents a label. Its index corresponds
to the index of the documents file (default: None)
:type labels_path: str
:param multilabel: if true, a document is supposed to have more than one label (labels are split by whitespace)
:type multilabel: bool
:return octis.dataset.dataset.Dataset
"""
docs = [line.strip() for line in open(documents_path, 'r').readlines()]
if self.num_processes is not None:
# with Pool(self.num_processes) as p:
# docs = p.map(self.simple_preprocessing_steps, docs)
chunksize = max(1, len(docs) // (self.num_processes * 20))
docs_list = process_map(self.simple_preprocessing_steps, docs, max_workers=self.num_processes, chunksize=chunksize)
else:
docs = list(map(self.simple_preprocessing_steps, tqdm(docs)))
if self.lowercase:
self.preprocessing_steps.append("lowercase")
if self.remove_punctuation:
self.preprocessing_steps.append('remove_punctuation')
if self.lemmatize:
self.preprocessing_steps.append('lemmatize')
vocabulary = self.filter_words(docs)
print("created vocab")
print(len(vocabulary))
final_docs, final_labels, document_indexes = [], [], []
if labels_path is not None:
if multilabel:
labels = [
line.strip().split()
for line in open(labels_path, 'r').readlines()]
else:
labels = [
line.strip()
for line in open(labels_path, 'r').readlines()]
vocab = set(vocabulary)
for i, doc, label in zip(range(len(docs)), docs, labels):
new_doc = [w for w in doc.split() if w in vocab]
if len(new_doc) > self.min_doc_words:
final_docs.append(new_doc)
final_labels.append(label)
document_indexes.append(i)
labels_to_remove = set([k for k, v in dict(
Counter(final_labels)).items() if v <= 3])
if len(labels_to_remove) > 0:
docs = final_docs
labels = final_labels
document_indexes, final_labels, final_docs = [], [], []
for i, doc, label in zip(range(len(docs)), docs, labels):
if label not in labels_to_remove:
final_docs.append(doc)
final_labels.append(label)
document_indexes.append(i)
else:
vocab = set(vocabulary)
for i, doc in enumerate(docs):
new_doc = [w for w in doc.split() if w in vocab]
if len(new_doc) > self.min_doc_words:
final_docs.append(new_doc)
document_indexes.append(i)
self.preprocessing_steps.append('filter documents with less than ' + str(self.min_doc_words) + " words")
if self.verbose:
print("words filtering done")
metadata = {"total_documents": len(docs), "vocabulary_length": len(vocabulary),
"preprocessing-info": self.preprocessing_steps
# ,"labels": list(set(final_labels)), "total_labels": len(set(final_labels))
}
if self.split:
if len(final_labels) > 0:
train, test, y_train, y_test = train_test_split(
range(len(final_docs)), final_labels, test_size=0.15, random_state=1, shuffle=True)#stratify=final_labels)
train, validation = train_test_split(train, test_size=3 / 17, random_state=1, shuffle=True)# stratify=y_train)
partitioned_labels = [final_labels[doc] for doc in train + validation + test]
partitioned_corpus = [final_docs[doc] for doc in train + validation + test]
document_indexes = [document_indexes[doc] for doc in train + validation + test]
metadata["last-training-doc"] = len(train)
metadata["last-validation-doc"] = len(validation) + len(train)
if self.save_original_indexes:
return Dataset(partitioned_corpus, vocabulary=vocabulary, metadata=metadata,
labels=partitioned_labels, document_indexes=document_indexes)
else:
return Dataset(partitioned_corpus, vocabulary=vocabulary, metadata=metadata,
labels=partitioned_labels)
else:
train, test = train_test_split(range(len(final_docs)), test_size=0.15, random_state=1)
train, validation = train_test_split(train, test_size=3 / 17, random_state=1)
metadata["last-training-doc"] = len(train)
metadata["last-validation-doc"] = len(validation) + len(train)
partitioned_corpus = [final_docs[doc] for doc in train + validation + test]
document_indexes = [document_indexes[doc] for doc in train + validation + test]
if self.save_original_indexes:
return Dataset(partitioned_corpus, vocabulary=vocabulary, metadata=metadata, labels=final_labels,
document_indexes=document_indexes)
else:
return Dataset(partitioned_corpus, vocabulary=vocabulary, metadata=metadata, labels=final_labels,
document_indexes=document_indexes)
else:
if self.save_original_indexes:
return Dataset(final_docs, vocabulary=vocabulary, metadata=metadata, labels=final_labels,
document_indexes=document_indexes)
else:
return Dataset(final_docs, vocabulary=vocabulary, metadata=metadata, labels=final_labels)
def filter_words(self, docs):
if self.vocabulary is not None:
self.preprocessing_steps.append('filter words by vocabulary')
self.preprocessing_steps.append('filter words with document frequency lower than ' + str(self.min_df) +
' and higher than ' + str(self.max_df))
self.preprocessing_steps.append('filter words with less than ' + str(self.min_chars) + " character")
vectorizer = TfidfVectorizer(max_df=self.max_df, min_df=self.min_df, vocabulary=self.vocabulary,
token_pattern=r"(?u)\b\w{" + str(self.min_chars) + ",}\b",
lowercase=self.lowercase, stop_words=self.stopwords)
elif self.max_features is not None:
self.preprocessing_steps.append('filter vocabulary to ' + str(self.max_features) + ' terms')
self.preprocessing_steps.append('filter words with document frequency lower than ' + str(self.min_df) +
' and higher than ' + str(self.max_df))
self.preprocessing_steps.append('filter words with less than ' + str(self.min_chars) + " character")
# we ignore df_max_freq e df_min_freq because self.max_features is not None
vectorizer = TfidfVectorizer(lowercase=self.lowercase, max_features=self.max_features,
stop_words=self.stopwords,
token_pattern=r"(?u)\b[\w|\-]{" + str(self.min_chars) + r",}\b")
else:
#string.punctuation
self.preprocessing_steps.append('filter words with document frequency lower than ' + str(self.min_df) +
' and higher than ' + str(self.max_df))
self.preprocessing_steps.append('filter words with less than ' + str(self.min_chars) + " character")
vectorizer = TfidfVectorizer(max_df=self.max_df, min_df=self.min_df, lowercase=self.lowercase,
token_pattern=r"(?u)\b[\w|\-]{" + str(self.min_chars) + r",}\b",
stop_words=self.stopwords)
vectorizer.fit_transform(docs)
vocabulary = vectorizer.get_feature_names_out()
return vocabulary
'''
def _foo(self, docs, vocabulary, labels_path):
final_docs, final_labels = [], []
if labels_path is not None:
labels = [line.strip() for line in open(labels_path, 'r').readlines()]
for doc, label in zip(docs, labels):
new_doc = [w for w in doc.split() if w in set(vocabulary)]
if len(new_doc) > self.min_doc_words:
final_docs.append(new_doc)
final_labels.append(label)
return final_docs, final_labels
else:
for doc in docs:
new_doc = [w for w in doc.split() if w in set(vocabulary)]
if len(new_doc) > self.min_doc_words:
final_docs.append(new_doc)
return final_docs, []
'''
def simple_preprocessing_steps(self, doc):
new_d = doc
new_d = new_d.replace('\n', '')
new_d = new_d.replace('\t', '')
if self.lowercase:
new_d = new_d.lower()
if self.lemmatize:
if self.remove_stopwords_spacy:
new_d = ' '.join([token.lemma_ for token in self.spacy_model(new_d) if not token.is_stop])
elif self.stopwords:
new_d = ' '.join(
[token.lemma_ for token in self.spacy_model(new_d) if token.lemma_ not in set(self.stopwords)])
else:
new_d = ' '.join([token.lemma_ for token in self.spacy_model(new_d)])
if self.remove_punctuation:
new_d = new_d.translate(str.maketrans(self.punctuation, ' ' * len(self.punctuation)))
if self.remove_numbers:
new_d = new_d.translate(str.maketrans("0123456789", ' ' * len("0123456789")))
new_d = " ".join(new_d.split())
return new_d