From 0b678f3c041b5ebf4d3f6b1e7693040cce6fd7cd Mon Sep 17 00:00:00 2001 From: Alexey Sukharevich Date: Fri, 5 Jul 2019 18:58:27 +0300 Subject: [PATCH] udpipe: fix pickling udpipe model --- orangecontrib/text/preprocess/normalize.py | 7 +++++++ orangecontrib/text/tests/test_preprocess.py | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/orangecontrib/text/preprocess/normalize.py b/orangecontrib/text/preprocess/normalize.py index f275dac37..d2da940b0 100644 --- a/orangecontrib/text/preprocess/normalize.py +++ b/orangecontrib/text/preprocess/normalize.py @@ -188,3 +188,10 @@ def language(self): def language(self, value): self._language = value self.model = None + + def __getstate__(self): + return {'language': self.language} + + def __setstate__(self, state): + self.__init__(state['language']) + diff --git a/orangecontrib/text/tests/test_preprocess.py b/orangecontrib/text/tests/test_preprocess.py index 39b4842c6..e2cc7f943 100644 --- a/orangecontrib/text/tests/test_preprocess.py +++ b/orangecontrib/text/tests/test_preprocess.py @@ -1,3 +1,4 @@ +import pickle import tempfile import unittest import os.path @@ -187,6 +188,15 @@ def test_udpipe_doc(self): self.assertListEqual(normalizer.normalize_doc('Gori na gori hiša gori'), ['gora', 'na', 'gora', 'hiša', 'goreti']) + def test_udpipe_pickle(self): + normalizer = preprocess.UDPipeLemmatizer() + normalizer.language = 'English' + + loaded = pickle.loads(pickle.dumps(normalizer)) + self.assertEqual(normalizer.language, loaded.language) + self.assertEqual(loaded.normalize_doc('peter piper pickled'), + ['peter', 'piper', 'pickle']) + def test_porter_with_bad_input(self): stemmer = preprocess.PorterStemmer() self.assertRaises(TypeError, stemmer, 10)