Beispiel #1
0
    def construct_patterns(self, pattern_groups):
        compile_start = datetime.now()
        compile_count = 0

        for group_name, patterns in pattern_groups:
            if group_name not in self.regex:
                self.regex[group_name] = []

            # Transform into weight groups
            if type(patterns[0]) is str or type(
                    patterns[0][0]) not in [int, float]:
                patterns = [(1.0, patterns)]

            for weight, patterns in patterns:
                weight_patterns = []

                for pattern in patterns:
                    # Transform into multi-fragment patterns
                    if type(pattern) is str:
                        pattern = (pattern, )

                    if type(pattern) is tuple and len(pattern) == 2:
                        if type(pattern[0]) is str and is_list_type(
                                pattern[1], str):
                            pattern = (pattern, )

                    result = []
                    for value in pattern:
                        if type(value) is tuple:
                            if len(value) == 2:
                                # Construct OR-list pattern
                                value = value[0] % '|'.join(value[1])
                            elif len(value) == 1:
                                value = value[0]

                        result.append(re.compile(value, re.IGNORECASE))
                        compile_count += 1

                    weight_patterns.append(tuple(result))

                self.regex[group_name].append((weight, weight_patterns))

        Logr.info("Compiled %s patterns in %ss", compile_count,
                  delta_seconds(datetime.now() - compile_start))
Beispiel #2
0
    def construct_patterns(self, pattern_groups):
        compile_start = datetime.now()
        compile_count = 0

        for group_name, patterns in pattern_groups:
            if group_name not in self.regex:
                self.regex[group_name] = []

            # Transform into weight groups
            if type(patterns[0]) is str or type(patterns[0][0]) not in [int, float]:
                patterns = [(1.0, patterns)]

            for weight, patterns in patterns:
                weight_patterns = []

                for pattern in patterns:
                    # Transform into multi-fragment patterns
                    if type(pattern) is str:
                        pattern = (pattern,)

                    if type(pattern) is tuple and len(pattern) == 2:
                        if type(pattern[0]) is str and is_list_type(pattern[1], str):
                            pattern = (pattern,)

                    result = []
                    for value in pattern:
                        if type(value) is tuple:
                            if len(value) == 2:
                                # Construct OR-list pattern
                                value = value[0] % '|'.join(value[1])
                            elif len(value) == 1:
                                value = value[0]

                        result.append(re.compile(value, re.IGNORECASE))
                        compile_count += 1

                    weight_patterns.append(tuple(result))

                self.regex[group_name].append((weight, weight_patterns))

        Logr.info("Compiled %s patterns in %ss", compile_count, delta_seconds(datetime.now() - compile_start))
Beispiel #3
0
    def construct_patterns(self, pattern_groups):
        compile_start = datetime.now()
        compile_count = 0

        for group_name, patterns in pattern_groups:
            if group_name not in self.regex:
                self.regex[group_name] = []

            # Transform into weight groups
            if type(patterns[0]) is str or type(patterns[0][0]) not in [int, float]:
                patterns = [(1.0, patterns)]

            for weight, patterns in patterns:
                weight_patterns = []

                for pattern in [CaperPattern.construct(v) for v in patterns if v]:
                    compile_count += pattern.compile()
                    weight_patterns.append(pattern)

                self.regex[group_name].append((weight, weight_patterns))

        Logr.info("Compiled %s patterns in %ss", compile_count, delta_seconds(datetime.now() - compile_start))