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



我曾与Spacy合作过,到目前为止,我发现NLP非常直观和稳健。我正在尝试进行文本外句子搜索,这是word basecontent type base搜索的两种方式,但到目前为止,我还找不到任何spacy的解决方案。

我有这样的文本:

在计算机科学中,人工智能(AI(,有时也称为机器智能,是由机器展示的智能,不同于人类和动物表现出的自然智慧。领先的人工智能教科书将该领域定义为";"智能代理":任何感知环境并采取最大化行动的设备成功实现目标的机会。[1] 口头上术语";人工智能;通常用于描述机器(或计算机(;认知的";人类与之相关的功能人类的心智,例如";学习;以及";解决问题";。[2]

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

人工智能作为一门学科创立于1955年,在此后的几年里乐观,[10][11]然后是失望和资金损失(被称为"人工智能的冬天"(,[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)

我尝试了这个,它没有提供完整的句子,只提供了带有匹配身份证号码的单词。

简而言之,

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

第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才能做到这一点。

最新更新