【问题标题】:How to extract sentences with key phrases in spaCy如何在 spaCy 中提取带有关键短语的句子
【发布时间】:2020-10-27 18:47:21
【问题描述】:

我与 Spacy 合作过,到目前为止,我发现 NLP 非常直观和强大。 我正在尝试使用文本句子搜索这两种方式 word base 以及 content type base 搜索,但到目前为止,我找不到任何带有 spacy 的解决方案。

我有这样的文字:

在计算机科学中,人工智能 (AI),有时称为 机器智能,是机器展示的智能,不像 人类和动物表现出的自然智慧。领先的人工智能 教科书将该领域定义为“智能代理”的研究:任何 感知其环境并采取最大化行动的设备 它成功实现其目标的机会。 [1]通俗地说, 术语“人工智能”通常用于描述机器(或 计算机)模仿人类与之相关的“认知”功能 人的思想,例如“学习”和“解决问题”。[2]

随着机器的能力越来越强,被认为需要 “智能”经常从人工智能的定义中删除, 被称为人工智能效应的现象。 [3]特斯勒定理中的一句俏皮话说 “人工智能是尚未完成的事情。”[4] 例如,光学 字符识别经常被排除在考虑的事情之外 成为人工智能,[5] 已成为一种常规技术。 [6]现代机器 通常归类为 AI 的能力包括成功 理解人类语言,[7] 在最高水平的领域竞争 战略游戏系统(如国际象棋和围棋),[8] 自主 运营汽车,内容交付网络中的智能路由,以及 军事模拟[9]。

人工智能作为一门学科成立于 1955 年, 并在此后的几年里经历了几波 乐观,[10][11] 其次是失望和资金损失 (被称为“AI 冬天”),[12][13] 紧随其后的是新方法,成功 并重新获得资金。 [11][14]在其历史的大部分时间里,人工智能研究 被划分为通常无法与每个子域进行通信的子域 其他。[15]这些子领域基于技术考虑, 例如特定目标(例如“机器人”或“机器学习”),[16] 使用特定工具(“逻辑”或人工神经网络), 或深刻的哲学差异。[17][18][19]子领域也有 基于社会因素(特定机构或工作 特定的研究人员)。[15]

现在,我想通过多个单词或字符串匹配来提取多个完整的句子。例如,我想搜索 intelligentmachine learning。它会打印所有包含这个或两个给定字符串的完整句子。

用 spacy 导入 spacy 模型有什么方法可以感知短语匹配.. 就像它找到所有包含单词的智能和机器学习并打印出来一样?以及其他选项,它是否也可以像搜索机器学习一样找到,还建议深度学习、人工智能、模式识别等?

import spacy
nlp = spacy.load("en_core_web_sm")
from spacy.matcher import PhraseMatcher
phrase_matcher = PhraseMatcher(nlp.vocab)

phrases = ['machine learning', ''intelligent, 'human']

patterns = [nlp(text) for text in phrases]

phrase_matcher.add('AI', None, *patterns)

sentence = nlp (processed_article)

matched_phrases = phrase_matcher(sentence)

for match_id, start, end in matched_phrases:
    string_id = nlp.vocab.strings[match_id]  
    span = sentence[start:end]                   
    print(match_id, string_id, start, end, span.text)

我试过这个,它没有提供完整的句子,而只提供与 ID 号匹配的单词。

简而言之,

  1. 我正在尝试使用多个输入的单词进行搜索,并找到包含输入单个字符串或全部的完整句子
  2. 我正在尝试使用经过训练的模型从输入中找到建议的句子。

【问题讨论】:

  • 如何知道原句中所有词组的起止索引?

标签: python nlp spacy


【解决方案1】:

第 1 部分:

我想搜索智能和机器学习。它会打印所有包含这个或两个给定字符串的完整句子。

您可以通过这种方式找到包含您要查找的关键字的完整句子。请记住,句子边界是通过统计确定的,因此,如果传入的段落来自新闻或维基百科,它会很好地工作,但如果数据来自社交媒体,它就不会很好地工作。

import spacy
from spacy.matcher import PhraseMatcher

text = """I like tomtom and I cannot lie. In computer science, artificial intelligence (AI), sometimes called machine intelligence, is intelligence demonstrated by machines, unlike the natural intelligence displayed by humans and animals.  Leading AI textbooks define the field as the study of "intelligent agents": any device that perceives its  environment and takes actions that maximize its chance of successfully achieving its goals.[1] Colloquially,  the term "artificial intelligence" is often used to describe machines (or computers) that mimic "cognitive"  functions that humans associate with the human mind, such as "learning" and "problem solving".[2] """

nlp = spacy.load("en_core_web_sm")

phrase_matcher = PhraseMatcher(nlp.vocab)
phrases = ['machine learning', 'artificial intelligence']
patterns = [nlp(text) for text in phrases]
phrase_matcher.add('AI', None, *patterns)

doc = nlp(text)

for sent in doc.sents:
    for match_id, start, end in phrase_matcher(nlp(sent.text)):
        if nlp.vocab.strings[match_id] in ["AI"]:
            print(sent.text)

输出

In computer science, artificial intelligence (AI), sometimes called machine intelligence, is intelligence demonstrated by machines, unlike the natural intelligence displayed by humans and animals.  
Colloquially,  the term "artificial intelligence" is often used to describe machines (or computers)

第 2 部分:

它是否也可以像搜索机器学习一样找到,还建议深度学习、人工智能、模式识别等?

是的。这很有可能,您需要使用word2vecsense2vec 才能做到这一点。

【讨论】:

  • 如何知道原句中所有词组的起止索引?
猜你喜欢
  • 1970-01-01
  • 2019-11-24
  • 2016-05-13
  • 2021-01-11
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
相关资源
最近更新 更多