Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify the word as a noun, verb or adjective

Given a single word such as "table", I want to identify what it is most commonly used as, whether its most common usage is noun, verb or adjective. I want to do this in python. Is there anything else besides wordnet too? I don't prefer wordnet. Or, if I use wordnet, how would I do it exactly with it?

like image 369
jonty rhodes Avatar asked Dec 03 '22 16:12

jonty rhodes


2 Answers

import nltk


text = 'This is a table. We should table this offer. The table is in the center.'
text = nltk.word_tokenize(text)
result = nltk.pos_tag(text)
result = [i for i in result if i[0].lower() == 'table']

print(result) # [('table', 'JJ'), ('table', 'VB'), ('table', 'NN')]
like image 92
Vidul Avatar answered Dec 05 '22 05:12

Vidul


If you have a word out of context and want to know its most common use, you could look at someone else's frequency table (e.g. WordNet), or you can do your own counts: Just find a tagged corpus that's large enough for your purposes, and count its instances. If you want to use a free corpus, the NLTK includes the Brown corpus (1 million words). The NLTK also provides methods for working with larger, non-free corpora (e.g, the British National Corpus).

import nltk
from nltk.corpus import brown
table = nltk.FreqDist(t for w, t in brown.tagged_words() if w.lower() == 'table')
print(table.most_common())

[('NN', 147), ('NN-TL', 50), ('VB', 1)]
like image 20
alexis Avatar answered Dec 05 '22 04:12

alexis