तत्काल जवाब यह अचार के लिए, https://wiki.python.org/moin/UsingPickle
देखना है लेकिन क्योंकि IBMModel1 एक लैम्ब्डा फ़ंक्शन, यह डिफ़ॉल्ट रूप से यह अचार को pickle
/cPickle
हम (https://github.com/nltk/nltk/blob/develop/nltk/align/ibm1.py#L74 और https://github.com/nltk/nltk/blob/develop/nltk/align/ibm1.py#L104 देखें) तो संभव नहीं है dill
का उपयोग करेंगे। सबसे पहले, dill
इंस्टॉल करें देखें Can Python pickle lambda functions?
$ pip install dill
$ python
>>> import dill as pickle
फिर
:
>>> import dill
>>> import dill as pickle
>>> from nltk.corpus import comtrans
>>> from nltk.align import IBMModel1
>>> bitexts = comtrans.aligned_sents()[:100]
>>> ibm = IBMModel1(bitexts, 20)
>>> with open('model1.pk', 'wb') as fout:
... pickle.dump(ibm, fout)
...
>>> exit()
मसालेदार मॉडल का उपयोग करने के लिए:
>>> import dill as pickle
>>> from nltk.corpus import comtrans
>>> bitexts = comtrans.aligned_sents()[:100]
>>> with open('model1.pk', 'rb') as fin:
... ibm = pickle.load(fin)
...
>>> aligned_sent = ibm.align(bitexts[0])
>>> aligned_sent.words
['Wiederaufnahme', 'der', 'Sitzungsperiode']
आप IBMModel1
वस्तु है, जो एक है अचार की कोशिश करते हैं लैम्ब्डा फ़ंक्शन, आप इसके साथ समाप्त हो जाएंगे:
>>> import cPickle as pickle
>>> from nltk.corpus import comtrans
>>> from nltk.align import IBMModel1
>>> bitexts = comtrans.aligned_sents()[:100]
>>> ibm = IBMModel1(bitexts, 20)
>>> with open('model1.pk', 'wb') as fout:
... pickle.dump(ibm, fout)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle function objects
(नोट: उपरोक्त कोड स्निपेट 3.0.0 NLTK संस्करण से आता है):
python3 में NLTK 3.0.0 के साथ, आप भी एक ही समस्या है क्योंकि IBMModel1 एक लैम्ब्डा फ़ंक्शन का सामना करना पड़ेगा
[email protected]:~$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> from nltk.corpus import comtrans
>>> from nltk.align import IBMModel1
>>> bitexts = comtrans.aligned_sents()[:100]
>>> ibm = IBMModel1(bitexts, 20)
>>> with open('mode1.pk', 'wb') as fout:
... pickle.dump(ibm, fout)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
_pickle.PicklingError: Can't pickle <function IBMModel1.train.<locals>.<lambda> at 0x7fa37cf9d620>: attribute lookup <lambda> on nltk.align.ibm1 failed'
>>> import dill
>>> with open('model1.pk', 'wb') as fout:
... dill.dump(ibm, fout)
...
>>> exit()
[email protected]:~$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> from nltk.corpus import comtrans
>>> with open('model1.pk', 'rb') as fin:
... ibm = dill.load(fin)
...
>>> bitexts = comtrans.aligned_sents()[:100]
>>> aligned_sent = ibm.aligned(bitexts[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'IBMModel1' object has no attribute 'aligned'
>>> aligned_sent = ibm.align(bitexts[0])
>>> aligned_sent.words
['Wiederaufnahme', 'der', 'Sitzungsperiode']
(नोट: python3 में, pickle
cPickle
है, http://docs.pythonsprints.com/python3_porting/py-porting.html देखें)
मैं मुझे यकीन नहीं है कि आपने क्या प्रयास किया है, लेकिन मैंने कोई भेड़िया नहीं देखा और वेनिला अचार के साथ "मॉडल" को चुनने में कोई समस्या नहीं थी। – alexis
@alexis यह दिलचस्प है, क्या आपको अद्यतन उत्तर के समान त्रुटि मिली? – alvas
अभी तक इसे आजमाने का मौका नहीं मिला है; लेकिन मैंने पाइथन 2 के साथ पिकलिंग का परीक्षण किया होगा, जो विभिन्न अनुभवों को समझाएगा (मुझे अभी तक एहसास नहीं हुआ कि मॉड्यूल इतना बदल गया है)। जब मैं कोशिश करता हूं तो मैं आपको बता दूंगा। – alexis