-
Notifications
You must be signed in to change notification settings - Fork 2
/
trimTei0.py
218 lines (190 loc) · 6.32 KB
/
trimTei0.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
211
212
213
214
215
216
217
218
import os
import re
import collections
from distill import META_KEY_TRANS_DEF
from lib import (
CHANGE_DIR,
REPORT_DIR,
TRIM_DIR,
PAGE_NUM_RE,
LT,
GT,
AMP,
rangesFromList,
specFromRanges,
)
trimDocBefore = None
trimDocPrep = None
processPage = None
trimDocPost = None
STAGE = 0
REP = f"{REPORT_DIR}{STAGE}"
DST = f"{TRIM_DIR}{STAGE}"
ADD_PAGE = {}
ADD_PAGE_SET = collections.defaultdict(set)
ADD_DOC = {}
REPLACE_PAGE = {}
def corpusPre(givenVol):
for (subPath, dest) in (
("doc", ADD_DOC),
("add", ADD_PAGE),
("replace", REPLACE_PAGE),
):
thisChangeDir = f"{CHANGE_DIR}/{subPath}"
with os.scandir(thisChangeDir) as dh:
for entry in dh:
if entry.is_dir():
vol = entry.name
if vol.isdigit() and (givenVol is None or vol in givenVol):
with os.scandir(f"{thisChangeDir}/{vol}") as dh:
for entry in dh:
if entry.is_file():
fileName = entry.name
if fileName.endswith(".xml"):
name = fileName[0:-4]
if subPath == "add":
(base, p) = name.split("-", 1)
pNum = int(p.lstrip("0"))
ADD_PAGE_SET[vol].add(pNum)
pNum = f"{pNum + 1:>04}"
storeName = f"{base}-{pNum}"
else:
storeName = name
doc = f"{vol}:{storeName}"
with open(
f"{thisChangeDir}/{vol}/{fileName}"
) as fh:
text = fh.read()
dest[doc] = text
def trimVolume(vol, letters, info, idMap, givenLid, mergeText):
pageDiag = info["pageDiag"]
for (doc, text) in ADD_DOC.items():
(docVol, name) = doc.split(":")
if docVol != vol:
continue
path = f"{DST}/{vol}/{name}.xml"
with open(path, "w") as fh:
fh.write(text)
pages = PAGE_NUM_RE.findall(text)
for p in pages:
p = int(p)
if p in pageDiag[vol]:
print(f"PAGE ERROR in volume {vol}: duplicate page {p}")
pageDiag[vol][p] = "ocr"
META_KV_RE = re.compile(
r"""
<interpGrp
\b[^>]*?\b
type="([^"]*)"
[^>]*
>
(.*?)
</interpGrp>
""",
re.S | re.X,
)
META_KEY_NO_TRANS = {"authorFull", "status"}
META_KEY_TRANS = {
old: new for (new, old) in META_KEY_TRANS_DEF if new not in META_KEY_NO_TRANS
}
META_KEYS = {x[1] for x in META_KEY_TRANS_DEF}
META_KEY_IGNORE = set(
"""
tocLevel
volume
witnessDayLevel1_to
witnessMonthLevel1_to
witnessYearLevel1_to
""".strip().split()
)
META_VAL_RE = re.compile(
r"""
<interp>
(.*?)
</interp>
""",
re.S | re.X,
)
def trimDocPrep(info, metaText, bodyText, previousMeta):
vol = info["vol"]
pageDiag = info["pageDiag"]
pages = PAGE_NUM_RE.findall(bodyText)
for p in pages:
p = int(p)
if p in pageDiag[vol]:
print(f"PAGE ERROR in volume {vol}: duplicate page {p}")
info["pageDiag"][vol][p] = "tei"
return (trimHeader(metaText, info), bodyText)
def trimPage(text, info, *args, **kwargs):
text = text.replace(''' rend=""''', "")
text = text.replace(''' rend=" "''', "")
text = (
text.replace(""", '"')
.replace("'", "'")
.replace("<", LT)
.replace(">", GT)
.replace("&", AMP)
)
page = info["page"]
return (
REPLACE_PAGE[page]
if page in REPLACE_PAGE
else (ADD_PAGE[page] if page in ADD_PAGE else "") + text
)
def trimHeader(text, info):
origMetadata = {k: v for (k, v) in META_KV_RE.findall(text)}
metadata = {
META_KEY_TRANS[k]: transVal(v)
for (k, v) in origMetadata.items()
if k in META_KEY_TRANS
}
unknownMetadata = {
k: v
for (k, v) in origMetadata.items()
if k not in META_KEYS and k not in META_KEY_IGNORE
}
doc = info["doc"]
metasUnknown = info["metasUnknown"]
if unknownMetadata:
metasUnknown.append((doc, unknownMetadata))
newText = "\n".join(
f"""<meta key="{k}" value="{v}"/>""" for (k, v) in metadata.items()
)
return f"<header>\n{newText}\n</header>\n"
def transVal(value):
values = META_VAL_RE.findall(value)
return ",".join(v for v in values if v)
def corpusPost(info):
metasUnknown = info["metasUnknown"]
print(f"\t{len(metasUnknown):>3} docs with unrecognized metadata")
with open(f"{REP}/metaUnrecognized.txt", "w") as fh:
for (doc, meta) in sorted(metasUnknown):
fh.write(f"{doc}\n")
for (k, v) in meta.items():
fh.write(f"\t{k:<10} = {v}\n")
fh.write("\n")
print("PAGES:")
pageDiag = info["pageDiag"]
with open(f"{REP}/pageDiag.txt", "w") as fh:
for vol in sorted(pageDiag):
fh.write(f"{vol}:\n")
stats = dict(ok=[], xx=[], dd=[])
pages = pageDiag[vol]
minPage = min(pages)
maxPage = max(pages)
for p in range(minPage, maxPage + 1):
if p not in pages:
label = "ocr" if p in ADD_PAGE_SET[vol] else "xx"
pageDiag[vol][p] = label
stats["ok" if label == "ocr" else label].append(p)
else:
stats["ok"].append(p)
fh.write(f"\tp{p:>04} {pageDiag[vol][p]}\n")
first = True
for label in ("ok", "xx", "dd"):
data = stats[label]
if data:
rep = specFromRanges(rangesFromList(data))
volRep = f"{vol}:" if first else " "
first = False
print(f"{volRep} {label} {rep}")