Skip to content

raise StringLengthException if vectoriser is applied to strings that … #67

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions string_grouper/string_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,20 @@ def wrapper(*args, **kwargs):
return wrapper


class StringGrouperNotFitException(Exception):
class Error(Exception):
pass


class StringGrouperNotFitException(Error):
"""Raised when one of the public functions is called which requires the StringGrouper to be fit first"""
pass


class StringLengthException(Error):
"""Raised when vectoriser is fit on strings that are not of length greater or equal to ngram size"""
pass


class StringGrouper(object):
def __init__(self, master: pd.Series,
duplicates: Optional[pd.Series] = None,
Expand Down Expand Up @@ -450,7 +459,10 @@ def _fit_vectorizer(self) -> TfidfVectorizer:
strings = pd.concat([self._master, self._duplicates])
else:
strings = self._master
self._vectorizer.fit(strings)
try:
self._vectorizer.fit(strings)
except ValueError:
raise StringLengthException('None of input string lengths are greater than or equal to n_gram length')
return self._vectorizer

def _build_matches(self, master_matrix: csr_matrix, duplicate_matrix: csr_matrix) -> csr_matrix:
Expand Down
7 changes: 6 additions & 1 deletion string_grouper/test/test_string_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
DEFAULT_REGEX, DEFAULT_NGRAM_SIZE, DEFAULT_N_PROCESSES, DEFAULT_IGNORE_CASE, \
StringGrouperConfig, StringGrouper, StringGrouperNotFitException, \
match_most_similar, group_similar_strings, match_strings, \
compute_pairwise_similarities
compute_pairwise_similarities, StringLengthException
from unittest.mock import patch


Expand Down Expand Up @@ -822,6 +822,11 @@ def test_prior_matches_added(self):
# All strings should now match to the same "master" string
self.assertEqual(1, len(df.deduped.unique()))

def test_group_similar_strings_stopwords(self):
"""StringGrouper shouldn't raise a ValueError if all strings are shorter than 3 characters"""
with self.assertRaises(StringLengthException):
StringGrouper(pd.Series(['zz', 'yy', 'xx,'])).fit()


if __name__ == '__main__':
unittest.main()