【问题标题】:processing grammar using spaCY使用 spaCY 处理语法
【发布时间】:2018-06-29 14:22:50
【问题描述】:

我将检查我输入的句子中的语法。如果 spaCy 在一个句子中识别 PRPMDNN 那么它会给我一个文本输出

句子中有prp、md和nn

问题是:我如何告诉 spaCy 检查PRPMDNN,然后给我所需的文本输出?

这是目前能够识别文本输入语法的代码:

import spacy

sent=input("insert sentence: \n\n")
nlp=spacy.load('en')
doc=nlp(sent)
for token in doc:
    print(token.text, token.tag_, token.dep_)

【问题讨论】:

  • 你能发布你想要的输出吗?
  • 正如我所写的,我试图将文本字符串“句子中存在 prp、md 和 nn”作为输出,仅当 spaCy 实际检测到 PRP、MD 和 NN 在输入(例如:“我要去商场”它包含所有这些)

标签: python python-3.x machine-learning artificial-intelligence spacy


【解决方案1】:

如果我理解正确:

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]: True

In [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']

【讨论】:

  • 是的。谢谢你,先生!它似乎工作正常。最后一件事,例如,如果我只想打印出句子“I will go to the mall”中包含的 NN 相关词,在这种情况下是“I”。如何读取标记为 NN 的单词并将其打印为输出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
相关资源
最近更新 更多