-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_files.py
164 lines (144 loc) · 7.38 KB
/
index_files.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
#!/usr/bin/env python
# coding=utf-8
import codecs, gzip, re, sys, os, lucene, threading, time
from datetime import datetime
from operator import itemgetter
import HTMLParser
"""
This class is loosely based on the Lucene (java implementation) demo class
org.apache.lucene.demo.IndexFiles. It will take a directory as an argument
and will index all of the files in that directory and downward recursively.
It will index on the file path, the file name and the file contents. The
resulting Lucene index will be placed in the current directory and called
'index'.
"""
class Ticker(object):
def __init__(self):
self.tick = True
def run(self):
while self.tick:
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1.0)
class IndexFiles(object):
"""Usage: python IndexFiles <doc_directory>"""
def __init__(self, root, storeDir, analyzer):
self.root = root
if not os.path.exists(storeDir):
os.mkdir(storeDir)
store = lucene.SimpleFSDirectory(lucene.File(storeDir))
self.writer = lucene.IndexWriter(store, analyzer, True)
self.writer.setMaxFieldLength(1048576)
def optimizeIndexer(self):
ticker = Ticker()
print 'commit index',
threading.Thread(target=ticker.run).start()
self.writer.optimize()
self.writer.close()
ticker.tick = False
print 'done'
def runIndexer(self):
self.indexDocs()
self.optimizeIndexer()
def indexDocs(self):
for root, dirnames, filenames in os.walk(self.root):
for filename in filenames:
if not filename.endswith('.txt'):
continue
print "adding", filename
try:
path = os.path.join(root, filename)
docsfile = open(path)
contents = unicode(docsfile.read(), 'iso-8859-1')
docsfile.close()
doc = lucene.Document()
doc.add(lucene.Field("name", filename, self.t1))
doc.add(lucene.Field("path", path, self.t2))
if len(contents) > 0:
doc.add(lucene.Field("contents", contents, self.t2))
else:
print "warning: no content in %s" % filename
self.writer.addDocument(doc)
except Exception, e:
print "Failed in indexDocs:", e
class IndexTweets(IndexFiles):
"""Usage: python IndexTweets <tweet_directory> | <tweet_file_path>"""
def __init__(self, root, storeDir, analyzer, location_hash):
super(IndexTweets, self).__init__(root, storeDir, analyzer)
self.location_hash = location_hash
self.linecutoff = 50000000
self.RTre = re.compile("RT @\w+")
self.tzre = re.compile("\+\w+")
#self.emoticonre = re.compile(u"http(s)?[:]//|[=<>]?[;:]+[\^'-]?[\\\/)(\]\[}{PpboO0|]+[X#]?|[+=>\^Tㅜㅠㅡ][ㅁㅇ._-]*[+=<\^Tㅜㅠㅡ]")
self.emoticonre = re.compile(u"http(s)?[:]//|[=<>]?(?<![A-Za-z0-9])[;:]{1}[\^'-]?[\\\/)(\]\[}{DPpboO|]+|[=<>]?(?<![A-Za-z0-9])[;:]{1}[\^'-]?[0X#]\s|[>\^ㅜㅠㅡ@][ㅁㅇ0oO\._\-]*[<\^ㅜㅠㅡ@];*|[T\-oOXx+=;][\._]+[T\-+oOXx=;];*|(?<!\w)TT\s")
self.emoticonhash = {}
self.emoticonhashfile = codecs.open("/Volumes/TerraFirma/SharedData/vdb5/emoticons_raw_files/emoticons_list.txt", encoding='utf-8', mode='w')
self.h = HTMLParser.HTMLParser()
def runIndexer(self):
if self.root.endswith('tweets.txt.gz'):
self.indexOneDoc()
self.optimizeIndexer()
def indexOneDoc(self):
try:
docsfile = gzip.open(self.root)
lctr = 0
for line in docsfile:
lctr+=1
if lctr%100000 == 0: print "on line: ", lctr, " at: ", time.time()
if lctr > self.linecutoff: break
tweet_id, user_id, date, tweet_id_replied, user_id_replied, source, some_flag, another_flag, location, text = unicode(line, 'utf-8').split('\t')
if not user_id_replied:
user_id_replied = '0'
if date:
tz = re.search(self.tzre, date).group(0)
#timestamp = str(int(time.mktime(time.strptime(date, "%a %b %d %H:%M:%S " + tz + " %Y"))))
timestamp = int(time.mktime(time.strptime(date, "%a %b %d %H:%M:%S " + tz + " %Y")))
else:
timestamp = '0'
text = self.h.unescape(text)
country = self.location_hash.get(user_id,"Unknown")
RT_search = re.search(self.RTre, text)
if RT_search: RT_name = RT_search.group(0).split()[1].lstrip("@")
else: RT_name = ''
emoticon_iter = re.finditer(self.emoticonre, text)
emoticon_str = ''
while True:
try:
emoticon_char = emoticon_iter.next().group(0)
emoticon_str += emoticon_char + " "
self.emoticonhash[emoticon_char] = self.emoticonhash.get(emoticon_char,0)+1
except Exception, e:
break
doc = lucene.Document()
doc.add(lucene.Field("tweet_id", tweet_id, lucene.Field.Store.YES, lucene.Field.Index.NO))
doc.add(lucene.Field("user_id", user_id, lucene.Field.Store.YES, lucene.Field.Index.UN_TOKENIZED))
doc.add(lucene.Field("user_id_replied", user_id_replied, lucene.Field.Store.YES, lucene.Field.Index.UN_TOKENIZED))
doc.add(lucene.Field("source", source, lucene.Field.Store.YES, lucene.Field.Index.UN_TOKENIZED))
doc.add(lucene.Field("country", country, lucene.Field.Store.YES, lucene.Field.Index.UN_TOKENIZED))
doc.add(lucene.NumericField("timestamp",4,lucene.Field.Store.YES, True).setIntValue(timestamp))
#doc.add(lucene.Field("timestamp", timestamp, lucene.Field.Store.YES, lucene.Field.Index.UN_TOKENIZED))
#clean_text = self.h.unescape(text)
if len(text) > 0: doc.add(lucene.Field("text", text, lucene.Field.Store.NO, lucene.Field.Index.TOKENIZED, lucene.Field.TermVector.YES))
if len(emoticon_str) > 0:
#print "emoticon_str: ", emoticon_str
doc.add(lucene.Field("emoticons", emoticon_str, lucene.Field.Store.YES, lucene.Field.Index.TOKENIZED))
self.writer.addDocument(doc)
for emoticon_char, count in sorted(self.emoticonhash.items(), key=itemgetter(1), reverse=True):
self.emoticonhashfile.write(emoticon_char + u"," + unicode(count) + u"\n")
self.emoticonhashfile.close()
except Exception, e:
print "failed to index file: ", docsfile, " with error: ", e
if __name__ == '__main__':
if len(sys.argv) < 2:
print IndexFiles.__doc__
sys.exit(1)
lucene.initVM()
print 'lucene', lucene.VERSION
start = datetime.now()
try:
IndexFiles(sys.argv[1], "index", lucene.StandardAnalyzer(lucene.Version.LUCENE_CURRENT))
end = datetime.now()
print end - start
except Exception, e:
print "Failed: ", e
raise e