Change language

NLP | Correct noun extraction

| |

We can then test this against the first tagged sentence treebank_chunk, to compare the results with the previous recipe:

Code # 1: Testing by the first tag offer treebank_chunk

from nltk.corpus import treebank_chunk

from nltk.chunk import RegexpParser

from chunkers import sub_leaves

 

chunker = RegexpParser (r "" " 

NAME:

{"NNP" +}

"" " )

 

print ( " Named Entities: "

  sub_leaves (chunker.parse (

  treebank_chunk.tagged_sents () [ 0 ]), ’ NAME’ ))

Output:

 Named Entities: [[(’Pierre’,’ NNP’), (’Vinken’,’ NNP’)], [(’Nov .’, ’NNP’)]] 

Note. The above code returns all native nouns — Pierre, Vinken, November. 
NAME chunker — this is a simple use of the RegexpParser class. All sequences of words marked with NNP are concatenated into NAME fragments. 
PersonChunker class can be used if you only want to separate the names of people.

Code # 2: PersonChunker class

from nltk.chunk import ChunkParserI

from nltk.chunk.util import conlltags2tree

from nltk. corpus import names

  

class PersonChunker (ChunkParserI):

  def __ init __ ( self ):

  self . name_set = set (names.words ())

  

  def parse ( self , tagged_sent):

 

iobs = []

in_person = False

for word, tag in tagged_sent:

if word in self . name_set and in_person:

iobs. append ((word, tag, ’I-PERSON’ ))

elif word in self . name_set:

iobs.append ((word, tag, ’B-PERSON’ ))

in_person = True

  else :

iobs.append ((word, tag, ’O’ ))

  in_person = False

 

  return conlltags2tree (iobs)

PersonChunker class checks if each word is in its names_set (generated from the name corpus) by iterating over the tag sentence ... It uses the tags I-BERSON or I-PERSON if the current word is in names_set, depending on whether the previous word was also in names_set. The IOB tag is assigned to a word that is not in the names_set argument. The IOB tag list is converted to a tree using conlltags2tree () upon completion.

Code # 3: Using the PersonChunker class in the same tag sentence

from nltk.corpus import treebank_chunk

from nltk.chunk import RegexpParser

from chunkers import sub_leaves

  

from chunkers import PersonChunker

chun ker = PersonChunker ()

print ( "Person name :"

sub_leaves (chunker.parse (

treebank_chunk.tagged_sents () [ 0 ]), ’PERSON’ ))

Output:

 Person name: [[(’Pierre’,’ NNP’)]] 

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