-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcognateLanguage.py
217 lines (174 loc) · 7.32 KB
/
cognateLanguage.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
from collections import OrderedDict
#------------------------
# variables:
#------------------------
words = OrderedDict()
words['Eng'] = ''
words['Chi'] = ''
words['Spa'] = ''
words['Hin'] = ''
words['Ara'] = ''
words['Rus'] = ''
originalWords = words
wordsMinusNoninitialVowels = words
shortList = []
newWord = ''
filename = 'data.txt'
allophones = {
'aeiou' : 'a',
'bp' : 'b',
'cjsz' : 'z',
'dt' : 'd',
'fv' : 'v',
'gkq' : 'g',
'hx' : 'h',
'lr' : 'l',
'mn' : 'm',
'w' : 'w',
'y' : 'y'
}
#------------------------
# methods:
#------------------------
def respellWithInitialVowelAndConsonants(word):
for char in word[1:]:
if char in 'aeiou':
word = word[0] + word[1:].replace(char,'')
return word
def respellWithAllophones(word):
for char in word:
for allo in allophones:
if char in allo:
word = word.replace(char,allophones[allo])
return word
def combineOverlappingWords(shortList):
for language in shortList:
for otherlanguage in shortList:
if language != otherlanguage and language != 'Eng' and otherlanguage != 'Eng':
a = shortList[language]
b = shortList[otherlanguage]
for i in range(1, len(b)):
if a.endswith(b[:i]):
shortList[otherlanguage] = ''
shortList[language] = a+b[i:]
return shortList
#------------------------
# main part of the program:
#------------------------
# get lines of file into a list:
with open(filename,'r') as fh:
data = fh.readlines()
# fill arrays:
for line in data:
words['Eng'] = line.split(',')[1]
words['Chi'] = line.split(',')[2]
words['Spa'] = line.split(',')[3]
words['Hin'] = line.split(',')[4]
words['Ara'] = line.split(',')[5]
words['Rus'] = line.split(',')[6]
if words['Eng'] == 'be':
break # get rid of this break condition in later versions of code
originalWords = words.copy() # must explicitly make copy of dictionary in Python (instead of a reference)
# show the words:
print '\nshow the words:'
for language in words:
if language != 'Eng':
print language + '\t' + words[language]
# show the words spelt with just the initial vowel and consonants:
print '\nshow the words spelt with just the initial vowel and consonants:'
for language in words:
if language != 'Eng':
words[language] = respellWithInitialVowelAndConsonants(words[language])
print language + '\t' + words[language]
wordsMinusNoninitialVowels = words.copy() # must explicitly make copy of dictionary in Python (instead of a reference)
# show the words with allophonic spelling:
print '\nshow the words with allophonic spelling:'
for language in words:
if language != 'Eng':
words[language] = respellWithAllophones(words[language])
print language + '\t' + words[language]
# ignore repeats:
print '\nignore repeats:'
shortList = words
for language in shortList:
for otherlanguage in shortList:
if language != otherlanguage and language != 'Eng' and otherlanguage != 'Eng':
if shortList[language] == shortList[otherlanguage]:
shortList[otherlanguage] = ''
for language in shortList:
if language != 'Eng':
print language + '\t' + shortList[language] # print the unique words
# ignore words embedded in other words:
print '\nignore words embedded in other words:'
for language in shortList:
for otherlanguage in shortList:
if language != otherlanguage and language != 'Eng' and otherlanguage != 'Eng':
if shortList[language] in shortList[otherlanguage]:
shortList[language] = ''
for language in shortList:
if language != 'Eng':
print language + '\t' + shortList[language]
# find overlaps in words:
for tries in range(5):
print '\nfind overlaps in words: try #' + str(tries+1)
shortList = combineOverlappingWords(shortList)
for language in shortList:
if language != 'Eng':
print language + '\t' + shortList[language]
# append remaining words:
print '\nappend remaining words:'
for language in shortList:
if language != 'Eng':
newWord += shortList[language]
print newWord
# put original consonants back in, using 1st letters of higher-priority words:
print '\nput original consonants back in, using 1st letters of higher-priority words:'
#print
#print 'originalWords = \n', originalWords, '\n'
#print 'wordsMinusNoninitialVowels = \n', wordsMinusNoninitialVowels, '\n'
#print 'words = \n', words, '\n'
print words
for language in reversed(words.keys()):
if language != 'Eng':
print wordsMinusNoninitialVowels[language]
patternCompressedAllo = respellWithAllophones(wordsMinusNoninitialVowels[language])
if wordsMinusNoninitialVowels[language] not in newWord:
if patternCompressedAllo in respellWithAllophones(newWord): # replace with compressed word in allophone form
index = respellWithAllophones(newWord).find(patternCompressedAllo)
newWord = newWord[:index] + wordsMinusNoninitialVowels[language] + newWord[index+len(patternCompressedAllo):]
print ':', patternCompressedAllo
else:
pattern = respellWithAllophones(wordsMinusNoninitialVowels[language])
newWord = newWord.replace(pattern, wordsMinusNoninitialVowels[language])
print ' ->\t', newWord
print "\nnewWord = ", newWord
# put original vowels (& consonants) back in, favouring higher-priority words:
print '\nput original vowels (& consonants) back in, favouring higher-priority words:'
print originalWords
for language in reversed(words.keys()):
if language != 'Eng':
if originalWords[language] not in newWord:
replacer = originalWords[language]
# types of patterns: (priorities: original word in allophonic form > compressed word in allophonic form > compressed original word, to enable vowel overwrites)
patternOrigAllo = respellWithAllophones(originalWords[language])
patternCompressedAllo = respellWithAllophones(wordsMinusNoninitialVowels[language])
patternOrigCompressed = wordsMinusNoninitialVowels[language]
tempWord = newWord
# check if allophones exist that can be replaced, before checking for replacing compressed original word:
if patternOrigAllo in respellWithAllophones(newWord):
print 'replace patternOrigAllo:'
index = respellWithAllophones(newWord).find(patternOrigAllo)
newWord = newWord[:index] + replacer + newWord[index+len(patternOrigAllo):]
elif patternCompressedAllo in respellWithAllophones(newWord):
print 'replace patternCompressedAllo:'
index = respellWithAllophones(newWord).find(patternCompressedAllo)
newWord = newWord[:index] + replacer + newWord[index+len(patternCompressedAllo):]
else:
print 'replace patternOrigCompressed:', patternOrigCompressed
newWord = newWord.replace(patternOrigCompressed, replacer,1)
print '\t', replacer, '\t->\t', newWord, '<changed>'
print "\nnewWord = ", newWord
print "\noriginal words = "
for language in words:
if language != 'Eng':
print language + '\t' + originalWords[language]