-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbow.py
768 lines (637 loc) · 24.9 KB
/
bow.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from six import text_type as str
from six import text_type as str
from six.moves import urllib
from six.moves.html_parser import HTMLParser
from zipfile import ZipFile
from json import JSONEncoder, JSONDecoder
import os
import copy
import uuid
import math
import inspect
import argparse
import unicodedata
__author__ = 'dmiro'
__version_info__ = (1, 0, 3)
__version__ = '.'.join(str(v) for v in __version_info__)
class BagOfWords(object):
"""Implementing a bag of words with their frequency of usages"""
def __init__(self, *args):
self._bow = {}
self.add(*args)
def __calc(self, operation, *args):
for words in args:
if isinstance(words, str):
words = [words]
for word in words:
n = 1
if isinstance(words, dict):
n = words[word]
self._bow[word] = operation(self._bow.get(word, 0), n)
if self._bow[word] < 1:
del self._bow[word]
def add(self, *args):
"""Add set of word, word list or word dict to bag of words.
:param args: set of word or word list to add
:return:nothing
"""
self.__calc(lambda x,y: x+y, *args)
def delete(self, *args):
"""Delete set of word, word list or word dict to bag of words.
:param args: set of word or word list to add
:return:nothing
"""
self.__calc(lambda x,y: x-y, *args)
@property
def rates(self):
"""Rate of occurrences
:return: Dict
"""
total = float(self.num())
if total:
return {k:v/total for k, v in list(self._bow.items())}
else:
return {}
@property
def sorted_rates(self):
"""Sorted rate of occurrences
:return: list sorted from greater to lowest rate
"""
total = float(self.num())
if total:
res = [(k,v/total) for k, v in list(self._bow.items())]
return sorted(res, key=lambda t: t[1], reverse=True)
else:
return []
def freq(self, word):
"""Frequency of a word.
:param word: word to query
:return: frequency
"""
if word in self._bow:
return self._bow[word]
else:
return 0
def rate(self, word):
"""Rate of a word.
:param word: word to query
:return: rate
"""
total = float(self.num())
if total:
return self.freq(word)/total
else:
return 0
def __add__(self, other):
""" Overloading of "+" operator to join BagOfWord+BagOfWord, BagOfWords+str or
BagOfWords+list.
:param other: BagOfWords, str or list
:return: BagOfWords
"""
result = self.copy()
if isinstance(other, BagOfWords):
result.add(dict(other))
else:
result.add(other)
return result
def __sub__(self, other):
""" Overloading of "-" operator to join BagOfWord+BagOfWord, BagOfWords+str or
BagOfWords+list.
:param other: BagOfWords, str or list
:return: BagOfWords
"""
result = self.copy()
if isinstance(other, BagOfWords):
result.delete(dict(other))
else:
result.delete(other)
return result
def __radd__(self, other):
return self.__add__(other)
def __rsub__(self, other):
return self.__sub__(other)
def __iter__(self):
return list(self._bow.items())
def __getitem__(self, offset):
return self._bow.__getitem__(offset)
def __len__(self):
return self._bow.__len__()
def __repr__(self):
return self._bow.__repr__()
def __delitem__(self, key):
del self._bow[key]
def __eq__(self, other):
if isinstance(other, BagOfWords):
return self._bow == other._bow
else:
return self._bow == other
def __ne__(self, other):
if isinstance(other, BagOfWords):
return self._bow !=other._bow
else:
return self._bow != other
def copy(self):
return copy.deepcopy(self)
def clear(self):
"""Clear word list."""
self._bow.clear()
def items(self):
"""Return an iterator over the word dictionary’s (word, frequency) pairs."""
return list(self._bow.items())
def keys(self):
"""Word list contained in the object."""
return list(self._bow.keys())
def words(self):
"""Word list contained in the object."""
return list(self.keys())
def items(self):
return list(self._bow.items())
def values(self):
return list(self._bow.values())
def num(self):
"""Total number of words."""
return sum(self._bow.values())
def __contains__(self, key):
"""Method key in y"""
return key in self._bow
def __call__(self, *args):
self.add(self, *args)
class TextFilters(object):
"""Filters for transforming a text"""
@staticmethod
def upper(text):
"""Convert text to uppercase."""
return text.upper()
@staticmethod
def lower(text):
"""Convert text to lowercase."""
return text.lower()
@staticmethod
def invalid_chars(text):
"""Remove invalid chars from a text."""
INVALID_CHARS = "/\¨º-~#@|¡!,·$%&()¿?'[^""`]+}{><;,:.=*^_"
return ''.join([char for char in text if char not in INVALID_CHARS])
@staticmethod
def html_to_text(text):
"""Conversion from HTML markup to plain text."""
class _HTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.text = []
def handle_data(self, data):
append = True
text = data.split()
if text:
tag = self.get_starttag_text()
if tag:
tag = tag.lower()
append = not tag.startswith(('<script','<style'))
if append:
self.text.extend(text)
parser = _HTMLParser()
parser.feed(text)
return ' '.join(parser.text)
class WordFilters(object):
"""Filters for transforming a set of words"""
@staticmethod
def stemming(lang, stemming, words):
"""Lemmatize text.
:param lang: lang text to lemmatize
:param stemming: number loops of lemmatizing
"""
import Stemmer as stemmer
try:
stemmer = stemmer.Stemmer(lang)
for i in range(stemming):
words = stemmer.stemWords(words)
return words
except KeyError:
return words
@staticmethod
def stopwords(lang, words):
"""Remove stop words from a text.
:param lang: language text where remove empty words
"""
import stop_words
try:
stopwords = stop_words.get_stop_words(lang)
return [word for word in words if word not in stopwords]
except stop_words.StopWordError:
return words
@staticmethod
def normalize(words):
"""Normalize chars from a text."""
return [''.join((char for char in unicodedata.normalize('NFD', str(word)) if unicodedata.category(char) != 'Mn'))
for word in words]
class Tokenizer(object):
"""Allows to break a string into tokens (set of words). Optionally allows you to set
filters before (TextFilters) and after (WordFilters) breaking the string into tokens.
"""
def __init__(self):
object.__init__(self)
def before_tokenizer(self, textfilters, text):
"""function to call before tokenizer text.
:param textfilters: static class with helper methods to filter text
:param text: The text will be split
"""
return text
def after_tokenizer(self, wordfilters, words):
"""function to call after tokenizer text.
:param wordfilters: static class with helper methods to filter words
:param words: split words
"""
return words
def tokenizer(self, text):
"""tokenize a text.
:param text: text to tokenizer
"""
text = self.before_tokenizer(TextFilters, text)
words = text.split()
words = self.after_tokenizer(WordFilters, words)
return words
def __call__(self, text):
return self.tokenizer(text)
class Document(BagOfWords, Tokenizer):
"""Implementing a bag of words where all words are of the same category. Retrieves
the text of a file, folder, url or zip, and also allows save or retrieve the Document
in json format.
"""
def __init__(self):
Tokenizer.__init__(self)
BagOfWords.__init__(self)
self.numdocs = 0
def _read(self, id_, text):
self.numdocs += 1
words = self.tokenizer(text)
self.add(words)
def clear(self):
"""Clear word list."""
BagOfWords.clear(self)
self.numdocs = 0
def read_text(self, text):
"""The text is stored in a BagOfWords identified by Id.
:param text: text to add a BagOfWords
:return: nothing
"""
self._read(None, text)
def read_files(self, *filenames):
"""The contents of each file or files is stored in a BagOfWord identified by the
filename.
:param *filenames: filenames to add
:return: nothing
"""
for filename in filenames:
text = open(filename, 'r').read()
self._read(filename, text)
def read_dir(self, *paths):
"""The contents of each file o files of a directory is stored in a BagOfWord
identified by the filename.
:param paths: directory or directories path to add files
:return: nothing
"""
for path in paths:
fn = []
for (_, _, filenames) in os.walk(path):
fn.extend([os.path.join(path,f) for f in filenames])
break
self.read_files(*fn)
def read_urls(self, *urls):
"""The contents of each url or urls is stored in a BagOfWord identified by the url.
:param *urls: urls to add
:return: nothing
"""
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20140129 Firefox/24.0'}
for url in urls:
req = urllib.Request(url=url, headers=headers)
text = urllib.request.urlopen(req).read()
self._read(url, text)
def read_zips(self, *zipfilenames):
"""The contents of each file o files of a zip file is stored in a BagOfWord
identified by the filename.
:param *zipfilenames: zip files to add
:return: nothing
"""
for zipfilename in zipfilenames:
input_zip = ZipFile(zipfilename)
for input_file in input_zip.infolist():
if input_file.file_size > 0:
text = input_zip.read(input_file)
self._read(input_file.filename, text)
def to_json(self):
"""Convert Document object to json string.
:return: json string
"""
class _Encoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, DocumentClass) or \
isinstance(obj, BagOfWords):
d = {'__class__': obj.__class__.__name__,
'__module__':obj.__module__}
d.update(obj.__dict__)
return d
if not inspect.isfunction(obj):
return super(_Encoder, self).default(obj)
return _Encoder().encode(self)
@staticmethod
def from_json(json_):
"""Convert json string to Document object.
:param json_: json string
:return: Document object
"""
class _Decoder(JSONDecoder):
def __init__(self):
JSONDecoder.__init__(self, object_hook=self.dict_to_object)
def dict_to_object(self, d):
if '__class__' in d:
class_name = d.pop('__class__')
module_name = d.pop('__module__')
module = __import__(module_name)
class_ = getattr(module, class_name)
## if issubclass(class_, BagOfWords):
## obj = class_(d.pop('_bow'))
## else:
## obj = class_()
obj = class_()
for k, v in list(d.items()):
setattr(obj, k, v)
return obj
return d
return _Decoder().decode(json_)
def save(self, filename):
"""Serialize Documentand save to a file in json format
:filename: file to save
:return: nothing
"""
with open(filename, 'w') as f:
json_ = self.to_json()
f.write(json_)
@staticmethod
def load(filename):
"""Load and deserialize Document from file saved in json format
:filename: file to load
:return: nothing
"""
with open(filename, 'r') as f:
json_ = f.read()
return Document.from_json(json_)
def __call__(self, text):
self.read_text(text)
class DocumentClass(Document):
"""Implementing a bag of words collection where all the bags of words are the same
category, as well as a bag of words with the entire collection of words. Each bag
of words has an identifier otherwise it's assigned an calculated identifier.
Retrieves the text of a file, folder, url or zip, and also allows save or retrieve
the collection in json format.
"""
def __init__(self):
Document.__init__(self)
self.docs = {}
def _read(self, id_, text):
words = self.tokenizer(text)
bow = BagOfWords(words)
if not id_:
id_ = uuid.uuid4().hex
if id_ in self.docs:
self.delete(dict(self.docs[id_]))
else:
self.numdocs += 1
self.docs[id_] = bow
self.add(words)
def clear(self):
"""Clear word and docs list."""
Document.clear(self)
self.docs = {}
def read_text(self, text, id_=None):
"""The text is stored in a BagOfWords identified by Id.
:param text: text to add a BagOfWords
:param id_: BagOfWord identifier. Optional. If not set then it's set an UUID4
identifier.
:return: nothing
"""
self._read(id_, text)
def __call__(self, text, id_=None):
self._read(id_, text)
class DefaultTokenizer(Tokenizer):
"""Tokenizer subclass that implements the text filters 'lower', 'invalid_chars'
and the word filters 'stopwords', 'stemming' and 'normalize'.
"""
def __init__(self, lang='english', stemming=1):
Tokenizer.__init__(self)
self.lang = lang
self.stemming = stemming
def before_tokenizer(self, textfilters, text):
text = textfilters.lower(text)
text = textfilters.invalid_chars(text)
return text
def after_tokenizer(self, wordfilters, words):
words = wordfilters.stopwords(self.lang, words)
words = wordfilters.stemming(self.lang, self.stemming, words)
words = wordfilters.normalize(words)
return words
class SimpleTokenizer(Tokenizer):
"""Tokenizer subclass that implements the text filters 'lower', 'invalid_chars'
and the word filter 'normalize'.
"""
def __init__(self):
Tokenizer.__init__(self)
def before_tokenizer(self, textfilters, text):
text = textfilters.lower(text)
text = textfilters.invalid_chars(text)
return text
def after_tokenizer(self, wordfilters, words):
words = wordfilters.normalize(words)
return words
class HtmlTokenizer(DefaultTokenizer):
"""Tokenizer subclass that implements the text filters 'htm_to_text', 'lower',
'invalid_chars' and the word filter 'normalize'.
"""
def __init__(self, lang='english', stemming=1):
DefaultTokenizer.__init__(self, lang, stemming)
def before_tokenizer(self, textfilters, text):
text = textfilters.html_to_text(text)
text = DefaultTokenizer.before_tokenizer(self, textfilters, text)
return text
class DefaultDocument(Document, DefaultTokenizer):
"""DefaultTokenizer and Document subclass"""
def __init__(self, lang='english', stemming=1):
Document.__init__(self)
DefaultTokenizer.__init__(self, lang, stemming)
class SimpleDocument(Document, SimpleTokenizer):
"""SimpleTokenizer and Document subclass"""
def __init__(self):
Document.__init__(self)
SimpleTokenizer.__init__(self)
class HtmlDocument(Document, HtmlTokenizer):
"""HtmlTokenizer and Document subclass"""
def __init__(self, lang='english', stemming=1):
Document.__init__(self)
HtmlTokenizer.__init__(self, lang, stemming)
class DefaultDocumentClass(DocumentClass, DefaultTokenizer):
"""DefaultTokenizer and DocumentClass subclass"""
def __init__(self, lang='english', stemming=1):
DocumentClass.__init__(self)
DefaultTokenizer.__init__(self, lang, stemming)
class SimpleDocumentClass(DocumentClass, SimpleTokenizer):
"""SimpleTokenizer and DocumentClass subclass"""
def __init__(self):
DocumentClass.__init__(self)
SimpleTokenizer.__init__(self)
class HtmlDocumentClass(DocumentClass, HtmlTokenizer):
"""HtmlTokenizer and DocumentClass subclass"""
def __init__(self, lang='english', stemming=1):
DocumentClass.__init__(self)
HtmlTokenizer.__init__(self, lang, stemming)
def document_classifier(document, **classifieds):
"""Text classification based on an implementation of Naive Bayes
:param document: document class instance to classify.
:param classifieds: dictionary with Document class instances have already been classified.
:return: list sorted from highest to lowest probability.
"""
# http://blog.yhathq.com/posts/naive-bayes-in-python.html
res = {}
total_docs = SimpleDocument()
for classified in list(classifieds.values()):
total_docs += classified
for k_classified, classified in list(classifieds.items()):
prior = float(classified.num()) / float(total_docs.num())
log_prob = 0.0
for word, value in list(document.items()):
if word in total_docs:
if classified.rate(word) > 0.0:
# log(probability) it requires fewer decimal places
log_prob += math.log(value * classified.rate(word) / total_docs.rate(word))
# log space to regular space
exp_prob = math.exp(log_prob + math.log(prior))
res[k_classified] = exp_prob
total = sum(res.values())
res = [(k,v/total) for k, v in list(res.items())]
return sorted(res, key=lambda t: t[1], reverse=True)
def _show_document(document, filename, verbose, top=50):
print('* filename: %s' % filename)
print('* filter:')
print(' type: %s' % document.__class__.__name__)
print(' lang: %s' % document.lang)
print(' stemming: %s' % document.stemming)
print('* total words: %d' % document.num())
print('* total docs: %d' % document.numdocs)
if verbose:
if top:
words = 'word (top %d)' % top
rates = document.sorted_rates[0:top]
else:
words = 'word'
rates = document.sorted_rates
posadj = len(str(len(rates)))+1
print('*','pos'.rjust(posadj),'|',words.ljust(35),'|','occurrence'.rjust(10),\
'|','rate'.rjust(10))
print(' ','-'*posadj,'|','-'*35,'|','-'*10,'|','-'*10)
for word, rate in rates:
print(' ',str(rates.index((word, rate))+1).rjust(posadj),'|',\
word.encode('utf-8').ljust(35),'|', str(document[word]).rjust(10),\
'|',('%.8f' % rate).rjust(10))
def _show(args):
try:
dc = Document.load(args.filename)
_show_document(document=dc, filename=args.filename, verbose=True, top=args.list_top_words)
except IOError:
print('No such classifier: %s' % args.filename)
def _create(args):
if args.filter == 'html':
dc = HtmlDocument(lang=args.lang_filter, stemming=args.stemming_filter)
else:
dc = DefaultDocument(lang=args.lang_filter, stemming=args.stemming_filter)
dc.save(args.filename)
_show_document(document=dc, filename=args.filename, verbose=False)
def _learn(args):
try:
dc = Document.load(args.filename)
if args.rewrite:
dc.clear()
print('\ncurrent')
print('=======')
_show_document(document=dc, filename=args.filename, verbose=False)
print('\nupdated')
print('=======')
if args.url:
dc.read_urls(*args.url)
if args.dir:
dc.read_dir(*args.dir)
if args.file:
dc.read_files(*args.file)
if args.zip:
dc.read_zips(*args.zip)
if not args.no_learn:
dc.save(args.filename)
_show_document(document=dc, filename=args.filename, verbose=True, top=args.list_top_words)
except IOError:
print('No such classifier: %s' % args.filename)
def _classify(args):
dclist = {}
for filename in args.classifiers:
dc = Document.load(filename)
dclist[filename] = dc
dc = list(dclist.values())[0].copy()
dc.clear()
## if args.filter == 'html':
## dc = HtmlDocument(lang=args.lang_filter, stemming=args.stemming_filter)
## else:
## dc = DefaultDocument(lang=args.lang_filter, stemming=args.stemming_filter)
if args.text:
dc.read_text(args.text)
elif args.url:
dc.read_urls(args.url)
elif args.file:
dc.read_files(args.file)
result = document_classifier(dc, **dclist)
print('*','classifier'.ljust(35),'|','rate'.rjust(10))
print(' ','-'*35,'|','-'*10)
for classifier, rate in result:
print(' ',classifier.encode('utf-8').ljust(35),'|',('%.8f' % rate).rjust(10))
def main():
parser = argparse.ArgumentParser(description='Manage several document to apply text classification.',
epilog="see https://github.com/dmiro/bagofwords for more info")
parser.add_argument('--version', action='version', version=__version__,
help='show version and exit')
subparsers = parser.add_subparsers(help='')
# create command
parser_create = subparsers.add_parser('create', help='create classifier')
parser_create.add_argument('filter', choices=['text', 'html'], help='filter type')
parser_create.add_argument('filename', help='file to be created where words learned are saved')
parser_create.add_argument('--lang-filter', default='english', type=str,
help='language text where remove empty words')
parser_create.add_argument('--stemming-filter', default=1, type=int,
help='number loops of lemmatizing')
parser_create.set_defaults(func=_create)
# learn command
parser_learn = subparsers.add_parser('learn', help='add words learned a classifier')
parser_learn.add_argument('filename', help='file to write words learned')
parser_learn.add_argument('--file', nargs='+', help='filenames to learn')
parser_learn.add_argument('--dir', nargs='+', help='directories to learn')
parser_learn.add_argument('--url', nargs='+', help='url resources to learn')
parser_learn.add_argument('--zip', nargs='+', help='zip filenames to learn')
parser_learn.add_argument('--no-learn', action='store_true', default=False,
help='not write to file the words learned')
parser_learn.add_argument('--rewrite', action='store_true', default=False,
help='overwrite the file')
parser_learn.add_argument('--list-top-words', default=50, type=int,
help='maximum number of words to list, 50 by default, -1 list all')
parser_learn.set_defaults(func=_learn)
# show command
parser_show = subparsers.add_parser('show', help='show classifier info')
parser_show.add_argument('filename', help='filename')
parser_show.add_argument('--list-top-words', default=50, type=int,
help='maximum number of words to list, 50 by default, -1 list all')
parser_show.set_defaults(func=_show)
# classify command
parser_classify = subparsers.add_parser('classify', help='Naive Bayes text classification')
parser_classify.add_argument('classifiers', nargs='+', help='classifiers')
parser_classify.add_argument('--file', help='file to classify')
parser_classify.add_argument('--url', help='url resource to classify')
parser_classify.add_argument('--text',help='text to classify')
parser_classify.set_defaults(func=_classify)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()