我打算检查我输入的句子中的语法。如果spaCy在一个句子中识别出PRP
、MD
和NN
,那么它会给我一个文本输出
句子中有prp、md和nn
问题是:我如何告诉spaCy检查PRP
、MD
和NN
,然后给我想要的文本输出?
到目前为止,这是能够识别文本输入语法的代码:
import spacysent=input("insert sentence: \n\n")nlp=spacy.load('en')doc=nlp(sent)for token in doc: print(token.text, token.tag_, token.dep_)
回答:
如果我理解正确的话:
In [34]: chk_set = set(['PRP','MD','NN'])In [35]: chk_set.issubset(t.tag_ for t in nlp("I will go to the mall"))Out[35]: TrueIn [36]: chk_set.issubset(t.tag_ for t in nlp("I will go"))Out[36]: False
更新:
我如何读取标记为NN的词并打印出来?
In [53]: [t.text for t in nlp("I will go to the mall") if t.tag_ in ['NN']]Out[53]: ['mall']