-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcldfbench_hueblerstability.py
190 lines (173 loc) · 6.89 KB
/
cldfbench_hueblerstability.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
import re
import pathlib
import itertools
import collections
import attr
from cldfbench import CLDFSpec
from cldfbench import Dataset as BaseDataset
REF_PATTERNS = [
re.compile(r'(?P<author>[^(]+)\((?P<year>[^:)]+)(:(?P<pages>.*))?\)'),
re.compile(r'(?P<author>[^\s]+)\s+(?P<year>[0-9]{4}[a-z]?)'),
]
SOURCES = {
'cho2015syllable': """
@article{cho2015syllable,
title={Syllable-based phonological processes},
author={Cho, Young-mee Yu},
journal={The handbook of Korean linguistics},
pages={22},
year={2015},
publisher={Wiley Online Library}
}
""",
'skhatyla': """
@misc{sakhatyla,
title = {SakhaTyla.Ru},
url = {https://sakhatyla.ru/},
howpublished = {Online dictionary}
}
"""
}
@attr.s
class Reference:
author = attr.ib(converter=lambda s: s.strip())
year = attr.ib(converter=lambda s: s.strip())
pages = attr.ib(converter=lambda s: s.strip() if s else s, default=None)
@property
def key(self):
return self.author, self.year
def as_cldf(self, key):
res = key
if self.pages:
res += '[{}]'.format(self.pages)
return res
def match_ref(ref):
ref = ref.strip()
for p in REF_PATTERNS:
m = p.fullmatch(ref)
if m:
return Reference(**m.groupdict())
class Dataset(BaseDataset):
dir = pathlib.Path(__file__).parent
id = "hueblerstability"
def cldf_specs(self):
return CLDFSpec(dir=self.cldf_dir, module="StructureDataset")
def cmd_download(self, args):
pass
def cmd_makecldf(self, args):
self._schema(args.writer.cldf)
glottolog = args.glottolog.api
sources = {
(r['author'], r['year']): r for r in self.etc_dir.read_csv('sources.csv', dicts=True)}
langs = {
r['Name']: r for r in self.etc_dir.read_csv('languages_geo.csv', dicts=True)}
examples = {eid: list(ex) for eid, ex in itertools.groupby(
sorted(self.etc_dir.read_csv('examples.csv', dicts=True), key=lambda r: r['ID']),
lambda r: r['ID']
)}
for eid, ex in examples.items():
for i, e in enumerate(ex, start=1):
e['ID'] = '{}-{}'.format(e['ID'], i)
pids, srcids, codes = set(), set(), collections.defaultdict(set)
for sheet in self.raw_dir.glob('*.tsv'):
m = re.fullmatch(r'(?P<name>[^\[]+)\[(?P<gc>[a-z0-9]{8})]', sheet.stem)
lname, gc = m.group('name').strip(), m.group('gc')
args.writer.objects['LanguageTable'].append(dict(
ID=gc,
Name=lname,
Glottocode=gc,
Latitude=float(langs[lname]['Latitude']),
Longitude=float(langs[lname]['Longitude']),
))
values = self.raw_dir.read_csv(sheet.name, delimiter='\t', dicts=True)
assert values
for val in values:
assert val['Grambank ID'] and val['Value'], str(val)
codes[val['Grambank ID']].add(val['Value'])
if val['Grambank ID'] not in pids:
args.writer.objects['ParameterTable'].append(dict(
ID=val['Grambank ID'],
Name=val['Feature'],
))
pids.add(val['Grambank ID'])
refs = []
for ref in val['Source'].split(';'):
ref = ref.strip()
if not ref:
continue
if ref == 'Savelev, Alexandr (p.c. 2016)':
val['Comment'] = 'Source: ' + ref
val['Source'] = ''
continue
if ref in {
'Sakha online dictionary (SakhaTyla.ru)',
'Sakha online dictionary, 2018',
'Sakha online dictionary',
}:
refs.append('sakhatyla')
srcids.add('sakhatyla')
continue
elif ref in {
'Cho (2015:28) Syllable-based phonological processes',
}:
refs.append('cho2015syllable')
srcids.add('cho2015syllable')
continue
reference = match_ref(ref)
if reference:
if reference.key not in sources:
args.log.warning(reference.key)
#raise ValueError(reference.key)
elif sources[reference.key]['matchid']:
refs.append(reference.as_cldf(sources[reference.key]['matchid']))
srcids.add(sources[reference.key]['matchid'])
else:
raise ValueError(ref)
args.writer.objects['ValueTable'].append(dict(
ID='{}-{}'.format(val['Grambank ID'], gc),
Value=val['Value'],
Language_ID=gc,
Parameter_ID=val['Grambank ID'],
Code_ID='{}-{}'.format(val['Grambank ID'], val['Value']) if val['Value'] != '?' else None,
Examples=[e['ID'] for e in examples.get('{}-{}'.format(gc, val['Grambank ID']), [])],
Source=refs,
))
for pid, vals in sorted(codes.items()):
for val in vals:
if val != '?':
args.writer.objects['CodeTable'].append(dict(
ID='{}-{}'.format(pid, val),
Parameter_ID=pid,
Name=val,
))
for exs in examples.values():
for ex in exs:
if not ex['Primary_Text']:
ex['Primary_Text'] = ex['Analyzed_Word']
ex['Analyzed_Word'] = ex['Analyzed_Word'].split()
ex['Gloss'] = ex['Gloss'].split()
if not ex['Gloss']:
ex['Gloss'] = None
if ex['Gloss'] and len(ex['Analyzed_Word']) != len(ex['Gloss']):
raise ValueError()
args.writer.objects['ExampleTable'].append(ex)
for srcid in sorted(srcids):
if srcid in SOURCES:
args.writer.cldf.add_sources(SOURCES[srcid])
else:
src = glottolog.bibfiles[srcid]
src.key = srcid
args.writer.cldf.add_sources(str(src))
def _schema(self, cldf):
cldf.add_columns(
'ValueTable',
{
'name': 'Examples',
'separator': ' ',
'propertyUrl': 'http://cldf.clld.org/v1.0/terms.rdf#exampleReference'},
)
cldf['ValueTable', 'value'].null = ['?']
cldf.add_component('ExampleTable')
cldf.add_component('LanguageTable')
cldf.add_component('ParameterTable')
cldf.add_component('CodeTable')