-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·345 lines (272 loc) · 11.5 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# -*- coding: utf-8 -*-
import tweaks
import speech_to_text
import common_functions
import meaning_space
import kmeans_nD
import part_of_speech
import audio_spliter
import pickle
__author__ = "Romain Claret"
__maintainer__ = "Romain Claret"
__copyright__ = "Copyright 2015, Romain Claret "
__credits__ = ["Romain Claret"]
# Copyright (C) Romain Claret, All Rights Reserved
# Unauthorized copying of this file, via any medium is strictly prohibited
__license__ = "Proprietary and confidential"
__version__ = "1.0.0"
__email__ = "[email protected]"
__status__ = "Prototype" # Prototype, Development, Production
__date__ = "21.01.2016"
"""@package main
Global documentation for the use of the application.
"""
"""
The following section is about the Voice Recognition
"""
def section_voice_recognition(toggle, max_files=None, filename=None):
if toggle:
if max_files is None:
max_files = len(common_functions.getListFolders(tweaks.baseAudioFile))
if tweaks.manual_split_control:
audio_spliter.splitAudioFiles(max_files)
count = 0
for fileFolder in common_functions.getListFolders(tweaks.outputDirectory):
if filename is None:
text_transcripted = speech_to_text.transcriptProcessedFiles(fileFolder)
section_part_of_speech(tweaks.run_part_of_speech, True, text_transcripted, None, fileFolder)
count += 1
if count >= max_files:
return True
elif filename == fileFolder:
text_transcripted = speech_to_text.transcriptProcessedFiles(fileFolder)
section_part_of_speech(tweaks.run_part_of_speech, True, text_transcripted, None, fileFolder)
return True
return True
"""
The following section is about the serialization of the data
"""
def serialize_matrix_of_meanings():
"""
Serialize the matrix of meanings into the serializedData folder.
"""
print("Serialization of the matrix_of_meanings...")
output = open(tweaks.serialized_folder + tweaks.serialized_matrix_of_meanings_file, 'wb')
pickle.dump(tweaks.matrix_of_meanings, output) # use of highest protocol available
output.close()
def load_serialized_matrix_of_meanings():
"""
Load the matrix of meanings from the serializedData folder.
"""
print("Loading serialized matrix_of_meanings...")
load_file = open(tweaks.serialized_folder + tweaks.serialized_matrix_of_meanings_file, 'rb')
tweaks.matrix_of_meanings = pickle.load(load_file)
load_file.close()
def serialize_clusters():
"""
Serialize the clusters into the serializedData folder.
"""
print("Serialization of the cluster_meaning...")
output = open(tweaks.serialized_folder + tweaks.serialized_clusters_file, 'wb')
pickle.dump(tweaks.cluster_meaning, output, -1) # use of highest protocol available
pickle.dump(tweaks.cluster_name, output, -1)
pickle.dump(tweaks.clusters_list, output, -1)
pickle.dump(tweaks.index_of_data_custering, output, -1)
output.close()
def load_serialized_clusters():
"""
Load the clusters from the serializedData folder.
"""
print("Loading serialized cluster_meaning...")
load_file = open(tweaks.serialized_folder + tweaks.serialized_clusters_file, 'rb')
tweaks.cluster_meaning = pickle.load(load_file)
tweaks.cluster_name = pickle.load(load_file)
tweaks.clusters_list = pickle.load(load_file)
tweaks.index_of_data_custering = pickle.load(load_file)
load_file.close()
"""
The following section is about the training of the Part of speech
"""
def print_clusters_top_meanings():
"""
Display an amount of top keywords related to the clusters defined by top_values.
"""
for cluster in tweaks.cluster_meaning:
print(cluster)
def print_clusters_names():
"""
Display the name of corpus related to the clusters.
"""
for cluster in tweaks.cluster_name:
print(cluster)
def section_part_of_speech_training(number_of_clusters, files=None):
"""
This section trains the part of speech by creating clusters of meanings.
:param number_of_clusters: number of cluster targeted
:param files: list of corpus to classify. If not specified, all corpus available will be used
"""
meaning_space.init_meaning_space()
if files is None:
files = common_functions.getListFolders(tweaks.textFilesDirectory)
for file in files:
f = open(tweaks.textFilesDirectory + file, encoding="utf-8")
text = f.read()
tweaks.matrix_of_names.append(file[:-4])
# remove non-ascii
processed_string = "".join(i for i in text if ord(i) < 128)
frequency = part_of_speech.get_words_frequency(processed_string, 0)
meaning_space.build_meaning_space(frequency)
f.close()
for file in files:
f = open(tweaks.textFilesDirectory + file, encoding="utf-8")
text = f.read()
# remove non-ascii
processed_string = "".join(i for i in text if ord(i) < 128)
frequency = meaning_space.normalize_frequencies(part_of_speech.get_words_frequency(processed_string, 0))
tweaks.top_matrix_of_meanings.append(part_of_speech.get_words_frequency(processed_string, top_values))
meaning_space.populate_meaning_matrix(frequency, files.index(file), file)
f.close()
tweaks.clusters_list = kmeans_nD.get_clusters_centroids(tweaks.matrix_of_meanings[1:], number_of_clusters)
tweaks.index_of_data_custering = kmeans_nD.get_index_assign_data_to_cluster(tweaks.matrix_of_meanings[1:],
tweaks.clusters_list)
cluster_meaning_tmp = [[i] for i in range(number_of_clusters)]
cluster_names_tmp = [[i] for i in range(number_of_clusters)]
# append cluster_number and top_values of the related file
for n in range(0, len(tweaks.index_of_data_custering)):
for meaning in tweaks.top_matrix_of_meanings[n]:
cluster_meaning_tmp[tweaks.index_of_data_custering[n]].append(meaning[0])
for n in range(0, len(tweaks.index_of_data_custering)):
cluster_names_tmp[tweaks.index_of_data_custering[n]].append(tweaks.matrix_of_names[n])
tweaks.cluster_meaning = []
for cluster in cluster_meaning_tmp:
for name in cluster:
if cluster.count(name) > 1:
cluster.remove(name)
tweaks.cluster_meaning.append(cluster)
tweaks.cluster_name = []
for cluster in cluster_names_tmp:
tweaks.cluster_name.append(cluster)
"""
The following section is about the use of the Part of speech
"""
def section_part_of_speech(toggle, voice=False, audio_to_compare=None, file_to_compare=None, fileFolder=None):
"""
This section will try to listen to an audio or text file to extract the corpus and then match it to the knowledge
database.
:param toggle: specify if the voice recognition should be speech should be running
:param voice: specify the use of an audio file
:param audio_to_compare: corpus
:param file_to_compare: specify name of file if used
:param fileFolder: name of the processed directory
"""
if toggle:
if not voice:
if file_to_compare is None:
file_to_compare = "wikipedia-intelligence.txt"
f = open(tweaks.textFilesDirectory + file_to_compare, encoding="utf-8")
text = f.read()
f.close()
else:
file_to_compare = "the audio file"
text = audio_to_compare
if fileFolder is not None:
file_to_compare = fileFolder
# remove non-ascii
processed_string = "".join(i for i in text if ord(i) < 128)
frequencies = meaning_space.normalize_frequencies(part_of_speech.get_words_frequency(processed_string, 0))
file_to_classify = []
for element in tweaks.matrix_of_meanings[0]:
count = 0
for frequency in frequencies:
if frequency[0] == element:
file_to_classify.append(frequency[1])
count += 1
break
if count == 0:
file_to_classify.append(0)
if not frequencies:
print("The speech_to_text process of " + str(
file_to_compare) + " has failed. Try again, or delete the audio source because it is too noisy.")
else:
print("The cos distance of " + str(file_to_compare) + " with the clusters is: [distance], [cluster]")
file_clusters_distance = list(kmeans_nD.cos_cdist_clusters(tweaks.clusters_list, file_to_classify))
distance_order = []
for i in range(0, len(tweaks.clusters_list)):
if tweaks.use_clusters_top_meanings_or_names == 0:
distance_order.append([file_clusters_distance[i], tweaks.cluster_meaning[i]])
else:
distance_order.append([file_clusters_distance[i], tweaks.cluster_name[i]])
distance_order.sort(key=lambda x: x[0], reverse=False)
distance_order = distance_order[:3]
for distance in distance_order:
print(distance)
print()
if __name__ == "__main__":
"""
This function is run if this file is run directly.
It will:
Configure the procedures of execution such as the use of the serialization, the number of clusters, display, etc.
Serialize data on the go if specified.
Run or load the training the of corpus.
Run or load audio recognition.
Classify the audio files with the current knowledge of the application.
Give results in a lab reading point of view.
"""
"""
Options about the use of the main sections
"""
tweaks.run_voice_recognition = False
tweaks.run_part_of_speech = True
"""
Options about the serialization of the data
"""
tweaks.serialize_training = True
tweaks.serialize_audio = True
tweaks.use_serialized_training = False
tweaks.use_serialized_audio = True
"""
Options about the use of the audio file splitter
"""
tweaks.manual_split_control = False
"""
Options about the display of the data
"""
tweaks.print_clusters_names = True
tweaks.print_clusters_top_meanings = False
"""
Options about the customization of the training
"""
tweaks.use_clusters_top_meanings_or_names = 1 # 0 for top_meanings, 1 for names
max_audio_files_to_transcribe = 5 # None for all # sorted order of the audioRaw folder
number_of_clusters = 75
top_values = 3
file_to_compare = "wikipedia-fire.txt"
"""
School Splash Screen + Application name
"""
print()
common_functions.splash_screen_arc()
print()
print("Manager of Emergency Calls by Romain Claret")
print("Made for the 1st semester of the 3rd year DLM at HE-ARC Engineering School Neuchatel")
print()
print()
"""
Execution of the main programme
"""
print("Training of the part of speech in progress...")
if tweaks.use_serialized_training:
load_serialized_matrix_of_meanings()
load_serialized_clusters()
else:
section_part_of_speech_training(number_of_clusters)
if tweaks.serialize_training:
serialize_matrix_of_meanings()
serialize_clusters()
if tweaks.print_clusters_names:
print_clusters_names()
if tweaks.print_clusters_top_meanings:
print_clusters_top_meanings()
print("Done Training")
print()
section_voice_recognition(tweaks.run_voice_recognition, max_audio_files_to_transcribe)