jksnw
jksnw

Reputation: 658

Tagging a single word with the nltk pos tagger tags each letter instead of the word

I'm try to tag a single word with the nltk pos tagger:

word = "going"
pos = nltk.pos_tag(word)
print pos

But the output is this:

[('g', 'NN'), ('o', 'VBD'), ('i', 'PRP'), ('n', 'VBP'), ('g', 'JJ')]

It's tagging each letter rather than just the one word.

What can I do to make it tag the word?

Upvotes: 7

Views: 12764

Answers (4)

wiam fadel
wiam fadel

Reputation: 21

Return the POS tag of one word

nltk.pos_tag(["going"])
----->[('going', 'VBG')]

Upvotes: 2

Kasravnd
Kasravnd

Reputation: 107347

nltk.tag.pos_tag accepts a list of tokens, separate and tags its elements. Therefore you need to put your words in an iterable like list:

>>> nltk.tag.pos_tag(['going'])
[('going', 'VBG')]

Upvotes: 14

Ashok Kumar Jayaraman
Ashok Kumar Jayaraman

Reputation: 3095

>>> word = 'going'
>>> word = nltk.word_tokenize(word)
>>> l1 = nltk.pos_tag(word)
>>> l1
[('going', 'VBG')]

Upvotes: 4

David Kelley
David Kelley

Reputation: 1448

The tagger works on a list of words. To turn the string into a list simply use something like

word_list = [word]

then use the pos tagger on the word_list. Note that if you have more than one word, you should run nltk.word_tokenize on the string first.

As for the success in tagging only one word, you should look into the lookup tagger mentioned in section 4.3 here. The pos_tag used by nltk is more complicated than just a one word lookup tagger, but it does use one as part of the process, so you should see ok results.

Upvotes: 0

Related Questions