Reformat with black
Browse files
parse.py
CHANGED
@@ -2,18 +2,20 @@ 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 ==
|
10 |
-
if
|
11 |
-
refs.append(child.attrib[
|
12 |
|
13 |
return refs
|
14 |
|
|
|
15 |
def get_text_content(root, strip_ref=False):
|
16 |
-
"""
|
17 |
|
18 |
This will:
|
19 |
|
@@ -26,7 +28,7 @@ def get_text_content(root, strip_ref=False):
|
|
26 |
|
27 |
for child in root:
|
28 |
if child.text:
|
29 |
-
if child.tag !=
|
30 |
text += child.text
|
31 |
if child.tail:
|
32 |
# Add the root node text between this child and the next child
|
@@ -41,10 +43,12 @@ def get_text_content(root, strip_ref=False):
|
|
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:
|
@@ -55,16 +59,16 @@ def get_paras():
|
|
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'".*?"',
|
67 |
-
text = re.sub(r
|
68 |
|
69 |
# Remove spaces before punctuation
|
70 |
text = re.sub(r"[\t\n ]\.", ".", text)
|
@@ -75,14 +79,15 @@ def get_paras():
|
|
75 |
|
76 |
refs = get_refs(p)
|
77 |
|
78 |
-
if len(text) > 160 and
|
79 |
yield {
|
80 |
-
"id": filename + ":" + p.attrib[
|
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)
|
|
|
2 |
import os
|
3 |
import xml.etree.ElementTree as ET
|
4 |
|
5 |
+
|
6 |
def get_refs(root):
|
7 |
refs = []
|
8 |
|
9 |
for child in root:
|
10 |
+
if child.tag == "scripRef":
|
11 |
+
if "osisRef" in child.attrib:
|
12 |
+
refs.append(child.attrib["osisRef"])
|
13 |
|
14 |
return refs
|
15 |
|
16 |
+
|
17 |
def get_text_content(root, strip_ref=False):
|
18 |
+
"""Return the plain text content of a node
|
19 |
|
20 |
This will:
|
21 |
|
|
|
28 |
|
29 |
for child in root:
|
30 |
if child.text:
|
31 |
+
if child.tag != "scripRef" or not strip_ref:
|
32 |
text += child.text
|
33 |
if child.tail:
|
34 |
# Add the root node text between this child and the next child
|
|
|
43 |
|
44 |
return text
|
45 |
|
46 |
+
|
47 |
def get_paras():
|
48 |
paras = []
|
49 |
|
50 |
from pathlib import Path
|
51 |
+
|
52 |
for filename in list(Path(".").rglob("*.xml")):
|
53 |
filename = str(filename)
|
54 |
if "authInfo." in filename:
|
|
|
59 |
except:
|
60 |
print("ERROR: Unable to parse:", filename)
|
61 |
continue
|
62 |
+
|
63 |
root = tree.getroot()
|
64 |
|
65 |
+
# for i, p in enumerate(root.findall(".//p[scripRef]")):
|
66 |
for i, p in enumerate(root.findall(".//p")):
|
67 |
text = get_text_content(p)
|
68 |
|
69 |
if False:
|
70 |
+
text = re.sub(r'".*?"', "...", text, flags=re.M | re.DOTALL)
|
71 |
+
text = re.sub(r"“.*?”", "...", text, flags=re.M | re.DOTALL)
|
72 |
|
73 |
# Remove spaces before punctuation
|
74 |
text = re.sub(r"[\t\n ]\.", ".", text)
|
|
|
79 |
|
80 |
refs = get_refs(p)
|
81 |
|
82 |
+
if len(text) > 160 and "id" in p.attrib:
|
83 |
yield {
|
84 |
+
"id": filename + ":" + p.attrib["id"],
|
85 |
"refs": refs,
|
86 |
"text": text,
|
87 |
+
"text-noref": get_text_content(p, strip_ref=True),
|
88 |
}
|
89 |
|
90 |
+
|
91 |
from datasets import Dataset
|
92 |
|
93 |
ds = Dataset.from_generator(get_paras)
|