Python 如何在spacy nlp中添加新实体(组织)实例
我试图将股票符号添加到被识别为组织实体的字符串中。对于每个符号,我执行以下操作:Python 如何在spacy nlp中添加新实体(组织)实例,python,nlp,spacy,Python,Nlp,Spacy,我试图将股票符号添加到被识别为组织实体的字符串中。对于每个符号,我执行以下操作: nlp.matcher.add(symbol, u'ORG', {}, [[{u'orth': symbol}]]) 我可以看到这个符号被添加到图案中: print "Patterns:", nlp.matcher._patterns 但在添加之前未被识别的任何符号在添加之后都不会被识别。显然,这些标记已经存在于词汇表中(这就是为什么vocab长度没有改变) 我应该做些什么不同的事情?我错过了什么 谢谢 下面是
nlp.matcher.add(symbol, u'ORG', {}, [[{u'orth': symbol}]])
我可以看到这个符号被添加到图案中:
print "Patterns:", nlp.matcher._patterns
但在添加之前未被识别的任何符号在添加之后都不会被识别。显然,这些标记已经存在于词汇表中(这就是为什么vocab长度没有改变)
我应该做些什么不同的事情?我错过了什么
谢谢
下面是我的示例代码:
“练习将股票代码符号添加为组织实体的简短片段”
以下是基于以下内容的工作示例: ->
NYSE
和ESV
现在标记为股票
实体类型。基本上,在每次匹配中,您应该手动合并令牌和/或分配所需的实体类型。还有一个函数,允许您在匹配时过滤/拒绝匹配。如果您使用的是>1.0,您应该为每个匹配器提供一个回调函数,并手动合并令牌。您可以提供更多详细信息吗?谢谢您的建议,但您可以提供更多详细信息吗?在哪里添加回调?回调的作用是什么?如何手动合并令牌?对不起,我刚开始使用Spacy。谢谢,谢谢。我正在旅行,但我一回来就会看这个。感谢您的帮助。@user1430965忘记检查了,对吗?
from spacy.en import English
import spacy.en
from spacy.attrs import ORTH, TAG, LOWER, IS_ALPHA, FLAG63
import os
import csv
import sys
nlp = English() #Load everything for the English model
print "Before nlp vocab length", len(nlp.matcher.vocab)
symbol_list = [u"CHK", u"JONE", u"NE", u"DO", u"ESV"]
txt = u"""drive double-digit rallies in Chesapeake Energy (NYSE: CHK), (NYSE: NE), (NYSE: DO), (NYSE: ESV), (NYSE: JONE)"""# u"""Drive double-digit rallies in Chesapeake Energy (NYSE: CHK), Noble Corporation (NYSE:NE), Diamond Offshore (NYSE:DO), Ensco (NYSE:ESV), and Jones Energy (NYSE: JONE)"""
before = nlp(txt)
for tok in before: #Before adding entities
print tok, tok.orth, tok.tag_, tok.ent_type_
for symbol in symbol_list:
print "adding symbol:", symbol
print "vocab length:", len(nlp.matcher.vocab)
print "pattern length:", nlp.matcher.n_patterns
nlp.matcher.add(symbol, u'ORG', {}, [[{u'orth': symbol}]])
print "Patterns:", nlp.matcher._patterns
print "Entities:", nlp.matcher._entities
for ent in nlp.matcher._entities:
print ent.label
tokens = nlp(txt)
print "\n\nAfter:"
print "After nlp vocab length", len(nlp.matcher.vocab)
for tok in tokens:
print tok, tok.orth, tok.tag_, tok.ent_type_
import spacy
nlp = spacy.load('en')
def merge_phrases(matcher, doc, i, matches):
'''
Merge a phrase. We have to be careful here because we'll change the token indices.
To avoid problems, merge all the phrases once we're called on the last match.
'''
if i != len(matches)-1:
return None
spans = [(ent_id, label, doc[start : end]) for ent_id, label, start, end in matches]
for ent_id, label, span in spans:
span.merge('NNP' if label else span.root.tag_, span.text, nlp.vocab.strings[label])
matcher = spacy.matcher.Matcher(nlp.vocab)
matcher.add(entity_key='stock-nyse', label='STOCK', attrs={}, specs=[[{spacy.attrs.ORTH: 'NYSE'}]], on_match=merge_phrases)
matcher.add(entity_key='stock-esv', label='STOCK', attrs={}, specs=[[{spacy.attrs.ORTH: 'ESV'}]], on_match=merge_phrases)
doc = nlp(u"""drive double-digit rallies in Chesapeake Energy (NYSE: CHK), (NYSE: NE), (NYSE: DO), (NYSE: ESV), (NYSE: JONE)""")
matcher(doc)
print(['%s|%s' % (t.orth_, t.ent_type_) for t in doc])
['drive|', 'double|', '-|', 'digit|', 'rallies|', 'in|', 'Chesapeake|ORG', 'Energy|ORG', '(|', 'NYSE|STOCK', ':|', 'CHK|', ')|', ',|', '(|', 'NYSE|STOCK', ':|', 'NE|GPE', ')|', ',|', '(|', 'NYSE|STOCK', ':|', 'DO|', ')|', ',|', '(|', 'NYSE|STOCK', ':|', 'ESV|STOCK', ')|', ',|', '(|', 'NYSE|STOCK', ':|', 'JONE|ORG', ')|']