如何在spaCy中提取带有关键短语的句子

ML8*_*L85 4 python nlp spacy

我曾与 Spacy 合作过,到目前为止,我发现 Spacy 在 NLP 方面非常直观和强大。我试图从文本句子中进行搜索,这既是搜索word base也是content type base搜索,但到目前为止,我还没有找到任何与 spacy 相关的解决方案。

我有这样的文字:

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

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

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

现在,我想提取多个单词或字符串匹配的多个完整句子。例如,我想搜索intelligent和machine 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)
Run Code Online (Sandbox Code Playgroud)

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

简而言之,

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

Raq*_*qib 5

第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)
Run Code Online (Sandbox Code Playgroud)

输出

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)
Run Code Online (Sandbox Code Playgroud)

第2部分:

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

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