-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigrams.py
More file actions
169 lines (134 loc) · 5.88 KB
/
bigrams.py
File metadata and controls
169 lines (134 loc) · 5.88 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import pickle
from utils import *
import sys
def get_bigram_frames(u, position):
""" Generate a diction of where key is a unigram and value is a list of unigrams that occur in its context
:param u: list of lists of tuples, e.g. [[('w1', 'V'), ('w2', 'V')], [('w1', 'V'),...]]
:return: bigrams_frames: dict where key is a unigram and value is a list of unigrams that occur in its context
"""
# Test data and results
# u = [[('w1', 'V'), ('w2', 'V')], [('w1', 'V'), ('w2', 'V')], [('w1', 'V'), ('w2', 'ADJ')], [('w3', 'ADV'), ('w4', 'N')]]
# result1 = {('w1', 'V'):[('w2', 'V'),('w2', 'V'),('w2', 'ADJ')], ('w3', 'ADV'):[('w4', 'N')]}
# position = False
# result2 = {('w2', 'V'):[('w1', 'V'),('w1', 'V')],('w2', 'ADJ'):[('w1', 'V')],('w4', 'N'):[('w3', 'ADV')]}
bigrams_frames = {}
w1 = 0
w2 = 1
if not position:
w1 = 1
w2 = 0
for i in u:
# Create w1 frames, i.e. {(w1,l1):[(w2,l2),(w3,l1)...], {}:[], ...}
if not i[w1] in bigrams_frames:
bigrams_frames[i[w1]] = []
bigrams_frames[i[w1]].append(i[w2])
else:
bigrams_frames[i[w1]].append(i[w2])
# Test
# print(bigrams_frames==result1)
# print(bigrams_frames == result2)
# print(bigrams_w2_frames==result2)
return bigrams_frames
def process_dyads(morphemes, type):
""" Process the parent-child dyads for bigrams probabilities. """
# Choose a language:
language = "Chintang"
# language = "Russian"
children = []
if language=="Chintang":
children = ['LDCh1', 'LDCh2', 'LDCh3', 'LDCh4']
if language=="Russian":
children = ['Child1', 'Child2', 'Child3', 'Child4']
for child in children:
print("Processing:", child)
u = None
if morphemes:
u = get_columns_as_tuples('select utterance_id_fk, morpheme, gloss from ChintangMorphemes where child = "' + child + '"')
else: # words
if language=="Chintang":
u = get_columns_as_tuples('select utterance_id_fk, word, pos from ChintangWords where child = "'+child+'"')
if language=="Russian":
u = get_columns_as_tuples('select utterance_id_fk, word, pos from RussianWords where child = "'+child+'"')
u = get_utterances(u) # wonky: this call to utils populates utils global vars
u = cut(u, 2) # clean the utterance and cut any that are length < N
# Get lists of ngrams
u = get_list_of_ngrams(u, 2)
# Create bigram frames: {(w1,l1):[(w2,l1),(...)],...]
bigrams_w1_frames = get_bigram_frames(u, position=True)
bigrams_w2_frames = get_bigram_frames(u, position=False)
# Containers to fill and pickle to disk.
bigrams_w1_pr = {}
bigrams_w2_pr = {}
# For w1 in bigrams
for k, v in bigrams_w1_frames.items():
precision = get_accuracy(v)
# recall = get_completeness(v, labels)
recall = get_recall(v)
bigrams_w1_pr[k] = (precision, recall)
with open(child+'_'+type+'_w1.pickle', 'wb') as handle:
pickle.dump(bigrams_w1_pr, handle)
# For w2 in bigrams
for k, v in bigrams_w2_frames.items():
precision = get_accuracy(v)
# recall = get_completeness(v, labels)
recall = get_recall(v)
bigrams_w2_pr[k] = (precision, recall)
with open(child+'_'+type+'_w2.pickle', 'wb') as handle:
pickle.dump(bigrams_w2_pr, handle)
def process_corpora(morphemes, type):
""" Process the corpora for bigram frequencies """
corpora = []
if morphemes:
corpora = ['Chintang', 'Inuktitut', 'Japanese_MiiPro', 'Sesotho', 'Turkish', 'Yucatec']
else:
corpora = ['Chintang', 'Inuktitut', 'Japanese_MiiPro', 'Russian', 'Sesotho', 'Turkish', 'Yucatec']
for corpus in corpora:
print("Processing:", corpus)
u = None
if morphemes:
u = get_columns_as_tuples(
'select utterance_id_fk, morpheme, gloss from morphemes where corpus = "' + corpus +'"')
else: # words
u = get_columns_as_tuples('select utterance_id_fk, word, pos from words where corpus = "' + corpus +'"')
u = get_utterances(u) # wonky: this call to utils populates utils global vars
u = cut(u, 2) # clean the utterance and cut any that are length < N
# Get lists of ngrams
u = get_list_of_ngrams(u, 2)
# Create bigram frames: {(w1,l1):[(w2,l1),(...)],...]
bigrams_w1_frames = get_bigram_frames(u, position=True)
bigrams_w2_frames = get_bigram_frames(u, position=False)
# Containers to fill and pickle to disk.
bigrams_w1_pr = {}
bigrams_w2_pr = {}
# For w1 in bigrams
for k, v in bigrams_w1_frames.items():
precision = get_accuracy(v)
# recall = get_completeness(v, labels)
recall = get_recall(v)
bigrams_w1_pr[k] = (precision, recall)
with open(corpus+'_'+type+'_w1.pickle', 'wb') as handle:
pickle.dump(bigrams_w1_pr, handle)
# For w2 in bigrams
for k, v in bigrams_w2_frames.items():
precision = get_accuracy(v)
# recall = get_completeness(v, labels)
recall = get_recall(v)
bigrams_w2_pr[k] = (precision, recall)
with open(corpus+'_'+type+'_w2.pickle', 'wb') as handle:
pickle.dump(bigrams_w2_pr, handle)
def main():
setup()
morphemes = 1
type = None
if morphemes == 0:
type = "words"
else:
type = "morphemes-gloss" # to distinguish between (morpheme,pos) vs (morpheme,gloss) analyses
# Process dyads or corpora
process_dyads(morphemes, type)
# process_corpora(morphemes, type)
if __name__ == '__main__':
import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))