我正在嘗試匹配特定模式:任何以 as、t 或 l 結尾的名詞的動詞。例如:喜歡貓,吃飯,做香料
我怎樣才能做到這一點?
我知道我正在這樣做:
nlp =spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocable)
pattern = [{"POS": "VERB"}, {"POS": "NOUN"}]
matcher.add("mypattern", [pattern])
?doc = nlp(Verbwithnoun)
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id]
print(doc[start:end)
但這會列印所有帶名詞的動詞,而不是以 at、l 或 s 結尾的名詞。我怎樣才能讓 spacy 只匹配以 at、l 或 s 結尾的特定名詞?
uj5u.com熱心網友回復:
您可以通過檢查您得到的短語是否以三個字母中的任何一個結尾來對結果進行后處理:
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
pattern = [{"POS": "VERB"}, {"POS": "DET", "OP" : "?"}, {"POS": "NOUN"}]
matcher.add("mypattern", [pattern])
Verbwithnoun = "I know the language. I like the cat, I eat a meal, I make spices."
doc = nlp(Verbwithnoun)
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id]
phrase = doc[start:end]
if phrase.text.endswith('s') or phrase.text.endswith('t') or phrase.text.endswith('l'):
print(doc[start:end])
輸出:
like the cat
eat a meal
make spices
uj5u.com熱心網友回復:
后處理很好,但您也可以直接在模式中使用正則運算式。請參閱檔案。
nlp =spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocable)
pattern = [{"POS": "VERB"}, {"POS": "NOUN", "TEXT": {"REGEX": "[lst]$"}}]
matcher.add("mypattern", [pattern])
?doc = nlp(Verbwithnoun)
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id]
print(doc[start:end)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399335.html
