-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
328 lines (269 loc) · 8.64 KB
/
Copy pathutil.py
File metadata and controls
328 lines (269 loc) · 8.64 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
'''
This module holds all the helper methods needed in the other modules.
'''
import os
import re
import inspect
import logging
import multiprocessing
import nltk
import gensim
CLASS_MAP = {
'MAKEDONIJA': 0,
'SVET': 1,
'EKONOMIJA': 2,
'SCENA': 3,
'ZDRAVJE': 4,
'KULTURA': 5,
'TEHNOLOGIJA': 6,
'ZIVOT': 7,
'FUDBAL': 8,
'KOSARKA': 9,
'RAKOMET': 10,
'TENIS': 11,
'MOTO': 12
}
INVERSE_CLASS_MAP = {
0: 'MAKEDONIJA',
1: 'SVET',
2: 'EKONOMIJA',
3: 'SCENA',
4: 'ZDRAVJE',
5: 'KULTURA',
6: 'TEHNOLOGIJA',
7: 'ZIVOT',
8: 'FUDBAL',
9: 'KOSARKA',
10: 'RAKOMET',
11: 'TENIS',
12: 'MOTO',
}
def to_unicode(text, code='utf-8'):
'''
Converts text into unicode
Arguments:
text : <str>
The text to be converted in unicode
code : <str>
Codec value
Default value is: utf-8
Returns:
text : <unicode>
The converted text
'''
if isinstance(text, str):
if not isinstance(text, unicode):
text = unicode(text, code)
return text
def to_utf(text, code='utf-8'):
'''
Converts text into bytestring utf-8
Arguments:
text : <unicode>
The text to be converted in bytestring
code : <str>
Codec value
Default value is: utf-8
Returns:
text : <str>
The converted text
'''
if isinstance(text, unicode):
text = text.encode(code)
elif isinstance(text, str):
text = unicode(text, code).encode(code)
return text
def setup_logger(logger_name, log_file, level=logging.INFO, to_stream=True, to_file=True):
'''
Creates a logger with the logger_name
Arguments:
logger_name : <str>
The name of the logger that will be created
log_file : <str>
The name of the file of where the logs will be writter
level : logging.LEVEL
Logging level
'''
logger = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s [%(filename)s:%(lineno)d]: %(message)s',\
datefmt='%m/%d/%Y %H:%M:%S')
logger.setLevel(level)
if to_file:
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if to_stream:
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
def get_logger(logger_name):
'''
Returns the logger with name logger_name
Arguments:
logger_name : <str>
The name of the logger
Returns:
logger : <str>
The logger with the given logger_name
'''
return logging.getLogger(logger_name)
def sentence_tokenizer(text):
'''
Tokenizes text into sentences using nltk.sent_tokenize tokenizer
Arguments:
text : <unicode>
The text to be tokenized into sentences
Returns:
sentences : <list>
The sentences in the text
'''
sentences = nltk.sent_tokenize(text)
return sentences
def word_tokenizer(text):
'''
Tokenizes text into words using regular expression
Arguments:
text : <unicode>
The text to be tokenized into words
Returns:
words : <list>
The words in the text
'''
tokenize_re = re.compile(r'(?u)\w+')
words = tokenize_re.findall(text)
return words
def process_line(line):
'''
Processes one line of the dataset
The line is in format:
class_name\tlink_address\ttext\n
Arguments:
line : <str>
The line to be processed
Returns:
processed_line : <tuple>
The processed line in the format:
(class_id <int>, link_address <str>, words_in_sentences <list>)
'''
line = to_unicode(line)
(class_name, link_address, text) = line.split('\t')
sentences = []
for sentence in sentence_tokenizer(text):
sentence = sentence.lower()
sentence.strip()
sentences.append(word_tokenizer(sentence))
return (CLASS_MAP[class_name], link_address, sentences)
def phrase_detection(dataset, delimeter='_'):
'''
Detects phrases from the dataset.
Arguments:
dataset : <list>
The dataset on which the phrases should be detected.
delimeter : <str>
The glue character used to join phrases.
Returns:
dataset : <list>
The dataset where each phrase have been merge into a single word.
The dataset is in format:
A list of tuples in format
(class_id <int>, link_address <str>, words_in_sentences <list>)
where words_in_sentences is list of list of words
[[word <str>, ...], ...]
'''
sentences = []
for line in dataset:
for sentence in line[2]:
sentences.append(sentence)
phrases = gensim.models.phrases.Phrases(sentences)
for line_num in range(len(dataset)):
(class_name, link_address, sentences) = dataset[line_num]
sentences = list(phrases[sentences])
dataset[line_num] = (class_name, link_address, sentences)
return dataset
def read_dataset_threaded(file_name, processes=4, logger_name=__name__):
'''
Reads the dataset from the file with file_name
It uses #processes to do the job
The dataset format in the file is:
class_name\tlink_address\ttext\n
Arguments:
file_name : <str>
The file name of the file to be read
processes : <int>
Number of workers to be used
Default value is: 4
Returns:
dataset : <list>
Returns a list of tuples in format
(class_id <int>, link_address <str>, words_in_sentences <list>)
where words_in_sentences is list of list of words
[[word <str>, ...], ...]
'''
logger = get_logger(logger_name)
logger.info('Function={0}, FileName={1}, Message="{2}"'.format(
inspect.currentframe().f_code.co_name,
file_name,
'Started processing file'
))
file_reader = open(file_name, 'r')
lines = [line for line in file_reader]
file_reader.close()
pool = multiprocessing.Pool(processes=processes)
dataset = pool.map(process_line, lines)
pool.close()
pool.join()
return dataset
def read_dataset(file_name, logger_name=__name__):
'''
Reads the dataset from the file with file_name
The dataset format in the file is:
class_name\tlink_address\ttext\n
Arguments:
file_name : <str>
The file name of the file to be read
Returns:
dataset : <list>
Returns a list of tuples in format
(class_id <str>, link_address <str>, words_in_sentences <list>)
where words_in_sentences is list of list of words
[[word <str>, ...], ...]
'''
logger = get_logger(logger_name)
logger.info('Function={0}, FileName={1}, Message="{2}"'.format(
inspect.currentframe().f_code.co_name,
file_name,
'Started processing file'
))
file_reader = open(file_name, 'r')
lines = [line for line in file_reader]
file_reader.close()
dataset = []
for line in lines:
line = to_unicode(line)
(class_name, link_address, text) = line.split('\t')
sentences = []
for sentence in sentence_tokenizer(text):
sentence = sentence.lower()
sentence.strip()
sentences.append(word_tokenizer(sentence))
dataset.append((CLASS_MAP[class_name], link_address, sentences))
return dataset
def main():
'''
Main entry.
Here goes code for testing the methods and classes of this module.
'''
logger_name = 'Testing'
setup_logger(logger_name, 'Test.log', to_file=False)
dataset = read_dataset_threaded(os.path.join('data', 'raw_texts.txt'), processes=4,
logger_name=logger_name)
dataset = phrase_detection(dataset)
print dataset
for line in dataset:
for sentence in line[2]:
for word in sentence:
print word,
print '-',
print ''
if __name__ == '__main__':
main()