-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexibank_allenbai.py
More file actions
153 lines (132 loc) · 6.04 KB
/
Copy pathlexibank_allenbai.py
File metadata and controls
153 lines (132 loc) · 6.04 KB
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
import attr
from pathlib import Path
import pylexibank
from cldfbench import CLDFSpec
from pyclts import CLTS
import lingpy
from clldutils.misc import slug
from unicodedata import normalize
@attr.s
class CustomConcept(pylexibank.Concept):
Chinese_Gloss = attr.ib(default=None)
Number = attr.ib(default=None)
@attr.s
class CustomLanguage(pylexibank.Language):
ChineseName = attr.ib(default=None)
DialectGroup = attr.ib(default=None)
SubGroup = attr.ib(default=None)
class Dataset(pylexibank.Dataset):
dir = Path(__file__).parent
id = "allenbai"
concept_class = CustomConcept
language_class = CustomLanguage
writer_options = dict(keep_languages=False, keep_parameters=False)
def cmd_download(self, **kw):
self.raw_dir.write("sources.bib", pylexibank.getEvoBibAsBibtex("Allen2007", **kw))
def cldf_specs(self):
return {
None: pylexibank.Dataset.cldf_specs(self),
"structure": CLDFSpec(
module="StructureDataset",
dir=self.cldf_dir,
data_fnames={"ParameterTable": "features.csv"},
),
}
def cmd_makecldf(self, args):
with self.cldf_writer(args) as writer:
wl = lingpy.Wordlist(self.raw_dir.joinpath("Bai-Dialect-Survey.tsv").as_posix())
writer.add_sources()
# TODO: add concepts with `add_concepts`
concept_lookup = {}
for concept in self.conceptlists[0].concepts.values():
idx = concept.id.split("-")[-1] + "_" + slug(concept.english)
writer.add_concept(
ID=idx,
Name=concept.english,
Chinese_Gloss=concept.attributes["chinese"],
Number=concept.number,
Concepticon_ID=concept.concepticon_id,
Concepticon_Gloss=concept.concepticon_gloss,
)
concept_lookup[concept.english] = idx
language_lookup = writer.add_languages(lookup_factory="Name")
for k in pylexibank.progressbar(wl, desc="wl-to-cldf"):
if wl[k, "value"]:
writer.add_lexemes(
Language_ID=language_lookup[wl[k, "doculect"]],
Parameter_ID=concept_lookup[wl[k, "concept"]],
Value=wl[k, "value"],
Source="Allen2007",
)
language_table = writer.cldf["LanguageTable"]
# Remove column for ISO639P3code since there are no ISO codes.
writer.cldf["LanguageTable"].tableSchema.columns = [
col
for col in writer.cldf["LanguageTable"].tableSchema.columns
if col.name != "ISO639P3code"
]
with self.cldf_writer(args, cldf_spec="structure", clean=False) as writer:
# We share the language table across both CLDF datasets:
writer.cldf.add_component(language_table)
writer.objects["LanguageTable"] = self.languages
inventories = self.raw_dir.read_csv(
"inventories.tsv", normalize="NFC", delimiter="\t", dicts=True
)
writer.cldf.add_columns(
"ParameterTable",
{"name": "CLTS_BIPA", "datatype": "string"},
{"name": "CLTS_Name", "datatype": "string"},
{"name": "Lexibank_BIPA", "datatype": "string"},
{"name": "Prosody", "datatype": "string"},
)
writer.cldf.add_columns("ValueTable", {"name": "Context", "datatype": "string"})
clts = CLTS(args.clts.dir)
bipa = clts.transcriptionsystem_dict["bipa"]
td = clts.transcriptiondata_dict["allenbai"]
pids, visited = set(), set()
for row in pylexibank.progressbar(inventories, desc="inventories"):
for s1, s2, p in zip(
row["Value"].split(), row["Lexibank"].split(), row["Prosody"].split()
):
pidx = (
"-".join([str(hex(ord(s)))[2:].rjust(4, "0") for s in row["Value"]])
+ "_"
+ p
)
s1 = normalize("NFD", s1)
if not s1 in td.grapheme_map:
args.log.warn(
"missing sound {0} / {1}".format(
s1, " ".join([str(hex(ord(x))) for x in s1])
)
)
else:
sound = bipa[td.grapheme_map[s1]]
sound_name = sound.name if sound.type not in ["unknown", "marker"] else ""
if not pidx in visited:
visited.add(pidx)
writer.objects["ParameterTable"].append(
{
"ID": pidx,
"Name": s1,
"Description": sound_name,
"CLTS_BIPA": td.grapheme_map[s1],
"CLTS_Name": sound_name,
"Lexibank_BIPA": s2,
"Prosody": p,
}
)
if row["Language_ID"] + "_" + pidx in pids:
continue
else:
writer.objects["ValueTable"].append(
{
"ID": row["Language_ID"] + "_" + pidx,
"Language_ID": row["Language_ID"],
"Parameter_ID": pidx,
"Value": s1,
"Context": p,
"Source": ["Allen2007"],
}
)
pids.add(row["Language_ID"] + "_" + pidx)