-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_parser.py
More file actions
47 lines (39 loc) · 1.47 KB
/
xml_parser.py
File metadata and controls
47 lines (39 loc) · 1.47 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
import xml.sax
from page import Page
from indexer import Indexer
class HandlePages(xml.sax.ContentHandler):
def __init__(self) -> None:
super().__init__()
self.currTag = ''
self.title = ''
self.text = ''
self.pageId = 0
def startElement(self, tag, attrs):
self.currTag = tag
def isUseful(self):
return not (self.title.startswith("File:") or self.title.startswith("Template:"))
def endElement(self, tag):
if tag == 'page':
if self.isUseful():
pag = Page()
title, infobox, body, categories, links, references = pag.processCorpus(self.text, self.title)
i = Indexer(title, body, infobox, categories, links, references, self.title.strip())
i.buildIndex()
self.pageId = 0
self.currTag = ''
self.title = ''
self.text = ''
def characters(self, content):
if self.currTag == 'title':
self.title = f"{self.title}{content}"
elif self.currTag == 'text':
self.text = f"{self.text}{content}"
elif self.currTag == 'id' and self.pageId == '':
self.pageId = content
class Parser():
def __init__(self, file) -> None:
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
handler = HandlePages()
parser.setContentHandler(handler)
parser.parse(file)