-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreader.py
More file actions
36 lines (26 loc) · 780 Bytes
/
reader.py
File metadata and controls
36 lines (26 loc) · 780 Bytes
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
"""
"""
import codecs
from bs4 import BeautifulSoup
from lxml import etree
from yaml import dump
def get_root(filename):
parser = etree.XMLParser(load_dtd=True, no_network=False)
tree = etree.parse(filename, parser=parser)
return tree.getroot()
def get_entries(filename):
root = get_root(filename)
lemmata = set()
d = {}
for entry in root.findall(".//entry"):
lemma = entry.get("key", "")
entry_bs = BeautifulSoup(etree.tostring(entry), features="lxml")
d[lemma] = entry_bs.text.strip()
lemmata.add(lemma)
return d
def save_yaml(data, filename):
with open(filename, "w") as f:
dump(data, f)
if __name__ == "__main__":
entries = get_entries("lewis.xml")
save_yaml(entries, "lewis.yaml")