forked from aalto-speech/speaker-diarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaku2ann.py
executable file
·69 lines (59 loc) · 2.21 KB
/
aku2ann.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
#!/usr/bin/python2
import argparse
import sys
import re
def parse_recipe(rfile):
"""Parses input recipe"""
r = []
audio_file = re.compile('audio=(\S+)')
lna_name = re.compile('lna=(\S+)')
start_time = re.compile('start-time=(\d+.\d+)')
end_time = re.compile('end-time=(\d+.\d+)')
speaker_tag = re.compile('speaker=(\S+)')
for line in rfile:
try:
audio = audio_file.search(line).groups()[0]
lna = lna_name.search(line).groups()[0]
start = float(start_time.search(line).groups()[0])
end = float(end_time.search(line).groups()[0])
try:
speaker = speaker_tag.search(line).groups()[0]
except AttributeError:
speaker = ''
r.append((audio, lna, start, end, speaker))
except AttributeError:
print 'Recipe line without recognizable data:', line
return r
def write_ann(recipe, outf):
"""Write ann (simple annotation) file"""
audio = ''
for line in recipe:
if audio != line[0]:
audio = line[0]
outf.write('# ' + audio + '\n')
outf.write(str(line[2]) + '\t' + str(line[3]) + '\t' + line[4] + '\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Converts an AKU recipe to \
simple annotation format.')
parser.add_argument('recfile', type=str,
help='Specifies the input recipe file')
parser.add_argument('-o', dest='outfile', type=str, default=sys.stdout,
help='Specifies an output file, default stdout.')
args = parser.parse_args()
# Process arguments
print 'Reading recipe from:', args.recfile
with open(args.recfile, 'r') as recfile:
parsed_recipe = parse_recipe(recfile)
if args.outfile != sys.stdout:
outfile = args.outfile
print 'Writing output to:', args.outfile
else:
outfile = sys.stdout
print 'Writing output to: stdout'
# End of argument processing
# Do the real work
if outfile != sys.stdout:
with open(outfile, 'w') as outf:
write_ann(parsed_recipe, outf)
else:
write_ann(parsed_recipe, outfile)