Skip to content

Commit adfe946

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 9992249 commit adfe946

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

strings/commentz_walter.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
2-
This is a pure Python implementation
2+
This is a pure Python implementation
33
of the Commentz-Walter algorithm
44
for searching multiple patterns in a single text.
55
6-
The algorithm combines Boyer-Moore's and
6+
The algorithm combines Boyer-Moore's and
77
Aho-Corasick's techniques for
8-
efficiently searching multiple patterns
8+
efficiently searching multiple patterns
99
by using pattern shifts and suffix automata.
1010
1111
For doctests run:
@@ -18,39 +18,43 @@
1818

1919
from typing import List, Dict, Set, Tuple
2020
from collections import defaultdict
21+
22+
2123
class CommentzWalter:
2224
"""
23-
Class to represent the Commentz-Walter algorithm
25+
Class to represent the Commentz-Walter algorithm
2426
for multi-pattern string searching.
2527
2628
Attributes:
2729
patterns (List[str]): List of patterns to search for.
2830
alphabet (Set[str]): Unique characters in the patterns.
29-
shift_table (Dict[str, int]): Table to store
31+
shift_table (Dict[str, int]): Table to store
3032
the shift values for characters.
31-
automaton (Dict[int, Dict[str, int]]):
33+
automaton (Dict[int, Dict[str, int]]):
3234
Automaton used for state transitions.
3335
3436
Methods:
35-
preprocess(): Builds the shift table
37+
preprocess(): Builds the shift table
3638
and automaton for pattern matching.
37-
search(text: str) -> List[Tuple[int, str]]:
39+
search(text: str) -> List[Tuple[int, str]]:
3840
Searches patterns in the given text.
3941
4042
Examples:
4143
>>> cw = CommentzWalter(["he", "she", "his", "hers"])
4244
>>> cw.search("ahishers")
4345
[(1, 'his'), (4, 'she'), (5, 'hers')]
4446
"""
47+
4548
def __init__(self, patterns: List[str]) -> None:
4649
self.patterns = patterns
4750
self.alphabet: Set[str] = set("".join(patterns))
4851
self.shift_table: Dict[str, int] = {}
4952
self.automaton: Dict[int, Dict[str, int]] = {}
5053
self.preprocess()
54+
5155
def preprocess(self) -> None:
5256
"""
53-
Builds the shift table and automaton required
57+
Builds the shift table and automaton required
5458
for the Commentz-Walter algorithm.
5559
"""
5660
# Build the shift table for the rightmost occurrence of characters in patterns
@@ -102,14 +106,17 @@ def search(self, text: str) -> List[Tuple[int, str]]:
102106
break
103107
else:
104108
for pattern in self.patterns:
105-
if text[i:i + len(pattern)] == pattern:
109+
if text[i : i + len(pattern)] == pattern:
106110
results.append((i, pattern))
107111
i += self.shift_table.get(text[i + m - 1], m)
108112
else:
109113
i += self.shift_table.get(text[i + j], m)
110114
return results
115+
116+
111117
if __name__ == "__main__":
112118
import doctest
119+
113120
doctest.testmod()
114121
# Example usage for manual testing
115122
patterns = ["abc", "bcd", "cde"]

0 commit comments

Comments
 (0)