【问题标题】:NLTK: How can I extract information based on sentence maps?NLTK:如何根据句子图提取信息?
【发布时间】:2016-07-10 11:39:17
【问题描述】:

我知道您可以使用名词提取从句子中提取名词,但我如何使用句子叠加/映射来提取短语?

例如:

句子叠加:

"First, @action; Second, Foobar"

输入:

"First, Dance and Code; Second, Foobar"

我要返回:

action = "Dance and Code"
  • 普通名词提取不起作用,因为它不会总是名词
  • 句子的措辞方式不同,因此不能是单词[x] ...因为单词的位置发生了变化

【问题讨论】:

  • 分类器和这个任务有什么关系?
  • 我想对输入进行分类,然后使用训练数据中@变量的映射从输入中提取变量
  • 你知道“分类输入”是什么意思吗?你追求什么分类?
  • 更广泛地说,我想使用朴素贝叶斯对输入进行分类以获得所需的操作。例如:“提醒我在 y 做 x”=>reminder.set。然后我想找到x和y
  • 好主意,去吧。

标签: python nlp artificial-intelligence nltk


【解决方案1】:

您可以稍微重写您的字符串模板以将它们转换为正则表达式,并查看哪个(或哪些)匹配。

>>> template = "First, (?P<action>.*); Second, Foobar"
>>> mo = re.search(template, "First, Dance and Code; Second, Foobar")
>>> if mo:
        print(mo.group("action"))
Dance and Code

您甚至可以将现有字符串转换为这种正则表达式(在转义像 .?*() 这样的正则表达式元字符之后)。

>>> template = "First, @action; (Second, Foobar...)"
>>> re_template = re.sub(r"\\@(\w+)", r"(?P<\g<1>>.*)", re.escape(template))
>>> print(re_template)
First\,\ (?P<action>.*)\;\ \(Second\,\ Foobar\.\.\.\)

【讨论】:

    猜你喜欢
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多