forked from cjardin/spring_2023_sms_fun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_matcher.py
26 lines (18 loc) · 966 Bytes
/
pattern_matcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from difflib import SequenceMatcher
#patterns = ["A B C", "A S C D A", "C JA FS AQ S", " ASR AS ASW ASQ"]
def pattern_match (given_pattern):
#patterns = dictionary
highest_match = 0; #Starting percent
closest_pattern = 0; #Saving the position of the pattern
for index, pattern in enumerate(patterns): #Walk through array
match_percent = SequenceMatcher(None, given_pattern, pattern).ratio()
if (match_percent == 1):
return index #Found the pattern
elif (match_percent > highest_match):
highest_match = match_percent #New closest match
closest_pattern = index #Closest pattern
if (match_percent < 1): #Pattern not found
patterns.append(given_pattern) #Append the new pattern
return (patterns[closest_pattern]) #Return the closest matching pattern
else: #Pattern found
return given_pattern #Return the pattern