-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlhandler.py
127 lines (99 loc) · 4.19 KB
/
xmlhandler.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
import re, sys, os, pickle
import xml.sax
import time
from indexgenerator import IndexGenerator
indGen = IndexGenerator()
class WikiContentHandler(xml.sax.ContentHandler):
def __init__(self):
self.docID = 0
self.CurrentTag = ""
self.CurrentPage = {
'title': '',
'infobox' : '',
'body' : '',
'categories' : '',
'references': '',
'externalLinks' : '',
'infoboxFound': 0,
'referencesFound': False,
'extLinksFound' : False
}
def startElement(self, tag, attributes):
self.CurrentTag = tag
def unsetPage(self):
self.CurrentPage = {
'title': '',
'body' : '',
'categories' : '',
'infobox' : '',
'references': '',
'externalLinks' : '',
'infoboxFound': 0,
'referencesFound': False,
'extLinksFound' : False
}
self.CurrentTag = ""
def endElement(self, tag):
if tag == "page":
global indGen
indGen.processDict(self.CurrentPage)
self.unsetPage()
def characters(self, content):
if self.CurrentTag == "title":
self.CurrentPage['title'] += content
elif self.CurrentTag == "text":
infoboxFound = re.search(r"{{Infobox", content, re.IGNORECASE)
condition_matched = False # To check if any condtion has already matched
if infoboxFound:
self.CurrentPage['infoboxFound'] = 1
condition_matched = True
if not condition_matched:
categoryFound = re.search(r'\[\[Category:', content, re.IGNORECASE)
if categoryFound:
self.CurrentPage['categories'] += content[categoryFound.start() :]
return
if not condition_matched:
referenceFound = re.search(r'==reference', content, re.IGNORECASE)
if referenceFound:
self.CurrentPage['referencesFound'] = True
condition_matched = True
if not condition_matched:
categoryFound = re.search(r'==(\ ?)External links==', content, re.IGNORECASE)
if categoryFound:
self.CurrentPage['extLinksFound'] = True
condition_matched = True
if not condition_matched:
self.CurrentPage['body'] += content
if self.CurrentPage['infoboxFound'] > 0:
self.CurrentPage['infobox'] += content
if '{{' in content:
self.CurrentPage['infoboxFound'] += 1
if '}}' in content:
self.CurrentPage['infoboxFound'] -= 1
if content.strip() == '}}':
self.CurrentPage['infoboxFound'] = 0
return
if self.CurrentPage['referencesFound']:
if content.startswith('==') and not (
content.startswith('==reference') or content.startswith('==Reference') ):
self.CurrentPage['referencesFound'] = False
if len(content) > 1:
self.CurrentPage['references'] += content
if self.CurrentPage['extLinksFound']:
if content[0] == '*':
self.CurrentPage['externalLinks'] += content
if __name__ == "__main__":
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
Handler = WikiContentHandler()
parser.setContentHandler(Handler)
start = time.time()
dumps = [u for u in sorted(os.listdir(sys.argv[1])) if u.startswith('enwiki')]
print(dumps)
for i in range(len(dumps)):
print("Dump #: ", str(i))
parser.parse(os.path.join(sys.argv[1], dumps[i].strip()))
indGen.write_index(i)
print(time.time()-start)
with open('ndocs.txt', 'w') as fp:
fp.write(str(indGen.docID + 1))