def read(self, path, **kwargs): T.reset() transcription = Transcription() previousNode = TStart arc = [] for line in self.iterlines(path, encoding='utf-8'): # skip comments if re.search(r'^;;', line): continue fields = line.strip().split() # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ word = fields[4] # remove punctuations if not self.punctuation: word = re.sub(r'[\.!,;?":]+', ' ', word) word = re.sub(r' +', ' ', word) word = word.strip() if not word: continue # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ confidence = float(fields[5]) start_time = round(float(fields[2]), 3) duration = round(float(fields[3]), 3) end_time = float(start_time)+float(duration) end_time = round(end_time, 3) if duration == 0: node_start = previousNode node_end = T() if len(arc) == 2: transcription.remove_edge(arc[0], arc[1]) transcription.add_edge(arc[0], node_end, **arc_data) node_inter = T() transcription.add_edge(node_end, node_inter, speech=word, confidence=confidence) transcription.add_edge(node_inter, arc[1]) arc.append(node_end) arc.append(node_inter) node_end=arc[1] elif len(arc) > 2: node_anc_start = arc[0] node_anc_end = arc[1] transcription.remove_edge(arc[len(arc)-1], node_anc_end) transcription.add_edge(arc[len(arc)-1], node_end, speech=word, confidence=confidence) transcription.add_edge(node_end, node_anc_end) arc.append(node_end) node_end=arc[1] else: addEdge = True node_start = T(start_time) node_end = T(end_time) if previousNode.drifting: if not transcription.has_edge(previousNode, node_start): transcription.add_edge(previousNode, node_start) else: if node_start < previousNode: node_start = previousNode elif node_start > previousNode: transcription.add_edge(previousNode, node_start) if node_start.anchored and node_end.anchored: if node_start == node_end: addEdge = False node_start = previousNode node_end = T() if len(arc) == 2: transcription.remove_edge(arc[0], arc[1]) transcription.add_edge(arc[0], node_end, **arc_data) node_inter = T() transcription.add_edge(node_end, node_inter, speech=word, confidence=confidence) transcription.add_edge(node_inter, arc[1]) arc.append(node_end) arc.append(node_inter) node_end = arc[1] elif len(arc) > 2: node_anc_start = arc[0] node_anc_end = arc[1] transcription.remove_edge(arc[len(arc) - 1], node_anc_end) transcription.add_edge(arc[len(arc) - 1], node_end, speech=word, confidence=confidence) transcription.add_edge(node_end, node_anc_end) arc.append(node_end) node_end = arc[1] else: arc = [node_start, node_end] arc_data = {'speech': word, 'confidence': confidence} if addEdge: transcription.add_edge(node_start, node_end, speech=word, confidence=confidence) previousNode = node_end transcription.add_edge(previousNode, TEnd) self._loaded = transcription return self
def get_transcription(self, path2ctm): T.reset() transcription = Transcription() with codecs.open(path2ctm, "rt", encoding='utf8') as f: lines = f.readlines() # last word previous = (None, TStart) # last word with duration greater than 0 previous_with_duration = (None, TStart) for line in lines: # skip comments if re.search(r'^;;', line): continue fields = line.strip().split() # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ word = fields[4] # remove punctuations if not self.punctuation: word = re.sub(r'[\.!,;?":]+',' ', word) word = re.sub(r' +',' ', word) word = word.strip() if not word: continue # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ confidence = fields[5] start_time = round(float(fields[2]), 3) duration = round(float(fields[3]), 3) end_time = float(start_time)+float(duration) end_time = round(end_time, 3) if duration > 0.: start = T(start_time) end = T(end_time) # if current word starts before the end of previous word, # make it start exactly at the end of previous word if start < previous_with_duration[1]: start = previous_with_duration[1] # if current word starts after the end of previous word, # add an empty edge if start > previous_with_duration[1]: transcription.add_edge(previous_with_duration[1], start) # add word and update `previous` transcription.add_edge(start, end, speech=word, confidence=confidence) previous_with_duration = (start, end) previous = (start, end) else: start = previous end = T() if duration == 0: node_start = previousNode node_end = T() if len(arc) == 2: transcription.remove_edge(arc[0], arc[1]) transcription.add_edge(arc[0], node_end, **arc_data) node_inter = T() transcription.add_edge(node_end, node_inter, speech=word, confidence=confidence) transcription.add_edge(node_inter, arc[1]) arc.append(node_end) arc.append(node_inter) node_end=arc[1] elif len(arc) > 2: node_anc_start = arc[0] node_anc_end = arc[1] transcription.remove_edge(arc[len(arc)-1], node_anc_end) transcription.add_edge(arc[len(arc)-1], node_end, speech=word, confidence=confidence) transcription.add_edge(node_end, node_anc_end) arc.append(node_end) node_end=arc[1] else: addEdge = True node_start = T(start_time) node_end = T(end_time) if previousNode.drifting: if not transcription.has_edge(previousNode, node_start): transcription.add_edge(previousNode, node_start) else: if node_start < previousNode: node_start = previousNode elif node_start > previousNode: transcription.add_edge(previousNode, node_start) if node_start.anchored and node_end.anchored: if node_start == node_end: addEdge = False node_start = previousNode node_end = T() if len(arc) == 2: transcription.remove_edge(arc[0], arc[1]) transcription.add_edge(arc[0], node_end, **arc_data) node_inter = T() transcription.add_edge(node_end, node_inter, speech=word, confidence=confidence) transcription.add_edge(node_inter, arc[1]) arc.append(node_end) arc.append(node_inter) node_end=arc[1] elif len(arc) > 2: node_anc_start = arc[0] node_anc_end = arc[1] transcription.remove_edge(arc[len(arc)-1], node_anc_end) transcription.add_edge(arc[len(arc)-1], node_end, speech=word, confidence=confidence) transcription.add_edge(node_end, node_anc_end) arc.append(node_end) node_end=arc[1] else: arc = [node_start, node_end] arc_data = {'speech':word, 'confidence':confidence} if addEdge: transcription.add_edge(node_start, node_end, speech=word, confidence=confidence) previousNode=node_end transcription.add_edge(previousNode, TEnd) return transcription