-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMRWikipediaGenerateLanguageLinks.py
77 lines (55 loc) · 2.3 KB
/
MRWikipediaGenerateLanguageLinks.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
#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:', 'Template:']
SourceDataType = 'wikipedia-nospace'
MinDocumentLength = 1000
SkipLanguages = set(['fr','de','ja','zh','it','pl','es','ru','pt','nl'])
BZ2ShardedMothership.OutputFile = '%s-language-links-%dmin-skip-%s.txt.bz2' % (SourceDataType, MinDocumentLength, '-'.join(SkipLanguages))
class MyMapper(Mapper):
def process(self, current_title, split_doc, links):
if len(split_doc) > MinDocumentLength:
output_set = set()
found = set()
for link in links:
# self.output('%s\t%s' % (current_title, link))
# print current_title, link.encode('ascii', 'replace')
for lang in SkipLanguages:
if link.startswith('[['+lang+':'):
found.add(lang)
missing = SkipLanguages - found
if missing:
self.output('%s\t%d\t%d\t%s' % (current_title, len(split_doc),
len(missing), ','.join(missing)))
def map(self, token):
from utils import get_document_iterator
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, BannedArticleTypes):
document = document.replace('<CR>', ' ')
split_doc = document.split()
self.process(current_title, split_doc, links)
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)