This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
forked from Torkn/tagger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_dict.py
executable file
·143 lines (107 loc) · 4.75 KB
/
build_dict.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
#!/usr/bin/env python
# Copyright (C) 2011 by Alessandro Presta
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE
'''
Usage: build_dict.py -o <output file> -s <stopwords file> <list of files>
'''
from __future__ import division
from tagger import Stemmer
from extras import SimpleReader
def build_dict(corpus, stopwords=None, measure='IDF'):
'''
@param corpus: a list of documents, represented as lists of (stemmed)
words
@param stopwords: the list of (stemmed) words that should have zero weight
@param measure: the measure used to compute the weights ('IDF'
i.e. 'inverse document frequency' or 'ICF' i.e.
'inverse collection frequency'; defaults to 'IDF')
@returns: a dictionary of weights in the interval [0,1]
'''
import collections
import math
try:
from collections import Counter
except ImportError:
# Python 2.5 and 2.6 lack the Counter class, so use the back-ported version
from counter import Counter
dictionary = {}
if measure == 'ICF':
words = [w for doc in corpus for w in doc]
term_count = Counter(words)
total_count = len(words)
scale = math.log(total_count)
for w, cnt in term_count.iteritems():
dictionary[w] = math.log(total_count / (cnt + 1)) / scale
elif measure == 'IDF':
corpus_size = len(corpus)
scale = math.log(corpus_size)
term_count = collections.defaultdict(int)
for doc in corpus:
words = set(doc)
for w in words:
term_count[w] += 1
for w, cnt in term_count.iteritems():
dictionary[w] = math.log(corpus_size / (cnt + 1)) / scale
if stopwords:
for w in stopwords:
dictionary[w] = 0.0
return dictionary
def build_dict_from_files(output_file, corpus_files, stopwords_file=None,
reader=SimpleReader(), stemmer=Stemmer(),
measure='IDF', verbose=False):
'''
@param output_file: the name of the file where the dictionary should be
saved
@param corpus_files: a list of files with words to process
@param stopwords_file: a file containing a list of stopwords
@param reader: the L{Reader} object to be used
@param stemmer: the L{Stemmer} object to be used
@param measure: the measure used to compute the weights ('IDF'
i.e. 'inverse document frequency' or 'ICF' i.e.
'inverse collection frequency'; defaults to 'IDF')
@param verbose: whether information on the progress should be
printed on screen
'''
import pickle
if verbose: print 'Processing corpus...'
corpus = []
for filename in corpus_files:
with open(filename, 'r') as doc:
corpus.append(reader(doc.read()))
corpus = [[w.stem for w in map(stemmer, doc)] for doc in corpus]
stopwords = None
if stopwords_file:
if verbose: print 'Processing stopwords...'
with open(stopwords_file, 'r') as sw:
stopwords = reader(sw.read())
stopwords = [w.stem for w in map(stemmer, stopwords)]
if verbose: print 'Building dictionary... '
dictionary = build_dict(corpus, stopwords, measure)
with open(output_file, 'wb') as out:
pickle.dump(dictionary, out, -1)
if __name__ == '__main__':
import getopt
import sys
try:
options = getopt.getopt(sys.argv[1:], 'o:s:')
output_file = options[0][0][1]
stopwords_file = options[0][1][1]
corpus = options[1]
except:
print __doc__
exit(1)
build_dict_from_files(output_file, corpus, stopwords_file, verbose=True)