首页 > 解决方案 > 我需要从英语句子中自动提取逻辑语句(SWRL)

问题描述

(对不起,我的英语,我也是新手,所以要温柔,谢谢)

我正在尝试从包含动作和条件的任何可能的句子中提取逻辑语句(SWRL)

这是我想要获得的那种逻辑陈述:

如果(条件)那么(动作|不动作|动作或不动作)

我一直在尝试将一些 NLP 技术与SpacyStanford NLP library一起应用,但我对语法英语结构的了解不足,这对我来说几乎是不可能的。

我想知道是否有人可以帮助我进行这项研究,无论是有想法还是为我提供未知的库。

例如:

import nltk
import spacy
nlp = spacy.load('en_core_web_sm')

sent="The speed limit is 90 kilometres per hour on roads outside built-up areas."
doc=nlp(sent)

获取根:

def sent_root(sent):
    for index,token in enumerate(sent):
        if token.head == token:
            return token, index

出:(是,3)

获取主题:

def sent_subj(sent):
    for index,token in enumerate(sent):
        if token.dep_ == 'nsubj':
            return token, index

输出:(限制,2)

获取孩子(单词的依赖):

def sent_child(token):
    complete_subj = ''
    for child in token.children:
        if(child.is_punct == False):
            if(child.dep_ == 'compound'):
                complete_subj += child.text + ' ' + token.text+' '
            else:
                complete_subj += child.text + ' '
            for child_token in child.children:
                if(child.is_punct == False):
                    complete_subj += child_token.text+' '

    return complete_subj

出:“限速”

文档 + 根:

def doc_ents_root(sent, root):
    ents_root = root.text+' '
    for token in sent.ents:
        ents_root += token.text + ' '

    return ents_root

出:'是每小时90公里'

提取动作:

def action(sent):
    #Obtaining the sent root
    root, root_idx = sent_root(sent)

    #Obtaining the subject
    subj, subj_idx = sent_subj(sent)

    #Obtaining the whole subject (subj + comps)
    complete_subj = sent_child(subj)

    complete_ents = doc_ents_root(sent, root)

    return complete_subj + complete_ents

应用所有功能

action(doc)

Out: '有信号的红绿灯指示'

标签: pythonnlptext-classificationspacy

解决方案


推荐阅读