【问题标题】:How can I get the noun clause that is the object of a certain verb?如何获得作为某个动词宾语的名词从句?
【发布时间】:2018-03-28 19:36:08
【问题描述】:

我正在处理来自药品标签的数据。文本始终使用动词短语“indicated for”来构建。

例如:

sentence = "Meloxicam tablet is indicated for relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis"

我已经使用 SpaCy 过滤到仅包含短语“indicated for”的句子。

我现在需要一个函数来接收句子,并返回作为“indicated for”对象的短语。所以对于这个例子,我称之为extract()的函数将像这样运行:

extract(sentence)
>> 'relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis'

有没有使用 spacy 的功能?

编辑: 简单地在 'indicated for' 之后拆分不适用于复杂的示例。

这里有一些例子:

'''丁丙诺啡和纳洛酮舌下片适用于阿片类药物依赖的维持治疗,应作为完整治疗计划的一部分,包括咨询和社会心理支持丁丙诺啡和纳洛酮舌下片含有丁丙诺啡是阿片类药物的部分激动剂,纳洛酮是阿片类药物的拮抗剂,适用于阿片类药物依赖的维持治疗'''

'''氧氟沙星滴眼液适用于治疗下列细菌敏感菌株引起的感染 结膜炎 革兰氏阳性菌 革兰氏阴性菌 金黄色葡萄球菌 表皮葡萄球菌 肺炎链球菌 肠杆菌阴沟杆菌 流感嗜血杆菌 奇异变形杆菌 绿脓杆菌 角膜溃疡 革兰氏阳性菌 革兰氏阴性菌 金黄色葡萄球菌 表皮葡萄球菌 肺炎链球菌 绿脓杆菌 粘质沙雷氏菌'''''''

我只想要粗体部分。

【问题讨论】:

  • 你能给出更多的段落或变体吗?

标签: python nlp spacy


【解决方案1】:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import unicode_literals
import spacy
nlp = spacy.load('en_core_web_sm')
text = 'Meloxicam tablet is indicated for relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis.'
doc = nlp(text)
for word in doc:
    if word.dep_ in ('pobj'):
        subtree_span = doc[word.left_edge.i : word.right_edge.i + 1]
        print(subtree_span.text)

输出:

relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis
the signs and symptoms of osteoarthritis and rheumatoid arthritis
osteoarthritis and rheumatoid arthritis

多个输出的原因是由于多个pobj。

编辑 2:

# -*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import unicode_literals
import spacy
nlp = spacy.load('en_core_web_sm')
para = '''Meloxicam tablet is indicated for relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis.
Ofloxacin ophthalmic solution is indicated for the treatment of infections caused by susceptible strains of the following bacteria in the conditions listed below.'''
doc = nlp(para)

# To extract sentences based on key word
indicated_for_sents = [sent for sent in doc.sents if 'indicated for' in sent.string]
print indicated_for_sents
print
# To extract objects of verbs
for word in doc:
    if word.dep_ in ('pobj'):
        subtree_span = doc[word.left_edge.i : word.right_edge.i + 1]
        print(subtree_span.text)

输出:

[Meloxicam tablet is indicated for relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis.
, Ofloxacin ophthalmic solution is indicated for the treatment of infections caused by susceptible strains of the following bacteria in the conditions listed below.]

relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis
the signs and symptoms of osteoarthritis and rheumatoid arthritis
osteoarthritis and rheumatoid arthritis


the treatment of infections caused by susceptible strains of the following bacteria in the conditions listed below
infections caused by susceptible strains of the following bacteria in the conditions listed below
susceptible strains of the following bacteria in the conditions listed below
the following bacteria in the conditions listed below
the conditions listed below

查看此链接

https://github.com/NSchrading/intro-spacy-nlp/blob/master/subject_object_extraction.py

【讨论】:

    【解决方案2】:

    你需要使用 Spacy 的依赖解析功能。选择的包含 ('indicated for') 的句子应该在 Spacy 中进行依赖解析,以显示所有单词之间的关系。您可以使用 Spacy here 看到问题中例句的依赖解析可视化。

    在 Spacy 返回依赖解析后,您需要搜索“指示”标记作为动词并找到依赖树的孩子。请参阅示例here。在您的情况下,您将寻找将“指示”匹配为动词并获取孩子而不是 Github 示例中的“xcomp”或“ccomp”。

    【讨论】:

      【解决方案3】:

      您不需要 SpaCy。你可以做正则表达式或者只是拆分:

      sentence = "Meloxicam tablet is indicated for relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis"
      sentence.split('indicated for ')[1]
      >>> relief of the signs and symptoms of osteoarthritis and rheumatoid arthritis
      

      这是基于对字符串的假设,例如“indicated for”只出现一次,之后的所有内容都是您想要的,等等。

      语法说明:您要查找的实际上是间接宾语,而不是主语。题目是“美洛昔康片”。

      【讨论】:

      • 好点,但这仅适用于简单的示例——请参阅我的编辑。
      【解决方案4】:

      尝试查看此Noun phrases with spacyhttps://spacy.io/usage/linguistic-features#noun-chunks。我不是 SpaCy 的专家,但这应该会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-18
        • 1970-01-01
        • 2010-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多