0

I'm trying to train my unigram tagger in nltk for hindi

#-*- coding : utf-8 -*-
from nltk.corpus import indian
from nltk import tag
from nltk import UnigramTagger
from nltk import DefaultTagger
t0=DefaultTagger('NN')
hpos=indian.tagged_sents('hindi.pos')
h_sen=indian.sents('hindi.pos')
unigram_tagger = UnigramTagger(train=hpos,model=None,backoff=None,
                               cutoff=0,verbose=False)
a='अच्छे स्वास्थ्य के लिए एक परिसंपत्ति है. यह मानव जीवन में सबसे बहुमूल्य कब्ज़ा है. सच बोलने के लिए, यह पृथ्वी पर सब बातों का सबसे बड़ा धन है. यह अच्छा स्वास्थ्य सभी खुशी का स्रोत है कि कहा जाता है. अच्छे स्वास्थ्य और खुशी कंधे से कंधा मिलाकर चलते हैं. स्वास्थ्य ध्वनि शरीर के राज्य के रूप में अच्छी तरह से मन का रूप है. एक स्वस्थ व्यक्ति के मन की सुदृढ़ता के साथ ही शरीर की सुदृढ़ता के लिए होना चाहिए. स्वस्थ होने के लिए, एक एक संतुलित तरीके से ऊपर हो जाना चाहिए. अच्छा स्वास्थ्य बनाए रखने के लिए, एक, एक संतुलित आहार खाने साफ पानी पीने, ताजा हवा लेते हैं, ठीक से आराम और नियमित रूप से शारीरिक व्यायाम में ले लिया है. मानसिक स्वास्थ्य की अवधारण के लिए, एक के बाद एक की भावनाओं को नियंत्रित करने के लिए है और एक अन्य लोगों की भावनाओं और अधिकार के लिए धैर्य और सम्मान के लिए है. हालांकि, अच्छे स्वास्थ्य के बिना जीवन नीरस और असहनीय है. यह तो अक्सर बीमार स्वास्थ्य रखने के लिए एक व्यक्ति के जीवन के लिए मौत पसंद है सुना है. इसलिए, हर किसी को अपने स्वास्थ्य का ध्यान रखना चाहिए.'
uniggram_tagger.tag(a)

but I keep getting this error

Traceback (most recent call last):
  File "C:\Users\Rajawat\Desktop\try.py", line 9, in <module>
    unigram_tagger = UnigramTagger(train=hpos,model=None,backoff=None,cutoff=0,verbose=False)
  File "C:\Python27\lib\site-packages\nltk\tag\sequential.py", line 317, in __init__
    backoff, cutoff, verbose)
  File "C:\Python27\lib\site-packages\nltk\tag\sequential.py", line 274, in __init__
    self._train(train, cutoff, verbose)
  File "C:\Python27\lib\site-packages\nltk\tag\sequential.py", line 177, in _train
    tokens, tags = zip(*sentence)
ValueError: need more than 0 values to unpack
Tim
  • 41,901
  • 18
  • 127
  • 145
RAJAWAT
  • 41
  • 3

1 Answers1

1

After some debugging, there seems to be an empty sentence in the indian corpus and it causes the script to break since the UnigramTagger reads the corpus by zipping through with zip(*sentence).

Let's go through this slowly:

First read the corpus and see whether it can be zip:

>>> from nltk.corpus import indian
>>> hindi_sents = indian.tagged_sents('hindi.pos')
>>> print zip(*hindi_sents)
[]

Now we see that there is something wrong with the corpus, let's see which sentence is causing the problem:

>>> [i for i,j in enumerate(hindi_sents) if len(j) < 1]
[540]
>>> print len(hindi_sents)
541

Now it seems like the last sentence is an empty sentence, so the following should clear the problem and now it zips through the corpus:

