-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
82 lines (72 loc) · 2.51 KB
/
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
import pandas as pd
import requests
import jinja2
import os
from io import BytesIO
from datetime import date
from dateutil.parser import parse, ParserError
from slugify import slugify
TEMPLATE_FILE = "tei-template.xml"
GDRIVE_BASE_URL = "https://docs.google.com/spreadsheet/ccc?key="
templateLoader = jinja2.FileSystemLoader(searchpath=".")
templateEnv = jinja2.Environment(loader=templateLoader)
template = templateEnv.get_template(TEMPLATE_FILE)
def gsheet_to_df(sheet_id):
url = f"{GDRIVE_BASE_URL}{sheet_id}&output=csv"
r = requests.get(url)
print(r.status_code)
data = r.content
df = pd.read_csv(BytesIO(data))
return df
def row_to_dict(df):
row = df.iloc[0]
doc_title = row['document_title']
if "Letter Signed W. H" in doc_title:
is_letter = True
else:
is_letter = False
written_date = row['document_date']
try:
parsed_date = parse(written_date)
except ParserError:
parsed_date = None
item = {
"id": row['image_id'],
"file_name": f"amp-transcript__{int(row['document_id']):04}.xml",
"title": row['document_title'],
"sender": row['document_author'],
"sender_id": f"{slugify(row['document_author'])}",
"receiver": "Musulin, Stella",
"receiver_id": f"{slugify('Musulin, Stella')}",
"lang_code": 'en',
"language": "English",
"written_date": written_date,
"parsed_date": parsed_date,
"idno": row['container'],
"pages": [],
"current_date": f"{date.today()}",
"is_letter": is_letter
}
return item
def create_templates(done, prefix="amp-transcript__"):
for gr, df in done.groupby('document_id'):
file_name = f"{prefix}{int(gr):04}.xml"
print(file_name)
item = row_to_dict(df)
file_name2 = item['file_name']
os.makedirs("tei", exist_ok=True)
with open(os.path.join("tei", file_name), 'w') as f:
for i, row in df.iterrows():
scan_id = row["image_id"]
scan_id_padd = f"amp-scan__{scan_id:04}.tif"
item['pages'].append(
{
"id": scan_id_padd,
"p_type": row['object_type'],
"width": row['image_width'],
"height": row['image_height'],
"url": f"https://iiif.acdh.oeaw.ac.at/amp/amp_{scan_id:04}/"
}
)
f.write(template.render(**item))
return done