Change language

NLP | Chunking Based on Classifier | Set 2

| |

Code # 1:

# loading libraries

from chunkers import ClassifierChunker

from nltk.corpus import treebank_chunk

 

train_data = treebank_chunk.chunked_sents () [: 3000 ]

test_data = treebank_chunk. chunked_sents () [ 3000 :]

  
# initialization

chunker = ClassifierChunker (train_data)

  
# rating

score = chunker.evaluate (test_data)

 

a = score.accuracy ()

p = score.precision ()

r = recall

 

print ( "Accuracy of ClassifierChunker:" , a)

print ( " Precision of ClassifierChunker: " , p)

print ( "Recall of ClassifierChunker:" , r)

Output:

 Accuracy of ClassifierChunker: 0.9721733155838022 Precision of ClassifierChunker: 0.9258838793383068 Recall of ClassifierChunker: 0.9359016393442623 

Code # 2: Let’s compare conll_train performance

chunker = ClassifierChunker (conll_train)

score = chunker .evaluate (conll_test)

  

a = score .accuracy ()

p = score .precision ()

r = score .recall ()

 

print ( "Accuracy of ClassifierChunker:" , a)

print ( "Precision of ClassifierChunker:" , p)

print ( "Recall of ClassifierChunker:" , r)

Output:

 Accuracy of Cla ssifierChunker: 0.9264622074002153 Precision of ClassifierChunker: 0.8737924310910219 Recall of ClassifierChunker: 0.9007354620620346 

a word can be passed via a tag to our object definition function by creating nested 2 tuples), ((word, pos) the chunk_trees2train_chunks () method creates these nested 2-tuples. 
The following functions are extracted:

  • Current word and part of speech
  • Previous word and IOB tag, part of speech tag
  • Next word and part speech

The ClassifierChunker class uses an internal ClassifierBasedTagger and prev_next_pos_iob () as its standard feature_detector. The results from the tagger, which are in the same nested form with 2 tuples, are then reformatted into 3 tuples to return the final tree using conlltags2tree ().

Code # 3: Different classifier builder

# loading libraries

from chunkers import ClassifierChunker

from nltk.corpus import treebank_chunk

from nltk.classify import MaxentClassifier

  

train_data = treebank_chunk.chunked_sents () [: 3000 ]

test_data = treebank_chunk.chunked_sents () [ 3000 :]

 

 

builder = lambda toks: MaxentClassifier.train (

toks, trace = 0 , max_iter = 10 , min_lldelta = 0.01 )

  

chunker = ClassifierChunker (

train_data, classifier_builder = builder)

  

score = chunker.evaluate (test_data)

 

a = score.accuracy ()

p = score.precision ()

r = score.recall ()

 

print ( "Accuracy of Class ifierChunker: " , a)

print ( "Precision of ClassifierChunker:" , p)

print ( " Recall of ClassifierChunker: " , r)

Output:

 Accuracy of ClassifierChunker: 0.9743204362949285 Precision of ClassifierChunker: 0.9334423548650859 Recall of ClassifierChunker: 0.9357377049180328 

The ClassifierBasedTagger class uses NabuildingBayesClassifier_train as the default. But any classifier can be used by overriding the classifier_builder key argument.

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