|
|
|
@ -15,6 +15,7 @@ import org.springframework.ai.ollama.OllamaChatModel;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
@ -102,15 +103,15 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
log.info("doTableEre响应结果:{}", response);
|
|
|
|
|
// todo:暂时不去处理异常返回
|
|
|
|
|
EREDTO eredto = EREDTO.fromTableJson(response, truncateDTO.getId());
|
|
|
|
|
// 手动设置表格标题
|
|
|
|
|
EntityExtractionDTO titleEntity = new EntityExtractionDTO();
|
|
|
|
|
titleEntity.setEntity("表");
|
|
|
|
|
titleEntity.setName(truncateDTO.getTitle());
|
|
|
|
|
//
|
|
|
|
|
// 添加关系
|
|
|
|
|
ArrayList<RelationExtractionDTO> relations = new ArrayList<>();
|
|
|
|
|
List<RelationExtractionDTO> relations = new ArrayList<>();
|
|
|
|
|
for (EntityExtractionDTO entity : eredto.getEntities()) {
|
|
|
|
|
RelationExtractionDTO relationExtractionDTO = new RelationExtractionDTO(truncateDTO.getId(),
|
|
|
|
|
titleEntity.getEntity(), titleEntity.getName(), "包含", entity.getEntity(), entity.getName(), entity.getAttributes());
|
|
|
|
|
titleEntity.getName(), titleEntity.getEntity(), "包含", entity.getName(), entity.getEntity(), entity.getAttributes());
|
|
|
|
|
relations.add(relationExtractionDTO);
|
|
|
|
|
}
|
|
|
|
|
eredto.getEntities().add(titleEntity);
|
|
|
|
@ -120,6 +121,7 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 合并实体关系抽取结果 主要是对实体和关系中的属性进行合并
|
|
|
|
|
* 表不参与合并
|
|
|
|
|
* @param eredtoList 实体关系抽取结果列表
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
@ -129,6 +131,13 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
if (CollUtil.isEmpty(eredtoList)){
|
|
|
|
|
return merged;
|
|
|
|
|
}
|
|
|
|
|
// 将表单独拿出来
|
|
|
|
|
merged = eredtoList.stream().filter(ere->
|
|
|
|
|
ere.getEntities().stream().anyMatch(e->StrUtil.equals(e.getEntity(),"表"))).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// 把剩下的数据进行合并计算
|
|
|
|
|
eredtoList = eredtoList.stream().filter(ere->
|
|
|
|
|
ere.getEntities().stream().noneMatch(e->StrUtil.equals(e.getEntity(),"表"))).collect(Collectors.toList());
|
|
|
|
|
Map<String, EntityExtractionDTO> entityMap = new HashMap<>();
|
|
|
|
|
Map<String, RelationExtractionDTO> relationMap = new HashMap<>();
|
|
|
|
|
for (EREDTO eredto : eredtoList) {
|
|
|
|
@ -184,17 +193,17 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
|
|
|
|
|
private void mergeAttribute(Map<String, RelationExtractionDTO> entityMap,RelationExtractionDTO relation, String key) {
|
|
|
|
|
|
|
|
|
|
RelationExtractionDTO cachedEntity = entityMap.get(key);
|
|
|
|
|
if (null == cachedEntity){
|
|
|
|
|
RelationExtractionDTO cachedRelation = entityMap.get(key);
|
|
|
|
|
if (null == cachedRelation){
|
|
|
|
|
entityMap.put(key, relation);
|
|
|
|
|
}else {
|
|
|
|
|
if (CollUtil.isEmpty(relation.getAttributes())){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 合并属性
|
|
|
|
|
List<ERAttributeDTO> attributes = relation.getAttributes();
|
|
|
|
|
if (null == attributes){
|
|
|
|
|
attributes = new ArrayList<>();
|
|
|
|
|
List<ERAttributeDTO> cachedAttributes = cachedRelation.getAttributes();
|
|
|
|
|
if (null == cachedAttributes){
|
|
|
|
|
cachedAttributes = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
for (ERAttributeDTO attribute : relation.getAttributes()) {
|
|
|
|
|
String attributeKey = attribute.getAttribute();
|
|
|
|
@ -203,8 +212,8 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// 如果属性已经存在,则不添加
|
|
|
|
|
if (attributes.stream().noneMatch(a -> StrUtil.equals(a.getAttribute(), attributeKey))) {
|
|
|
|
|
attributes.add(attribute);
|
|
|
|
|
if (cachedAttributes.stream().noneMatch(a -> StrUtil.equals(a.getAttribute(), attributeKey))) {
|
|
|
|
|
cachedAttributes.add(attribute);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -219,9 +228,10 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 合并属性
|
|
|
|
|
List<ERAttributeDTO> attributes = entity.getAttributes();
|
|
|
|
|
if (null == attributes){
|
|
|
|
|
attributes = new ArrayList<>();
|
|
|
|
|
List<ERAttributeDTO> cachedAttributes = cachedEntity.getAttributes();
|
|
|
|
|
if (null == cachedAttributes){
|
|
|
|
|
cachedAttributes = new ArrayList<>();
|
|
|
|
|
cachedEntity.setAttributes(cachedAttributes);
|
|
|
|
|
}
|
|
|
|
|
for (ERAttributeDTO attribute : entity.getAttributes()) {
|
|
|
|
|
String attributeKey = attribute.getAttribute();
|
|
|
|
@ -230,8 +240,8 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// 如果属性已经存在,则不添加
|
|
|
|
|
if (attributes.stream().noneMatch(a -> StrUtil.equals(a.getAttribute(), attributeKey))) {
|
|
|
|
|
attributes.add(attribute);
|
|
|
|
|
if (cachedAttributes.stream().noneMatch(a -> StrUtil.equals(a.getAttribute(), attributeKey))) {
|
|
|
|
|
cachedAttributes.add(attribute);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -242,6 +252,6 @@ public class TripleConversionPipelineImpl implements TripleConversionPipeline {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String generateRelationMapKey(RelationExtractionDTO relationExtractionDTO) {
|
|
|
|
|
return relationExtractionDTO.getSource() + "_" + relationExtractionDTO.getTarget() + "_" + relationExtractionDTO.getRelation();
|
|
|
|
|
return relationExtractionDTO.getSource()+ "_" + relationExtractionDTO.getRelation() + "_" + relationExtractionDTO.getTarget();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|