Spaces:
Runtime error
Runtime error
import difflib | |
from fuzzywuzzy import fuzz | |
# Custom phonetic matching function | |
def phonetic_match(word1, word2): | |
""" | |
Compares two words based on their phonetic similarity. | |
""" | |
return fuzz.ratio(word1, word2) | |
# Custom sequence matching function | |
def sequence_match(a, b): | |
""" | |
Uses sequence matching to compare two sequences of words. | |
""" | |
return difflib.SequenceMatcher(None, a, b).ratio() | |
# Main function to compare texts with percentage match | |
def compare_texts(text1, text2): | |
""" | |
Compares two texts using phonetic matching and sequence matching, | |
returning a percentage match score. | |
""" | |
words1 = text1.lower().split() | |
words2 = text2.lower().split() | |
total_matches = len(words1) | |
mismatches = 0 | |
for word1, word2 in zip(words1, words2): | |
if word1 != word2: | |
mismatches += 1 | |
if phonetic_match(word1, word2) < 80: | |
# Use sequence matching only if phonetic is low | |
if sequence_match(word1, word2) < 0.8: | |
mismatches += 1 # Penalty for bad sequence match | |
accuracy = 1 - (mismatches / total_matches) | |
return accuracy * 100 # Convert to percentage | |
def match(original, transcription): | |
return compare_texts(original, transcription) | |