-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
99 lines (84 loc) · 1.78 KB
/
parser.py
File metadata and controls
99 lines (84 loc) · 1.78 KB
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
import xml.sax
import wiki_parser as wparser
import time
pid='';
page_text='';
revision=0;
contributor=0;
temp_pid='';
progrss=0;
tag=0;
ns='';
def conv(a):
b='';
for i in range(0,len(a)):
if (a[i].isdigit()):
b+=a[i];
return str(b);
class MyHandler(xml.sax.handler.ContentHandler):
def __init__(self):
xml.sax.ContentHandler.__init__(self);
self.tagname=''
def startElement(self,name,attr):
global pid;
global revision;
global tag;
global contributor;
self.tagname=name;
if (name=='revision'):
revision=1;
elif (name=='contributor'):
contributor=1;
elif (name=='id' and revision==0 and contributor==0):
pid='';
elif (name=='id' and revision==1 and contributor==0):
pid+='_'
if (name=='text' or tag!=0):
tag+=1;
def endElement(self,name):
global progrss;
global page_text;
global pid;
global revision;
global tag;
global contributor;
global ns;
global temp_pid;
if (name=='text'):
if(ns==0):
wparser.parse_text(page_text,pid); #Parses the given text
page_text='';
contributor=0;
revision=0;
progrss+=1;
elif(name=='id' and contributor==0):
pid+=str(int(temp_pid));
temp_pid='';
elif (name=='ns'):
ns=int(ns)
if (tag>0):
tag-=1;
def characters(self,content):
global page_text;
global pid;
global temp_pid;
global ns;
if (self.tagname=='id' and contributor==0):
temp_pid+=content;
elif (self.tagname=='text' or tag==1):
page_text+=content;
elif (self.tagname=='ns'):
bb=conv(content);
if (bb!=''):
ns=bb;
def main():
global page_text
tic=time.time();
parser=xml.sax.make_parser()
handler=MyHandler()
parser.setContentHandler(handler)
parser.parse('b.xml');
toc=time.time();
print(str(toc-tic) + ' seconds elapsed');
if __name__=='__main__' :
main();