-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelled_audiofile.py
161 lines (91 loc) · 4.26 KB
/
labelled_audiofile.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
import logging, re
from os import path
from sentence import Sentence
from drill import Drill
from drill_section import DrillSection
class CannotParseLineException(Exception):
pass
class Label(object):
regex = re.compile('(?P<start>\d+[\.,]\d+)\t(?P<end>\d+[,\.]\d+)\t(?P<section>[^;]*);(?P<drill>[^;]*);(?P<text>.*)')
def __init__(self, line):
self._parse_label_line(line)
def _parse_label_line(self, line):
x = Label.regex.match(line)
if x == None:
raise CannotParseLineException('the line given does not have the right format')
self.start_s = self._comma_separated_decimal_to_float(x.group('start'))
self.end_s = self._comma_separated_decimal_to_float(x.group('end'))
self.section = x.group('section')
self.drill = x.group('drill')
self.text = x.group('text')
@staticmethod
def _comma_separated_decimal_to_float(comma_separated_decimal,
delimiter=','):
#if comma_separated_decimal.count(delimiter) != 1:
#raise Exception('improper format')
return float(comma_separated_decimal.replace(delimiter, '.'))
def __str__(self):
return '<section:%s|drill:%s|text:%s>' \
% (self.section, self.drill, self.text)
class LabelledAudiofile(object):
def __init__(self, audiofile_path):
if not path.exists(audiofile_path):
raise Exception('no such file: %s' % audiofile_path)
self._labelfile_path = self._get_labelfile_path(audiofile_path)
if not path.exists(self._labelfile_path):
raise Exception('cannot find any labels for %s'
% audiofile_path)
self._audiofile_path = audiofile_path
self._drill_sections = list()
@staticmethod
def _labels(labelfile_path):
with open(labelfile_path) as f:
for number, line in enumerate(f):
try:
label = Label(line)
except CannotParseLineException:
pass
else:
yield label
def parse(self):
current_drill_section = None
previous_line = None
for current in self._labels(self._labelfile_path):
logging.info(current)
if not current_drill_section or \
current_drill_section.get_name() != current.section:
current_drill_section = DrillSection(current.section)
self._drill_sections.append(current_drill_section)
if not previous_line:
previous_line = current
continue
assert previous_line.section == current.section
if previous_line.drill != current.drill:
example_sentence = Sentence(previous_line.text,
self._audiofile_path,
previous_line.start_s,
previous_line.end_s)
current_drill_section.set_example(example_sentence)
previous_line = current
continue
assert previous_line.section == current.section
assert previous_line.drill == current.drill
teacher_sentence = Sentence(previous_line.text,
self._audiofile_path,
previous_line.start_s,
previous_line.end_s)
previous_line = None
student_sentence = Sentence(current.text,
self._audiofile_path,
current.start_s,
current.end_s)
new_drill = Drill(teacher_sentence,
student_sentence)
current_drill_section.add_drill(new_drill)
def get_drill_sections(self):
return self._drill_sections
@staticmethod
def _get_labelfile_path(audiofile_path):
_path, ext = path.splitext(audiofile_path)
expected_labelfile_extension = 'txt'
return _path + '.' + expected_labelfile_extension