-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaatsarchiv_utils.py
412 lines (329 loc) · 10.9 KB
/
staatsarchiv_utils.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import re
from tqdm import tqdm
import pandas as pd
import numpy as np
import glob
from bs4 import BeautifulSoup
import warnings
from bs4 import XMLParsedAsHTMLWarning
import spacy
from transformers import AutoTokenizer
warnings.filterwarnings("ignore", category=XMLParsedAsHTMLWarning)
def read_XML_files(folder_path, remove_memberlists=False):
"""Read XML files recursively from a directory and return a list of file paths.
Parameters
----------
folder_path : str
The path to the directory containing the XML files.
remove_memberlists : bool, optional
Whether to remove files containing the word "mitgliederliste" in their name.
Returns
-------
all_files : list
A list of file paths of XML files.
"""
xml_file_paths = []
for filename in glob.glob(f"{folder_path}**/*.xml", recursive=True):
xml_file_paths.append(filename)
if remove_memberlists:
xml_file_paths = [x for x in xml_file_paths if "mitgliederliste" not in x]
print(f"{len(xml_file_paths):,.0f} files found.")
return xml_file_paths
def parse_XML_files(file_paths):
"""
Parse XML files and extract relevant information.
Parameters
----------
paths : list
List of paths to XML files.
Returns
-------
data : pd.DataFrame
DataFrame with extracted information.
"""
results = []
for file_path in tqdm(file_paths):
tmp = []
with open(file_path) as file:
soup = BeautifulSoup(file, "lxml")
tmp.append(file_path)
# Check if file is empty.
if len(soup) == 0:
print(file_path)
continue
# Parse date.
if soup.sourcedesc.date is not None:
tmp.append(soup.sourcedesc.date.get("when"))
tmp.append(soup.sourcedesc.date.get("from"))
tmp.append(soup.sourcedesc.date.get("to"))
tmp.append(soup.sourcedesc.date.text)
else:
tmp.append(np.nan)
tmp.append(np.nan)
tmp.append(np.nan)
tmp.append(np.nan)
# Parse identifier.
if soup.sourcedesc.ident is not None:
tmp.append(soup.sourcedesc.ident.text)
else:
tmp.append(np.nan)
# Parse link to search portal.
target = soup.sourcedesc.ref.get("target")
if target == "leer":
tmp.append(np.nan)
else:
tmp.append(target)
# Parse title.
tmp.append(soup.sourcedesc.title.get_text())
# Parse text taking nested tables elements into account.
text = soup.find("text")
tmp_text = []
for elem in text.find_all(recursive=False):
if elem.name == "table":
for row in elem.find_all("row"):
for cell in row.find_all("cell"):
if cell.text:
tmp_text.append(cell.text)
else:
if elem.text:
tmp_text.append(elem.text)
tmp.append(" ".join(tmp_text))
results.append(tmp)
data = pd.DataFrame(
results,
columns=[
"path",
"date_when",
"date_from",
"date_to",
"date_text",
"ident",
"ref",
"title",
"text",
],
)
# Extract filenames from paths.
data["filename"] = data.path.apply(lambda x: x.split("/")[-1])
return data
def fix_missing_dates(data):
"""Fill missing dates in `date_when` with `date_from`.
Parameters
----------
data : pd.DataFrame
Data to fix.
Returns
-------
data : pd.DataFrame
Data with missing dates filled.
"""
to_fill = data[data.date_when.isna()].index
data.loc[to_fill, "date_when"] = data.loc[to_fill, "date_from"]
return data
def fix_incomplete_dates(data):
"""Add first day of month to dates in `date_when` where the day is missing.
Parameters
----------
data : pd.DataFrame
Data to fix.
Returns
-------
data : pd.DataFrame
Data with fixed dates.
"""
tmp = pd.to_datetime(data.date_when, errors="coerce", format="%Y-%m-%d")
to_check = tmp[tmp.isna()].index
data.loc[to_check, "date_when"] = data.loc[to_check, "date_when"].apply(
lambda x: x + "-01"
)
return data
def add_missing_month_and_day_to_dates(data):
"""Add missing month and day to dates in `date_when` where the month and day are missing.
Parameters
----------
data : pd.DataFrame
Data to fix.
Returns
-------
data : pd.DataFrame
Data with fixed dates.
"""
# Successively add month and day to dates where they are missing.
for _ in range(2):
tmp = pd.to_datetime(data.date_when, errors="coerce", format="%Y-%m-%d")
incomplete_dates = tmp[tmp.isna()].index
data.loc[incomplete_dates, "date_when"] = (
data.loc[incomplete_dates, "date_when"] + "-01"
)
return data
def parse_dates(data):
"""Parse dates in `date_when` column.
Parameters
----------
data : pd.DataFrame
Data to parse.
Returns
-------
data : pd.DataFrame
Data with parsed dates.
"""
data.date_when = pd.to_datetime(data.date_when, errors="coerce", format="%Y-%m-%d")
return data
def clean_identifiers(data):
"""Remove line breaks from identifiers in `ident` column.
Parameters
----------
data : pd.DataFrame
Data to clean.
Returns
-------
data : pd.DataFrame
Data with cleaned identifiers.
"""
data.ident = data.ident.apply(lambda x: x.replace("\n", ""))
data.ident = data.ident.apply(lambda x: re.sub(r"\s+", " ", x))
data.ident = data.ident.apply(lambda x: x.strip())
return data
def reformat_rrb_ids(data):
"""
Reformat rrb_id to standard of [year]-[decision number]-[document number].
Several hundert RRBs consist of several documents.
Retrieve consecutive nummber of additional docs that belong to the RRB as integer.
Parameters
----------
data : pd.DataFrame
DataFrame containing the data.
Returns
-------
pd.DataFrame
DataFrame with reformatted rrb_id.
"""
data["rrb_year"] = data.rrb_id.apply(lambda x: x.split("_")[0])
data["rrb_no"] = data.rrb_id.apply(lambda x: x.split("_")[1])
data["rrb_no_add"] = data.rrb_no.apply(lambda x: x[4:])
data.rrb_no = data.rrb_no.apply(lambda x: x[:4])
additional_id = sorted([x for x in data.rrb_no_add.unique()])
mapping = dict(zip(additional_id, range(1, len(additional_id) + 2)))
data.rrb_no_add = data.rrb_no_add.map(mapping)
data.rrb_id = (
data.rrb_year.astype(str)
+ "-"
+ data.rrb_no.astype(str)
+ "-"
+ data.rrb_no_add.astype(str)
)
data.drop(columns=["rrb_year", "rrb_no", "rrb_no_add"], inplace=True)
return data
BASE_DOC_LINK_RRB = "https://archives-quickaccess.ch/stazh/rrb/ref/"
def create_rrb_doc_link(fn):
"""Create link to online version of RRBs by parsing the filename and adding a base link.
Parameters
----------
fn : str
Filename of the RRB document.
Returns
-------
link : str
Link to the online version of the RRB document.
"""
fn = fn.split("_")
band = fn[1:3]
band = ".".join(band)
year = fn[4]
doc_no = fn[5].replace(".xml", "")
link = f"{BASE_DOC_LINK_RRB}MM+{band}+RRB+{year}/{doc_no}"
return link
pattern = r"(\x04|\x1a|\xa0|\x00|\ue83a|\x19|\uf06c|\x10|\x17|\x13|\x11|<space>|\x16|\x18|\x1b|\x15|\t\b|\f)"
ESCAPE_SEQS = re.compile(pattern)
pattern = (
r"(?<=\s)(I.|II.|III.|IV.|V.|VI.|VII.|VIII.|IX.|X.|XI.|XII.|XIII.|XIV.|XV.)(?=\s)"
)
ROMAN_NUMERALS = re.compile(pattern)
def generic_text_cleaning(d):
"""
Clean some generic text issues.
Parameters
----------
d : str
Text to be cleaned.
Returns
-------
d : str
Cleaned text.
"""
d = d.strip()
# Remove non breaking spaces and escape sequences.
d = re.sub(ESCAPE_SEQS, " ", d)
# Remove punctuations.
d = re.sub(r"[;«»„“:()\[\]]", " ", d)
# Remove multiple dots.
d = re.sub(r"\.{2,}", " ", d)
# Remove roman numerals.
d = re.sub(ROMAN_NUMERALS, " ", d)
# Replace several symbolic chars with actual word.
d = re.sub(r"&", " und ", d)
d = re.sub(r"§", " Paragraph ", d)
d = re.sub(r"=", " gleich ", d)
# Replace multiple spaces with single space.
d = re.sub(r"\s+", " ", d)
# Again strip whitespace after all cleaning operations.
d = d.strip()
return d
nlp = spacy.load("de_core_news_lg")
# https://huggingface.co/jinaai/jina-embeddings-v2-base-de
model_path = "jinaai/jina-embeddings-v2-base-de"
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=True)
def chunk_text(data, max_token_count=512, overlap_tokens=50):
"""Chunk text into parts of max_token_count tokens with overlap_sents sentences overlap.
Parameters
----------
data : pd.DataFrame
DataFrame containing the data.
max_token_count : int, optional
The maximum number of tokens per chunk, by default 512.
overlap_sents : int, optional
The number of sentences to overlap between chunks, by default 5.
Returns
-------
list
List of tuples containing the identifier and the chunked text.
"""
# Sentencize text.
doc = nlp(data.text)
sents = [sent.text for sent in doc.sents]
# Count tokens in each sentence.
# TODO: Sentences can potentially be longer than max_token_count. Find a way to handle this.
tokens = [len(tokenizer.tokenize(sent)) for sent in sents]
# Create chunks by adding full sentences until max_token_count is reached.
chunks = []
current_chunk_start = 0
current_sent = 0
current_chunk = []
current_tokens = 0
while True:
if current_sent >= len(sents):
chunks.append(" ".join(current_chunk))
break
current_tokens += tokens[current_sent]
if current_tokens < max_token_count:
current_chunk.append(sents[current_sent])
current_sent += 1
else:
chunks.append(" ".join(current_chunk))
current_chunk = []
current_tokens = 0
# Go back n sents until we create an overlap of overlap_tokens or more.
count_back_tokens = 0
count_back_sents = 0
while True:
count_back_tokens += tokens[current_sent]
count_back_sents += 1
if count_back_tokens > overlap_tokens:
break
current_sent -= 1
current_sent -= count_back_sents
# Avoid endless loop if overlap_sents is too high.
if current_sent <= current_chunk_start:
current_sent = current_chunk_start + 1
current_chunk_start = current_sent
return [(data.identifier, chunk) for chunk in chunks]