Upload parse.py
Browse files
parse.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import os
|
3 |
+
import xml.etree.ElementTree as ET
|
4 |
+
|
5 |
+
def get_refs(root):
|
6 |
+
refs = []
|
7 |
+
|
8 |
+
for child in root:
|
9 |
+
if child.tag == 'scripRef':
|
10 |
+
if 'osisRef' in child.attrib:
|
11 |
+
refs.append(child.attrib['osisRef'])
|
12 |
+
|
13 |
+
return refs
|
14 |
+
|
15 |
+
def get_text_content(root, strip_ref=False):
|
16 |
+
""" Return the plain text content of a node
|
17 |
+
|
18 |
+
This will:
|
19 |
+
|
20 |
+
1. Remove all notes, footnotes, and references
|
21 |
+
2. Remove all newlines and combine whitespace to single spaces
|
22 |
+
"""
|
23 |
+
|
24 |
+
# Start with the root text prior to the first node
|
25 |
+
text = root.text or ""
|
26 |
+
|
27 |
+
for child in root:
|
28 |
+
if child.text:
|
29 |
+
if child.tag != 'scripRef' or not strip_ref:
|
30 |
+
text += child.text
|
31 |
+
if child.tail:
|
32 |
+
# Add the root node text between this child and the next child
|
33 |
+
text += child.tail
|
34 |
+
|
35 |
+
if strip_ref:
|
36 |
+
# Remove empty parens from removed references
|
37 |
+
text = re.sub(r"\([\n\t; ]*\)", " ", text)
|
38 |
+
|
39 |
+
# Combine whitespace to single space
|
40 |
+
# text = re.sub(r"[\n\t ]+", " ", text)
|
41 |
+
|
42 |
+
return text
|
43 |
+
|
44 |
+
def get_paras():
|
45 |
+
paras = []
|
46 |
+
|
47 |
+
from pathlib import Path
|
48 |
+
for filename in list(Path(".").rglob("*.xml")):
|
49 |
+
filename = str(filename)
|
50 |
+
if "authInfo." in filename:
|
51 |
+
continue
|
52 |
+
print(filename)
|
53 |
+
try:
|
54 |
+
tree = ET.parse(filename)
|
55 |
+
except:
|
56 |
+
print("ERROR: Unable to parse:", filename)
|
57 |
+
continue
|
58 |
+
|
59 |
+
root = tree.getroot()
|
60 |
+
|
61 |
+
#for i, p in enumerate(root.findall(".//p[scripRef]")):
|
62 |
+
for i, p in enumerate(root.findall(".//p")):
|
63 |
+
text = get_text_content(p)
|
64 |
+
|
65 |
+
if False:
|
66 |
+
text = re.sub(r'".*?"', '...', text, flags=re.M|re.DOTALL)
|
67 |
+
text = re.sub(r'“.*?”', '...', text, flags=re.M|re.DOTALL)
|
68 |
+
|
69 |
+
# Remove spaces before punctuation
|
70 |
+
text = re.sub(r"[\t\n ]\.", ".", text)
|
71 |
+
text = re.sub(r"[\t\n ],", ",", text)
|
72 |
+
|
73 |
+
# Combine elipses
|
74 |
+
text = re.sub(r"\.\.\.+", "...", text)
|
75 |
+
|
76 |
+
refs = get_refs(p)
|
77 |
+
|
78 |
+
if len(text) > 160 and 'id' in p.attrib:
|
79 |
+
yield {
|
80 |
+
"id": filename + ":" + p.attrib['id'],
|
81 |
+
"refs": refs,
|
82 |
+
"text": text,
|
83 |
+
"text-noref": get_text_content(p, strip_ref=True)
|
84 |
+
}
|
85 |
+
|
86 |
+
from datasets import Dataset
|
87 |
+
|
88 |
+
ds = Dataset.from_generator(get_paras)
|
89 |
+
ds.push_to_hub("jncraton/ccel-paragraphs")
|