~linuxgoose/linguistics-robin

ref: 59cf63802579dea3f5452fd56a5ac61564297949 linguistics-robin/linguistics_robin/phonetics/phonetic_algorithm.py -rw-r--r-- 1.1 KiB
59cf6380 — Jordan Update __init__.py 8 months ago
                                                                                
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
from ..distance_metrics import levenshtein_distance, hamming_distance
from ..exceptions import DistanceMetricError


class PhoneticAlgorithm:
    """
    The main Phonetic Algorithm class, to ensure a unified API
    for all the included algorithms.
    """
    def __init__(self):
        self.distances = {
            'levenshtein': levenshtein_distance,
            'hamming': hamming_distance,
        }

    def phonetics(self, word):
        """Get the phonetic representation of the word."""
        pass

    def sounds_like(self, word1, word2):
        """Compare the phonetic representations of 2 words, and return a boolean value."""
        return self.phonetics(word1) == self.phonetics(word2)

    def distance(self, word1, word2, metric='levenshtein'):
        """Get the similarity of the words, using the supported distance metrics."""
        if metric in self.distances:
            distance_func = self.distances[metric]
            return distance_func(self.phonetics(word1), self.phonetics(word2))
        else:
            raise DistanceMetricError('Distance metric not supported! Choose from levenshtein, hamming.')