-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-tokenpatterns-in-english-xml-abstracts.py
210 lines (175 loc) · 7.84 KB
/
find-tokenpatterns-in-english-xml-abstracts.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# =============================================================================
# Project : Enrich
# Package : NER
# Module : find-tokenpatterns-in-english-xml-abstracts.py
# Creator : Ceri Binding, University of South Wales / Prifysgol de Cymru
# Contact : ceri.binding@southwales.ac.uk
# Summary : Identifies matching token patterns in English text
# Imports : spacy rematch2.VocabularyAnnotator
# Example : python en-find-tokenpatterns-in-xml-abstracts.py -i "path/to/abstracts/file.xml"
# License : http://creativecommons.org/publicdomain/zero/1.0/ [CC0]
# =============================================================================
# History
# 01/09/2021 CFB Initially created script
# 03/02/2023 CFB Adapted to use new rematch2 components
# =============================================================================
import argparse # for argument parsing
import os # for general file/directory functionality
from datetime import datetime as DT # For timestamps and durations
import re
import json
from lxml import etree as ET
from collections import defaultdict
import spacy
from spacy import displacy
from rematch2.VocabularyAnnotator import VocabularyAnnotator
def writeEntitiesToTsvFile(doc, targetFileNameWithPath="", mode="w"):
writeEntitiesToDelimitedFile(doc, targetFileNameWithPath, "\t", mode)
def writeEntitiesToDelimitedFile(doc, targetFileNameWithPath="", delimiter="\t", mode="w"):
txt = ""
for ent in doc.ents:
txt += f"{ent.ent_id_}{delimiter}{ent.text}{delimiter}{ent.start_char}{delimiter}{ent.end_char}{delimiter}{ent.label_}\n"
with open(targetFileNameWithPath, mode, encoding='utf-8-sig') as f:
f.write(txt)
def writeEntitiesToHtmlFile(doc, targetFileNameWithPath=""):
options = {
"ents": [
"YEARSPAN",
"PERIOD",
"FISH_MONUMENT",
"FISH_OBJECT",
"FISH_ARCHSCIENCE",
"FISH_EVIDENCE",
"FISH_MATERIAL",
"FISH_EVENTTYPE"
],
"colors": {
"YEARSPAN": "moccasin",
"PERIOD": "yellow",
"FISH_MONUMENT": "cyan",
"FISH_OBJECT": "plum",
"FISH_ARCHSCIENCE": "pink",
"FISH_EVIDENCE": "aliceblue",
"FISH_MATERIAL": "antiquewhite",
"FISH_EVENTTYPE": "coral",
}
}
html = displacy.render([doc], style="ent", page=True,
minify=True, options=options)
with open(targetFileNameWithPath, 'w', encoding='utf-8-sig') as f:
f.write(html)
def main(sourceFilePath):
print(f"running main({sourceFilePath})")
annotator = VocabularyAnnotator(
language="en", periodo_authority_id="p0kh9ds", labels=["FISH_OBJECT", "FISH_MONUMENT", "PERIOD"])
results = defaultdict(dict)
sourceFileDirectory = os.path.dirname(sourceFilePath)
baseDirectory = os.path.join(sourceFileDirectory, "fromxml")
if "fromxml" not in os.listdir(sourceFileDirectory):
os.mkdir(baseDirectory)
tsvDirectory = os.path.join(baseDirectory, "tsv")
if "tsv" not in os.listdir(baseDirectory):
os.mkdir(tsvDirectory)
htmlDirectory = os.path.join(baseDirectory, "html")
if "html" not in os.listdir(baseDirectory):
os.mkdir(htmlDirectory)
# targetFilePath = f"{sourceFilePath}.output.xml"
try:
# read XML file
tree = ET.parse(sourceFilePath)
root = tree.getroot()
except:
print(f"Could not read from {sourceFilePath}")
return 0
# locate the abstracts in the XML file
# find records to be processed in the XML file
# xpathRecords = "/Collections/records/record"
xpathRecords = "/table/rows/row" # OASIS example data from Tim
print(f"looking for xpath {xpathRecords}")
records = tree.xpath(xpathRecords)
totalRecords = len(records)
print(f"found {totalRecords} records")
# process each record located
currentRecord = 0
for record in records:
# find abstract(s) in the current record
# abstracts = record.xpath('dc:description/text()',
# namespaces={'dc': 'http://purl.org/dc/elements/1.1/'})
# find identifier(s) in the current record
# identifiers = record.xpath('dc:source/text()',
# namespaces={'dc': 'http://purl.org/dc/elements/1.1/'})
# OASIS example data from Tim
abstracts = record.xpath("value[@columnNumber='1']/text()")
# OASIS example data from Tim
identifiers = record.xpath("value[@columnNumber='0']/text()")
if (len(abstracts) > 0):
abstract = abstracts[0]
else:
abstract = ""
if (len(identifiers) > 0):
identifier = identifiers[0]
identifier = identifier.replace(
"https://archaeologydataservice.ac.uk/archsearch/record?titleId=", "")
else:
identifier = ""
# print(f"Identifier: {identifier}\nAbstract:\n{abstract}\n*****\n")
# print(f"abstract: {str(abstract)}")
doc = annotator.annotateText(input_text=str(abstract), format="doc")
# write results to JSON file.
now = DT.now()
ents = []
for ent in doc.ents:
ents.append({
"text": ent.text,
"start": ent.start_char,
"end": ent.end_char,
"label": ent.label_
})
results[identifier]["source"] = sourceFilePath
results[identifier]["identifier"] = identifier
results[identifier]["created"] = f"{now.year}-{now.month}-{now.day}"
results[identifier]["process"] = __file__
results[identifier]["abstract"] = abstract
results[identifier]["subjects"] = ents
currentRecord += 1
if currentRecord == 10000: # premature break during testing
break
# remove any non-word or non-space characters from ID
cleanIdentifier = re.sub(r'[^\w\s]', '', identifier)
print(
f"processing record { currentRecord } of { totalRecords } [{ identifier }]")
# append data to 'overall' results TSV file
tsvOverallFileNameWithPath = os.path.join(
tsvDirectory, f"output.fromxml.txt")
delimiter = "\t"
txt = ""
for ent in doc.ents:
txt += f"{identifier}{delimiter}{ent.ent_id_}{delimiter}{ent.text}{delimiter}{ent.label_}\n"
with open(tsvOverallFileNameWithPath, 'a', encoding='utf-8-sig') as f:
f.write(txt)
tsvFileNameWithPath = os.path.join(
tsvDirectory, f"{cleanIdentifier}.fromxml.txt")
writeEntitiesToTsvFile(doc, tsvFileNameWithPath)
htmlFileNameWithPath = os.path.join(
htmlDirectory, f"{cleanIdentifier}.fromxml.html")
writeEntitiesToHtmlFile(doc, htmlFileNameWithPath)
# write results dict to (JSON) file
jsonFileNameWithPath = f"{sourceFilePath}.json"
with open(jsonFileNameWithPath, 'w', encoding="utf-8-sig") as file:
json.dump(results, file, sort_keys=True)
if __name__ == '__main__':
# initiate the input arguments parser
parser = argparse.ArgumentParser(
prog=__file__, description="Find token patterns in XML metadata abstracts")
# add long and short argument descriptions
parser.add_argument("--inputfilepath", "-i", required=False,
help="Input XML metadata file with path")
# parse command line arguments
args = parser.parse_args()
# clean required arguments
if args.inputfilepath:
sourceFilePath = args.inputfilepath.strip()
else:
# temp harcoded test..
sourceFilePath = "./oasis_descr_examples.xml"
main(sourceFilePath)