-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrammaticon_makecsv.py
369 lines (332 loc) · 13.3 KB
/
grammaticon_makecsv.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
#!/usr/bin/env python3
import csv
import sys
from pathlib import Path
from csvw.metadata import Column, Table, TableGroup
RAW_TO_CSWV_MAP = {
'Concepts.csv': {
'name': 'concepts.csv',
'columns': {
'id': {
'name': 'ID',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#id'},
'label': {
'name': 'Name',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#name'},
'grammacode': {
'name': 'Grammacode',
'datatype': 'string'},
'definition': {
'name': 'Description',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#definition'},
'comments': {
'name': 'Comment',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#comment'},
'SIL counterpart': {
'name': 'SIL_Counterpart',
'datatype': 'string'},
'SIL URL': {
'name': 'SIL_URL',
'datatype': 'string'},
'Wikipedia counterpart': {
'name': 'Wikipedia_Counterpart',
'datatype': 'string'},
'Wikipedia URL': {
'name': 'Wikipedia_URL',
'datatype': 'string'}}},
'Metafeatures.csv': {
'name': 'metafeatures.csv',
'columns': {
'id': {
'name': 'ID',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#id'},
'name': {
'name': 'Name',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#name'},
'feature_area': {
'name': 'Feature_Area',
'datatype': 'string'}}},
'Feature_lists.csv': {
'name': 'feature-lists.csv',
'columns': {
'id': {
'name': 'ID',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#id'},
'name': {
'name': 'Name',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#name'},
'URL': {
'name': 'URL',
'datatype': 'string'},
# TODO: split authors?
'authors': {
'name': 'Authors',
'datatype': 'string'},
'number of features': {
'name': 'Number_of_Features',
'datatype': 'integer'},
'year': {
'name': 'Year',
'datatype': 'string'}}},
'Features.csv': {
'name': 'features.csv',
'columns': {
'feature_ID': {
'name': 'ID',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#id'},
'feature name': {
'name': 'Name',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#name'},
'feature description': {
'name': 'Description',
'datatype': 'string',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#definition'},
'meta_feature_id': {
'name': 'Metafeature_ID',
'datatype': 'string'},
'collection_id': {
'name': 'Feature_List_ID',
'datatype': 'string'},
'collection URL': {
'name': 'Feature_List_URL',
'datatype': 'string'},
# TODO: should this be a list?
'collection numbers': {
'name': 'Feature_List_Numbers',
'datatype': 'string'}},
'foreign-keys': {
'Metafeature_ID': 'metafeatures.csv',
'Feature_List_ID': 'feature-lists.csv'}},
'Concepts_metafeatures.csv': {
'name': 'concepts-metafeatures.csv',
'columns': {
'x_Concepts_Metafeatures::concept_id': {
'name': 'Concept_ID',
'datatype': 'string'},
'x_Concepts_Metafeatures::meta_feature__id': {
'name': 'Metafeature_ID',
'datatype': 'string'}},
'foreign-keys': {
'Concept_ID': 'concepts.csv',
'Metafeature_ID': 'metafeatures.csv'}}}
def simplified_concept_hierarchy(original_hierarchy, concept_ids):
CONCEPT_ID_COL = 'x_concepthierarchy::concept_id'
CHILD_COL = 'x_concepthierarchy::concept_child_id'
PARENT_COL = 'x_concepthierarchy::concept_parent_id'
# The table looks like rows only have *either* a child_id *or* a parent id.
# Check this assumption:
conflicting = [
row
for row in original_hierarchy
# source for the hacky xor: https://stackoverflow.com/a/433161
if bool(row.get(CHILD_COL)) == bool(row.get(PARENT_COL))]
assert not conflicting, f'concepthierarchy: concepts with parent *and* child: {conflicting}'
# The table also looks reflexive. Every concept--child pair seems to have
# a redundant concept--parent pair.
# Check this assumption:
children = {
(row[CHILD_COL], row[CONCEPT_ID_COL])
for row in original_hierarchy
if row.get(CHILD_COL) in concept_ids}
parents = {
(row[CONCEPT_ID_COL], row[PARENT_COL])
for row in original_hierarchy
if row.get(PARENT_COL) in concept_ids}
assert children == parents, f'I expect all pairs to be reflexive'
def valid_hierarchy_path(child_id, parent_id):
if child_id not in concept_ids:
msg = (
'concept-hierarchy.csv:'
" unknown child id '{}' for parent {}".format(
child_id, parent_id))
print(msg, file=sys.stderr)
return False
elif parent_id not in concept_ids:
msg = (
'concept-hierarchy.csv:'
" unknown parent id '{}' for child {}".format(
parent_id, child_id))
print(msg, file=sys.stderr)
return False
else:
return True
assocs = sorted(parents, key=lambda row: tuple(map(int, row)))
return [
{'Child_ID': child_id, 'Parent_ID': parent_id}
for child_id, parent_id in assocs
if valid_hierarchy_path(child_id, parent_id)]
def main():
here = Path(__file__).parent
src_dir = here / 'raw' / 'csv-export'
dest_dir = here / 'csvw'
raw_tables = [
src_dir / 'Concepts.csv',
src_dir / 'Metafeatures.csv',
src_dir / 'Feature_lists.csv',
src_dir / 'Features.csv',
src_dir / 'Concepts_metafeatures.csv',
]
table_data = {}
# TODO: add more metadata
# excerpt from tseltal:
# {"rdf:ID": "tseltal",
# "dc:title": "Tseltal-Spanish multidialectal dictionary",
# "dcat:accessURL": "https://github.com/dictionaria/tseltal",
# "prov:wasGeneratedBy": [
# {"dc:title": "python",
# "dc:description": "3.8.10"},
# {"dc:title": "python-packages",
# "dc:relation": "requirements.txt"}]}
table_meta_data = TableGroup(at_props={
"context": [
"http://www.w3.org/ns/csvw",
{"@language": "en"},
]
})
# load data
# FIXME: meta feature names are not unique
# So far the webapp has just collapsed them and mapped all the references
# in other tables to the first occurence:
#
# | ID | Name | Area | | Bla | Metafeature_ID |
# |----+--------+--------------| |-----+----------------|
# | 1 | name 1 | an area | | ... | 1 |
# | 2 | name 2 | another area | | ... | 2 |
# | 3 | name 1 | an area | | ... | 3 |
# | 4 | name 4 | | | ... | 4 |
#
# vvvv vvvv
#
# | ID | Name | Area | | Bla | Metafeature_ID |
# |----+--------+--------------| |-----+----------------|
# | 1 | name 1 | an area | | ... | 1 |
# | 2 | name 2 | another area | | ... | 2 |
# | 4 | name 4 | | | ... | 1 |
# | ... | 4 |
#
# It does not do that anymore because this should be handled at the data
# curation level.
for raw_path in raw_tables:
table_spec = RAW_TO_CSWV_MAP[raw_path.name]
table_name = table_spec['name']
columns = table_spec['columns']
with open(raw_path, encoding='utf-8') as f:
reader = csv.reader(f)
header = next(reader)
missing = [col for col in columns if col not in header]
assert not missing, f'{raw_path}: missing fields: {missing}'
unknown_fields = [col for col in header if col not in columns]
assert not unknown_fields, f'{raw_path}: unknown fields: {unknown_fields}'
mapped_colnames = [
columns[colname]['name']
for colname in header]
table_data[table_name] = [
{k: v for k, v in zip(mapped_colnames, row) if v}
for row in reader]
table = Table(url=table_name)
table.tableSchema.columns = list(
map(Column.fromvalue, columns.values()))
for col, target_table in table_spec.get('foreign-keys', {}).items():
table.add_foreign_key(col, target_table, 'ID')
table_meta_data.tables.append(table)
# deal with the concept hierarchy separately
with open(src_dir / 'Concepthierarchy.csv', encoding='utf-8') as f:
reader = csv.reader(f)
header = list(next(reader))
original_hierarchy = [
{k: v for k, v in zip(header, row) if v}
for row in reader]
table_data['concept-hierarchy.csv'] = simplified_concept_hierarchy(
original_hierarchy,
{row['ID'] for row in table_data['concepts.csv']})
table = Table(url='concept-hierarchy.csv')
table.tableSchema.columns = [
Column.fromvalue({'name': 'Child_ID', 'datatype': 'string'}),
Column.fromvalue({'name': 'Parent_ID', 'datatype': 'string'})]
table.add_foreign_key('Child_ID', 'concepts.csv', 'ID')
table.add_foreign_key('Parent_ID', 'concepts.csv', 'ID')
table_meta_data.tables.append(table)
# ensure valid data
metafeature_ids = {row['ID'] for row in table_data['metafeatures.csv']}
assert '' not in metafeature_ids
assert None not in metafeature_ids
feature_list_ids = {r['ID'] for r in table_data['feature-lists.csv']}
assert '' not in feature_list_ids
assert None not in feature_list_ids
def correct_concept(row):
if 'Name' not in row:
msg = (
'concepts.csv:'
' missing name for concept {}'.format(row['ID']))
print(msg, file=sys.stderr)
return False
else:
return True
table_data['concepts.csv'] = [
row
for row in table_data['concepts.csv']
if correct_concept(row)]
def correct_concept_metadata(row):
if 'Metafeature_ID' not in row:
msg = (
'concepts-metafeatures.csv:'
' missing metafeature id for concept {}'.format(
row['Concept_ID']))
print(msg, file=sys.stderr)
return False
elif (mfid := row['Metafeature_ID']) not in metafeature_ids:
msg = (
'concepts-metafeatures.csv:'
' invalid metafeature id for concept {}: {}'.format(
row['Concept_ID'], mfid))
print(msg, file=sys.stderr)
return False
else:
return True
table_data['concepts-metafeatures.csv'] = [
row
for row in table_data['concepts-metafeatures.csv']
if correct_concept_metadata(row)]
def correct_feature(row):
if 'Metafeature_ID' not in row or 'Feature_List_ID' not in row:
msg = (
'features.csv:'
' missing metafeature id or missing feature list id'
' for feature {}'.format(row['ID']))
print(msg, file=sys.stderr)
return False
elif (mfid := row['Metafeature_ID']) not in metafeature_ids:
msg = (
'features.csv:'
' invalid metafeature id for feature {}: {}'.format(
mfid, row['ID']))
print(msg, file=sys.stderr)
return False
elif (flid := row['Feature_List_ID']) not in feature_list_ids:
msg = (
'features.csv:'
' invalid feature list id for feature {}: {}'.format(
flid, row['ID']))
print(msg, file=sys.stderr)
return False
else:
return True
table_data['features.csv'] = [
row
for row in table_data['features.csv']
if correct_feature(row)]
# write data
dest_dir.mkdir(parents=True, exist_ok=True)
table_meta_data.write(dest_dir / 'csvw-metadata.json', **table_data)
if __name__ == '__main__':
main()