-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_md.py
130 lines (123 loc) · 4.13 KB
/
extract_md.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
import glob
import os
import pandas as pd
from tqdm import tqdm
from acdh_tei_pyutils.tei import TeiReader
from AcdhArcheAssets.uri_norm_rules import get_normalized_uri
from config import CMI_MD_FILE, NAMESPACES
ns = NAMESPACES
files = glob.glob('./data/cmi/*.xml')
def get_coords(url):
item = {
"url": url,
"lat": None,
"long": None,
"name": None,
"country_code": None
}
try:
doc = TeiReader(f"{url}about.rdf")
lat = doc.tree.xpath('.//wsg:lat/text()', namespaces=ns)[0]
long = doc.tree.xpath('.//wsg:long/text()', namespaces=ns)[0]
name = doc.tree.xpath('.//gn:name/text()', namespaces=ns)[0]
country_code = doc.tree.xpath('.//gn:countryCode/text()', namespaces=ns)[0]
except:
item
return {
"url": url,
"lat": lat,
"long": long,
"name": name,
"country_code": country_code
}
data = []
for x in tqdm(sorted(files), total=len(files)):
_, file_name = os.path.split(x)
hash_id = file_name.replace('cmi__', '').replace('.xml', '')
try:
doc = TeiReader(x)
except Exception as e:
continue
cor_title = " ".join(" ".join(doc.any_xpath(
'.//tei:sourceDesc/tei:bibl//text()'
)).split())
for cur_num, c in enumerate(doc.any_xpath('.//tei:correspDesc')):
item = {}
item['key'] = f"gen_id__{cur_num}"
item['hash_id'] = hash_id
item['cor_title'] = cor_title
try:
item['place_sender_name'] = c.xpath(
'.//tei:correspAction[@type="sent"]/tei:placeName[1]/text()',
namespaces=ns
)[0]
except IndexError:
item['place_sender_name'] = "unbekannt"
try:
item['place_sender_ref'] = get_normalized_uri(
c.xpath(
'.//tei:correspAction[@type="sent"]/tei:placeName[1]/@ref',
namespaces=ns
)[0]
)
except IndexError:
item['place_sender_ref'] = "unbekannt"
try:
item['sender_name'] = c.xpath(
'.//tei:correspAction[@type="sent"]/tei:persName[1]/text()',
namespaces=ns
)[0]
except IndexError:
item['sender_name'] = "unbekannt"
try:
item['sender_ref'] = get_normalized_uri(
c.xpath(
'.//tei:correspAction[@type="sent"]/tei:persName[1]/@ref',
namespaces=ns
)[0]
)
except IndexError:
item['sender_ref'] = "unbekannt"
try:
item['place_receiver_name'] = c.xpath(
'.//tei:correspAction[@type="received"]/tei:placeName[1]/text()',
namespaces=ns
)[0]
except IndexError:
item['place_receiver_name'] = "unbekannt"
try:
item['place_receiver_ref'] = get_normalized_uri(
c.xpath(
'.//tei:correspAction[@type="received"]/tei:placeName[1]/@ref',
namespaces=ns
)[0]
)
except IndexError:
item['place_receiver_ref'] = "unbekannt"
try:
item['receiver_name'] = c.xpath(
'.//tei:correspAction[@type="received"]/tei:persName[1]/text()',
namespaces=ns
)[0]
except IndexError:
item['receiver_name'] = "unbekannt"
try:
item['receiver_ref'] = get_normalized_uri(
c.xpath(
'.//tei:correspAction[@type="received"]/tei:persName[1]/@ref',
namespaces=ns
)[0]
)
except IndexError:
item['receiver_ref'] = "unbekannt"
try:
date = c.xpath('.//tei:date[1]', namespaces=ns)[0]
dates = []
for key, value in date.attrib.items():
dates.append(value[:4])
item['year'] = dates[0]
except:
item['year'] = "0000"
data.append(item)
df = pd.DataFrame(data)
df.to_csv(CMI_MD_FILE, index=False, compression='zip')