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

问题描述 投票:0回答:2

我曾与 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)

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

简而言之,

  1. 我正在尝试使用多个单词输入进行搜索,并找到包含输入单个字符串或全部的完整句子
  2. 我正在尝试使用经过训练的模型来从输入中找到建议的句子。
python nlp spacy
2个回答
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)

输出

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 部分:

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

是的。这很有可能,您需要使用

word2vec
sense2vec
才能做到这一点。


0
投票

在 spaCy 中,您可以使用 (NER) 命名实体识别来抽象带有关键短语的句子。首先,加载 spaCy 模型。然后,分析你的文本。重复检查句子并使用 NER 来识别实体。如果某个对象与您的关键短语匹配,请提取相似的句子。这样,spaCy 可以帮助您在文本数据中找到包含特殊关键短语的句子。

© www.soinside.com 2019 - 2024. All rights reserved.