Add title, author, and year metadata
Browse files- generate.py +16 -6
generate.py
CHANGED
@@ -38,6 +38,14 @@ def get_text_content(root, strip_ref=False):
|
|
38 |
|
39 |
return text
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
log = open("log.txt", "w")
|
43 |
jsonl = open("train.jsonl", "w")
|
@@ -66,16 +74,15 @@ def get_paras():
|
|
66 |
# The copyright statements aren’t consistent.
|
67 |
# The ones that contain the text "public domain" (case insensitive)
|
68 |
# or nothing at all should be safe.
|
69 |
-
|
70 |
-
rights = root.find(".//DC.Rights").text
|
71 |
-
rights = re.sub(r"\s+", " ", rights)
|
72 |
-
except:
|
73 |
-
rights = None
|
74 |
|
75 |
if rights and 'public domain' not in rights.lower():
|
76 |
print(f"Skipped {filename} due to copyright: {rights}", file=log, flush=True)
|
77 |
continue
|
78 |
-
|
|
|
|
|
|
|
79 |
|
80 |
for i, p in enumerate(root.findall(".//p")):
|
81 |
xml = ET.tostring(p, encoding="unicode")
|
@@ -92,7 +99,10 @@ def get_paras():
|
|
92 |
"text": text,
|
93 |
"thml": xml,
|
94 |
"refs": refs,
|
|
|
|
|
95 |
"rights": rights,
|
|
|
96 |
"all-MiniLM-L6-v2": emb,
|
97 |
}
|
98 |
|
|
|
38 |
|
39 |
return text
|
40 |
|
41 |
+
def get_field(root, field):
|
42 |
+
try:
|
43 |
+
text = root.find(f".//{field}").text
|
44 |
+
text = re.sub(r"\s+", " ", text)
|
45 |
+
except:
|
46 |
+
text = None
|
47 |
+
|
48 |
+
return text
|
49 |
|
50 |
log = open("log.txt", "w")
|
51 |
jsonl = open("train.jsonl", "w")
|
|
|
74 |
# The copyright statements aren’t consistent.
|
75 |
# The ones that contain the text "public domain" (case insensitive)
|
76 |
# or nothing at all should be safe.
|
77 |
+
rights = get_field(root, "DC.Rights")
|
|
|
|
|
|
|
|
|
78 |
|
79 |
if rights and 'public domain' not in rights.lower():
|
80 |
print(f"Skipped {filename} due to copyright: {rights}", file=log, flush=True)
|
81 |
continue
|
82 |
+
|
83 |
+
title = get_field(root, "title")
|
84 |
+
published = get_field(root, "firstPublished")
|
85 |
+
author = get_field(root, "authorID")
|
86 |
|
87 |
for i, p in enumerate(root.findall(".//p")):
|
88 |
xml = ET.tostring(p, encoding="unicode")
|
|
|
99 |
"text": text,
|
100 |
"thml": xml,
|
101 |
"refs": refs,
|
102 |
+
"author": author,
|
103 |
+
"title": title,
|
104 |
"rights": rights,
|
105 |
+
"published": published,
|
106 |
"all-MiniLM-L6-v2": emb,
|
107 |
}
|
108 |
|