文書の束の中に見えるようにすべての電子メールを照合し、それを「EMAIL」というカスタムNERラベルに追加しようとしています。 テストケースのコードは次のとおりです。spaCyでmatcherを使用して新しいエンティティを追加しようとするとAssertionErrorが発生する
nlp = spacy.load('en_core_web_sm')
matcher = Matcher(nlp.vocab)
EMAIL = nlp.vocab.strings['EMAIL']
def add_email_ent(matcher, doc, i, matches):
match_id, start, end = matches[i]
doc.ents += ((EMAIL, start, end),)
matcher.add('EmailPII', add_email_ent, [{'LIKE_EMAIL': True}])
text = u"Hi, this is John. My email is [email protected] and an alternate is [email protected]"
doc = nlp(text)
matches = matcher(doc)
for i,[match_id, start, end] in enumerate(matches):
print (i+1, doc[start:end])
for ent in doc.ents:
print (ent.text, ent.label_)
このコードを実行すると、次のような結果が得られます。必要に応じて
Traceback (most recent call last):
File "C:/Python27/emailpii.py", line 26, in <module>
matches = matcher(doc)
File "matcher.pyx", line 407, in spacy.matcher.Matcher.__call__
File "C:/Python27/emailpii.py", line 19, in add_event_ent
doc.ents += ((EMAIL, start, end),)
File "doc.pyx", line 415, in spacy.tokens.doc.Doc.ents.__get__
File "span.pyx", line 61, in spacy.tokens.span.Span.__cinit__
AssertionError: 17587345535198158200
しかし、同様の例
import spacy
print "*****************"
print(spacy.__version__)
print "*****************"
from spacy.matcher import Matcher
#from spacy import displacy
nlp = spacy.load('en_core_web_sm')
matcher = Matcher(nlp.vocab)
EVENT = nlp.vocab.strings['EVENT']
def add_event_ent(matcher, doc, i, matches):
match_id, start, end = matches[i]
doc.ents += ((EVENT, start, end),)
matcher.add('GoogleIO', add_event_ent,
[{'ORTH': 'Google'}, {'ORTH': 'I'}, {'ORTH': '/'}, {'ORTH': 'O'}],
[{'ORTH': 'Google'}, {'ORTH': 'I'}, {'ORTH': '/'}, {'ORTH': 'O'}, {'IS_DIGIT': True}])
text = u"Google I/O was great this year. See you all again in Google I/O 2018"
doc = nlp(text)
matches = matcher(doc)
for i,[match_id, start, end] in enumerate(matches):
print (i, doc[start:end])
for ent in doc.ents:
print (ent.text, ent.label_)
#displacy.serve(doc, style = 'ent')
を実行する上で、私は出力を得る:
2.0.1
(0、GoogleのI/O)
(1、グーグルI/O)
(2、GoogleのI/O 2018)
(u'Google I/O 'u'EVENT')
(u'this今年、u'DATE ')
(u'Google I/O 2018'、u'EVENT ')
は、私がここで何かが足りないのですか?
ありがとうございます!これはmatcher.add関数に渡された最初の引数を理解するのに役立ちました。私は 'add_label'とスパンを 'assert'することで、私の問題に対処しました。しかし、それを実装するより良い方法を得るのを待っていた。これは役に立ちます。 – hkr