1
1
import numpy as np
2
2
from numpy .fft import fft , ifft
3
3
4
+
4
5
def preprocess_text_and_pattern (text , pattern ):
5
6
"""Preprocesses text and pattern for pattern matching.
6
7
@@ -15,14 +16,17 @@ def preprocess_text_and_pattern(text, pattern):
15
16
"""
16
17
17
18
unique_chars = set (text + pattern )
18
- char_to_int = {char : i + 1 for i , char in enumerate (unique_chars )} # Unique non-zero integers
19
+ char_to_int = {
20
+ char : i + 1 for i , char in enumerate (unique_chars )
21
+ } # Unique non-zero integers
19
22
20
23
# Replace pattern '*' with 0, other characters with their unique integers
21
- pattern_int = [char_to_int [char ] if char != '*' else 0 for char in pattern ]
24
+ pattern_int = [char_to_int [char ] if char != "*" else 0 for char in pattern ]
22
25
text_int = [char_to_int [char ] for char in text ]
23
26
24
27
return text_int , pattern_int
25
28
29
+
26
30
def fft_convolution (a , b ):
27
31
"""Performs convolution using the Fast Fourier Transform (FFT).
28
32
@@ -39,6 +43,7 @@ def fft_convolution(a, b):
39
43
B = fft (b , n )
40
44
return np .real (ifft (A * B ))
41
45
46
+
42
47
def compute_A_fft (text_int , pattern_int ):
43
48
"""Computes the A array for the pattern matching algorithm.
44
49
@@ -68,14 +73,15 @@ def compute_A_fft(text_int, pattern_int):
68
73
sum3 = fft_convolution (p1 [::- 1 ], t3 )
69
74
70
75
# Calculate A[i] using the convolution results
71
- A = sum1 [:n - m + 1 ] - 2 * sum2 [:n - m + 1 ] + sum3 [:n - m + 1 ]
76
+ A = sum1 [: n - m + 1 ] - 2 * sum2 [: n - m + 1 ] + sum3 [: n - m + 1 ]
72
77
73
78
return A
74
79
80
+
75
81
# Main function to run the matching
76
82
if __name__ == "__main__" :
77
-
78
83
import doctest
84
+
79
85
doctest .testmod ()
80
86
# Get text and pattern as input from the user
81
87
# text = input("Enter the text: ")
@@ -84,13 +90,9 @@ def compute_A_fft(text_int, pattern_int):
84
90
text = "abcabc"
85
91
pattern = "abc*"
86
92
87
-
88
-
89
-
90
93
text_int , pattern_int = preprocess_text_and_pattern (text , pattern )
91
94
A = compute_A_fft (text_int , pattern_int )
92
95
93
96
# Matches occur where A[i] == 0
94
97
matches = [i for i in range (len (A )) if np .isclose (A [i ], 0 )]
95
98
print ("Pattern matches at indices:" , matches )
96
-
0 commit comments