How to check two POS tags are in the same category in NLTK?
<p>Like the title says, how can I check two POS tags are in the same category?</p>

<p>For example,</p>

<pre><code>go -&gt; VB
goes -&gt; VBZ
</code></pre>

<p>These two words are both verbs. Or,</p>

<pre><code>bag -&gt; NN
bags -&gt; NNS
</code></pre>

<p>These two are both nouns.
So my question is that whether there exists any function in NLTK to check if two given tags are in the same category?</p>

<p>Not sure if this is what you are looking for, but you can tag with a <a href="http://www.nltk.org/book/ch05.html#reading-tagged-corpora" rel="nofollow"><code>universal</code> tagset</a>:</p>

<pre><code>from pprint import pprint
from collections import defaultdict

from nltk import pos_tag
from nltk.tokenize import sent_tokenize, word_tokenize

s = "I go. He goes. This bag is brown. These bags are brown."

d = defaultdict(list)
for sent in sent_tokenize(s):
    text = word_tokenize(sent)
    for value, tag in pos_tag(text, tagset='universal'):
        d[tag].append(value)

pprint(dict(d))
</code></pre>

<p>Prints:</p>

<pre><code>{'.': ['.', '.', '.', '.'],
 'ADJ': ['brown'],
 'DET': ['This', 'These'],
 'NOUN': ['bag', 'bags'],
 'PRON': ['I', 'He'],
 'VERB': ['go', 'goes', 'is', 'brown', 'are']}
</code></pre>

<p>Note how <code>bag</code> and <code>bags</code> fall into <code>NOUN</code> category and <code>go</code> and <code>goes</code> fall into <code>VERB</code>.</p>

python nltk pos-tagging