>>> print (zip(*hindi_sents[:-1])

So it seems like you can skip the last empty sentence and just train the tagger as before:

# -*- coding: utf-8 -*-
from nltk.corpus import indian
from nltk import UnigramTagger

hindi_sents = indian.tagged_sents('hindi.pos')[:-1]
# Trains the tagger.
unigram_tagger = UnigramTagger(train=hindi_sents)
# Let's try tagging the first sentence
first_sent = [i for i,j in hindi_sents[0]]
print " ".join(first_sent)
print unigram_tagger.tag(hindi_sents)

[out]:

पूर्ण प्रतिबंध हटाओ : इराक
[(u'\u092a\u0942\u0930\u094d\u0923', u'JJ'), (u'\u092a\u094d\u0930\u0924\u093f\u092c\u0902\u0927', u'NN'), (u'\u0939\u091f\u093e\u0913', u'VFM'), (u':', u'SYM'), (u'\u0907\u0930\u093e\u0915', u'NNP')]

But the corpus is small and so it doesn't scale to any word that isn't seen in the corpus. That results in several None tags:

# -*- coding: utf-8 -*-
from nltk.corpus import indian
from nltk import UnigramTagger

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

hindi_sents = indian.tagged_sents('hindi.pos')[:-1]
unigram_tagger = UnigramTagger(train=hindi_sents)
first_sent = [i for i,j in hindi_sents[0]]
print " ".join(first_sent)
print unigram_tagger.tag(first_sent)

a=u'अच्छे स्वास्थ्य के लिए एक परिसंपत्ति है. यह मानव जीवन में सबसे बहुमूल्य कब्ज़ा है. सच बोलने के लिए, यह पृथ्वी पर सब बातों का सबसे बड़ा धन है. यह अच्छा स्वास्थ्य सभी खुशी का स्रोत है कि कहा जाता है. अच्छे स्वास्थ्य और खुशी कंधे से कंधा मिलाकर चलते हैं. स्वास्थ्य ध्वनि शरीर के राज्य के रूप में अच्छी तरह से मन का रूप है. एक स्वस्थ व्यक्ति के मन की सुदृढ़ता के साथ ही शरीर की सुदृढ़ता के लिए होना चाहिए. स्वस्थ होने के लिए, एक एक संतुलित तरीके से ऊपर हो जाना चाहिए. अच्छा स्वास्थ्य बनाए रखने के लिए, एक, एक संतुलित आहार खाने साफ पानी पीने, ताजा हवा लेते हैं, ठीक से आराम और नियमित रूप से शारीरिक व्यायाम में ले लिया है. मानसिक स्वास्थ्य की अवधारण के लिए, एक के बाद एक की भावनाओं को नियंत्रित करने के लिए है और एक अन्य लोगों की भावनाओं और अधिकार के लिए धैर्य और सम्मान के लिए है. हालांकि, अच्छे स्वास्थ्य के बिना जीवन नीरस और असहनीय है. यह तो अक्सर बीमार स्वास्थ्य रखने के लिए एक व्यक्ति के जीवन के लिए मौत पसंद है सुना है. इसलिए, हर किसी को अपने स्वास्थ्य का ध्यान रखना चाहिए.'

print unigram_tagger.tag(a.split())

[out]:

[(u'\u0905\u091a\u094d\u091b\u0947', None), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f', u'PREP'), (u'\u090f\u0915', u'QFNUM'), (u'\u092a\u0930\u093f\u0938\u0902\u092a\u0924\u094d\u0924\u093f', None), (u'\u0939\u0948.', None), (u'\u092f\u0939', u'PRP'), (u'\u092e\u093e\u0928\u0935', u'NNC'), (u'\u091c\u0940\u0935\u0928', u'NN'), (u'\u092e\u0947\u0902', u'PREP'), (u'\u0938\u092c\u0938\u0947', u'INTF'), (u'\u092c\u0939\u0941\u092e\u0942\u0932\u094d\u092f', u'JJ'), (u'\u0915\u092c\u094d\u091c\u093c\u093e', None), (u'\u0939\u0948.', None), (u'\u0938\u091a', None), (u'\u092c\u094b\u0932\u0928\u0947', None), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f,', None), (u'\u092f\u0939', u'PRP'), (u'\u092a\u0943\u0925\u094d\u0935\u0940', None), (u'\u092a\u0930', u'PREP'), (u'\u0938\u092c', u'INTF'), (u'\u092c\u093e\u0924\u094b\u0902', None), (u'\u0915\u093e', u'PREP'), (u'\u0938\u092c\u0938\u0947', u'INTF'), (u'\u092c\u0921\u093c\u093e', None), (u'\u0927\u0928', u'NN'), (u'\u0939\u0948.', None), (u'\u092f\u0939', u'PRP'), (u'\u0905\u091a\u094d\u091b\u093e', u'JVB'), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0938\u092d\u0940', u'QF'), (u'\u0916\u0941\u0936\u0940', None), (u'\u0915\u093e', u'PREP'), (u'\u0938\u094d\u0930\u094b\u0924', None), (u'\u0939\u0948', u'VAUX'), (u'\u0915\u093f', u'CC'), (u'\u0915\u0939\u093e', u'VFM'), (u'\u091c\u093e\u0924\u093e', u'VAUX'), (u'\u0939\u0948.', None), (u'\u0905\u091a\u094d\u091b\u0947', None), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0914\u0930', u'CC'), (u'\u0916\u0941\u0936\u0940', None), (u'\u0915\u0902\u0927\u0947', None), (u'\u0938\u0947', u'PREP'), (u'\u0915\u0902\u0927\u093e', None), (u'\u092e\u093f\u0932\u093e\u0915\u0930', None), (u'\u091a\u0932\u0924\u0947', u'PREP'), (u'\u0939\u0948\u0902.', None), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0927\u094d\u0935\u0928\u093f', None), (u'\u0936\u0930\u0940\u0930', None), (u'\u0915\u0947', u'PREP'), (u'\u0930\u093e\u091c\u094d\u092f', u'NN'), (u'\u0915\u0947', u'PREP'), (u'\u0930\u0942\u092a', u'NN'), (u'\u092e\u0947\u0902', u'PREP'), (u'\u0905\u091a\u094d\u091b\u0940', u'JVB'), (u'\u0924\u0930\u0939', None), (u'\u0938\u0947', u'PREP'), (u'\u092e\u0928', None), (u'\u0915\u093e', u'PREP'), (u'\u0930\u0942\u092a', u'NN'), (u'\u0939\u0948.', None), (u'\u090f\u0915', u'QFNUM'), (u'\u0938\u094d\u0935\u0938\u094d\u0925', None), (u'\u0935\u094d\u092f\u0915\u094d\u0924\u093f', u'NN'), (u'\u0915\u0947', u'PREP'), (u'\u092e\u0928', None), (u'\u0915\u0940', u'PREP'), (u'\u0938\u0941\u0926\u0943\u0922\u093c\u0924\u093e', None), (u'\u0915\u0947', u'PREP'), (u'\u0938\u093e\u0925', u'PREP'), (u'\u0939\u0940', u'RP'), (u'\u0936\u0930\u0940\u0930', None), (u'\u0915\u0940', u'PREP'), (u'\u0938\u0941\u0926\u0943\u0922\u093c\u0924\u093e', None), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f', u'PREP'), (u'\u0939\u094b\u0928\u093e', u'VNN'), (u'\u091a\u093e\u0939\u093f\u090f.', None), (u'\u0938\u094d\u0935\u0938\u094d\u0925', None), (u'\u0939\u094b\u0928\u0947', u'VNN'), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f,', None), (u'\u090f\u0915', u'QFNUM'), (u'\u090f\u0915', u'QFNUM'), (u'\u0938\u0902\u0924\u0941\u0932\u093f\u0924', u'JJ'), (u'\u0924\u0930\u0940\u0915\u0947', u'NN'), (u'\u0938\u0947', u'PREP'), (u'\u090a\u092a\u0930', None), (u'\u0939\u094b', u'VFM'), (u'\u091c\u093e\u0928\u093e', u'VAUX'), (u'\u091a\u093e\u0939\u093f\u090f.', None), (u'\u0905\u091a\u094d\u091b\u093e', u'JVB'), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u092c\u0928\u093e\u090f', None), (u'\u0930\u0916\u0928\u0947', u'VNN'), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f,', None), (u'\u090f\u0915,', None), (u'\u090f\u0915', u'QFNUM'), (u'\u0938\u0902\u0924\u0941\u0932\u093f\u0924', u'JJ'), (u'\u0906\u0939\u093e\u0930', None), (u'\u0916\u093e\u0928\u0947', None), (u'\u0938\u093e\u092b', u'JVB'), (u'\u092a\u093e\u0928\u0940', None), (u'\u092a\u0940\u0928\u0947,', None), (u'\u0924\u093e\u091c\u093e', None), (u'\u0939\u0935\u093e', None), (u'\u0932\u0947\u0924\u0947', None), (u'\u0939\u0948\u0902,', None), (u'\u0920\u0940\u0915', None), (u'\u0938\u0947', u'PREP'), (u'\u0906\u0930\u093e\u092e', None), (u'\u0914\u0930', u'CC'), (u'\u0928\u093f\u092f\u092e\u093f\u0924', None), (u'\u0930\u0942\u092a', u'NN'), (u'\u0938\u0947', u'PREP'), (u'\u0936\u093e\u0930\u0940\u0930\u093f\u0915', None), (u'\u0935\u094d\u092f\u093e\u092f\u093e\u092e', None), (u'\u092e\u0947\u0902', u'PREP'), (u'\u0932\u0947', u'VFM'), (u'\u0932\u093f\u092f\u093e', u'VAUX'), (u'\u0939\u0948.', None), (u'\u092e\u093e\u0928\u0938\u093f\u0915', None), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0915\u0940', u'PREP'), (u'\u0905\u0935\u0927\u093e\u0930\u0923', None), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f,', None), (u'\u090f\u0915', u'QFNUM'), (u'\u0915\u0947', u'PREP'), (u'\u092c\u093e\u0926', u'PREP'), (u'\u090f\u0915', u'QFNUM'), (u'\u0915\u0940', u'PREP'), (u'\u092d\u093e\u0935\u0928\u093e\u0913\u0902', None), (u'\u0915\u094b', u'PREP'), (u'\u0928\u093f\u092f\u0902\u0924\u094d\u0930\u093f\u0924', u'JVB'), (u'\u0915\u0930\u0928\u0947', u'VNN'), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f', u'PREP'), (u'\u0939\u0948', u'VAUX'), (u'\u0914\u0930', u'CC'), (u'\u090f\u0915', u'QFNUM'), (u'\u0905\u0928\u094d\u092f', u'JJ'), (u'\u0932\u094b\u0917\u094b\u0902', u'NN'), (u'\u0915\u0940', u'PREP'), (u'\u092d\u093e\u0935\u0928\u093e\u0913\u0902', None), (u'\u0914\u0930', u'CC'), (u'\u0905\u0927\u093f\u0915\u093e\u0930', u'NNC'), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f', u'PREP'), (u'\u0927\u0948\u0930\u094d\u092f', None), (u'\u0914\u0930', u'CC'), (u'\u0938\u092e\u094d\u092e\u093e\u0928', None), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f', u'PREP'), (u'\u0939\u0948.', None), (u'\u0939\u093e\u0932\u093e\u0902\u0915\u093f,', None), (u'\u0905\u091a\u094d\u091b\u0947', None), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0915\u0947', u'PREP'), (u'\u092c\u093f\u0928\u093e', u'NEG'), (u'\u091c\u0940\u0935\u0928', u'NN'), (u'\u0928\u0940\u0930\u0938', None), (u'\u0914\u0930', u'CC'), (u'\u0905\u0938\u0939\u0928\u0940\u092f', None), (u'\u0939\u0948.', None), (u'\u092f\u0939', u'PRP'), (u'\u0924\u094b', u'RP'), (u'\u0905\u0915\u094d\u0938\u0930', None), (u'\u092c\u0940\u092e\u093e\u0930', None), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0930\u0916\u0928\u0947', u'VNN'), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f', u'PREP'), (u'\u090f\u0915', u'QFNUM'), (u'\u0935\u094d\u092f\u0915\u094d\u0924\u093f', u'NN'), (u'\u0915\u0947', u'PREP'), (u'\u091c\u0940\u0935\u0928', u'NN'), (u'\u0915\u0947', u'PREP'), (u'\u0932\u093f\u090f', u'PREP'), (u'\u092e\u094c\u0924', u'NN'), (u'\u092a\u0938\u0902\u0926', None), (u'\u0939\u0948', u'VAUX'), (u'\u0938\u0941\u0928\u093e', None), (u'\u0939\u0948.', None), (u'\u0907\u0938\u0932\u093f\u090f,', None), (u'\u0939\u0930', u'QF'), (u'\u0915\u093f\u0938\u0940', u'QW'), (u'\u0915\u094b', u'PREP'), (u'\u0905\u092a\u0928\u0947', u'PRP'), (u'\u0938\u094d\u0935\u093e\u0938\u094d\u0925\u094d\u092f', None), (u'\u0915\u093e', u'PREP'), (u'\u0927\u094d\u092f\u093e\u0928', u'NVB'), (u'\u0930\u0916\u0928\u093e', None), (u'\u091a\u093e\u0939\u093f\u090f.', None)]
alvas
  • 115,346
  • 109
  • 446
  • 738