-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add wildcard pattern matching algorithm using FFT #12014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
OnePunchMonk
wants to merge
10
commits into
TheAlgorithms:master
from
OnePunchMonk:add-wildcard-pattern-matching-fft
Closed
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e7c0701
Add wildcard pattern matching algorithm using FFT
OnePunchMonk 276f46a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9254927
Update wildcard pattern matching algorithm with type hints and doctests
OnePunchMonk d0aa99b
Update wildcard pattern matching algorithm with type hints and doctests
OnePunchMonk 56441f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ccb29d2
Fix style issues and update variable names
OnePunchMonk 1da2f1f
Resolved merge conflict in wildcard_pattern_matching_fft.py
OnePunchMonk 9d6751c
Fixed Issues
OnePunchMonk f4efe93
Resolved merge conflict in wildcard_pattern_matching_fft.py
OnePunchMonk 756c0ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import numpy as np | ||
from numpy.fft import fft, ifft | ||
|
||
def preprocess_text_and_pattern(text, pattern): | ||
"""Preprocesses text and pattern for pattern matching. | ||
|
||
Args: | ||
text: The input text string. | ||
pattern: The input pattern string, potentially containing wildcards ('*'). | ||
|
||
Returns: | ||
A tuple containing: | ||
- A list of integers representing the text characters. | ||
- A list of integers representing the pattern characters, with 0 for wildcards. | ||
""" | ||
|
||
unique_chars = set(text + pattern) | ||
char_to_int = {char: i + 1 for i, char in enumerate(unique_chars)} # Unique non-zero integers | ||
|
||
# Replace pattern '*' with 0, other characters with their unique integers | ||
pattern_int = [char_to_int[char] if char != '*' else 0 for char in pattern] | ||
text_int = [char_to_int[char] for char in text] | ||
|
||
return text_int, pattern_int | ||
|
||
def fft_convolution(a, b): | ||
OnePunchMonk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"""Performs convolution using the Fast Fourier Transform (FFT). | ||
|
||
Args: | ||
a: The first sequence. | ||
b: The second sequence. | ||
|
||
Returns: | ||
The convolution of the two sequences. | ||
""" | ||
|
||
n = len(a) + len(b) - 1 | ||
A = fft(a, n) | ||
B = fft(b, n) | ||
return np.real(ifft(A * B)) | ||
|
||
def compute_A_fft(text_int, pattern_int): | ||
OnePunchMonk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"""Computes the A array for the pattern matching algorithm. | ||
|
||
Args: | ||
text_int: The integer representation of the text. | ||
pattern_int: The integer representation of the pattern. | ||
|
||
Returns: | ||
The A array. | ||
""" | ||
|
||
n = len(text_int) | ||
m = len(pattern_int) | ||
|
||
# Power transforms of the pattern and text based on the formula | ||
p1 = np.array(pattern_int) | ||
p2 = np.array([p**2 for p in pattern_int]) | ||
p3 = np.array([p**3 for p in pattern_int]) | ||
|
||
t1 = np.array(text_int) | ||
t2 = np.array([t**2 for t in text_int]) | ||
t3 = np.array([t**3 for t in text_int]) | ||
|
||
# Convolution to calculate the terms for A[i] | ||
sum1 = fft_convolution(p3[::-1], t1) | ||
sum2 = fft_convolution(p2[::-1], t2) | ||
sum3 = fft_convolution(p1[::-1], t3) | ||
|
||
# Calculate A[i] using the convolution results | ||
A = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] | ||
|
||
return A | ||
|
||
# Main function to run the matching | ||
if __name__ == "__main__": | ||
|
||
import doctest | ||
doctest.testmod() | ||
# Get text and pattern as input from the user | ||
# text = input("Enter the text: ") | ||
# pattern = input("Enter the pattern (use '*' for wildcard): ") | ||
|
||
text = "abcabc" | ||
pattern = "abc*" | ||
|
||
|
||
|
||
|
||
text_int, pattern_int = preprocess_text_and_pattern(text, pattern) | ||
A = compute_A_fft(text_int, pattern_int) | ||
|
||
# Matches occur where A[i] == 0 | ||
matches = [i for i in range(len(A)) if np.isclose(A[i], 0)] | ||
print("Pattern matches at indices:", matches) | ||
|
||
Check failure on line 96 in strings/wildcard_pattern_matching_fft.py
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.