2
名前付きエンティティ認識の2つの新しいクラスを認識できるようにSpacy NERを訓練する必要があります。新しいクラスにあるはずのアイテムのリストを持つファイルです。Spacyの新しい名前付きエンティティクラス
例:Rolling Stones、Muse、Arctic Monkeys - アーティスト どのようにこれを行うことができますか?
名前付きエンティティ認識の2つの新しいクラスを認識できるようにSpacy NERを訓練する必要があります。新しいクラスにあるはずのアイテムのリストを持つファイルです。Spacyの新しい名前付きエンティティクラス
例:Rolling Stones、Muse、Arctic Monkeys - アーティスト どのようにこれを行うことができますか?
これはMatcherまたはPhraseMatcher(パフォーマンスが気になる場合)の最適な使用例のようです。
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='1', label='ARTIST', attrs={}, specs=[[{spacy.attrs.ORTH: 'Rolling'}, {spacy.attrs.ORTH: 'Stones'}]], on_match=merge_phrases)
matcher.add(entity_key='2', label='ARTIST', attrs={}, specs=[[{spacy.attrs.ORTH: 'Muse'}]], on_match=merge_phrases)
matcher.add(entity_key='3', label='ARTIST', attrs={}, specs=[[{spacy.attrs.ORTH: 'Arctic'}, {spacy.attrs.ORTH: 'Monkeys'}]], on_match=merge_phrases)
doc = nlp(u'The Rolling Stones are an English rock band formed in London in 1962. The first settled line-up consisted of Brian Jones, Ian Stewart, Mick Jagger, Keith Richards, Bill Wyman and Charlie Watts')
matcher(doc)
for ent in doc.ents:
print(ent)
詳細については、ドキュメントを参照してください。私の経験から、Matcherの400k個のエンティティでは、各文書にマッチするのに約1秒かかるでしょう。 PhraseMatcherの方がはるかに高速ですが、使用するのが少し面倒です。これは厳密なマッチャーであり、これまで見たことのないエンティティとは一致しません。
ありがとう、私はそれを試してみます –
私は新しいエンティティのすべての要素を手動で追加する必要がありますか? –
そうです、私が言ったように。これまで見たことのないエンティティと一致させたい場合は、まずそれを訓練する必要があります。これはより複雑で、多くのデータを必要とします。 –