使用 NLTK 和 Spacy 的 NLP 命名实体识别
NLP Named Entity Recognition using NLTK and Spacy
我在 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 时,return 没有任何结果
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 .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, .1 billion, Wednesday)
如有不妥请告知
Spacy 模型是统计模型。因此,这些模型识别的命名实体取决于训练这些模型的数据集。
根据 spacy 文档,命名实体是一个“真实世界对象”,它被分配了一个名字——例如,一个人、国家、产品或书名。
例如,姓名 Zoni 并不常见,因此模型不会将该姓名识别为命名实体(人)。如果我在你的句子 Zoni 中将名字 Zoni 更改为 William spacy recognize William作为一个人。
import spacy
nlp = spacy.load('en_core_web_lg')
doc = nlp('William I want to find a pencil, a eraser and a sharpener')
for entity in doc.ents:
print(entity.label_, ' | ', entity.text)
#output
PERSON | William
人们会假设 铅笔、橡皮 和 卷笔刀 是对象,所以它们会可能被归类为产品,因为 spacy documentation 状态 'objects' 是产品。但是您句子中的 3 个对象似乎并非如此。
我还注意到,如果在输入文本中未找到命名实体,则输出将为空。
import spacy
nlp = spacy.load("en_core_web_lg")
doc = nlp('Zoni I want to find a pencil, a eraser and a sharpener')
if not doc.ents:
print ('No named entities were recognized in the input text.')
else:
for entity in doc.ents:
print(entity.label_, ' | ', entity.text)
我不确定我是否理解您要进行的比较。在您使用 NLTK 的第一个示例中,您正在查看句子中的 POS 标签。但是在 spaCy 的第二个示例中,您正在查看 命名实体 。这是两个不同的东西。统计模型应该始终为每个标记提供一个 POS 标签(尽管有时可能会有所不同),但命名实体的识别(如 'Life is complex' 在 post 中解释的那样)取决于数据集这些模型是在其上训练的。如果模型 "feel like" 句子中没有命名实体,您将得到一个空结果集。但为了获得公平的比较,您还应该显示 NLTK 找到的命名实体,并与之进行比较。
如果您想比较 POS 标签,您可以使用 spaCy 运行 这个:
for token in doc:
print(token.text, token.pos_, token.tag_)
我在 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 时,return 没有任何结果
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 .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, .1 billion, Wednesday)
如有不妥请告知
Spacy 模型是统计模型。因此,这些模型识别的命名实体取决于训练这些模型的数据集。
根据 spacy 文档,命名实体是一个“真实世界对象”,它被分配了一个名字——例如,一个人、国家、产品或书名。
例如,姓名 Zoni 并不常见,因此模型不会将该姓名识别为命名实体(人)。如果我在你的句子 Zoni 中将名字 Zoni 更改为 William spacy recognize William作为一个人。
import spacy
nlp = spacy.load('en_core_web_lg')
doc = nlp('William I want to find a pencil, a eraser and a sharpener')
for entity in doc.ents:
print(entity.label_, ' | ', entity.text)
#output
PERSON | William
人们会假设 铅笔、橡皮 和 卷笔刀 是对象,所以它们会可能被归类为产品,因为 spacy documentation 状态 'objects' 是产品。但是您句子中的 3 个对象似乎并非如此。
我还注意到,如果在输入文本中未找到命名实体,则输出将为空。
import spacy
nlp = spacy.load("en_core_web_lg")
doc = nlp('Zoni I want to find a pencil, a eraser and a sharpener')
if not doc.ents:
print ('No named entities were recognized in the input text.')
else:
for entity in doc.ents:
print(entity.label_, ' | ', entity.text)
我不确定我是否理解您要进行的比较。在您使用 NLTK 的第一个示例中,您正在查看句子中的 POS 标签。但是在 spaCy 的第二个示例中,您正在查看 命名实体 。这是两个不同的东西。统计模型应该始终为每个标记提供一个 POS 标签(尽管有时可能会有所不同),但命名实体的识别(如 'Life is complex' 在 post 中解释的那样)取决于数据集这些模型是在其上训练的。如果模型 "feel like" 句子中没有命名实体,您将得到一个空结果集。但为了获得公平的比较,您还应该显示 NLTK 找到的命名实体,并与之进行比较。
如果您想比较 POS 标签,您可以使用 spaCy 运行 这个:
for token in doc:
print(token.text, token.pos_, token.tag_)