python-crfsuite icon indicating copy to clipboard operation
python-crfsuite copied to clipboard

Cannot pickle a Tagger object

Open wboag opened this issue 11 years ago • 1 comments

I'm unable to pickle the Tagger object. I can follow the example code given at http://nbviewer.ipython.org/github/tpeng/python-crfsuite/blob/master/examples/CoNLL%202002.ipynb, but when I try to pickle the Tagger I get the following error.

Traceback (most recent call last): File "pcrf.py", line 127, in pickle.dump(tagger,m) File "/data1/wboag/clicon/venv_clicon/lib/python2.7/copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.name TypeError: can't pickle Tagger objects

wboag avatar Aug 07 '14 05:08 wboag

Hi @wboag,

Yes, it is an important issue, but I'm not sure there is a single best solution.

Tagger requires an on-disk file to work, so to pickle Tagger so that it just works you need to pickle the contents of this file and re-create it (in a temporary place?). To do that you can use this module: https://github.com/scrapinghub/webstruct/blob/master/webstruct/_fileresource.py; an example of how to use it can be found here: https://github.com/scrapinghub/webstruct/blob/master/webstruct/crfsuite.py.

If you are OK with copying the model file yourselves then you can pickle a filename instead of pickling Tagger object, and re-create Tagger on unpickling. It can be more efficient because you'll end with a single copy of the model file.

Sorry for the delay, I thought I've already answered you when the question was posted, but apparently I didn't click "Comment" button and closed the browser tab.

kmike avatar Sep 01 '14 01:09 kmike