-
Notifications
You must be signed in to change notification settings - Fork 94
/
fuzzy_text_similarity_transformers.py
77 lines (53 loc) · 2.19 KB
/
fuzzy_text_similarity_transformers.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Row-by-row similarity between two text columns based on FuzzyWuzzy"""
# https://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/
# https://github.com/seatgeek/fuzzywuzzy
from h2oaicore.transformer_utils import CustomTransformer
import datatable as dt
import numpy as np
_global_modules_needed_by_name = ['nltk==3.8.1']
import nltk
class FuzzyBaseTransformer:
_modules_needed_by_name = ['fuzzywuzzy==0.18.0']
_method = NotImplemented
_parallel_task = False
_testing_can_skip_failure = False # ensure tested as if shouldn't fail
@staticmethod
def get_default_properties():
return dict(col_type="text", min_cols=2, max_cols=2, relative_importance=1)
def fit_transform(self, X: dt.Frame, y: np.array = None):
return self.transform(X)
def transform(self, X: dt.Frame):
from fuzzywuzzy import fuzz
method = getattr(fuzz, self.__class__._method)
output = []
X = X.to_pandas()
text1_arr = X.iloc[:, 0].values
text2_arr = X.iloc[:, 1].values
for ind, text1 in enumerate(text1_arr):
try:
text1 = str(text1).lower().split()
text2 = text2_arr[ind]
text2 = str(text2).lower().split()
ratio = method(text1, text2)
output.append(ratio)
except:
output.append(-1)
return np.array(output)
class FuzzyQRatioTransformer(FuzzyBaseTransformer, CustomTransformer):
_unsupervised = True
_method = "QRatio"
class FuzzyWRatioTransformer(FuzzyBaseTransformer, CustomTransformer):
_unsupervised = True
_method = "WRatio"
class FuzzyPartialRatioTransformer(FuzzyBaseTransformer, CustomTransformer):
_unsupervised = True
_method = "partial_ratio"
class FuzzyTokenSetRatioTransformer(FuzzyBaseTransformer, CustomTransformer):
_unsupervised = True
_method = "token_set_ratio"
class FuzzyTokenSortRatioTransformer(FuzzyBaseTransformer, CustomTransformer):
_unsupervised = True
_method = "token_sort_ratio"
class FuzzyPartialTokenSortRatioTransformer(FuzzyBaseTransformer, CustomTransformer):
_unsupervised = True
_method = "partial_token_sort_ratio"