-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
160 lines (148 loc) · 5.87 KB
/
analysis.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
"""
Analysis for the study on fuzzy reconstructions.
"""
from lingrex.fuzzy import FuzzyReconstructor, ntile
from lingpy import Wordlist, basictypes, Alignments
from lingrex.reconstruct import transform_alignment
from functools import partial
from sklearn.svm import SVC
from sys import argv
from tabulate import tabulate
import random
import itertools
import networkx as nx
from tqdm import tqdm as progressbar
random.seed(1234)
# switch if you want to use alignment in the source
align_data = True
align_s = partial(
transform_alignment, align=align_data, position=False,
prosody=True, startend=False, gap="-")
proto_language = "Proto"+argv[1]
clf = lambda : SVC(kernel="linear")
wl = Alignments("data/{0}.tsv".format(argv[1].lower()), ref="cogids",
transcription="form")
print("[i] loaded wordlist")
fr = FuzzyReconstructor(wl, proto_language, ref="cogids")
print("[i] loaded fuzzy reconstructor")
fr.random_splits(10, retain=0.9)
print("[i] carried out random splits")
fr.fit_samples(clf=clf, onehot=True, func=align_s, aligned=True if not
align_data else False, pb=True)
output = []
# now, we iterate over individual etymologies and predict them one by one
hits, fails = 0, 0
etd = wl.get_etymdict(ref="cogids")
all_languages = [l for l in wl.cols if l != proto_language]
confusion = nx.Graph()
ntile_dict = {}
for cogid, idxs_ in progressbar(etd.items(), desc="predictions"):
idxs = []
for idx in idxs_:
if idx:
idxs += [idx[0]]
languages = [wl[idx, "doculect"] for idx in idxs]
tokens = []
for idx in idxs:
tokens += [basictypes.lists(wl[idx, "tokens" if align_data else "alignment"]).n[
wl[idx, "cogids"].index(cogid)]]
if proto_language in languages and len(languages) > 2:
selected_idxs, selected_languages, selected_tokens = [], [], []
for idx, language, tks in zip(idxs, languages, tokens):
if language != proto_language:
selected_idxs += [idx]
selected_languages += [language]
selected_tokens += [tks]
target_word = " ".join(
[y for x, y in zip(languages, tokens) if x == proto_language][0]
)
words, pred = fr.predict(
selected_tokens, selected_languages, output="wp")
sounds = []
for i, snd in enumerate(pred):
sound, score = snd.split("¦")[0].split(":")
if score == "100":
if sound != "-":
sounds += [sound]
else:
sounds += ["?"]
all_sounds = snd.split("¦")
for sA, sB in itertools.combinations(all_sounds, r=2):
sndA, scoreA = sA.split(":")
sndB, scoreB = sB.split(":")
try:
confusion[sndA][sndB]["vals"] += [(cogid, i, scoreA, scoreB)]
except:
confusion.add_edge(
sndA,
sndB, vals=[(cogid, i, scoreA, scoreB)])
if " ".join(sounds) == target_word:
hits += 1
ntile_dict[cogid] = (1, target_word)
else:
ntile_dict[cogid] = (0, ntile(words))
fails += 1
alm = align_s(selected_tokens, selected_languages,
all_languages)
almr = []
for i in range(len(all_languages)):
try:
oridx = selected_languages.index(all_languages[i])
idx = selected_idxs[oridx]
concept = wl[idx, "concept"]
cogidx = wl[idx, "cogids"].index(cogid)
except:
concept = ""
cogidx = ""
almr += [[
all_languages[i],
concept,
cogidx
]+[row[i] for row in alm]]
almr += [["Fuzzy", "", "",]+pred]
almr += [[proto_language, "", ""]+target_word.split(" ")]
output += [[cogid, wl[idxs[0], "concept"], almr, len(almr[0])]]
# create ntile output
print("[i] writing quintiles to wordlist file")
wl.add_entries("quintiles", "tokens", lambda x: x)
# align data only for burmish and karen
if argv[1] in ["Karen", "Burmish"]:
wl.align()
for idx, doculect, tokens, cogids in wl.iter_rows("doculect", "tokens", "cogids"):
if doculect == "Proto" + argv[1]:
words = []
for i, cogid in enumerate(cogids):
if cogid in ntile_dict:
success, value = ntile_dict[cogid]
if success:
words += ["( "+value+" )"]
else:
words += [value]
else:
words += [" ".join(["?" + t for t in tokens.n[i]])]
wl[idx, "quintiles"] = " + ".join(words)
wl.output('tsv', filename=argv[1].lower()+"-quintiles", prettify=False, ignore="all")
print("[i] quintiles have been written to wordlist file.")
table = []
for nA, nB, data in confusion.edges(data=True):
table += [[
nA,
nB,
len(data["vals"]),
" ".join([str(x[0]) for x in data["vals"]])
]]
table_s = tabulate(sorted(table, key=lambda x: (x[2], x[0], x[1]), reverse=True), headers=[
"sound A", "sound B", "occurrences", "cognate sets"], tablefmt="pipe")
tmp = open("template.md")
template = tmp.read()
tmp.close()
with open(argv[1].lower()+".md", "w") as f:
f.write(template)
f.write("# Confused Sounds\n\n")
f.write(table_s+"\n\n")
f.write("# Individual Alignments with Fuzzy Reconstructions\n\n")
for row in output:
f.write("## COGID {0} / «{1}»\n\n".format(row[0], row[1]))
f.write(tabulate(row[2], tablefmt="pipe", headers=[
"language", "concept", "pos"]+["S{0}".format(i) for i in range(1, row[3]+1)])+"\n\n")
print(hits, hits/(hits+fails), fails, fails/(hits+fails), hits+fails)