Change language

NLP | Chunking Based on Classifier | Set 1

| |

3-tuples (word, pos, iob) are converted to 2-tuples ((word, pos), iob) using chunk_trees2train_chunks () from tree2conlltags () ClassiferBasedTagger class .

Code # 1: Let’s figure it out

# Loading libraries

from nltk.chunk import ChunkParserI

from nltk.chunk.util import tree2conlltags, conlltags2tree

from nltk.tag import ClassifierBasedTag ger

 

def chunk_trees2train_chunks (chunk_sents):

  

  # Using tree2conlltags

tag_sents = [tree2conlltags (sent) for  

sent in chunk_sents]

 

3 - tuple is to 2 - tuple

  return [ [((w, t), c) for  

(w, t, c) in sent] for sent in tag_sents]

The function detector function is now needed to go to ClassifierBasedTagger. Any object detector function used with the ClassifierChunker class (defined below) must recognize that tokens are a list of tuples (word, pos) and have the same function signature as prev_next_pos_iob (). To give the classifier as much information as possible, this feature set contains the current, previous, and next word and part of speech tag as well as the previous IOB tag.

Code # 2: Detector Function

def prev_next_pos_iob (tokens, index, history):

 

word, pos = tokens [index]

if index = = 0 :

prevword, prevpos, previob = ( ’"START"’ ,) * 3

else :

prevword, prevpos = tokens [index - 1 ]

previob = history [index - 1 ]

 

if index = = len (tokens) - 1 :

nextword, nextpos = ( ’"END"’ ,) * 2

else :

nextword, nextpos = tokens [index + 1 ]

  feats = { ’word’ : word,

  ’ pos’ : pos,

’ nextword’ : nextword,

’ nextpos’ : nextpos,

’ prevword’ : prevword,

’ prevpos’ : prevpos,

’ previob’ : previob

}

  return feats

Now need a ClassifierChunker class which uses internally A new ClassifierBasedTagger with tutorial sentences from chunk_trees2train_chunks () and functions extracted using prev_next_pos_iob () . As a subclass of ChunkerParserI ClassifierChunker implements the parse () method to convert ((w, t), c) tuples generated by the inner tag into trees using conlltags2tree()

Code # 3:

class ClassifierChunker (ChunkParserI):

def __ init __ ( self , train_sents, 

feature_detector = prev_next_pos_iob , * * kwargs):

 

  if not feature_detector:

feature_detector = self . feature_detector

  train_chunks = chunk_trees2train_chunks (train_sents)

self . tagger = ClassifierBasedTagger (train = train_chunks,

  feature_detector = feature_detector, * * kwargs)

  

def parse ( self , tagged_sent):

 

  if not tagged_sent: return None

chunks = self . tagger.tag (tagged_sent)

 

return conlltags2tree (

[(w , t, c) < code class = "keyword"> for ((w, t), c) in chunks])

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