forked from Information-Retrieval-Research/Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorizationWorld.py
More file actions
163 lines (136 loc) · 5.33 KB
/
VectorizationWorld.py
File metadata and controls
163 lines (136 loc) · 5.33 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
import time
from progress.bar import IncrementalBar
import epitran
from epitran.backoff import Backoff
import time
language_list = {
"Amharic": ["amh-Ethi", "amh-Ethi-pp", "amh-Ethi-red"],
"Arabic": ["ara-Arab"],
"Cebuano": ["ceb-Latn"],
"Croatian": ["hrv-Latn"],
"Czech": ["ces-Latn"],
"Dutch": ["nld-Latn"],
"Farsi": ["fas-Arab"],
"French": ["fra-Latn", "fra-Latn-np", "fra-Latn-p"],
"German": ["deu-Latn", "deu-Latn-np", "deu-Latn-nar"],
"Hausa": ["hau-Latn"],
"Hindi": ["hin-Deva"],
"Hungarian": ["hun-Latn"],
"Indonesian": ["ind-Latn"],
"Kazakh": ["kaz-Cyrl-bab", "kaz-Cyrl", "kaz-Latn"],
"Malayalam": ["mal-Mlym"],
"Maori": ["mri-Latn"],
"Marathi": ["mar-Deva"],
"Polish": ["pol-Latn"],
"Porteguese": ["por-Latn"],
"Russian": ["rus-Cyrl"],
"Somali": ["som-Latn"],
"Spanish": ["spa-Latn", "spa-Latn-eu"],
"Swedish": ["swe-Latn"],
"Telugu": ["tel-Telu"],
"Thai": ["tha-Thai"],
"Urdu": ["urd-Arab"],
"Vietnamese": ["vie-Latn"],
"Xhosa": ["xho-Latn"],
"Zulu": ["zul-Latn"]}
def word_to_token(word, lang_list):
backoff = Backoff(lang_list)
return backoff.transliterate(word)
def add_to_dict(dic, elem):
if elem in dic:
dic[elem] += 1
else:
dic[elem] = 1
return dic
global_token_list_link = "IPA.csv"
global_set = set()
# with open(global_token_list_link, 'r' ,encoding = "utf-8") as global_token_list_file:
# init_global_string = global_token_list_file.read()
# # global_token_list_file.truncate(0)
# global_list = init_global_string.split(",")
# for i in global_list:
# global_set.add(i)
for language in language_list:
print("Working on " + language)
from_link = f"../Corpus/Languages/{language}/{language}.txt"
to_link = f"../Corpus/Languages/{language}/IPA.txt"
to_unigram_link = f"../Output/Languages/{language}/unigram.csv"
to_bigram_link = f"../Output/Languages/{language}/bigram.csv"
to_trigram_link = f"../Output/Languages/{language}/trigram.csv"
from_file = open(from_link, 'r', encoding="utf-8")
to_file = open(to_link, 'w', encoding="utf-8")
text = from_file.read()
from_file.close()
unigram = {}
bigram = {}
trigram = {}
word_count = 0
combined_tokenized_array = []
print("Converting text to IPA")
text1 = text.split(" ")
bar = IncrementalBar('Countdown', max=len(text1))
to_file_string = ""
backoff = Backoff(language_list[language])
for word in text1:
if word == backoff.transliterate(word):
continue
word = word.replace('"', '').replace("'", "").replace(
"(", "").replace(")", "").replace("[", "").replace("]", "").replace("`", "").replace(".", "")
word = word.replace(";", "").replace(":", "").replace("!", "").replace(
".", "").replace("“", "").replace("’", "").replace("”", "").replace("=", "")
word = word.replace("-", "").replace("_",
"").replace(",", "").replace("?", "").replace("_", "").replace("-", "").replace("+", "").replace("~", "")
for i in range(0, 10):
word = word.replace(str(i), '')
if (word):
word_count += 1
tokenized_array = backoff.trans_list(word)
s = ''.join(tokenized_array)
combined_tokenized_array.extend(tokenized_array)
to_file_string += " " + s
# to_file.write(' ' + s)
# TO Write to the global list of unique token
for token in tokenized_array:
global_set.add(token)
# Create Unigram
for token in tokenized_array:
unigram = add_to_dict(unigram, token)
# Create Bigram
for i in range(len(tokenized_array)):
if i >= 1:
token = tokenized_array[i-1] + tokenized_array[i]
bigram = add_to_dict(bigram, token)
# Create trigram
for i in range(len(tokenized_array)):
if i >= 2:
token = tokenized_array[i-2] + \
tokenized_array[i-1] + tokenized_array[i]
trigram = add_to_dict(trigram, token)
bar.next()
bar.finish()
print("Writing to file")
to_file.write(to_file_string)
to_file.close()
print("Writing to file finished")
unigram_file = open(to_unigram_link, 'w', encoding="utf-8")
for token in unigram:
unigram_file.write(token+','+str(unigram[token])+'\n')
unigram_file.close()
bigram_file = open(to_bigram_link, 'w', encoding="utf-8")
for token in bigram:
bigram_file.write(token+','+str(bigram[token])+'\n')
bigram_file.close()
trigram_file = open(to_trigram_link, 'w', encoding="utf-8")
for token in trigram:
trigram_file.write(token+','+str(trigram[token])+'\n')
trigram_file.close()
no_of_phonemes = open(
f"../Output/Languages/{language}/no_phonemes.txt", 'w', encoding="utf-8")
no_of_phonemes.write(str(len(unigram)))
no_of_phonemes.close()
word_counter = open(
f"../Output/Languages/{language}/word_count.txt", 'w', encoding="utf-8")
word_counter.write(str(word_count))
word_counter.close()
with open(global_token_list_link, 'w', encoding="utf-8") as global_token_list_file:
global_token_list_file.write(str(global_set))