Change language

NLP | Training a named chunker object

| |

Named entity chunk trees can be generated from the ieer corpus using ieertree2conlltags () and ieer_chunked_sents () . This can be used to train the ClassifierChunker class generated in the chunk based on the classification.

Code # 1: ieertree2conlltags ()

import nltk.tag

from nltk.chunk.util import conlltags2tree

from nltk.corpus import ieer

 

def ieertree2conlltags (tree, tag = nltk.tag.pos_tag):

words, ents =   zip ( * tree.pos ())

iobs = []

prev = None

for ent in ents:

if ent = = tree.label ():

iobs.append ( ’O’ )

prev = None

elif prev = = ent:

  iobs.append ( ’I-% s’ % ent)

else :

iobs.append ( ’B-% s’ % ent)

prev = ent

 

words, tags = zip ( * tag (words))

 

return zip (words, tags, iobs)

Code # 2: ieer_chunked_sents ()

import nltk.tag

from nltk.chunk.util import conlltags2tree

from nltk.corpus import ieer

< p>  

def ieer_chunked_sents (tag = nltk.tag.pos_tag):

for doc in ieer .parsed_docs ():

tagged = ieertree2conlltags (doc.text, tag)

yield conlltags2tree (tagged)

Using 80 of 94 sentences for training and remaining for testing.

Code # 3: How the classifier works in the first sentence of the treebank_chunk corpus.

from nltk.corpus import ieer

from chunkers import ieer_chunked_sents, ClassifierChunker

from nltk.corpus import treebank_chunk

 

ieer_chunks = list (ieer_chunked_sents ())

 

print ( "Length of ieer_chunks:" , len (ieer_chunks))

 
# chunker initialization

chunker = ClassifierChunker (ieer_chunks [: 80 ])

print ( "parsing:" , chunker.parse (

treebank_chunk.tagged_sents () [ 0 ]))

 
# rating

score = chunker.evaluate (ieer_chunks [ 80 :])

 

a  = score.accuracy ()

p = score.precision ()

r = score.recall ()

 

print ( "Accuracy:" , a)

print ( "Precision: " , p)

print ( "Recall:" , r)

Output:

 Length of ieer_chunks: 94 parsing: Tree (’S’, [Tree ( ’LOCATION’, [(’ Pierre’, ’NNP’), (’ Vinken’, ’NNP’)]), (’, ’,’, ’), Tree (’ DURATION’, [(’61’,’ CD’), (’years’, ’NNS’)]), Tree (’ MEASURE’, [(’old’,’ JJ’)]), (’,’, ’,’), (’will’,’ MD’), (’join’ , ’VB’), (’ the’, ’DT’), (’ board’, ’NN’), (’ as’, ’IN’), (’ a’, ’DT’), (’ nonexecutive’ , ’JJ’), (’ director’, ’NN’), Tree (’ DATE’, [(’Nov.’,’ NNP’), (’29’,’ CD’)]), (’.’ , ’.’)]) Accuracy: 0.8829018388070625 Precision: 0.4088717454194793 Recall: 0.5053635280095352 

How does it work?
The ieer trees generated by ieer_chunked_sents () are not entirely accurate. There are no explicit sentence breaks here, so each document is a single tree. Also, the words are not explicitly tagged, this is work using nltk.tag.pos_tag ().

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