STAY WITH US

[Hindi]NLP 23# Named Entity Recognition P.2 |NLP|Python 3|Natural Language Processing|2019

[Hindi]NLP 23# Named Entity Recognition P.2  |NLP|Python 3|Natural Language Processing|2019

Code:

# -*- coding: utf-8 -*-
"""NLP_Ex15.ipynb

Automatically generated by Colaboratory.

"""

import spacy

nlp = spacy.load('en_core_web_sm')

def show_ents(doc):
  if doc.ents:
    for ent in doc.ents:
      print(ent.text+' - '+ent.label_+' - '+str(spacy.explain(ent.label_)))
  else:
    print('No entites found!!!')

doc = nlp(u"Our Company created a brand new vacuum cleaner."
          u"The new vacuum-cleaner is the best in show.")

show_ents(doc)

from spacy.matcher import PhraseMatcher

matcher = PhraseMatcher(nlp.vocab)

phrase_list = ['vacuum cleaner','vacuum-cleaner']

phrase_patters = [nlp(text) for text in phrase_list]

matcher.add('newproduct',None,*phrase_patters)

found_matches = matcher(doc)

found_matches

from spacy.tokens import Span

PROD = doc.vocab.strings[u"PRODUCT"]

found_matches

new_ents = [Span(doc, match[1], match[2], label=PROD) for match in found_matches]

doc.ents = list(doc.ents)+new_ents

show_ents(doc)

doc = nlp(u"Originally I paid $29.95 for this toy car, but now it is marked down by 10 dollars.")

[ent for ent in doc.ents if ent.label_=="MONEY"]

len([ent for ent in doc.ents if ent.label_=="MONEY"])

YouTube:


Recommended Book:

Post a Comment

0 Comments