示例#1
0
def calculate_merged_string(string1, string2):
    merged_templates = Template.merge_templates_wagner_fischer(
        Template.from_string(string1, slot_token="[SLOT]"),
        Template.from_string(string2, slot_token="[SLOT]"),
        allow_longer_template=False
    )
    return next(merged_templates).to_flat_string(detokenizer=lambda x: " ".join(x))
示例#2
0
def _merge_templates(t1: Template,
                     t2: Template,
                     minimal_variables: bool,
                     allow_empty_string=True) -> Template:
    return next(
        Template.merge_templates_wagner_fischer(
            t1,
            t2,
            minimal_variables=minimal_variables,
            allow_empty_string=allow_empty_string,
        ))
示例#3
0
 def _get_any_merge_candidate(self, t1: Template,
                              t2: Template) -> MergeCandidate:
     """ Unused version of _get_best_merge_candidate, but might be prefered for performance gains """
     max_length = max(t1.get_number_of_non_slots(),
                      t2.get_number_of_non_slots())
     min_slots = min(t1.get_number_of_slots(), t2.get_number_of_slots())
     merged_template = next(
         Template.merge_templates_wagner_fischer(
             t1, t2, minimal_variables=self._minimal_variables))
     return MergeCandidate(
         t1, t2,
         _get_distance_of_merged(merged_template, max_length, min_slots))
示例#4
0
    def _get_best_merge_candidate(self, t1: Template,
                                  t2: Template) -> MergeCandidate:
        """
        Calculates the distance between two given templates, that can contain slots
        """
        max_length = max(t1.get_number_of_non_slots(),
                         t2.get_number_of_non_slots())
        min_slots = min(t1.get_number_of_slots(), t2.get_number_of_slots())

        merged_templates = set(
            Template.merge_templates_wagner_fischer(
                t1, t2, minimal_variables=self._minimal_variables))
        merge_candidates = []
        for merged_template in merged_templates:
            distance = _get_distance_of_merged(merged_template, max_length,
                                               min_slots)
            merge_candidates.append(
                MergeCandidate(t1, t2, distance, merged=merged_template))
        return min(merge_candidates)
示例#5
0
def _merge_templates(t1: Template, t2: Template,
                     minimal_variables: bool) -> Template:
    return next(
        Template.merge_templates_wagner_fischer(
            t1, t2, minimal_variables=minimal_variables))