|
| 1 | +""" |
| 2 | +This code was based on py_stringmatching: https://github.com/anhaidgroup/py_stringmatching |
| 3 | +""" |
| 4 | +from abc import ABC, abstractmethod |
| 5 | +from stringcompare import Jaro |
| 6 | +import re |
| 7 | + |
| 8 | + |
| 9 | +class WhitespaceTokenizer(ABC): |
| 10 | + def tokenize(self, sentence): |
| 11 | + whitespace_pattern = re.compile(r'\s+') |
| 12 | + tokens = whitespace_pattern.split(sentence.strip()) |
| 13 | + tokens = [token for token in tokens if token] |
| 14 | + return tokens |
| 15 | + |
| 16 | +class StringMatcher(ABC): |
| 17 | + """String Matchers based on py_stringmatching""" |
| 18 | + flag = True |
| 19 | + |
| 20 | + |
| 21 | + def check_instance_type(self, te1, te2) -> None: |
| 22 | + if not isinstance(te1, list) and not isinstance(te1, set): |
| 23 | + raise TypeError("Must be either list or set") |
| 24 | + if not isinstance(te2, list) and not isinstance(te2, set): |
| 25 | + raise TypeError("Must be either list or set") |
| 26 | + |
| 27 | + def exact_match(self, te1, te2): |
| 28 | + return te1 == te2 |
| 29 | + |
| 30 | + def empty_match(self, te1, te2): |
| 31 | + return len(te1) == 0 or len(te2) == 0 |
| 32 | + |
| 33 | + |
| 34 | + @abstractmethod |
| 35 | + def compare(self, te1, te2): |
| 36 | + pass |
| 37 | + |
| 38 | + |
| 39 | +class Cosine(StringMatcher): |
| 40 | + def compare(self, te1, te2) -> float: |
| 41 | + |
| 42 | + self.check_instance_type(te1, te2) |
| 43 | + |
| 44 | + # if exact match return 1.0 |
| 45 | + if self.exact_match(te1, te2): |
| 46 | + return 1.0 |
| 47 | + |
| 48 | + # if one of the strings is empty return 0 |
| 49 | + if self.empty_match(te1, te2): |
| 50 | + return 0.0 |
| 51 | + |
| 52 | + intersection = len(set(te1) & set(te2)) |
| 53 | + norm1 = len(te1) ** 0.5 |
| 54 | + norm2 = len(te2) ** 0.5 |
| 55 | + return intersection / (norm1 * norm2) if norm1 * norm2 > 0 else 0 |
| 56 | + |
| 57 | +class Dice(StringMatcher): |
| 58 | + def compare(self, te1, te2) -> float: |
| 59 | + print("TIFASIII??") |
| 60 | + self.check_instance_type(te1, te2) |
| 61 | + |
| 62 | + set1 = set(te1) |
| 63 | + set2 = set(te2) |
| 64 | + |
| 65 | + # if exact match return 1.0 |
| 66 | + if self.exact_match(set1, set2): |
| 67 | + return 1.0 |
| 68 | + |
| 69 | + # if one of the strings is empty return 0 |
| 70 | + if self.empty_match(set1, set2): |
| 71 | + return 0.0 |
| 72 | + |
| 73 | + return 2.0 * float(len(set1 & set2)) / float(len(set1) + len(set2)) |
| 74 | + |
| 75 | +class Jaccard(StringMatcher): |
| 76 | + def compare(self, te1, te2) -> float: |
| 77 | + self.check_instance_type(te1, te2) |
| 78 | + |
| 79 | + set1 = set(te1) |
| 80 | + set2 = set(te2) |
| 81 | + |
| 82 | + # if exact match return 1.0 |
| 83 | + if self.exact_match(set1, set2): |
| 84 | + return 1.0 |
| 85 | + |
| 86 | + # if one of the strings is empty return 0 |
| 87 | + if self.empty_match(set1, set2): |
| 88 | + return 0.0 |
| 89 | + |
| 90 | + intersection = len(set(te1) & set(te2)) |
| 91 | + return intersection/(len(set1) + len(set2) + intersection) |
| 92 | + |
| 93 | +class GeneralizedJaccard(StringMatcher): |
| 94 | + def compare(self, te1, te2) -> float: |
| 95 | + self.check_instance_type(te1, te2) |
| 96 | + |
| 97 | + set1 = set(te1) |
| 98 | + set2 = set(te2) |
| 99 | + |
| 100 | + # if exact match return 1.0 |
| 101 | + if self.exact_match(set1, set2): |
| 102 | + return 1.0 |
| 103 | + |
| 104 | + # if one of the strings is empty return 0 |
| 105 | + if self.empty_match(set1, set2): |
| 106 | + return 0.0 |
| 107 | + |
| 108 | + set1_x = set() |
| 109 | + set2_y = set() |
| 110 | + match_score = 0.0 |
| 111 | + match_count = 0 |
| 112 | + list_matches = [] |
| 113 | + threshold=0.5 |
| 114 | + for element in set1: |
| 115 | + for item in set2: |
| 116 | + score = Jaro().compare(element, item) |
| 117 | + if score > 1 or score < 0: |
| 118 | + raise ValueError('Similarity measure should' + \ |
| 119 | + ' return value in the range [0,1]') |
| 120 | + if score > threshold: |
| 121 | + list_matches.append((element, item, score)) |
| 122 | + |
| 123 | + # position of first string, second string and sim score in tuple |
| 124 | + first_string_pos = 0 |
| 125 | + second_string_pos = 1 |
| 126 | + sim_score_pos = 2 |
| 127 | + |
| 128 | + # sort the score of all the pairs |
| 129 | + list_matches.sort(key=lambda x: x[sim_score_pos], reverse=True) |
| 130 | + |
| 131 | + # select score in increasing order of their weightage, |
| 132 | + # do not reselect the same element from either set. |
| 133 | + for element in list_matches: |
| 134 | + if (element[first_string_pos] not in set1_x and |
| 135 | + element[second_string_pos] not in set2_y): |
| 136 | + set1_x.add(element[first_string_pos]) |
| 137 | + set2_y.add(element[second_string_pos]) |
| 138 | + match_score += element[sim_score_pos] |
| 139 | + match_count += 1 |
| 140 | + |
| 141 | + return float(match_score) / float(len(set1) + len(set2) - match_count) |
| 142 | + |
| 143 | +class OverlapCoefficient(StringMatcher): |
| 144 | + def compare(self, te1, te2) -> float: |
| 145 | + self.check_instance_type(te1, te2) |
| 146 | + |
| 147 | + set1 = set(te1) |
| 148 | + set2 = set(te2) |
| 149 | + |
| 150 | + # if exact match return 1.0 |
| 151 | + if self.exact_match(set1, set2): |
| 152 | + return 1.0 |
| 153 | + |
| 154 | + # if one of the strings is empty return 0 |
| 155 | + if self.empty_match(set1, set2): |
| 156 | + return 0.0 |
| 157 | + return float(len(set1 & set2)) / min(len(set1), len(set2)) |
| 158 | + |
0 commit comments