-
Notifications
You must be signed in to change notification settings - Fork 0
/
cldfbench_sinnemakizeromarking.py
410 lines (351 loc) · 13.8 KB
/
cldfbench_sinnemakizeromarking.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
import io
import pathlib
import re
import sys
import unicodedata
import zipfile
from itertools import chain
from urllib import request
from pybtex import database
from yaml import load as load_yaml
try:
from yaml import CLoader as YamlLoader
except ImportError:
from yaml import Loader as YamlLoader
from cldfbench import CLDFSpec, Dataset as BaseDataset
from csvw.utils import slug
UPSTREAM_REPO = 'https://version.helsinki.fi/hals/sinnemaki/sinnemaki2010'
UPSTREAM_COMMIT = '4b37720b3c2e643660ab1426c73d9e3729f5b43f'
UPSTREAM_URL = '{repo}/-/archive/{commit}/sinnemaki2010-{commit}.zip'.format(
repo=UPSTREAM_REPO,
commit=UPSTREAM_COMMIT)
LANGUAGE_COL_MAP = [
('Glottocode', 'glottocode'),
('ISO639P3code', 'iso_code'),
('WALS_code', 'wals_code'),
('Name', 'language'),
('Genus', 'genus'),
('Family', 'family'),
('Longitude', 'longitude'),
('Latitude', 'latitude'),
('Area', 'area'),
('Continent', 'continent'),
('Macroarea', 'macroarea'),
('Macrocontinent', 'macrocontinent.3'),
('Macrocontinent_SplitOldWorld', 'macrocontinent.4'),
('Circumpacific', 'circumpacific'),
]
LANGUAGES_WITH_DUPLICATE_GLOTTOCODES = [
# damu1236
'Galo',
'Bokar',
# halk1245
'Halkomelem (Island)',
'Halkomelem (Upriver)',
# tama1331
'Miisiirii',
'Tama',
]
def iter_bibtex_entries(lines):
entry = []
for line in lines:
line = line.rstrip('\n')
if line.startswith('@'):
if entry:
yield '\n'.join(entry)
entry = [line]
elif line:
entry.append(line)
if entry:
yield '\n'.join(entry)
def parse_bibtex(entry_strings):
sources = {}
for entry_string in entry_strings:
bibdata = database.parse_string(entry_string, 'bibtex')
if not bibdata.entries:
continue
assert len(bibdata.entries) == 1, "splitting didn't work: {}".format(
entry_string)
bibkey = next(iter(bibdata.entries))
new_bibkey = bibkey
number = 1
while new_bibkey in sources:
number += 1
new_bibkey = '{}-{}'.format(bibkey, number)
if new_bibkey != bibkey:
print(
'WARNING: duplicate bibkey', bibkey,
'renamed to', new_bibkey,
file=sys.stderr)
sources[new_bibkey] = bibdata
return sources
def clean_bibtex(bibtex_code):
bibtex_code = bibtex_code.replace(
' title = {Deep linguistic prehistory with particular reference to Andamanese},\n',
'',
1)
return bibtex_code
def language_id(glottocode, name):
if name in LANGUAGES_WITH_DUPLICATE_GLOTTOCODES:
return f'{glottocode}-{slug(name)}'
else:
return glottocode
def prepare_references_string(ref):
ref = re.sub(
r'\((?:quoted )?in ([^)]+)\)',
r'\1',
ref,
flags=re.IGNORECASE)
ref = ref.strip()
return ref
def iter_references(references_string):
rest = prepare_references_string(references_string)
while rest:
reference = {}
# skip 'Personal knowledge'
personal_knowledge_match = re.match(r'\s*Personal knowledge,?\s*', rest)
if personal_knowledge_match:
rest = rest[len(personal_knowledge_match.group()):]
continue
author_match = re.match('[^0-9(]+', rest)
if author_match is None:
raise ValueError("Expected author: '{}' (in '{}')".format(
rest, references_string))
reference['author'] = author_match.group().strip(', ')
rest = rest[len(author_match.group()):]
# skip (p.c.)
pc_match = re.match(r'\(p.c.\),?\s*', rest)
if pc_match:
rest = rest[len(pc_match.group()):]
continue
# XXX: this can prob be one regex
year_match = (
re.match(r'\((no year|to appear)\)', rest)
# for Hoel et al 1994-2003
or re.match(r'(1994)-2003', rest)
# for Sapir 1990 [1909]
or re.match(r'(1990) \[1909\]', rest)
# for Launey 2001-2
or re.match(r'(2001-2)', rest)
or re.match(r'(\d+[a-z]*)', rest))
if year_match is None:
raise ValueError("Expected year: '{}' (in '{}')".format(
rest, references_string))
reference['year'] = year_match.group(1).strip()
rest = rest[len(year_match.group()):].strip()
pages_match = re.match(
r': ?(?:(?:[0-9\-]+|passim|iv|vi),?\s*)*',
rest)
if pages_match is not None:
reference['pages'] = pages_match.group().strip(':, ')
rest = rest[len(pages_match.group()):].strip()
yield reference
VAN_PARTS = ['van', 'de', 'der']
def first_author_no_van(reference):
names = [
name
for name in reference['author'].split()
if name.lower() not in VAN_PARTS]
return names[0]
def first_author_with_van(reference):
name_parts = []
for name in reference['author'].split():
if name.lower() in VAN_PARTS:
name_parts.append(name)
else:
name_parts.append(name)
break
return ''.join(name_parts)
def unaccent(s):
return ''.join(
c for c in unicodedata.normalize('NFKD', s)
if c.isascii())
def prepare_author(author):
author = author.replace('Lindström', 'Lindstroem')
author = author.replace('Schönig', 'Schoenig')
author = author.replace('Facundes', 'SilvaFacundes')
author = unaccent(author.strip(', '))
return author
def add_pages(bibkey, reference):
if 'pages' in reference:
return '{}[{}]'.format(bibkey, reference['pages'])
else:
return bibkey
def get_bibkey(reference, sources, lang_id):
guess = '{}{}'.format(
prepare_author(first_author_no_van(reference)),
reference['year'])
if guess in sources:
return add_pages(guess, reference)
guess = '{}{}'.format(
prepare_author(first_author_with_van(reference)),
reference['year'])
if guess in sources:
return add_pages(guess, reference)
# TODO: match reference against manually added matches, to find missing
# refs.
# print(
# 'WARNING: {}: cannot match reference:'.format(lang_id), reference,
# file=sys.stderr)
return None
class Dataset(BaseDataset):
dir = pathlib.Path(__file__).parent
id = "sinnemakizeromarking"
def cldf_specs(self): # A dataset must declare all CLDF sets it creates.
return CLDFSpec(
dir=self.cldf_dir,
module='StructureDataset',
metadata_fname='cldf-metadata.json')
def cmd_download(self, args):
"""
Download files to the raw/ directory. You can use helpers methods of `self.raw_dir`, e.g.
>>> self.raw_dir.download(url, fname)
"""
print('downloading zipped raw data...')
with request.urlopen(UPSTREAM_URL) as response:
raw_data = response.read()
print('extracting zipped raw data...')
with zipfile.ZipFile(io.BytesIO(raw_data)) as zipped_data:
for member in zipped_data.infolist():
if member.is_dir():
continue
# strip the the "$REPO-$COMMIT/" folder
# (I wish there was a less low-level way to do this...)
path_suffix = member.filename.replace(
'sinnemaki2010-{}/'.format(UPSTREAM_COMMIT), '')
dest = self.raw_dir / 'sinnemaki2010' / path_suffix
print('extracting {}...'.format(dest))
if not dest.parent.exists():
dest.parent.mkdir(parents=True)
with open(dest, 'wb') as f:
f.write(zipped_data.read(member))
def cmd_makecldf(self, args):
"""
Convert the raw data to a CLDF dataset.
>>> args.writer.objects['LanguageTable'].append(...)
"""
# read raw data
data_folder = self.raw_dir / 'sinnemaki2010' / 'data'
metadata_folder = self.raw_dir / 'sinnemaki2010' / 'metadata'
bib_folder = self.raw_dir / 'sinnemaki2010' / 'bibliography'
language_data = data_folder.read_csv(
'Register.csv', delimiter=';', dicts=True)
value_data = data_folder.read_csv(
'LingVariables.csv', delimiter=';', dicts=True)
custom_parameters = self.etc_dir.read_csv('parameters.csv', dicts=True)
custom_parameters = {param['ID']: param for param in custom_parameters}
custom_codes = {
code['ID']: code
for code in self.etc_dir.read_csv('codes.csv', dicts=True)}
with open(metadata_folder / 'LingVariables.yaml') as f:
ling_variables = load_yaml(f, YamlLoader)
for parameter in ling_variables.values():
if 'Levels' in parameter:
# Apparently, codes are stored in a multi-line string
# containing something that is almost yaml
codes_string = parameter['Levels'].replace(': >', ':')
parameter['Levels'] = load_yaml(codes_string, YamlLoader)
reference_assocs = {
gc: [trimmed for r in refs.split(';') if (trimmed := r.strip())]
for gc, refs in self.etc_dir.read_csv('language-references.csv')}
with open(bib_folder / 'references.bib', encoding='utf-8') as f:
bibtex_strings = [
clean_bibtex(entry_string)
for entry_string in iter_bibtex_entries(f)]
# create cldf data
sources = parse_bibtex(bibtex_strings)
language_references = {
row['glottocode']:
(
row['Sources'],
sorted(filter(None, set(chain(
(get_bibkey(
reference,
sources,
language_id(row['glottocode'], row['language']))
for reference in iter_references(row['Sources'])),
reference_assocs.get(row['glottocode']) or [])))),
)
for row in value_data}
language_table = [
{new_name: lang[old_name]
for new_name, old_name in LANGUAGE_COL_MAP}
for lang in language_data]
for lang in language_table:
source_prose, bibkeys = language_references[lang['Glottocode']]
lang['Source_prose'] = source_prose
lang['Source'] = list(filter(None, bibkeys))
lang['ID'] = language_id(lang['Glottocode'], lang['Name'])
if lang['Latitude']:
lang['Latitude'] = float(lang['Latitude'].replace(',', '.'))
if lang['Longitude']:
lang['Longitude'] = float(lang['Longitude'].replace(',', '.'))
parameter_table = [
{
'ID': slug(param_name),
'Name': param_name,
'Original_Name': param_name,
'Description': param['Description'].strip(),
}
for param_name, param in ling_variables.items()
if param.get('VariableType') == 'data']
for param in parameter_table:
if (custom_parameter := custom_parameters.get(param['ID'])):
param['Name'] = custom_parameter.get('Name')
param['Description'] = custom_parameter.get('Description')
if (grammacodes := custom_parameter.get('Grammacodes')):
param['Grammacodes'] = [
id_.strip()
for id_ in grammacodes.split(',')]
code_table = [
{
'ID': '{}-{}'.format(slug(param_id), slug(value)),
'Parameter_ID': slug(param_id),
'Name': value,
'Description': description,
}
for param_id, param in ling_variables.items()
for value, description in param.get('Levels', {}).items()]
for code in code_table:
if (custom_code := custom_codes.get(code['ID'])):
code['Name'] = custom_code.get('Name', '')
code['Description'] = custom_code.get('Description', '')
code['Map_Icon'] = custom_code.get('Map_Icon', '')
value_table = [
{
'ID': '{}-{}'.format(
language_id(row['glottocode'], row['language']),
slug(parameter)),
'Language_ID': language_id(row['glottocode'], row['language']),
'Parameter_ID': slug(parameter),
'Code_ID': '{}-{}'.format(slug(parameter), slug(row[parameter])),
'Value': row[parameter],
}
for row in value_data
for parameter in map(lambda p: p['Original_Name'], parameter_table)
if row.get(parameter, '').strip()]
# cldf schema
args.writer.cldf.add_component(
'LanguageTable',
'WALS_code', 'Genus', 'Family', 'Area', 'Continent',
'Macrocontinent', 'Macrocontinent_SplitOldWorld', 'Circumpacific',
{
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#source',
'separator': ';',
'name': 'Source',
},
'Source_prose')
args.writer.cldf.add_component(
'ParameterTable',
{
'name': 'Grammacodes',
'separator': ';',
})
args.writer.cldf.add_component('CodeTable', 'Map_Icon')
# write cldf
args.writer.cldf.add_sources(*sources.values())
args.writer.objects['LanguageTable'] = language_table
args.writer.objects['ParameterTable'] = parameter_table
args.writer.objects['CodeTable'] = code_table
args.writer.objects['ValueTable'] = value_table