API Documentation#

class patternpiece.matcher.PatternPiece(patterns, mode='memory')[source]#

Pattern piece matching class, used to call the AC automaton’s Cython implementation.

Multi-pattern, multi-element sequence matching.

Examples

>>> patterns = {(1, 2, 3): 0, (4, 5, 6) : 1}  # In this dict, key -> pattern, value -> index
>>> automation = PatternPiece(patterns)
match(encoded, num_workers=-1)[source]#

Search for the added patterns in the given sequences.

Parameters:
  • encoded (List[List[int]]) – The encoded sequences to search patterns in.

  • num_workers (int, optional, default: -1) – The number of worker processes to use. If -1, all available CPUs are used. If greater than the number of available CPUs, an exception is raised.

Returns:

A list of found patterns with their positions and indexes (Index, start, end).

Return type:

list[list[tuple[int, int, int]]]

Examples

>>> patterns = {(1, 40, 500): 6}
>>> automation = PatternPiece(patterns)
>>> sequences = [[(1, 10, 100), (4, 40, 400), (5, 50, 500)]]
>>> results = automation.match(sequences)
>>> # results = [[(6, 0, 3)]]
Raises:
  • Exception – If num_workers is not an integer, or exceeds the maximum number of available CPUs.

  • ValueError – If the automaton has not been built.