-
Notifications
You must be signed in to change notification settings - Fork 1
/
autotyp_dataset.py
426 lines (383 loc) · 16.3 KB
/
autotyp_dataset.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import collections
import re
import json
import os
import pathlib
import itertools
from csvw.metadata import Datatype
import attr
import yaml
from clldutils import jsonlib
from clldutils.path import walk
from cldfbench import Dataset as BaseDataset, CLDFSpec
DTYPES = {
'comment': 'string',
'logical': 'boolean',
'value-list': 'string',
'table': 'json',
'list-of<value-list>': 'string',
'list-of<integer>': 'integer',
}
# is there a better way to do this?
# global_root_dir = None
def data_path(mdpath, raw_dir):
autotyp_data = mdpath.parent
while autotyp_data != raw_dir:
autotyp_data = autotyp_data.parent
assert autotyp_data.parent != autotyp_data
assert autotyp_data == raw_dir
base = autotyp_data / 'data' / 'json'
if mdpath.stem != 'Register':
dp = base / mdpath.parent.name / '{}.json'.format(mdpath.stem)
else:
dp = base / '{}.json'.format(mdpath.stem)
assert dp.exists()
return dp
class Counts:
def __init__(self):
self.cid = 0
self.vid = 0
def inc(self, att):
setattr(self, att, getattr(self, att) + 1)
return getattr(self, att)
@attr.s
class Parameter:
id = attr.ib()
name = attr.ib()
vars = attr.ib()
dataset = attr.ib()
counts = attr.ib()
unitset = attr.ib()
raw_dir = attr.ib()
data = attr.ib(default=None)
md = attr.ib(default=attr.Factory(dict))
code_map = attr.ib(default=attr.Factory(dict))
dt = attr.ib(default=None)
def __attrs_post_init__(self):
self.md.update(self.vars[0])
if self.unitset:
self.md.update(self.dataset[1])
else:
self.md.update(self.dataset[1]['fields'][self.name])
#print(self.dataset[0])
self.data = data_path(self.dataset[0], self.raw_dir)
#
# FIXME: adapt description for datatype == 'table'
#
if self.datatype == 'table':
assert self.md['fields']
self.dt = Datatype.fromvalue('json')
else:
self.dt = Datatype.fromvalue(DTYPES.get(self.datatype, self.datatype))
@property
def dataset_id(self):
return self.md['dataset']
@property
def fields(self):
return self.md['fields']
@property
def datatype(self):
return self.md['data']
@property
def multivalued(self):
return self.datatype == 'table' or self.datatype.startswith('list-of<')
def iter_codes(self):
if not self.unitset:
if self.datatype in ['value-list', 'list-of<value-list>']:
for code, desc in self.md['values'].items():
yield self.check_code(code, desc=desc)
def _iter_values(self):
objs = {}
for obj in jsonlib.load(self.data):
lid = obj.get('LID')
if lid:
if not self.unitset:
if lid in objs:
assert objs[lid] == obj
# drop duplicates
continue
objs[lid] = obj
v = obj[self.name]
if v is not None:
yield obj['LID'], v
else:
yield obj['LID'], {
k: v for k, v in obj.items()
if k not in ['LID', 'Language', 'Glottocode'] and v != None}
def check_code(self, value, desc=None):
assert value not in self.code_map
cid = self.counts.inc('cid')
self.code_map[value] = str(cid)
return dict(
ID=str(cid),
Name=value,
Description=desc,
Parameter_ID=str(self.id),
)
def iter_values(self):
for lid, v in self._iter_values():
values = []
if self.unitset:
values.append(v)
else:
if self.multivalued:
assert isinstance(v, list)
for vv in v:
if self.datatype == 'table':
nv = {}
for kkk, vvv in vv.items():
if vvv is not None:
if 'values' in self.md['fields'][kkk]:
if vvv not in self.md['fields'][kkk]['values']:
# FIXME: do something here!
pass
nv[kkk] = vvv
values.append(nv)
elif self.datatype == 'list-of<integer>':
values.append(vv)
elif self.datatype == 'list-of<value-list>':
values.append(vv)
else:
values.append(v)
for value in values:
yield dict(
ID=str(self.counts.inc('vid')),
Language_ID=lid,
Parameter_ID=self.id,
Value=self.dt.formatted(value),
Code_ID=self.code_map.get(value) if not (self.unitset or isinstance(value, dict)) else None,
)
def iter_cols(md, fmap=None):
fmap = fmap or {}
for col, spec in md['fields'].items():
if col not in fmap:
csvwspec = {
"name": col,
"dc:description": spec['description'],
"dc:format": spec['kind'],
"datatype": DTYPES.get(spec['data']) or spec['data'],
}
if spec['data'] == 'value-list':
if col not in ['OriginContinent']:
csvwspec['datatype'] = {
'base': 'string', 'format': '|'.join(re.escape(k) for k in spec['values'].keys())}
#csvwspec['separator'] = "; "
csvwspec['dc:description'] = "{}\n\n{}".format(
csvwspec['dc:description'],
'\n'.join('{}: {}'.format(k, v) for k, v in spec['values'].items())
)
yield csvwspec
# def fix_bib(s):
# n = []
# for line in s.split('\n'):
# if 'author' in line or ('editor' in line):
# line = line.replace(' / ', ' and ')
# line = line.replace(' & ', ' and ')
# line = line.replace('&', ' and ')
# line = line.replace('/', ' and ')
# n.append(line)
# s = '\n'.join(n)
# repls = {
# 'Csató, Éva Ágnes, Isaksson, Bo': 'Csató, Éva Ágnes and Isaksson, Bo',
# 'Aikhenvald, A., R.M.W.Dixon,': 'Aikhenvald, A. and R.M.W.Dixon,',
# 'Rivai, F.S., Sorrentino, A.': 'Rivai, F.S. and Sorrentino, A.',
# 'E. Ashton, E. M. K. Ostell, E. G. M. Mulira, Ndawula': 'E. Ashton, E. M. K. and Ostell, E. G. M. and Mulira, Ndawula',
# 'Bickel, Balthasar, Martin Gaenszle, Arjun Rai, Prem D. Rai, Shree K. Rai, Vishnu S. Rai, Narayan P. Sharma (Gautam)':
# 'Bickel, Balthasar and Martin Gaenszle and Arjun Rai and Prem D. Rai and Shree K. Rai and Vishnu S. Rai and Narayan P. Sharma (Gautam)',
# 'Zigmond, Maurice L. , Munro, Pamela': 'Zigmond, Maurice L. and Munro, Pamela',
# 'Balthasar Bickel, Manoj Rai, Netra P. Paudyal, Goma Banjade, Toya N. Bhatta, Martin Gaenszle, Elena Lieven, Ichchha Purna Rai, Novel Kishore Rai,':
# 'Balthasar Bickel and Manoj Rai and Netra P. Paudyal and Goma Banjade and Toya N. Bhatta and Martin Gaenszle and Elena Lieven and Ichchha Purna Rai and Novel Kishore Rai',
# }
# for k, v in repls.items():
# s = s.replace(k, v)
# return s
class Dataset(BaseDataset):
@property
def cldf_dir(self):
return self.dir / 'data' / 'cldf'
@property
def raw_dir(self):
return self.dir
dir = os.getcwd()
id = "autotyp"
def cldf_specs(self): # A dataset must declare all CLDF sets it creates.
return CLDFSpec(dir=self.cldf_dir, module='StructureDataset')
def cmd_download(self, args):
# FIXME: update git submodule?
pass
def cmd_makecldf(self, args):
"""
We can meaningfully only split datasets with no multiples per language into individual variables!
I.e. for the ones listed below, each autotyp "record" must be converted to **one**
composite JSON value.
"""
unitsets = {
# Morphology:
'MorphemeClasses': None,
'GrammaticalMarkers': None,
'LocusOfMarkingPerMicrorelation': None,
'MaximallyInflectedVerbSynthesis': (
[ # Nested variables:
"VerbInflectionExponenceType",
"VerbInflectionMaxCategoryCount",
"VerbInflectionMaxCategorySansAgreementCount",
"VerbInflectionMaxFormativeCount",
"VerbHasBipartiteStem",
"VerbIsSyntacticallyCoherent",
"VerbIsProsodicallyCoherent",
"VerbProsodicCoherencyNotes",
"VerbIsPhonologicallyCoherent",
"VerbHasAnyIncorporation",
"VerbHasNounIncorporation",
"VerbHasVerbIncorporation",
"VerbHasNounOrVerbIncorporation",
],
# Condition for value inclusion:
lambda obj: obj["IsVerbInflectionSurveyComplete"] and obj["IsVerbAgreementSurveyComplete"],
),
'DefaultLocusOfMarkingPerMacrorelation': None,
# GrammaticalRelations
'PredicateClasses': None,
'GrammaticalRelations': None,
'GrammaticalRelationsRaw': None,
'Alignment': None,
# PerLanguageSummaries
'NPStructurePresence': None,
# Word
'WordDomains': None,
# NP
'NPStructure': None,
# Sentence
'ClauseLinkage': None,
}
# set the global root so that Parameter can correctly find the data
# global global_root_dir
# global_root_dir = self.raw_dir
# read the bib!
# args.writer.cldf.sources.add(fix_bib(self.raw_dir.joinpath(
# 'autotyp-data', 'bibliography', 'autotyp.bib').read_text(encoding='utf8')))
args.writer.cldf.sources.add(self.raw_dir.joinpath('bibliography', 'autotyp.bib').read_text(encoding='utf8'))
l2src = collections.defaultdict(set)
for src in args.writer.cldf.sources.items():
for lid in src.get('languageid', "").split(','):
lid = lid.strip()
if lid:
l2src[lid].add(src.id)
counts = Counts()
datasets = dict()
for p in walk(self.raw_dir / 'metadata', mode='files'):
if p.suffix == '.yaml' and p.parent.name != 'Definitions':
try:
datasets[p.stem] = (p.resolve(), yaml.load(p.read_text(encoding='utf8'), yaml.CLoader))
except Exception as e:
print(f"Error loading {p.stem}")
raise e
# datasets = {
# p.stem: (p.resolve(), yaml.load(p.read_text(encoding='utf8'), yaml.CLoader))
# for p in walk(self.raw_dir / 'metadata', mode='files')
# if p.suffix == '.yaml' and p.parent.name != 'Definitions'}
assert len(datasets) == 46
parameters, pid = [], 0
variables = collections.OrderedDict([
((r['dataset'], r['variable']), r)
for r in self.raw_dir.read_csv('variables_overview.csv', dicts=True)])
for pid, ((ds, var), rows) in enumerate(itertools.groupby(
variables.values(),
lambda r: (r['dataset'], None if r['dataset'] in unitsets else r['variable'].split('$')[0]),
), start=1):
if ds != 'Register' and (var in ['LID', 'Language', 'Glottocode']):
continue
rows = list(rows)
assert ds in datasets
datasets[ds][1]['dataset_kind'] = rows[0]['dataset_kind']
parameters.append(Parameter(id=pid, name=var or ds, vars=rows, dataset=datasets[ds], counts=counts, unitset=var is None, raw_dir = self.raw_dir))
fmap = {
'LID': 'ID',
'Language': 'Name',
'ISO639_3': 'ISO639P3code',
'Glottocode': None,
'Latitude': None,
'Longitude': None,
}
t = args.writer.cldf.add_component(
'LanguageTable',
{"name": "Source", "separator": "; ", "propertyUrl": "http://cldf.clld.org/v1.0/terms.rdf#source"},
*list(iter_cols(datasets['Register'][1], fmap)))
t.common_props['dc:description'] = datasets['Register'][1]['description']
args.writer.cldf.add_component('ContributionTable', 'dataset_kind')
args.writer.cldf.add_component(
'ParameterTable', 'module', 'submodule', 'kind', 'datatype',
{"name": 'dim', "datatype": "integer"},
{"name": "unitset", "datatype": "boolean"},
{"name": "multivalued", "datatype": "boolean"},
{"name": "dataset", "propertyUrl": "http://cldf.clld.org/v1.0/terms.rdf#contributionReference"}
)
args.writer.cldf.add_component('CodeTable')
for name, (_, md) in datasets.items():
args.writer.objects['ContributionTable'].append(dict(
ID=name, Name=name, Description=md['description'], dataset_kind=md['dataset_kind'],
))
for row in jsonlib.load(data_path(datasets['Register'][0], self.raw_dir)):
obj = {
fmap.get(k) or k: None if v == 'NA' else v for k, v in row.items()
}
obj['Source'] = l2src.pop(str(obj['ID']), [])
args.writer.objects['LanguageTable'].append(obj)
for p in [pp for pp in parameters if pp.dataset_id != 'Register']:
desc = p.md['description']
if p.datatype == 'table':
# There can be nested tables now!
#assert not any(vv['data'] == 'table' for vv in p.fields.values()), str(p.name)
# FIXME: add field name, and value description
desc = desc + '\n\n' + '\n'.join(vv['description'] for kk, vv in p.fields.items())
args.writer.objects['ParameterTable'].append(dict(
ID=str(p.id),
Name=p.name,
Description=desc,
module=p.md['modules'],
dataset=p.dataset_id,
kind=p.md['kind'],
datatype=p.md['data'],
dim=len(p.fields) if p.datatype == 'table' else 1,
ColumnSpec=dict(datatype=DTYPES.get(p.datatype, p.datatype)),
multivalued=p.multivalued,
unitset=p.unitset,
))
#print(p.name, p.data)
for code in p.iter_codes():
args.writer.objects['CodeTable'].append(code)
subvars, condition = unitsets.get(p.name), None
idmap = {}
if subvars:
subvars, condition = subvars
for subvar in subvars:
md = variables[p.name, subvar]
pid += 1
idmap[subvar] = str(pid)
args.writer.objects['ParameterTable'].append(dict(
ID=str(pid),
Name='{}_{}'.format(p.name, subvar),
#Description=desc,
module=md['modules'],
dataset=p.name,
kind=md['kind'],
datatype=md['data'],
dim=1,
ColumnSpec=dict(datatype=DTYPES.get(md['data'], md['data'])),
multivalued=False,
unitset=False,
))
for val in p.iter_values():
args.writer.objects['ValueTable'].append(val)
if subvars:
v = json.loads(val['Value'])
if condition(v):
for subvar in subvars:
if subvar in v:
args.writer.objects['ValueTable'].append(dict(
ID=str(counts.inc('vid')),
Language_ID=val['Language_ID'],
Parameter_ID=idmap[subvar],
Value=v[subvar],
Code_ID=None,
))