-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipaAnalyse.py
146 lines (118 loc) · 4.42 KB
/
ipaAnalyse.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
import os
import shutil
import csv
from difflib import SequenceMatcher
import jellyfish
import string
import difflib
import re
from jiwer import wer
import math
import re
from collections import Counter
#Copy audio from testfolder to input
dir_path = os.path.dirname(os.path.realpath(__file__))
shutil.rmtree(dir_path+'/InputAudioData')
os.makedirs(dir_path+'/InputAudioData')
for folder in os.listdir(dir_path+'/testIpa/input'):
for file in os.listdir(dir_path+'/testIpa/input/'+folder):
if file[-4:] != '.txt':
shutil.copy(dir_path+'/testIpa/input/'+folder+'/'+file, dir_path+'/InputAudioData')
with open("speechProcess.py") as f:
exec(f.read())
# Compress results
shutil.rmtree(dir_path+'/testIpa/results/')
os.makedirs(dir_path+'/testIpa/results/')
for folder in os.listdir(dir_path+'/output'):
os.makedirs(dir_path+'/testIpa/results/'+folder)
f = open(dir_path+'/testIpa/results/'+folder+"/transcipt.txt", "x")
f.close
ls = os.listdir(dir_path+'/output/'+folder+'/')
ls.sort()
for output in ls:
test_path = dir_path+'/output/'+folder+'/'+output
if os.path.isdir(test_path):
with open(test_path + "/Alosourus_noTime.txt" ,'r') as read:
with open(dir_path+'/testIpa/results/'+folder+"/transcipt.txt", "a+") as store:
for line in read.readlines():
line = line.replace(" ", ' ')
store.write(line)
# field names
fields = ['FileName', 'WER', 'NO. of expected chars','NO. of actual chars', 'NO. Added chars', 'NO. Missing chars', 'levenshtein Distance','Proportional Lev']
#analyses and output data
actStats = "actaulStates.csv"
tranStats = "transcioptionStates.csv"
with open(dir_path+'/testIpa/'+actStats , 'w') as file:
csvwriter = csv.writer(file)
# writing the fields
csvwriter.writerow(fields)
with open(dir_path+'/testIpa/'+tranStats , 'w') as file:
csvwriter = csv.writer(file)
# writing the fields
csvwriter.writerow(fields)
for folder in os.listdir(dir_path+'/testIpa/results'):
act =''
tran = ''
calc = ''
with open(dir_path+'/testIpa/input/'+folder+'/actual.txt' , 'r') as r:
for line in r.readlines():
act+= line
with open(dir_path+'/testIpa/input/'+folder+'/transcipt.txt' , 'r') as r:
for line in r.readlines():
tran+= line
with open(dir_path+'/testIpa/results/'+folder+'/transcipt.txt' , 'r') as r:
for line in r.readlines():
calc+= line
# data rows of csv file
actRows = [ ]
transRows = [ ]
act = act.replace(" ",' ')
tran = tran.replace(" ",' ')
actChars = len(act.split())
transChars = len(tran.split())
calcChars = len(calc.split())
actDict = {}
transDict = {}
calcDict = {}
lis = [actDict , transDict , calcDict]
text = [act, tran , calc]
count = 0
for l in lis:
for word in text[count].split():
if not word in l:
l[word] = 1
else:
l[word] += 1
count+=1
chars = [[0,0],[0,0]]
lis2= [actDict , transDict]
count = 0
for l in lis2:
noAddedWords = 0
noMissingWords = 0
for word in calcDict:
if not word in l:
noAddedWords += calcDict[word]
else:
if (calcDict[word] - l[word])>0:
noAddedWords += calcDict[word] - l[word]
else:
noMissingWords -= calcDict[word] - l[word]
for word in l:
if not word in calcDict:
noMissingWords += l[word]
chars[count][0]=noAddedWords
chars[count][1] = noMissingWords
count+=1
WER = wer(act, calc)
WER2 = wer(tran, calc)
lev_dist = jellyfish.levenshtein_distance(act, calc)
lev_dist2 = jellyfish.levenshtein_distance(tran, calc)
with open(dir_path+'/testIpa/'+actStats, '+a') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
csvwriter.writerow([folder ,WER , actChars, calcChars, str(chars[0][0]),str(chars[0][1]),lev_dist,str(lev_dist/actChars) ])
with open(dir_path+'/testIpa/'+tranStats, '+a') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
csvwriter.writerow([folder ,WER2 , transChars, calcChars, chars[1][0],chars[1][1],lev_dist2 ,str(lev_dist2/actChars)])