构建生物医学知识图谱from zero to hero (5):关系抽取
发布时间
阅读量:
阅读量
在当前研究中,我们致力于探索医学概念间的关联。基于经验判断,在此领域中进行关系抽取要比进行命名实体抽取高出一个数量级。即使不想使命名实体识别系统达到完美的效果,在某种程度上也会导致抽取过程出现一些偏差。
采用零剪切法中的关系提取模块FewRel。尽管我不推荐将此模型部署到生产环境中,但该模型能够演示其基本功能。通过HuggingFace平台获取该model无需进行额外的训练并完成配置参数设置。
from transformers import AutoTokenizer
from zero_shot_re import RelTaggerModel, RelationExtractor
model = RelTaggerModel.from_pretrained("fractalego/fewrel-zero-shot")
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
relations = ['associated', 'interacts']
extractor = RelationExtractor(model, tokenizer, relations)
AI助手
通过zero-shot关系提取器技术,在本例中用于定义需检测的具体关系类型。在这一案例中特别应用了关联性与互动性两种主要关系进行分析。然而,在进一步探索时发现,并非所有的特定类别都能获得预期效果
基于该模型进行关系抽取时,请明确指定需检测的具体实体对范围。该过程将依赖于命名实体识别所得的结果作为输入数据源。首先, 我们会识别包含两个以上实体的语句段落, 然后通过预处理生成候选关系对并传递给关系提取模型进行运算以获取潜在连接信息。此外, 本系统设定最低置信度为0.85, 即只有当模型预测结果达到此标准及以上时才认为存在显著关联
import itertools
# Candidate sentence where there is more than a single entity present
candidates = [s for s in parsed_entities if (s.get('entities')) and (len(s['entities']) > 1)]
predicted_rels = []
for c in candidates:
combinations = itertools.combinations([{'name':x['entity'], 'id':x['entity_id']} for x in c['entities']], 2)
for combination in list(combinations):
try:
ranked_rels = extractor.rank(text=c['text'].replace(",", " "), head=combination[0]['name'], tail=combination[1]['name'])
# Define threshold for the most probable relation
if ranked_rels[0][1] > 0.85:
predicted_rels.append({'head': combination[0]['id'], 'tail': combination[1]['id'], 'type':ranked_rels[0][0], 'source': c['text_sha256']})
except:
pass
# Store relations to Neo4j
neo4j_query("""
UNWIND $data as row
MATCH (source:Entity {id: row.head})
MATCH (target:Entity {id: row.tail})
MATCH (text:Sentence {id: row.source})
MERGE (source)-[:REL]->(r:Relation {type: row.type})-[:REL]->(target)
MERGE (text)-[:MENTIONS]->(r)
""", {'data': predicted_rels})
AI助手

抽取关系到图数据库中
全部评论 (0)
还没有任何评论哟~
