Spacy Ask Questionを使用して動詞句を抽出する

Spacy Ask Questionを使用して動詞句を抽出する

私は、Spacy が提供する Doc.noun_chunks プロパティを使用して名詞チャンク抽出に Spacy を使用しています。Spacy ライブラリ (形式 'VERB ? ADV * VERB +') を使用して入力テキストから動詞句を抽出するにはどうすればよいですか?

ベストアンサー1

これが役に立つかもしれません。

from __future__ import unicode_literals
import spacy,en_core_web_sm
import textacy
nlp = en_core_web_sm.load()
sentence = 'The author is writing a new book.'
pattern = r'<VERB>?<ADV>*<VERB>+'
doc = textacy.Doc(sentence, lang='en_core_web_sm')
lists = textacy.extract.pos_regex_matches(doc, pattern)
for list in lists:
    print(list.text)

出力:

is writing

動詞句を強調表示する方法に関しては、以下のリンクを確認してください。

Spacy と HTML を使用して動詞句を強調表示する

別のアプローチ:

最近、Textacy が正規表現の一致にいくつかの変更を加えたことがわかりました。そのアプローチに基づいて、この方法を試しました。

from __future__ import unicode_literals
import spacy,en_core_web_sm
import textacy
nlp = en_core_web_sm.load()
sentence = 'The cat sat on the mat. He dog jumped into the water. The author is writing a book.'
pattern = [{'POS': 'VERB', 'OP': '?'},
           {'POS': 'ADV', 'OP': '*'},
           {'POS': 'VERB', 'OP': '+'}]
doc = textacy.make_spacy_doc(sentence, lang='en_core_web_sm')
lists = textacy.extract.matches(doc, pattern)
for list in lists:
    print(list.text)

出力:

sat
jumped
writing

このリンクの POS 一致を確認しましたが、結果は意図したものとは異なるようです。

[https://explosion.ai/demos/matcher][1]

動詞句を見つけるために、正規表現パターンの代わりに POS タグをフレーミングしようとした人はいますか?

編集2:

import spacy   
from spacy.matcher import Matcher
from spacy.util import filter_spans

nlp = spacy.load('en_core_web_sm') 

sentence = 'The cat sat on the mat. He quickly ran to the market. The dog jumped into the water. The author is writing a book.'
pattern = [{'POS': 'VERB', 'OP': '?'},
           {'POS': 'ADV', 'OP': '*'},
           {'POS': 'AUX', 'OP': '*'},
           {'POS': 'VERB', 'OP': '+'}]

# instantiate a Matcher instance
matcher = Matcher(nlp.vocab)
matcher.add("Verb phrase", None, pattern)

doc = nlp(sentence) 
# call the matcher to find matches 
matches = matcher(doc)
spans = [doc[start:end] for _, start, end in matches]

print (filter_spans(spans))   

出力:

[sat, quickly ran, jumped, is writing]

mdmjsh の回答の助けに基づきます。

編集3: 奇妙な動作。次の文では、次のパターンの動詞句が正しく識別されます。https://explosion.ai/demos/matcher

pattern = [{'POS': 'VERB', 'OP': '?'},
           {'POS': 'ADV', 'OP': '*'},
           {'POS': 'VERB', 'OP': '+'}]

真っ黒な猫本当にニャーニャー鳴いているに違いない庭で本当にうるさいです。

ただし、コードから実行すると次のように出力されます。

[本当にニャーニャー鳴いている]

おすすめ記事