NeuralCorefは、ニューラルネットワークを使用してコアレファレンスクラスターに注釈を付けて解決するSpacy 2.1+のパイプライン拡張です。 NeuralCorefは生産準備が整っており、SpacyのNLPパイプラインに統合されており、新しいトレーニングデータセットに拡張可能です。
Coreference ResolutionとNeuralCorefの簡単な紹介については、ブログ投稿を参照してください。 NeuralCorefはPython/Cythonで書かれており、英語のみの事前に訓練された統計モデルが付属しています。
NeuralCorefには、視覚化クライアントNeuralCoref-Vizが伴います。これは、オンラインで試すことができるRESTサーバーを搭載したWebインターフェイスです。 NeuralCorefはMITライセンスの下でリリースされます。
バージョン4.0今すぐ! PIPで利用可能で、Spacy 2.1+と互換性があります。
これは、NeuralCorefをインストールする最も簡単な方法です。
pip install neuralcorefspacy.strings.StringStore size changedエラーspacy.strings.StringStore size changed, may indicate binary incompatibility NeuralCorefにimport neuralcorefをロードするときにバイナリの互換性を示している可能性があります。つまり、システムの最新バージョンのSpacyに対してビルドするためにホイールの代わりに分布のソースからNeuralCorefをインストールする必要があります。
この場合、次のようにNeuralCorefを再インストールするだけです。
pip uninstall neuralcoref
pip install neuralcoref --no-binary neuralcorefNeuralCorefを使用できるようにするには、スペイシーの英語モデルも必要です。
アプリケーションには英語モデルが正常に機能するものはすべて使用できますが、NeuralCorefのパフォーマンスは、スペイシーモデルのパフォーマンス、特にSpacyモデルのタガー、パーサー、NERコンポーネントのパフォーマンスに強く依存していることに注意してください。したがって、より大きなスペイシー英語モデルは、コアレファレンス解像度の品質も向上します(以下の内部およびモデルセクションの詳細を参照してください)。
スペイシーとスペイシーの(小さな)英語モデルをインストールする方法の例は次のとおりです。詳細については、SpacyのWebサイトをご覧ください。
pip install -U spacy
python -m spacy download enソースからNeuralCorefをインストールすることもできます。 CythonとSpacyを含む依存関係を最初にインストールする必要があります。
これがプロセスです:
venv .env
source .env/bin/activate
git clone https://github.com/huggingface/neuralcoref.git
cd neuralcoref
pip install -r requirements.txt
pip install -e . NeuralCorefは2つのサブモジュールでできています。
NeuralCorefをPythonで初めてインポートするときは、Neural Networkモデルの重みをキャッシュフォルダーにダウンロードします。
キャッシュフォルダーはデフォルトで~/.neuralcoref_cacheに設定されています(file_utils.pyを参照)が、この動作は環境変数NEURALCOREF_CACHEを設定して別の場所を指すことで克服できます。
キャッシュフォルダーはいつでも安全に削除でき、モジュールは次にロードされたときにモデルを再度ダウンロードします。
NeuralCorefを次のようにロードする前に、Pythonのloggingモジュールをアクティブにすることにより、内部モデルの場所、ダウンロード、およびキャッシュプロセスに関する詳細情報を入手できます。
import logging ;
logging . basicConfig ( level = logging . INFO )
import neuralcoref
> >> INFO : neuralcoref : Getting model from https : // s3 . amazonaws . com / models . huggingface . co / neuralcoref / neuralcoref . tar . gz or cache
> >> INFO : neuralcoref . file_utils : https : // s3 . amazonaws . com / models . huggingface . co / neuralcoref / neuralcoref . tar . gz not found in cache , downloading to / var / folders / yx / cw8n_njx3js5jksyw_qlp8p00000gn / T / tmp_8y5_52m
100 % | █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 40155833 / 40155833 [ 00 : 06 < 00 : 00 , 6679263.76 B / s ]
> >> INFO : neuralcoref . file_utils : copying / var / folders / yx / cw8n_njx3js5jksyw_qlp8p00000gn / T / tmp_8y5_52m to cache at / Users / thomaswolf / . neuralcoref_cache / f46bc05a4bfba2ae0d11ffd41c4777683fa78ed357dc04a23c67137abf675e14 . 7 d6f9a6fecf5cf09e74b65f85c7d6896b21decadb2554d486474f63b95ec4633
> >> INFO : neuralcoref . file_utils : creating metadata file for / Users / thomaswolf / . neuralcoref_cache / f46bc05a4bfba2ae0d11ffd41c4777683fa78ed357dc04a23c67137abf675e14 . 7 d6f9a6fecf5cf09e74b65f85c7d6896b21decadb2554d486474f63b95ec4633
>> > INFO : neuralcoref . file_utils : removing temp file / var / folders / yx / cw8n_njx3js5jksyw_qlp8p00000gn / T / tmp_8y5_52m
>> > INFO : neuralcoref : extracting archive file / Users / thomaswolf / . neuralcoref_cache / f46bc05a4bfba2ae0d11ffd41c4777683fa78ed357dc04a23c67137abf675e14 . 7 d6f9a6fecf5cf09e74b65f85c7d6896b21decadb2554d486474f63b95ec4633 to dir / Users / thomaswolf / . neuralcoref_cache / neuralcoref NeuralCorefをインスタンス化し、それをスペイシーの注釈のパイプラインに追加する推奨方法を次に示します。
# Load your usual SpaCy model (one of SpaCy English models)
import spacy
nlp = spacy . load ( 'en' )
# Add neural coref to SpaCy's pipe
import neuralcoref
neuralcoref . add_to_pipe ( nlp )
# You're done. You can now use NeuralCoref as you usually manipulate a SpaCy document annotations.
doc = nlp ( u'My sister has a dog. She loves him.' )
doc . _ . has_coref
doc . _ . coref_clustersSpacyモデルパイプにNeuralCorefを追加する同等の方法は、最初にNeuralCorefクラスをインスタンス化し、次にスペイシー言語モデルのパイプに手動で追加することです。
# Load your usual SpaCy model (one of SpaCy English models)
import spacy
nlp = spacy . load ( 'en' )
# load NeuralCoref and add it to the pipe of SpaCy's model
import neuralcoref
coref = neuralcoref . NeuralCoref ( nlp . vocab )
nlp . add_pipe ( coref , name = 'neuralcoref' )
# You're done. You can now use NeuralCoref the same way you usually manipulate a SpaCy document and it's annotations.
doc = nlp ( u'My sister has a dog. She loves him.' )
doc . _ . has_coref
doc . _ . coref_clusters NeuralCorefは、Coreferencesを解決し、._の下のSpacy Doc 、 SpanおよびTokenオブジェクトの拡張属性として注釈を付けます._.辞書。
これが注釈のリストです:
| 属性 | タイプ | 説明 |
|---|---|---|
doc._.has_coref | ブール | ドキュメントでコアレファレンスが解決されましたか |
doc._.coref_clusters | Clusterのリスト | ドキュメントでのコアファリングのすべてのクラスターは言及されています |
doc._.coref_resolved | Unicode | 各コアファー化された言及が関連するクラスターの主な言及に置き換えられるドキュメントのユニコード表現。 |
doc._.coref_scores | dict of dict | 言及間のコアレファレンス解像度のスコア。 |
span._.is_coref | ブール | スパンが少なくとも1つのコアファイリングの言及を持っているかどうか |
span._.coref_cluster | Cluster | スパンとCoreferが言及するクラスター |
span._.coref_scores | dict | 他の言及(該当する場合)とのコアレファレンス解像度のスコア。 |
token._.in_coref | ブール | トークンが少なくとも1つのコアファイリングの言及の中にあるかどうか |
token._.coref_clusters | Clusterのリスト | トークンを含むコアファリングの言及のすべてのクラスター |
Clusterは、3つの属性とクラスター内のナビゲーションを簡素化するいくつかの方法を備えたコアファーリング言及のクラスターです。
| 属性またはメソッド | タイプ /リターンタイプ | 説明 |
|---|---|---|
i | int | ドキュメントのクラスターのインデックス |
main | Span | クラスターで最も代表的な言及のスパン |
mentions | Spanのリスト | クラスター内のすべての言及のリスト |
__getitem__ | 戻りSpan | クラスターに言及にアクセスします |
__iter__ | Spanを生成します | クラスターでの言及を反復します |
__len__ | int | クラスターでの言及の数 |
Coreferenceクラスターチェーンを簡単にナビゲートし、クラスターや言及を表示することもできます。
いくつかの例を紹介します。自分でテストするために試してみてください。
import spacy
import neuralcoref
nlp = spacy . load ( 'en' )
neuralcoref . add_to_pipe ( nlp )
doc = nlp ( u'My sister has a dog. She loves him' )
doc . _ . coref_clusters
doc . _ . coref_clusters [ 1 ]. mentions
doc . _ . coref_clusters [ 1 ]. mentions [ - 1 ]
doc . _ . coref_clusters [ 1 ]. mentions [ - 1 ]. _ . coref_cluster . main
token = doc [ - 1 ]
token . _ . in_coref
token . _ . coref_clusters
span = doc [ - 1 :]
span . _ . is_coref
span . _ . coref_cluster . main
span . _ . coref_cluster . main . _ . coref_cluster重要:NeuralCorefの言及は、SPACY SPANオブジェクトです。つまり、 span.start (ドキュメント内のSPANの最初のトークンのインデックスのインデックス)、 span.end (ドキュメントのスパン後の最初のトークンのインデックス)などのすべての通常のSPAN属性にアクセスできます。
例: doc._.coref_clusters[1].mentions[-1].startドキュメント内の2番目のCoreferenceクラスターの最後の言及の最初のトークンのインデックスを提供します。
neuralcoref.add_to_pipeまたはNeuralCoref()にいくつかの追加パラメーターを渡すことができ、NeuralCorefの挙動を制御できます。
これらのパラメーターとその説明の完全なリストは次のとおりです。
| パラメーター | タイプ | 説明 |
|---|---|---|
greedyness | フロート | 0と1の間で、モデルがコアファレンスの決定を行うことについての貪欲であると判断します(より貪欲なことは、より多くのコアファレンスリンクを意味します)。デフォルト値は0.5です。 |
max_dist | int | 現在の言及の前件の可能性を検討する際に、何件の言及が見られるか。値を減らすと、システムはより速く実行されますが、より正確に実行されます。デフォルト値は50です。 |
max_dist_match | int | システムは、名詞または適切な名詞を共有する場合、現在の言及を前の言及にさらにmax_distするものにリンクすることを検討します。この場合、代わりにmax_dist_matchを離れて見えます。デフォルト値は500です。 |
blacklist | ブール | 次のリストで["i", "me", "my", "you", "your"]システムが代名詞の共同参照を解決した場合:デフォルト値はtrueです(Coreference Resolved)。 |
store_scores | ブール | システムは、注釈のコアフェルエンスのスコアを保存する必要があります。デフォルト値は真です。 |
conv_dict | dict(str、list(str)) | まれな単語(キー)の埋め込みを、一般的な単語のリスト(値)の平均埋め込みに置き換えるために使用できる変換辞書。例: conv_dict={"Angela": ["woman", "girl"]} Angelaの埋め込みの代わりに、より一般的なwomanとgirlに埋め込みを使用して、 Angelaの共同参照を解決するのに役立ちます。これは現在、単一の単語でのみ機能します(単語グループではありません)。 |
import spacy
import neuralcoref
# Let's load a SpaCy model
nlp = spacy . load ( 'en' )
# First way we can control a parameter
neuralcoref . add_to_pipe ( nlp , greedyness = 0.75 )
# Another way we can control a parameter
nlp . remove_pipe ( "neuralcoref" ) # This remove the current neuralcoref instance from SpaCy pipe
coref = neuralcoref . NeuralCoref ( nlp . vocab , greedyness = 0.75 )
nlp . add_pipe ( coref , name = 'neuralcoref' )パラメーターconv_dict使用して、名前のようなまれな単語のコアフェレンスの解決を支援する方法の例を以下に示します。
import spacy
import neuralcoref
nlp = spacy . load ( 'en' )
# Let's try before using the conversion dictionary:
neuralcoref . add_to_pipe ( nlp )
doc = nlp ( u'Deepika has a dog. She loves him. The movie star has always been fond of animals' )
doc . _ . coref_clusters
doc . _ . coref_resolved
# >>> [Deepika: [Deepika, She, him, The movie star]]
# >>> 'Deepika has a dog. Deepika loves Deepika. Deepika has always been fond of animals'
# >>> Not very good...
# Here are three ways we can add the conversion dictionary
nlp . remove_pipe ( "neuralcoref" )
neuralcoref . add_to_pipe ( nlp , conv_dict = { 'Deepika' : [ 'woman' , 'actress' ]})
# or
nlp . remove_pipe ( "neuralcoref" )
coref = neuralcoref . NeuralCoref ( nlp . vocab , conv_dict = { 'Deepika' : [ 'woman' , 'actress' ]})
nlp . add_pipe ( coref , name = 'neuralcoref' )
# or after NeuralCoref is already in SpaCy's pipe, by modifying NeuralCoref in the pipeline
nlp . get_pipe ( 'neuralcoref' ). set_conv_dict ({ 'Deepika' : [ 'woman' , 'actress' ]})
# Let's try agin with the conversion dictionary:
doc = nlp ( u'Deepika has a dog. She loves him. The movie star has always been fond of animals' )
doc . _ . coref_clusters
# >>> [Deepika: [Deepika, She, The movie star], a dog: [a dog, him]]
# >>> 'Deepika has a dog. Deepika loves a dog. Deepika has always been fond of animals'
# >>> A lot better! REST APIにNeuralCorefを統合するためのサーバースクリプトの簡単な例はexamples/server.pyの例として提供されています。
それを使用するには、最初にFalconをインストールする必要があります:
pip install falconその後、次のようにサーバーを起動できます。
cd examples
python ./server.pyそして、このようなサーバーをクエリします:
curl --data-urlencode " text=My sister has a dog. She loves him. " -G localhost:8000NeuralCorefを管理および展開できる他の多くの方法があります。いくつかの例は、スペイシーの宇宙にあります。
モデルを再訓練するか、別の言語でトレーニングする場合は、トレーニングの指示とブログ投稿をご覧ください