-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMRGenerateESARepresentation.py
85 lines (62 loc) · 2.54 KB
/
MRGenerateESARepresentation.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
#import psyco
import logging, os
import codecs
from bz2 import *
import re
from ufo import *
from utils.cleaner import *
from string import lower
from collections import defaultdict
from random import random
ClientRegistry.Port = 65520 # port to connect to the mothership
Mapper.Port = 65521 # port for the RPC server on the child
# Output level
logger.setLevel( logging.INFO )
# HeadWords = 'wordsim-353.heads.bz2'
HeadWords = 'wikipedia-term-5v-10df.txt.bz2'
MinDocLength = 100
BZ2ShardedMothership.OutputFile = 'ESA-%s-min%dw.inverted-index.bz2' % (HeadWords, MinDocLength)
BannedArticleTypes = ['Image:', 'Wikipedia:', 'Template:', 'Category:', 'File:']
class MyMapper(Mapper):
def initialize(self, arg):
self.heads = set()
logger.info('Reading in clean words...')
reader = codecs.getreader('utf8')(BZ2File(HeadWords))
for line in reader.readlines():
word = line.strip()
if word:
self.heads.add(word)
reader.close()
logger.info('done.')
def map(self, token):
word_index = defaultdict(int)
document_size = defaultdict(int)
vocab_size = defaultdict(int)
logger.info('Mapping token [%r]' % token)
reader = codecs.getreader('utf8')(BZ2File(token))
for (doc_count, (current_title, document, _)) in enumerate(clean_wikipedia_documents(reader, BannedArticleTypes,
filter_extraneous=False)):
terms = document.split()
if len(terms) > MinDocLength:
for w in terms:
if w in self.heads:
word_index[(current_title, w)] += 1
document_size[current_title] = len(terms)
vocab_size[current_title] = len(set(terms))
# Collect doc size for everything, not just docs we find useful
# words in
self.output('_\t%s\t%d\t%d\t%d' %
(current_title,0,document_size[current_title],
vocab_size[current_title]))
if doc_count % 100 == 0:
logger.info('Processed %d documents' % doc_count)
reader.close()
for ((c,w), tf) in word_index.iteritems():
self.output('%s\t%s\t%d\t%d\t%d' % (w,c,tf,document_size[c], vocab_size[c]))
# Return success
return True
UFOMapper = MyMapper
UFOMothership = BZ2ShardedMothership
if __name__ == '__main__':
#psyco.full()
start_ufo(UFOMapper, UFOMothership)