File size: 1,289 Bytes
c3c3a5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)