def fragment_match(self, fragment, group_name=None): """Follow a fragment chain to try find a match :type fragment: caper.objects.CaperFragment :type group_name: str or None :return: The weight of the match found between 0.0 and 1.0, where 1.0 means perfect match and 0.0 means no match :rtype: (float, dict, int) """ group_name, weight_groups = self.find_group(group_name) for weight, patterns in weight_groups: for pattern in patterns: success = True result = {} num_matched = 0 fragment_iterator = fragment.take_right( return_type='value', include_separators=pattern.include_separators, include_source=True ) for subject, fragment_pattern in itertools.izip_longest(fragment_iterator, pattern): # No patterns left to match if not fragment_pattern: break # No fragments left to match against pattern if not subject: success = False break value, source = subject matches = pattern.execute(fragment_pattern, value) if matches: for match in pattern.process(matches): update_dict(result, match) else: success = False break if source == 'subject': num_matched += 1 if success: Logr.debug('Found match with weight %s using regex pattern "%s"' % (weight, [sre.pattern for sre in pattern.patterns])) return float(weight), result, num_matched return 0.0, None, 1
def fragment_match(self, fragment, group_name=None): """Follow a fragment chain to try find a match :type fragment: caper.objects.CaperFragment :type group_name: str or None :return: The weight of the match found between 0.0 and 1.0, where 1.0 means perfect match and 0.0 means no match :rtype: (float, dict, int) """ group_name, weight_groups = self.find_group(group_name) for weight, patterns in weight_groups: for pattern in patterns: cur_fragment = fragment success = True result = {} # Ignore empty patterns if len(pattern) < 1: break for fragment_pattern in pattern: if not cur_fragment: success = False break match = fragment_pattern.match(cur_fragment.value) if match: update_dict(result, match.groupdict()) else: success = False break cur_fragment = cur_fragment.right if cur_fragment else None if success: Logr.debug("Found match with weight %s" % weight) return float(weight), result, len(pattern) return 0.0, None, 1