-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclassify.py
232 lines (187 loc) · 7.31 KB
/
classify.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
Copyright 2012 Lemur Consulting Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import re
import cPickle
import csv
import time
from lib import docx
from lib import xmldoc
from lib import taxonomy
from lib import ner
from lib import csv_unicode
import sunburnt
import httplib2
import settings
csv.field_size_limit(1000000000)
WORD_RE = re.compile(r'[\w\-\']+')
RE_WORD_DELIM = re.compile(r'[\w\-\']+|[\.\,\:\;\!\?\"\(\)\[\]]')
DELIMS = '.,:;!?"()[]'
LINKWORDS = set(('and', 'of', 'the'))
def process_dir(solr, dir):
""" Process the .docx files in dir, using the supplied taxonomies to
classify them, and storing the results in the xapian index.
"""
def f(arg, dirname, names):
for name in names:
if name.lower().endswith('.docx'):
process_file(solr, os.path.join(dirname, name))
os.path.walk(dir, f, None)
solr.commit()
def process_dir_xml(solr, dir):
""" Process the .xml files in dir, using the supplied taxonomies to
classify them, and storing the results in the xapian index.
"""
def f(arg, dirname, names):
for name in names:
if name.lower().endswith('.xml'):
process_xml_file(solr, os.path.join(dirname, name))
os.path.walk(dir, f, None)
solr.commit()
def process_dir_text(solr, dir):
""" Process the text files in dir, etc.
"""
docidbase = int(time.time()) - 1262304000
for count, name in enumerate(os.listdir(dir)):
if name[0] != '.':
with open(os.path.join(dir, name)) as f:
text = f.read()
name = name.decode('utf8', 'ignore')
docid = '%s.%s' % (docidbase, count)
update_solr(solr, docid, name, text.decode('utf8'))
solr.commit()
def process_file(solr, file):
_, _, text, title = docx.parse_docx(file)
filename = os.path.basename(file)
update_solr(solr, filename, title or filename, text)
solr.commit()
def process_xml_file(solr, file):
_, _, text, title = xmldoc.parse_xml(file)
filename = os.path.basename(file)
update_solr(solr, filename, title or filename, text)
solr.commit()
def process_csv(solr, path):
with open(path) as f:
for line in csv_unicode.UnicodeReader(f):
text = "%s\n\n%s" % (line[0], line[1])
update_solr(solr, line[2], line[6] or 'no title', text)
solr.commit()
def update_solr(solr, unique_term, title, text):
print 'updating', unique_term, title
doc = { 'title': title, 'text': text, 'doc_id': unique_term }
doc['entity'] = tuple(set(x for x in iter_entity_terms(text)))
solr.add(doc)
def iter_entity_terms(text):
for term in ner.get_entities(settings.ner_host, settings.ner_port, text):
if len(term) < 50:
yield unicode(term, "utf-8")
def iter_text_terms(text):
phrase = []
not1st = False
for word in RE_WORD_DELIM.findall(text):
if word in DELIMS:
if (len(phrase) > 1 and len(phrase) < 6 and
phrase[0] not in LINKWORDS and
phrase[-1] not in LINKWORDS):
yield ' '.join(phrase)
phrase = []
else:
if word[0].isalnum():
w = word.lower()
if w not in settings.stopwords:
yield w
if not1st:
if word[0].isupper():
phrase.append(word.lower())
elif phrase and word.lower() in LINKWORDS:
phrase.append(word.lower())
else:
if (len(phrase) > 1 and len(phrase) < 6 and
phrase[0] not in LINKWORDS and
phrase[-1] not in LINKWORDS):
yield ' '.join(phrase)
phrase = []
not1st = (word not in '.?!')
def get_doctext(solr, did):
""" Return the text for a document ID.
"""
return solr.query(doc_id=did).execute()[0]["text"]
if __name__ == '__main__':
import sys
import cPickle
# FIXME - help message
def _parse(path):
with open(path) as f:
if path.endswith('.xml'):
return taxonomy.parse_xml(f)
elif path.endswith('.csv'):
return taxonomy.parse_csv(f)
else:
print 'unrecognized input file extension (use .xml or .csv)'
sys.exit(1)
if sys.argv[1] == 'import':
with open(settings.taxonomy_path, 'w') as f:
taxes = [_parse(x) for x in sys.argv[2:]]
cPickle.dump(taxes, f)
print 'imported', len(taxes), 'taxonomies'
else:
with open(settings.taxonomy_path) as f:
taxes = cPickle.load(f)
h = httplib2.Http(cache=settings.http_cache)
_solr = sunburnt.SolrInterface(settings.solr_url, http_connection=h)
if sys.argv[1] == 'export':
with open(sys.argv[2], 'w') as f:
if sys.argv[2].endswith('.xml'):
if len(sys.argv) > 3:
solr = _solr
try:
rows = int(sys.argv[3])
except ValueError:
print 'rows value must be integer'
sys.exit(1)
else:
solr = None
rows = None
taxonomy.write_xml(f, taxes, _solr=solr, rows=rows)
elif sys.argv[2].endswith('.csv'):
taxonomy.write_csv(f, taxes)
else:
print 'unrecognized output file extension (use .xml or .csv)'
elif sys.argv[1] == 'lscat':
tax = taxonomy.term_for_path(taxes, sys.argv[2])
if tax:
print 'tax:', tax
for doc in taxonomy.get_docs_for_category(_solr, tax):
print doc
else:
print 'no matching taxonomy'
elif sys.argv[1] == 'classify':
with open(sys.argv[2], 'w') as f:
taxonomy.write_classification(_solr, f, taxes)
elif sys.argv[1] == 'doctext':
print get_doctext(_solr, int(sys.argv[2]))
elif sys.argv[1] == 'suggest':
for term in taxonomy.suggest_keywords(_solr, sys.argv[2:], [], 7):
print term
elif sys.argv[1] == 'docdir':
process_dir(_solr, sys.argv[2])
elif sys.argv[1] == 'xmldocdir':
process_dir_xml(_solr, sys.argv[2])
elif sys.argv[1] == 'docfile':
process_file(_solr, sys.argv[2])
elif sys.argv[1] == 'csvfile':
process_csv(_solr, sys.argv[2])
elif sys.argv[1] == 'textdir':
process_dir_text(_solr, sys.argv[2])