-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
1192 lines (1066 loc) · 41.2 KB
/
utils.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# import re
import base64
import fcntl
import json
import math
import os
import sys
import tempfile
import time
import traceback
import urllib
import ffmpy
import numpy as np
import requests
import soundfile as sf
from flask_apiexceptions import ApiError, ApiException
from g2p_en import G2p
from nltk.tokenize import TweetTokenizer
from ponddySyllableBreaking import getSyllableBreakingSingleUtt
# from g2p import G2p
from pydub import AudioSegment
word_tokenize = TweetTokenizer().tokenize
JWT_PRODUCTION_SERVER = "https://api.ponddy.com"
JWT_PRODUCTION = "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InBvbmRkeS1zdXBlciIsImV4cCI6MTU0ODEyODM4OSwiZW1haWwiOiIifQ.WK06nkIlY0Fo51vy2pajtP_K1g2-YDvZTPFFsq_m44I"
JWT_STAGING_SERVER = "https://api-staging.ponddy.com"
JWT_STAGING = "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InBvbmRkeS1zdXBlciIsImV4cCI6MTU2ODE4NDg3MiwiZW1haWwiOiIifQ.U-WtxF6jzGpTE4hWvRp8umjvCguvCJG7HdUxyToD-7U"
JWT_DEVELOP_SERVER = "https://api-dev.ponddy.com"
JWT_DEVELOP = "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InBvbmRkeS1zdXBlciIsImV4cCI6MTUzNTY5NTExNywiZW1haWwiOiIifQ.A6KqSO4JNMuH8r_qEi447_xMEccg7QjiPircLSF7GS8"
JWT_SJ = "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InBvbmRkeSIsImV4cCI6MTU4NDcyNzI2NywiZW1haWwiOiJmcmFuY29jaGVuQHBvbmRkeS1lZHUuY29tIn0.DZArlDZwi5q4H3CxAilKj0-XX2HOhOTMJNrniLaImC4"
# JWT_SJ_SERVER = 'http://sj-server3.ponddy-one.com'
JWT_SJ_SERVER = "http://sj-server2.ponddy-one.com"
# taipei ML server
JWT_TAIPEIML_SERVER = "http://alpha.ponddy-one.com:8800"
JWT_TAIPEIML = "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTU3MTI1NTQ4LCJlbWFpbCI6Im93ZW56aG9uZ0Bwb25kZHktZWR1LmNvbSJ9.6YB9Kvy2ZDUv4L16NYibpAiQTjMR2ug-WU6B28m9g4E"
minscores_angel = 60
minscores_rigorous = 80
minscores_missing = 15
low_pass_width = 6000
chi_puncts = "﹐﹑,。?、:;⋯:;,?!.!「」()()】【::╱〈〉{}[]“”《》"
punctuations = set('!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~' + chi_puncts)
LOGFILEPATH = "./access_parameter.log"
# ENGLISH_WEB_ROOT = 'https://85.ponddy-one.com:9479'
# ENGLISH_WEB_ROOT = os.environ.get('ENGLISH_WEB_ROOT', 'https://sj-server3.ponddy-one.com:9479')
# ENGLISH_WEB_ROOT = os.environ.get('ENGLISH_WEB_ROOT', 'https://sj-server2.ponddy-one.com:9479')
ENGLISH_WEB_ROOT = os.environ.get("ENGLISH_WEB_ROOT", "https://192.168.1.27:9479")
Widget_level_cache = "./Widget_level_cache.npy"
word2level = {}
if os.path.isfile(Widget_level_cache):
print("load...level cache")
word2level = np.load(Widget_level_cache, allow_pickle=True).item()
SELECT_SERVER = os.environ.get("SELECT_SERVER", "develop")
# 處理setence 存取 setence等級的快取
def get_level_cache_status(inputText, forcesave=False):
speakwords_encode = urllib.parse.quote(inputText, safe="")
cache_lingo_key = "level__%s" % speakwords_encode
if word2level.get(cache_lingo_key, ""):
# print('++++++Read from cache')
inputText, input_text_lv = word2level[cache_lingo_key]
else:
# {'text': 'He makes more money than his dad and uncle.', 'prediction': 'A2', 'prediction_list': 'A2, C1, A1'}
retdic = get_sentence_level(inputText)
input_text_lv = retdic.get("prediction")
if input_text_lv:
word2level[cache_lingo_key] = [inputText, input_text_lv]
if int(time.time()) % 3 == 1 or forcesave:
# print('%%%%%%Save pinyin cache:%s' % Widget_level_cache)
file = open(Widget_level_cache, "a+b")
fcntl.flock(file, fcntl.LOCK_EX)
np.save(Widget_level_cache, word2level)
fcntl.flock(file, fcntl.LOCK_UN)
file.close()
return inputText, input_text_lv
def toCMUSeq(sent):
print("toCMUSeq", sent)
g2p = G2p()
res = g2p(sent)
cmu = res[0]
tmp = []
buf = []
for c in cmu:
if c == " " or c in punctuations:
if (
len(tmp) > 0
and tmp[-1] == "DH_AH0"
and any(buf[0].startswith(x) for x in "AEIOU")
):
tmp[-1] = "DH_IY1"
if len(buf) > 0:
tmp.append("_".join(buf))
buf = []
else:
buf.append(c)
if len(buf) > 0:
if (
len(tmp) > 0
and tmp[-1] == "DH_AH0"
and any(buf[0].startswith(x) for x in "AEIOU")
):
tmp[-1] = "DH_IY1"
tmp.append("_".join(buf))
return " ".join(tmp), res[1]
def word_score_offset(gopresult):
# offset = 20
targets = "I, his, the, a, an, and, are".upper().split(", ")
for w in gopresult:
if w["word"] in targets:
w["GOPScore"] = min(100, w["GOPScore"] + 20)
return gopresult
# Apply low pass filter
def sr_change(inputFilePath, outputFilePath, LPF=8000, SR=16000):
ff = ffmpy.FFmpeg(
inputs={inputFilePath: None},
outputs={
outputFilePath: '-af "lowpass=f=' + str(LPF) + '" -ar ' + str(SR) + " -y"
},
)
ff.run()
if not os.path.isfile(outputFilePath):
print("Error in converting sample rate for file %s" % (outputFilePath))
# 保留使用者記錄,當wrtdata格式有base64資料時,會將檔案儲存於 ./logs/{hashkey_uuid}.base64 方便存取及讀取資料
def savelog(hashkey_uuid, action, wrtdata):
# print('----savelog----')
file = open(LOGFILEPATH, "a+")
fcntl.flock(file, fcntl.LOCK_EX)
tm = time.localtime()
date = "%04d/%02d/%02d" % (tm[0], tm[1], tm[2])
wrtdatalog = ""
if type(wrtdata) == dict:
wrtdatalog = wrtdata.copy()
if wrtdatalog.get("base64", "") and hashkey_uuid:
wrtbasebs = "./logs"
if not os.path.isdir(wrtbasebs):
os.mkdir(wrtbasebs)
wrtbasedir = "%s/%s" % (wrtbasebs, hashkey_uuid[0:6])
if not os.path.isdir(wrtbasedir):
os.mkdir(wrtbasedir)
wrtbasepath = "%s/%s.base64" % (wrtbasedir, hashkey_uuid)
if not os.path.isfile(wrtbasepath):
wf = open(wrtbasepath, "w")
wf.write("%s" % wrtdatalog.get("base64", ""))
wf.close()
del wrtdatalog["base64"]
file.write("=========[%s]%s(%s)=========\n" % (date, hashkey_uuid, action))
if wrtdatalog:
file.write("%s\n" % wrtdatalog)
else:
file.write("%s\n" % wrtdata)
fcntl.flock(file, fcntl.LOCK_UN)
file.close()
def getEngASRprediction(wav_path):
"""Get results from ponddy English ASR api.
Args:
wav_path: the path of the input waveform file
Returns:
result: ASR prediction results
"""
enc = base64.b64encode(open(wav_path, "rb").read())
enc = enc.decode("utf-8")
data = {"base64": enc}
r = requests.post(
"http://api.ponddy.com/api/voice_asr/en/predict",
json=data,
headers={
"Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InBvbmRkeS1zdXBlciIsImV4cCI6MTU5MDA5MTI4NiwiZW1haWwiOiIifQ.d3IPqFIK8RP8FX7d5dYDRMW2iHol6JEysncOc8NoOVo"
},
)
r.encoding = "utf-8"
result = json.loads(r.text)
return result
def getEngGOPresult(wav_path, text, client, cmu_ans=None, accountid="", logstatus=True):
"""Get results from ponddy English GOP api.
Args:
wav_path: the path of the input waveform file
text: input text as ground truth
Returns:
result: GOP scoring results
"""
enc = base64.b64encode(open(wav_path, "rb").read())
enc = enc.decode("utf-8")
data = {
"base64": enc,
"words": text,
"appname": "widget",
"diagnosis_by": "GOP_Result",
"dereverb_check": "N",
}
# wf = open('/tmp/err_english', 'w')
# wf.write('%s' % data)
# wf.close()
if cmu_ans is not None:
data["cmu_ans"] = cmu_ans
if accountid:
data["uuid"] = accountid
# print('logstatus', logstatus)
if not logstatus:
data["logsave"] = "N"
select_server_type = "(getEngGOPresult)"
# production / staging / develop / SJ
select_server = SELECT_SERVER
# select_server = 'taipeiml'
print("select_server", select_server)
if select_server == "production":
print("===production%s===" % select_server_type)
server_url = JWT_PRODUCTION_SERVER
headers = {"Authorization": JWT_PRODUCTION}
elif select_server == "staging":
print("===staging%s===" % select_server_type)
server_url = JWT_STAGING_SERVER
headers = {"Authorization": JWT_STAGING}
elif select_server == "develop":
print("===develop%s===" % select_server_type)
server_url = JWT_DEVELOP_SERVER
headers = {"Authorization": JWT_DEVELOP}
elif select_server == "taipeiml":
server_url = JWT_TAIPEIML_SERVER
headers = {"Authorization": JWT_TAIPEIML}
elif select_server == "SJ":
print("===SJ%s===" % select_server_type)
server_url = JWT_SJ_SERVER
headers = {"Authorization": JWT_SJ}
r = client.post(
"%s/api/voice_score/en/diagnosis" % server_url, json=data, headers=headers
)
r.encoding = "utf-8"
# print("r.status_code", r.status_code)
result = json.loads(r.text)
if r.status_code == 422:
# result {'detail': 'The input word must be chinese!', 'code_sn': 'c10'}
code_sn = result.get("code_sn")
code_message = result.get("detail")
error_silent = ApiError(code=code_sn, message=code_message)
raise ApiException(status_code=422, error=error_silent)
# print('result', result)
return result
def getEngTTS(text):
"""Get results from ponddy English TTS api.
Args:
text: input text for speech synthesis
Returns:
result: generated audio with base64 encoded and other information
"""
select_server_type = "(EngTTS)"
# production / staging / develop / SJ
# select_server = 'production'
select_server = SELECT_SERVER
if select_server == "production":
print("===production%s===" % select_server_type)
server_url = JWT_PRODUCTION_SERVER
headers = {"Authorization": JWT_PRODUCTION}
elif select_server == "staging":
print("===staging%s===" % select_server_type)
server_url = JWT_STAGING_SERVER
headers = {"Authorization": JWT_STAGING}
elif select_server == "develop":
print("===develop%s===" % select_server_type)
server_url = JWT_DEVELOP_SERVER
headers = {"Authorization": JWT_DEVELOP}
elif select_server == "SJ":
print("===SJ%s===" % select_server_type)
server_url = JWT_SJ_SERVER
headers = {"Authorization": JWT_SJ}
if select_server == "SJ":
data = {"text": text, "gender": "male"}
r = requests.post("%s:8508/" % JWT_SJ_SERVER, json=data)
else:
data = {"text": text, "gender": "female", "datatype": "mp3"}
r = requests.post(
"%s/api/voice_tts/en/predict" % server_url, json=data, headers=headers
)
# print('r', r.text)
# r = requests.post('http://api.ponddy.com/api/voice_tts/en/predict', json=data, headers={'Authorization': 'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InBvbmRkeS1zdXBlciIsImV4cCI6MTU5MDA5MTI4NiwiZW1haWwiOiIifQ.d3IPqFIK8RP8FX7d5dYDRMW2iHol6JEysncOc8NoOVo'})
r.encoding = "utf-8"
result = json.loads(r.text)
return result
def getEngRecommendSents(gop_dict):
data = {"uttGOP": gop_dict}
print("data", data)
try:
r = requests.post(
"%s:6954/eng_sent_recommend" % JWT_SJ_SERVER,
json=data,
headers={"Connection": "close"},
)
# print('result', result)
result = json.loads(r.text)
return result
except Exception:
traceback.print_exc()
sys.exit()
def get_sentence_level(ss):
data = {"text": ss}
select_server_type = "(Sentence_level)"
# production / staging / develop / SJ
select_server = SELECT_SERVER
if select_server == "production":
print("===production%s===" % select_server_type)
server_url = JWT_PRODUCTION_SERVER
headers = {"Authorization": JWT_PRODUCTION}
elif select_server == "staging":
print("===staging%s===" % select_server_type)
server_url = JWT_STAGING_SERVER
headers = {"Authorization": JWT_STAGING}
elif select_server == "develop":
print("===develop%s===" % select_server_type)
server_url = JWT_DEVELOP_SERVER
headers = {"Authorization": JWT_DEVELOP}
elif select_server == "SJ":
print("===SJ%s===" % select_server_type)
server_url = JWT_SJ_SERVER
headers = {"Authorization": JWT_SJ}
r = requests.post(
"%s/api/sentence_level/en/predict" % server_url, json=data, headers=headers
)
r.encoding = "utf-8"
# print("r.status_code", r.status_code)
if r.status_code == 200:
result = json.loads(r.text)
else:
result = {}
return result
def getFluencyLevel(wav_path):
"""Get results of fluency level model
Args:
wav_path: the path of the input waveform file
Returns:
result: fluency level results
"""
enc = base64.b64encode(open(wav_path, "rb").read())
enc = enc.decode("utf-8")
data = {"base64": enc}
select_server_type = "(Fluency)"
# production / staging / develop / SJ
# 2020.09.11 develop english fluency 已經可以運作了
select_server = SELECT_SERVER
# print('data', data)
if select_server == "production":
print("===production%s===" % select_server_type)
server_url = JWT_PRODUCTION_SERVER
headers = {"Authorization": JWT_PRODUCTION}
elif select_server == "staging":
print("===staging%s===" % select_server_type)
server_url = JWT_STAGING_SERVER
headers = {"Authorization": JWT_STAGING}
elif select_server == "develop":
print("===develop%s===" % select_server_type)
server_url = JWT_DEVELOP_SERVER
headers = {"Authorization": JWT_DEVELOP}
elif select_server == "SJ":
print("===SJ%s===" % select_server_type)
server_url = JWT_SJ_SERVER
headers = {"Authorization": JWT_SJ}
if select_server == "SJ":
r = requests.post("%s:3018/fluency_score" % JWT_SJ_SERVER, json=data)
else:
r = requests.post(
"%s/api/voice_fluency/en/predict" % server_url, json=data, headers=headers
)
result = json.loads(r.text)
# print('FFFFFFFFFFFFFFFFFFFFF', result)
return result
def CEFR_2_level(level):
cefrlevel = ""
tocflevel = ""
if level == "Pre A1":
cefrlevel = "Pre A1"
tocflevel = "Pre A1"
elif level == "A1":
cefrlevel = "A1"
tocflevel = "A1"
elif level == "A2":
cefrlevel = "A2"
tocflevel = "A2"
elif level == "B1":
cefrlevel = "B1"
tocflevel = "B1"
elif level == "B2":
cefrlevel = "B2"
tocflevel = "B2"
elif level == "C1":
cefrlevel = "C1"
tocflevel = "C1"
elif level == "C2":
cefrlevel = "C2"
tocflevel = "C1"
return cefrlevel, tocflevel
# {'5': '#C638AD', '0': '#71E67C', '6': '#E54956', '2': '#8BC7EF', '4': '#511AE8', '3': '#3E86F9', '1': '#02CCC2',
# 'U': '#BABABA'}
def CEFR_2_color(level):
colorv = ""
# 0
if level == "Pre A1":
colorv = "#71E67C"
# 1
elif level == "A1":
colorv = "#02CCC2"
# 2
elif level == "A2":
colorv = "#8BC7EF"
# 3
elif level == "B1":
colorv = "#3E86F9"
# 4
elif level == "B2":
colorv = "#511AE8"
# 5
elif level == "C1":
colorv = "#C638AD"
# 6
elif level == "C2":
colorv = "#E54956"
return colorv
def getSentSet(maxlength=99):
f = open("data/sent_set.txt")
lines = f.readlines()
retlist = []
sentences_structure = []
# no_show_num_pat = re.compile(r'[0-9]+')
for line in lines:
line = line.strip()
# if len(line) <= maxlength and not re.search(no_show_num_pat, line):
# if line not in retlist:
# retlist.append(line)
# ret_text, ret_text_lv = get_level_cache_status(line)
# # print(ret_text, ret_text_lv)
# cefr_color = CEFR_2_color(ret_text_lv)
# sentences_structure.append({'en': line, 'level': {'CEFR': ret_text_lv, 'CEFR_color': cefr_color}})
sent, lv = line.split("\t")
if sent not in retlist:
retlist.append(sent)
cefr_color = CEFR_2_color(lv)
sentences_structure.append(
{"en": sent, "level": {"CEFR": lv, "CEFR_color": cefr_color}}
)
# lines = [line.strip() for line in lines]
f.close()
# print('sentences_structure', sentences_structure)
return retlist, sentences_structure
def wer(ref, hyp, debug=False):
"""Calculate edit distance between two input sequences and return the results.
Args:
ref: the ground truth sequence as reference
hyp: the prediction results as hypothesis
Returns:
result_dict: the dictionary contains all informations
"""
SUB_PENALTY = 1
INS_PENALTY = 1
DEL_PENALTY = 1
r = ref.split()
h = hyp.split()
# costs will holds the costs, like in the Levenshtein distance algorithm
costs = [[0 for inner in range(len(h) + 1)] for outer in range(len(r) + 1)]
# backtrace will hold the operations we've done.
# so we could later backtrace, like the WER algorithm requires us to.
backtrace = [[0 for inner in range(len(h) + 1)] for outer in range(len(r) + 1)]
OP_OK = 0
OP_SUB = 1
OP_INS = 2
OP_DEL = 3
# First column represents the case where we achieve zero
# hypothesis words by deleting all reference words.
for i in range(1, len(r) + 1):
costs[i][0] = DEL_PENALTY * i
backtrace[i][0] = OP_DEL
# First row represents the case where we achieve the hypothesis
# by inserting all hypothesis words into a zero-length reference.
for j in range(1, len(h) + 1):
costs[0][j] = INS_PENALTY * j
backtrace[0][j] = OP_INS
# computation
for i in range(1, len(r) + 1):
for j in range(1, len(h) + 1):
if r[i - 1] == h[j - 1]:
costs[i][j] = costs[i - 1][j - 1]
backtrace[i][j] = OP_OK
else:
substitutionCost = (
costs[i - 1][j - 1] + SUB_PENALTY
) # penalty is always 1
insertionCost = costs[i][j - 1] + INS_PENALTY # penalty is always 1
deletionCost = costs[i - 1][j] + DEL_PENALTY # penalty is always 1
costs[i][j] = min(substitutionCost, insertionCost, deletionCost)
if costs[i][j] == substitutionCost:
backtrace[i][j] = OP_SUB
elif costs[i][j] == insertionCost:
backtrace[i][j] = OP_INS
else:
backtrace[i][j] = OP_DEL
# back trace though the best route:
i = len(r)
j = len(h)
numSub = 0
numDel = 0
numIns = 0
numCor = 0
if debug:
print("OP\tREF\tHYP")
lines = []
while i > 0 or j > 0:
if backtrace[i][j] == OP_OK:
numCor += 1
i -= 1
j -= 1
lines.append("OK\t" + r[i] + "\t" + h[j])
elif backtrace[i][j] == OP_SUB:
numSub += 1
i -= 1
j -= 1
lines.append("SUB\t" + r[i] + "\t" + h[j])
elif backtrace[i][j] == OP_INS:
numIns += 1
j -= 1
lines.append("INS\t" + "****" + "\t" + h[j])
elif backtrace[i][j] == OP_DEL:
numDel += 1
i -= 1
lines.append("DEL\t" + r[i] + "\t" + "****")
tmp = [x.split() for x in lines]
tmp = list(map(list, zip(*tmp)))
info = [x[::-1] for x in tmp]
order = [1, 2, 0]
info = [info[i] for i in order]
if debug:
lines = reversed(lines)
for line in lines:
print(line)
print("#cor " + str(numCor))
print("#sub " + str(numSub))
print("#del " + str(numDel))
print("#ins " + str(numIns))
wer_result = round((numSub + numDel + numIns) / (float)(len(r)), 3)
return {
"WER": wer_result,
"Cor": numCor,
"Sub": numSub,
"Ins": numIns,
"Del": numDel,
"word_count": len(r),
"total_err": (numSub + numDel + numIns),
"info": info,
}
def generateEditDistanceResult(wav_path, inputText):
"""Wrapped function to generate edit distance results from input waveform file and input text.
Apply ASR first to get ASR prediction as hypothesis and then apply wer function to calculate
edit distance between ASR prediction and input text.
Args:
wav_path: the path of the input waveform file
inputText: input text
Returns:
ref: the list of all words in reference text
hyp: the list of all words in hypothesis text
ins_indices: the indices of all insertions
del_indices: the indices of all deletions
sub_indices: the indices of all substitutions
"""
inputText = inputText.upper()
puncts_dict = {}
alternated_text = []
for i, wd in enumerate(inputText.split()):
if wd in punctuations:
puncts_dict[i] = wd
else:
alternated_text.append(wd)
inputText = " ".join(alternated_text)
res = getEngASRprediction(wav_path)
predicted_text = res["asr"]
# print('ASR', predicted_text)
error_silent = ApiError(
code="Unable to analyze", message="Unable to analyze, please try again."
)
if predicted_text.strip() == "":
raise ApiException(status_code=422, error=error_silent)
result = wer(ref=inputText, hyp=predicted_text, debug=False)
ref, hyp, label = result["info"]
puncts_offset = [0] * len(puncts_dict)
for i, wd in enumerate(hyp):
if ref[i] == "****":
for j, idx in enumerate(puncts_dict.keys()):
if idx + puncts_offset[j] >= i:
puncts_offset[j] += 1
if not label[i] == "INS":
hyp[i] = ref[i]
# print('puncts_dict', puncts_dict)
# for idx in puncts_dict:
for i, idx in enumerate(puncts_dict):
ref.insert(idx + puncts_offset[i], puncts_dict[idx])
hyp.insert(idx + puncts_offset[i], puncts_dict[idx])
label.insert(idx + puncts_offset[i], puncts_dict[idx])
ins_indices, del_indices, sub_indices = [], [], []
for i, x in enumerate(label):
if x == "INS":
ins_indices.append(i)
elif x == "SUB":
sub_indices.append(i)
elif x == "DEL":
del_indices.append(i)
# ins_indices = [i for i,x in enumerate(label) if x=="INS"]
# del_indices = [i for i,x in enumerate(label) if x=="DEL"]
# sub_indices = [i for i,x in enumerate(label) if x=="SUB"]
acc = (1 - result["WER"]) * 100
return ref, hyp, ins_indices, del_indices, sub_indices, acc
def generatePartialGOPResult(
wav_path, inputText, hashkey_uuid="", logstatus=True, prtmsg=False
):
"""Wrapped function to generate partial GOP results from input waveform file and input text..
Args:
wav_path: the path of the input waveform file
inputText: input text
Returns:
ref: the list of all words in reference text
hyp: the list of all words in hypothesis text
ins_indices: the indices of all insertions
del_indices: the indices of all deletions
sub_indices: the indices of all substitutions
"""
cmu_ans, inputText = toCMUSeq(inputText)
inputText = inputText.upper()
puncts_dict = {}
alternated_text = []
for i, wd in enumerate(inputText.split()):
if wd in punctuations:
puncts_dict[i] = wd
else:
alternated_text.append(wd)
inputText = " ".join(alternated_text)
if prtmsg:
print("GOP input text:%s" % inputText)
print("logstatus - 2", logstatus)
res = getEngGOPresult(
wav_path=wav_path, text=inputText, cmu_ans=cmu_ans, logstatus=logstatus
)
if prtmsg:
print("str(res)", str(res)[0:200])
error_silent = ApiError(
code="e02", message="Unable to analyze, Listen once more and try again."
)
gopresult = res.get("gop", {}).get("parts", "")
if gopresult == "":
savelog(
hashkey_uuid, "diagnosis:ERROR", "Gop Unable to analyze, please try again."
)
raise ApiException(status_code=422, error=error_silent)
res["gop"]["parts"] = word_score_offset(res["gop"]["parts"])
gop_results = [
(x["GOPScore"] >= minscores_angel, x["GOPScore"])
for _, x in enumerate(res["gop"]["parts"])
]
ipas_dicts = getSyllableBreakingSingleUtt(res["gop"])
ipa_ans = [" ".join(d["ipa_break_merged"]) for d in ipas_dicts]
# print('ipas_dicts', ipas_dicts)
ipa_pred = []
for i, d in enumerate(ipas_dicts):
ipa_pred_tmp = []
for dv in d["ipa_break_pred_merged"]:
if type(dv) == str:
ipa_pred_tmp.append(dv)
elif type(dv) == list:
ipa_pred_tmp.append("".join(dv))
cur_pred_ipa = " ".join(ipa_pred_tmp)
# 如果預測 IPA 與答案 IPA 相符,即使 gop 分數低於70,都需要強迫設定成70分
# 如果預測 IPA 與答案 IPA 相符,即使 gop 分數低於80,都需要強迫設定成80分
if cur_pred_ipa == ipa_ans[i] and gop_results[i][1] < 80:
gop_results[i] = (True, 80)
ipa_pred.append(cur_pred_ipa)
predicted_text = " ".join([x["word"] for _, x in enumerate(res["gop"]["parts"])])
result = wer(ref=inputText, hyp=predicted_text, debug=False)
ref = word_tokenize(inputText)
_, hyp, label = result["info"]
puncts_offset = [0] * len(puncts_dict)
for i, wd in enumerate(hyp):
if ref[i] == "****":
for j, idx in enumerate(puncts_dict.keys()):
if idx + puncts_offset[j] >= i:
puncts_offset[j] += 1
if not label[i] == "INS":
hyp[i] = ref[i]
acc = np.mean([x[1] for x in gop_results])
# print('puncts_dict', puncts_dict)
# for idx in puncts_dict:
for i, idx in enumerate(puncts_dict):
ref.insert(idx + puncts_offset[i], puncts_dict[idx])
hyp.insert(idx + puncts_offset[i], puncts_dict[idx])
label.insert(idx + puncts_offset[i], puncts_dict[idx])
gop_results.insert(idx + puncts_offset[i], (True, puncts_dict[idx]))
ins_indices, del_indices, sub_indices = [], [], []
for i, x in enumerate(label):
if x == "INS":
ins_indices.append(i)
elif x == "SUB":
sub_indices.append(i)
elif x == "DEL":
del_indices.append(i)
# ins_indices = [i for i,x in enumerate(label) if x=="INS"]
# del_indices = [i for i,x in enumerate(label) if x=="DEL"]
# sub_indices = [i for i,x in enumerate(label) if x=="SUB"]
# del_indices, sub_indices = [], []
# for i, x in enumerate(gop_results):
# try:
# if x[1] < minscores_missing:
# del_indices.append(i)
# elif minscores_missing<=x[1]<minscores_angel:
# sub_indices.append(i)
# except:
# pass
# del_indices = [i for i,x in enumerate(gop_results) if not x[0] and x[1]<minscores_missing]
sub_indices = [i for i, x in enumerate(gop_results) if not x[0]]
# acc = (1-result['WER'])*100
# acc = (1-(len(sub_indices)/len(gop_results)))*100
return ref, hyp, ins_indices, del_indices, sub_indices, acc
def generateGOPResult(
wav_path,
asr_lvs_score,
inputText,
ref,
hyp,
ins_indices,
del_indices,
sub_indices,
hashkey_uuid,
accountID="",
logstatus=True,
prtmsg=False,
):
"""Wrapped function to generate gop results.
Modify the input text according to the indices of insertions, deletions and substitutions first
to avoid gop misaligned error.
Args:
wav_path: the path of the input waveform file
ref: the list of all words in reference text
hyp: the list of all words in hypothesis text
ins_indices: the indices of all insertions
del_indices: the indices of all deletions
sub_indices: the indices of all substitutions
Returns:
gop_results: the results of gop scoring
ipas: syllable based ipa of the text
"""
if prtmsg:
print("======GOP result{S}======")
print("hyp", hyp)
print("ref", ref)
print("ins_indices(多)", ins_indices)
print("del_indices(少)", del_indices)
print("ins_indices(錯)", ins_indices)
puncts_indices = []
inputText = " ".join(inputText)
# print('DEBUG: inputText:', inputText)
cmu_ans, _ = toCMUSeq(inputText)
# print('DEBUG: cmu_ans = ', cmu_ans)
alternated_text = []
for i, wd in enumerate(hyp):
if wd in punctuations:
puncts_indices.append(i)
else:
alternated_text.append(wd)
text = " ".join(alternated_text)
res = getEngGOPresult(
wav_path=wav_path,
text=text,
cmu_ans=cmu_ans,
accountid=accountID,
logstatus=logstatus,
)
if prtmsg:
print("GOP text", text)
print("puncts_indices", puncts_indices)
print("GOP response json", str(res)[0:150])
error_silent = ApiError(
code="e02", message="Unable to analyze, Listen once more and try again."
)
gopresult = res.get("gop", {}).get("parts", "")
if gopresult == "":
savelog(
hashkey_uuid, "diagnosis:ERROR", "Gop Unable to analyze, please try again."
)
raise ApiException(status_code=422, error=error_silent)
detail_parts = res["gop"]["parts"]
# print('detail_parts', detail_parts)
parts_ctm_lists = do_userwav_ctm(wav_path, detail_parts)
minscores_angel = 60
res["gop"]["parts"] = word_score_offset(res["gop"]["parts"])
gop_results = [
(
x["GOPScore"] >= minscores_angel,
x["GOPScore"],
x.get("word_model_active", ""),
)
for _, x in enumerate(res["gop"]["parts"])
]
ipas_dicts = getSyllableBreakingSingleUtt(res["gop"])
ipa_ans = [" ".join(d["ipa_break_merged"]) for d in ipas_dicts]
print("gop_results--1", gop_results)
# print('ipas_dicts', ipas_dicts)
ipa_pred = []
for i, d in enumerate(ipas_dicts):
ipa_pred_tmp = []
for dv in d["ipa_break_pred_merged"]:
if type(dv) == str:
ipa_pred_tmp.append(dv)
elif type(dv) == list:
ipa_pred_tmp.append("".join(dv))
cur_pred_ipa = " ".join(ipa_pred_tmp)
# 如果預測 IPA 與答案 IPA 相符,即使 gop 分數低於80,都需要強迫設定成80分
if cur_pred_ipa == ipa_ans[i] and gop_results[i][1] < 80:
gop_results[i] = (True, 80, gop_results[i][2])
ipa_pred.append(cur_pred_ipa)
print("gop_results--2", gop_results)
# 如果GOP分數有達C級,則讓 ipa_pred[i] = ipa_ans[i]
for i, (gs, gv, word_model) in enumerate(gop_results):
if gv >= 80:
ipa_pred[i] = ipa_ans[i]
print("ipa_pred", ipa_pred)
print("gop_results", gop_results)
# ['ɪt', '*ʌv', 'faɪŋ', 'naʊ']
# ipa_pred = [' '.join(d['ipa_break_pred_merged']) for d in ipas_dicts]
# print('===ipa_pred===', ipa_pred)
# 計算gop 分數 * Levenshtein(asr)的分數
gop_correct = 0
for gr, gs, word_model in gop_results:
if str(gr).lower() == "true":
gop_correct += 1
gop_score = gop_correct / len(gop_results)
gop_score = round(gop_score * 100)
if prtmsg:
print("gop_results", gop_results)
print("asr_lvs_score", asr_lvs_score)
print("ipa_ans", ipa_ans)
print("ipa_pred", ipa_pred)
print("gop_score", gop_score)
# gop_score_lvs = round((gop_score * asr_lvs_score) / 100)
gop_score_lvs = round(np.mean([x[1] for x in gop_results]))
# print('gop_score_lvs', gop_score_lvs)
# 星等分數
total_star = round(gop_score_lvs / 20, 1)
# print('total_star', total_star)
# recommended_sents_res = getEngRecommendSents(res['gop'])
recommended_sents_res = {}
recommended_sents_res["recommendation"] = [{"recommend": []}]
# print("recommended_sents_res['recommendation']", recommended_sents_res['recommendation'])
# TTS 挑選100字以下
recommended_sentences = []
# [(False, 35.50183903770348), (True, 73.59730599132585), (False, 12.28575122817972), (True, 100), (False, 14.015328757050762)]
diagnosis_error_index = []
for i, d in enumerate(gop_results):
status, score, word_model = d
# if status == False:
if not status:
diagnosis_error_index.append(i)
errindex = 0
for i, x in enumerate(recommended_sents_res["recommendation"]):
if len(x["recommend"]) == 0: # no recommendation at all
recommended_sentences.append(
(["n/a"] if i in diagnosis_error_index else [])
)
else:
try:
sent, lv, ptrn = sorted(x["recommend"])[0]
cefr_color = CEFR_2_color(lv)
recommended_sentences.append(
[
{
"en": sent,
"level": {"CEFR": lv, "CEFR_color": cefr_color},
"grammar": ptrn.split(", "),
}
]
)
except Exception as e:
recommended_sentences.append(["n/a"])
errindex += 1
for idx in sorted(del_indices + puncts_indices):
gop_results.insert(idx, ["****"])
ipa_ans.insert(idx, "****")
ipa_pred.insert(idx, "****")
recommended_sentences.insert(idx, ["****"])
parts_ctm_lists.insert(idx, ["****"])
if prtmsg:
print("gop_results", gop_results)
print("ipa_ans", ipa_ans)
print("ipa_pred", ipa_pred)
print("recommended_sentences", recommended_sentences)
if prtmsg:
print("======GOP result{E}======")
return (
gop_results,
ipa_ans,
ipa_pred,
recommended_sentences,
parts_ctm_lists,
total_star,
gop_score_lvs,
res,
)
def do_userwav_ctm(wav_path, detail_parts):
wav = AudioSegment.from_wav(wav_path)
# print('wav_path', wav_path)
wav_bp = "./wavs/ctm"
wav_path_base = os.path.splitext(wav_path)[0].split(os.sep)[-1]
parts_ctm_lists = []
rn = 1
for dic in detail_parts:
# print('dic', dic)
word = dic.get("phone")
# "intervals":[0.61, 1.05],