Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update _tfidf.py to allow for sentence-level TFIDF ngram-matching #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions polyfuzz/models/_tfidf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def __init__(self,
min_similarity: float = 0.75,
top_n: int = 1,
cosine_method: str = "sparse",
model_id: str = None):
model_id: str = None,
use_word_grams: bool = False):
super().__init__(model_id)
self.type = "TF-IDF"
self.n_gram_range = n_gram_range
Expand All @@ -61,6 +62,7 @@ def __init__(self,
self.top_n = top_n
self.vectorizer = None
self.tf_idf_to = None
self.use_word_grams = use_word_grams

def match(self,
from_list: List[str],
Expand Down Expand Up @@ -125,10 +127,17 @@ def _create_ngrams(self, string: str) -> List[str]:
string = _clean_string(string)

result = []
for n in range(self.n_gram_range[0], self.n_gram_range[1]+1):
ngrams = zip(*[string[i:] for i in range(n)])
ngrams = [''.join(ngram) for ngram in ngrams if ' ' not in ngram]
result.extend(ngrams)
if self.use_word_grams:
tokens = [token for token in string.split(" ") if token != ""]
for n in range(self.n_gram_range[0], self.n_gram_range[1]+1):
ngrams = zip(*[tokens[i:] for i in range(n)])
ngrams = [''.join(ngram) for ngram in ngrams if ' ' not in ngram]
result.extend(ngrams)
else:
for n in range(self.n_gram_range[0], self.n_gram_range[1]+1):
ngrams = zip(*[string[i:] for i in range(n)])
ngrams = [''.join(ngram) for ngram in ngrams if ' ' not in ngram]
result.extend(ngrams)

return result

Expand Down