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

[FIX] Normalize - fix unpickling for Normalizers before caching was implemented #838

Merged
merged 1 commit into from
Jun 3, 2022
Merged
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
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
recursive-include orangecontrib/text/datasets *.tab *.txt
recursive-include orangecontrib/text/models *.ftz
recursive-include orangecontrib/text/sentiment *.txt
recursive-include orangecontrib/text/tests *.txt *.json
recursive-include orangecontrib/text/tests *.txt *.json *.pkl
recursive-include orangecontrib/text/tutorials *.ows
recursive-include orangecontrib/text/widgets/icons *.svg *.png *.ai
recursive-include orangecontrib/text/widgets/resources *.js *.css *.html
Expand Down
8 changes: 7 additions & 1 deletion orangecontrib/text/preprocess/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def __getstate__(self):
d["_normalization_cache"] = {}
return d

def __setstate__(self, state):
self.__dict__.update(state)
# support old pickles (before caching was implemented) that are missing
# _normalization_cache
self._normalization_cache = {}


class WordNetLemmatizer(BaseNormalizer):
name = 'WordNet Lemmatizer'
Expand Down Expand Up @@ -201,7 +207,7 @@ def __setstate__(self, state):

Note: __model will be loaded on __call__
"""
self.__dict__.update(state)
super().__setstate__(state)
self.models = UDPipeModels()


Expand Down
Binary file added orangecontrib/text/tests/normalizer-v1.pkl
Binary file not shown.
10 changes: 10 additions & 0 deletions orangecontrib/text/tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ def test_cache(self):
loaded_normalizer = pickle.loads(pickle.dumps(normalizer))
self.assertEqual(0, len(loaded_normalizer._normalization_cache))

def test_nocache_normalizer_restorable(self):
"""
Pickled normalizers made before implementing cache in normalizer must
load correctly
"""
test_folder = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(test_folder, "normalizer-v1.pkl"), "rb") as f:
loaded_normalizer = pickle.load(f)
loaded_normalizer(self.corpus)


class UDPipeModelsTests(unittest.TestCase):
def test_label_transform(self):
Expand Down