【发布时间】:2020-02-01 06:26:16
【问题描述】:
我在 NLTK 和 Spacy 的以下句子中使用了 NER,结果如下:
"Zoni I want to find a pencil, a eraser and a sharpener"
我在 Google Colab 上运行了以下代码。
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
ex = "Zoni I want to find a pencil, a eraser and a sharpener"
def preprocess(sent):
sent = nltk.word_tokenize(sent)
sent = nltk.pos_tag(sent)
return sent
sent = preprocess(ex)
sent
#Output:
[('Zoni', 'NNP'),
('I', 'PRP'),
('want', 'VBP'),
('to', 'TO'),
('find', 'VB'),
('a', 'DT'),
('pencil', 'NN'),
(',', ','),
('a', 'DT'),
('eraser', 'NN'),
('and', 'CC'),
('a', 'DT'),
('sharpener', 'NN')]
但是当我在同一文本上使用 spacy 时,它没有返回任何结果
import spacy
from spacy import displacy
from collections import Counter
import en_core_web_sm
nlp = en_core_web_sm.load()
text = "Zoni I want to find a pencil, a eraser and a sharpener"
doc = nlp(text)
doc.ents
#Output:
()
它只适用于某些句子。
import spacy
from spacy import displacy
from collections import Counter
import en_core_web_sm
nlp = en_core_web_sm.load()
# text = "Zoni I want to find a pencil, a eraser and a sharpener"
text = 'European authorities fined Google a record $5.1 billion on Wednesday for abusing its power in the mobile phone market and ordered the company to alter its practices'
doc = nlp(text)
doc.ents
#Output:
(European, Google, $5.1 billion, Wednesday)
如果有问题请告诉我。
【问题讨论】:
标签: python-3.x nlp nltk spacy named-entity-recognition