def match_lexeme_forms_to_template(lexeme_forms: list, template: Template) -> MatchedTemplate: template = cast(MatchedTemplate, copy.deepcopy(template)) for lexeme_form in lexeme_forms: best_template_forms = match_lexeme_form_to_template_forms( 'test' in template, lexeme_form, template['forms']) if len(best_template_forms) == 1: best_template_form = cast(MatchedTemplateForm, best_template_forms[0]) best_template_form.setdefault('lexeme_forms', []).append(lexeme_form) elif best_template_forms: template.setdefault('ambiguous_lexeme_forms', []).append(lexeme_form) else: template.setdefault('unmatched_lexeme_forms', []).append(lexeme_form) return template