python - 我需要从英语句子中自动提取逻辑语句(SWRL)
问题描述
(对不起,我的英语,我也是新手,所以要温柔,谢谢)
我正在尝试从包含动作和条件的任何可能的句子中提取逻辑语句(SWRL)
这是我想要获得的那种逻辑陈述:
如果(条件)那么(动作|不动作|动作或不动作)
我一直在尝试将一些 NLP 技术与Spacy和Stanford 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: '有信号的红绿灯指示'
解决方案
推荐阅读
- ruby-on-rails - rspec 测试时处理环境源常量的最佳方法?
- java - 如何使用日期格式 yyyy-MM-dd hh:mm a 在 Java 8 中解决不可解析的日期异常
- javascript - 改变画布中两个图形之间旋转速度的问题
- cakephp - 如何从 Microsoft OAuth2 解码访问令牌
- c++ - C++ 在执行多态时是否接受内联虚函数?
- node.js - 从 docker 容器获取音频到主机
- kubernetes - 使用 terraform 覆盖 helm 图表中的配置文件
- c++ - esp8266mod板的gps和超声波代码
- python - Django TypeError:“PosixPath”类型的参数不可迭代
- java - Debuggee 节点在 VMStartEvent 之后立即发送 VMDeathEvent | Java 调试接口