-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMRWikipediaGenerateLinkGraph.py
67 lines (49 loc) · 1.92 KB
/
MRWikipediaGenerateLinkGraph.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
#import psyco
import logging, os
import codecs
from bz2 import *
import re
from ufo import *
from utils.cleaner import *
from string import lower
ClientRegistry.Port = 65520 # port to connect to the mothership
Mapper.Port = 65521 # port for the RPC server on the child
BadRedirectLinkCountThreshold = 4
# Output level
logger.setLevel( logging.INFO )
BannedArticleTypes = ['Image:', 'Wikipedia:']
BZ2ShardedMothership.OutputFile = 'wikipedia-link-graph.txt.bz2'
SourceDataType = 'wikipedia-nospace'
class MyMapper(Mapper):
def map(self, token):
logger.info('Mapping token [%r]' % token)
inside_body = False # are we inside the body text or not
buffer = []
current_title = None
doc_count = 0
reader = codecs.getreader('utf8')(BZ2File(token))
for (doc_count, (current_title, document, links, flags)) in get_document_iterator(SourceDataType, token):
document = document.replace('<CR>', ' ')
split_doc = document.split()
output_set = set()
for link in links:
link = link.replace('[[','').replace(']]','')
if link:
#print ('%s\t%s' % (current_title,
# link)).encode('utf8','replace')
if 'REDIRECT' in flags and len(links) <= BadRedirectLinkCountThreshold:
output_set.add('REDIRECT\t%s\t%s' % (current_title, link))
else:
output_set.add('NORMAL\t%s\t%s' % (current_title, link))
for x in output_set:
self.output(x)
if doc_count % 100 == 0:
logger.info('Processed %d documents' % doc_count)
reader.close()
# Return success
return True
UFOMapper = MyMapper
UFOMothership = BZ2ShardedMothership
if __name__ == '__main__':
#psyco.full()
start_ufo(UFOMapper, UFOMothership)