@staticmethod
def __add_repetition(repetition, spk_tier, start_idx, trs_out):
"""Add a repetition - source and echos - in tiers.
:param repetition: (DataRepetition)
:param spk_tier: (sppasTier) The tier of the speaker (to detect sources)
:param start_idx: (int) start index of the interval in spk_tier
:param trs_out: (sppasTranscription)
:returns: (bool) the repetition was added or not
"""
src_tier = trs_out.find('SR-Source')
echo_tier = trs_out.find('SR-Repet')
sr_index = len(src_tier)
s, e = repetition.get_source()
src_begin = spk_tier[start_idx + s].get_lowest_localization()
src_end = spk_tier[start_idx + e].get_highest_localization()
iitime = sppasInterval(src_begin.copy(), src_end.copy())
try:
a = src_tier.create_annotation(sppasLocation(iitime), sppasLabel(sppasTag('S' + str(sr_index + 1))))
src_id = a.get_meta('id')
except:
return False
echo_labels = list()
for s, e in repetition.get_echos():
rep_begin = spk_tier[start_idx + s].get_lowest_localization()
rep_end = spk_tier[start_idx + e].get_highest_localization()
eetime = sppasInterval(rep_begin.copy(), rep_end.copy())
anns = spk_tier.find(rep_begin, rep_end)
for a in anns:
for lbl in a.get_labels():
echo_labels.append(lbl.copy())
a = echo_tier.create_annotation(sppasLocation(eetime), sppasLabel(sppasTag('R' + str(sr_index + 1))))
a.set_meta('is_self_repetition_of', src_id)
anns = spk_tier.find(src_begin, src_end)
src_labels = list()
for a in anns:
for lbl in a.get_labels():
src_labels.append(lbl.copy())
a = trs_out.find('SR-SrcStrain').create_annotation(sppasLocation(iitime), src_labels)
a.set_meta('source_id', src_id)
a = trs_out.find('SR-SrcLen').create_annotation(sppasLocation(iitime), sppasLabel(sppasTag(len(src_labels), 'int')))
a.set_meta('source_id', src_id)
or_type = 'variation'
if len(repetition.get_echos()) > 1:
or_type = 'split:{:d}'.format(len(repetition.get_echos()))
elif len(src_labels) > len(echo_labels):
or_type = 'reduction'
elif len(src_labels) == len(echo_labels):
equals = True
for ls, le in zip(src_labels, echo_labels):
if ls.get_best() != le.get_best():
equals = False
break
if equals is True:
or_type = 'strict'
a = trs_out.find('SR-SrcType').create_annotation(sppasLocation(iitime), sppasLabel(sppasTag(or_type)))
a.set_meta('source_id', src_id)
logging.info('OR {:d}. {} {} -> {:s}'.format(sr_index + 1, src_labels, echo_labels, or_type))
return True