Change language

NLP | WordNet for tags

| |

Code # 1: Create a WordNet Word Search Class.

from nltk. tag import SequentialBackoffTagger

from nltk.corpus import wordnet

from nltk.probability import FreqDist

 

class WordNetTagger (SequentialBackoffTagger):

 

"" "

""" wt = WordNetTagger ()

""" wt.tag ([& # 39; food & # 39 ;, & # 39; is & # 39 ;, & # 39; great & # 39;])

[(& # 39; food & # 39 ;, & # 39; NN & # 39;), (& # 39; is & # 39 ;, & # 39; VB & # 39;), (& # 39; great & # 39 ;, & # 39; JJ & # 39;)]

"" "

  

  def __ init __ ( self , * args, * * kwargs):

 

SequentialBackoffTagger .__ init __ ( self , * args, * * kwargs)

self . wordnet_tag_map = {

’n’ : ’NN’ ,

  ’s’ : ’ JJ’ ,

’a’ : ’JJ’ ,

  ’r’ : ’ RB’ ,

’v’ : ’VB’

  }

 

def choose_tag ( self , tokens, index, history):

 

word = tokens [index]

fd = FreqDist ()

 

for synset in wordnet.synsets (word):

  fd [synset.pos ()] + = 1

 

  

return self . wordnet_tag_map.get (fd. max ())

This WordNetTagger class will consider no. of every POS tag found in Synsets for a word, and then the most common tag is the treebank tag using internal mapping.

Code # 2: Using a simple WordNetTagger ()

from taggers import WordNetTagger

from nltk.corpus import treebank

  
# Initialization

default_tag = DefaultTagger ( ’NN’ )

  
# init Set up training and testing a set

train_data = treebank.tagged_sents () [: 3000 ]

test_data = treebank.tagged_sents () [ 3000 :]

 

wn_tagging = WordNetTagger ()

a = wn_tagger.evaluate (test_data)

 

print ( "Accuracy of WordNetTagger: " , a)

Output:

 Accuracy of WordNetTagger: 0.17914876598160262 

Using Code 3, we can improve the accuracy. 
Code # 3: WordNetTagger class at the end of the NgramTagger rollback chain

from taggers import WordNetTagger

from nltk.corpus import treebank

from tag_util import backoff_tagger

from nltk.tag import UnigramTagger, BigramTagger, TrigramTagger

 
# Initialization

default_tag DefaultTagger ( ’NN’ )

 
# initialize learning and testing the suite

train_data = treebank.tagged_sents () [: 3000 ]

test_data = treebank.tagged_sents () [ 3000 :]

 

tagger = backoff_tagger (train_data,

[UnigramTagger, BigramTagger,

TrigramTagger], backoff = wn_tagger)

 

a = tagger.evaluate (test_data)

 

print ( "Accuracy:" , a)

Output:

 Accuracy: 0.8848262464925534 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically