-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcognateLanguage_LessPrinting.py
166 lines (131 loc) · 5.48 KB
/
cognateLanguage_LessPrinting.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
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'] == 'test3':
break # get rid of this later
originalWords = words.copy() # must explicitly make copy of dictionary in Python (instead of a reference)
# the words spelt with just the initial vowel and consonants:
for language in words:
if language != 'Eng':
words[language] = respellWithInitialVowelAndConsonants(words[language])
wordsMinusNoninitialVowels = words.copy() # must explicitly make copy of dictionary in Python (instead of a reference)
# the words with allophonic spelling:
for language in words:
if language != 'Eng':
words[language] = respellWithAllophones(words[language])
# ignore 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] = ''
# ignore 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] = ''
# find overlaps in words:
for tries in range(5):
shortList = combineOverlappingWords(shortList)
# append remaining words:
for language in shortList:
if language != 'Eng':
newWord += shortList[language]
# put original consonants back in, using 1st letters of higher-priority words:
for language in reversed(words.keys()):
if language != 'Eng':
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):]
else:
pattern = respellWithAllophones(wordsMinusNoninitialVowels[language])
newWord = newWord.replace(pattern, wordsMinusNoninitialVowels[language])
# put original vowels (& consonants) back in, favouring higher-priority words:
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):
index = respellWithAllophones(newWord).find(patternOrigAllo)
newWord = newWord[:index] + replacer + newWord[index+len(patternOrigAllo):]
elif patternCompressedAllo in respellWithAllophones(newWord):
index = respellWithAllophones(newWord).find(patternCompressedAllo)
newWord = newWord[:index] + replacer + newWord[index+len(patternCompressedAllo):]
else:
newWord = newWord.replace(patternOrigCompressed, replacer,1)
print newWord