@staticmethod
def __add_repetition(repetition, spk1_tier, spk2_tier, start_idx1, start_idx2, trs_out):
"""Add a repetition - source and echos - in tiers.
:param repetition: (DataRepetition)
:param spk1_tier: (Tier) The tier of speaker 1 (to detect sources)
:param spk2_tier: (Tier) The tier of speaker 2 (to detect echos)
:param start_idx1: start index of the interval in spk1_tier
:param start_idx2: start index of the interval in spk2_tier
:param trs_out: (sppasTranscription) The resulting tiers
:returns: (bool) the repetition was added or not
"""
src_tier = trs_out.find('OR-Source')
echo_tier = trs_out.find('OR-Echo')
or_index = len(src_tier)
s, e = repetition.get_source()
src_begin = spk1_tier[start_idx1 + s].get_lowest_localization()
src_end = spk1_tier[start_idx1 + e].get_highest_localization()
iitime = sppasInterval(src_begin.copy(), src_end.copy())
try:
a = src_tier.create_annotation(sppasLocation(iitime), sppasLabel(sppasTag('S' + str(or_index + 1))))
src_id = a.get_meta('id')
except TierAddError:
return False
logging.debug('==> Source {:d}'.format(or_index))
echo_labels = list()
for s, e in repetition.get_echos():
rep_begin = spk2_tier[start_idx2 + s].get_lowest_localization()
rep_end = spk2_tier[start_idx2 + e].get_highest_localization()
anns = spk2_tier.find(rep_begin, rep_end)
for a in anns:
for lbl in a.get_labels():
echo_labels.append(lbl.copy())
logging.debug(' -> echo {} {}: {}'.format(s, e, lbl))
eetime = sppasInterval(rep_begin.copy(), rep_end.copy())
r = sppasLabel(sppasTag('R' + str(or_index + 1)))
try:
a = echo_tier.create_annotation(sppasLocation(eetime), r)
a.set_meta('source_id', src_id)
except TierAddError:
a = echo_tier.find(rep_begin, rep_end)
if len(a) > 0:
a[0].append_label(r)
anns = spk1_tier.find(src_begin, src_end)
src_labels = list()
for a in anns:
for lbl in a.get_labels():
src_labels.append(lbl.copy())
logging.debug(' => src: {}'.format(lbl))
a = trs_out.find('OR-SrcStrain').create_annotation(sppasLocation(iitime), src_labels)
a.set_meta('source_id', src_id)
a = trs_out.find('OR-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('OR-SrcType').create_annotation(sppasLocation(iitime), sppasLabel(sppasTag(or_type)))
a.set_meta('source_id', src_id)
all_echos_tier = trs_out.find('OR-AllEchos')
if all_echos_tier is not None:
for tok_idx in repetition.get_all_echos():
ann = spk2_tier[start_idx2 + tok_idx]
r = sppasLabel(sppasTag('R' + str(or_index + 1)))
find_a = all_echos_tier.find(ann.get_lowest_localization(), ann.get_highest_localization(), overlaps=False)
if len(find_a) == 1:
find_a[0].append_label(r)
else:
a = ann.copy()
a.set_labels([r])
all_echos_tier.add(a)
a.set_meta('source_id_of_R{:d}'.format(or_index + 1), src_id)
return True