Change language

NLP | Tagger-based Training Choker | Set 1

| |

In the codes below, we use the treebank_chunk corpus to create fragmented sentences in the form of trees. 
-" To train tag-based chunker — the methods of the chunked_sents () class are used by the TagChunker class. 
-" To extract a list of (pos, iob) tuples from a list of trees — the TagChunker class uses the conll_tag_chunks () helper function.

These tuples are then finally used to train the tag. and it learns IOB tags for part-of-speech tags.

Code # 1: Let’s take a look at the Chunker class for training.

Output:

 Training TagChunker 

Code # 2: Usage Tag Chunker.

from nltk.chunk import ChunkParserI

from nltk.chunk.util import tree2conlltags, conlltags2tree

from nltk.tag import UnigramTagger, BigramTagger

from tag_util import backoff_tagger

 

  

def conll_tag_chunks (chunk_data):

 

tagged_data = [tree2conlltags (tree) for  

tree in chunk_data]

 

return [[(t, c) for (w, t, c) in sent] 

for sent in tagged_data]

  

class TagChunker (ChunkParserI):

  

  def __ init__ ( self , train_chunks, 

tagger_classes = [UnigramTagger, BigramTagger]):

 

train_data = conll_tag_chunks (train_chunks)

self . tagger = backoff_tagger ( train_data, tagger_classes)

 

< code class = "undefined spaces">  def parse ( self , tagged_sent):

if not tagged_sent: 

return None

 

(words, tags) = zip ( * tagged_sent)

chunks = self . tagger.tag (tags)

wtc < / code> = zip (words, chunks)

 

return conlltags2tree ([(w, t, c) for (w, (t, c) ) in wtc])

# loading libraries

from chunkers import TagChunker

from nltk.corpus import treebank_chunk

  
# data from treebank_chunk

train_data = treebank_chunk.chunked_sents () [: 3000 ]

test_data = treebank_chunk.chunked_sents () [ 3000 :]

 
# Initailazing

chunker = TagChunker (train_data )

Code # 3: TagChunker Rating

# testing

score = chunker.evaluate (test_data)

  

a = score.accuracy ()

p = score.precision ()

r = recall

 

print ( "Accuracy of TagChunker:" , a)

print ( "Precision of TagChunker:" , p)

print ( "Recal l of TagChunker: " , r)

Output:

 Accuracy of TagChunker: 0.9732039335251428 Precision of TagChunker: 0.9166534370535006 Recall of TagChunker: 0.9465573770491803 

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