You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
8.9 KiB
Java

5 months ago
package com.supervision.pdfqaserver.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.TimeInterval;
5 months ago
import cn.hutool.core.util.StrUtil;
import com.supervision.pdfqaserver.constant.DomainMetaGenerationEnum;
import com.supervision.pdfqaserver.domain.ChineseEnglishWords;
import com.supervision.pdfqaserver.domain.DocumentTruncation;
5 months ago
import com.supervision.pdfqaserver.domain.DomainMetadata;
import com.supervision.pdfqaserver.dto.*;
5 months ago
import com.supervision.pdfqaserver.domain.PdfAnalysisOutput;
import com.supervision.pdfqaserver.service.*;
import com.supervision.pdfqaserver.thread.KnowledgeGraphGenerateTreadPool;
5 months ago
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
5 months ago
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
5 months ago
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class KnowledgeGraphServiceImpl implements KnowledgeGraphService {
private final TripleConversionPipeline tripleConversionPipeline;
private final TripleToCypherExecutor tripleToCypherExecutor;
private final ChineseEnglishWordsService chineseEnglishWordsService;
private final DocumentTruncationService documentTruncationService;
private final DomainMetadataService domainMetadataService;
private final PdfAnalysisOutputService pdfAnalysisOutputService;
private final TruncationEntityExtractionService truncationEntityExtractionService;
private final TruncationRelationExtractionService relationExtractionService;
5 months ago
private final ChinesEsToEnglishGenerator chinesEsToEnglishGenerator;
5 months ago
private final PdfInfoService pdfInfoService;
5 months ago
@Override
public void generateGraph(String documentId) {
((KnowledgeGraphService)AopContext.currentProxy()).resetGraphData(documentId);
List<PdfAnalysisOutput> pdfAnalysisOutputs = pdfAnalysisOutputService.queryByPdfId(Integer.valueOf(documentId));
5 months ago
if (CollUtil.isEmpty(pdfAnalysisOutputs)) {
log.info("没有找到pdfId为{}的pdf分析结果", documentId);
return;
}
List<DocumentDTO> documentDTOList = pdfAnalysisOutputs.stream().map(DocumentDTO::new).toList();
// 对文档进行切分
TimeInterval timer = new TimeInterval();
timer.start("sliceDocuments");
log.info("开始切分文档,初始文档个数:{}",documentDTOList.size());
5 months ago
List<TruncateDTO> truncateDTOS = tripleConversionPipeline.sliceDocuments(documentDTOList);
log.info("切分文档完成,切分后文档个数:{},耗时:{}秒",truncateDTOS.size(), timer.intervalSecond("sliceDocuments"));
5 months ago
// 保存分片信息
documentTruncationService.batchSave(truncateDTOS);
// 对切分后的文档进行命名实体识别
timer.start("doEre");
log.info("开始命名实体识别...");
5 months ago
List<EREDTO> eredtoList = new ArrayList<>();
for (TruncateDTO truncateDTO : truncateDTOS) {
EREDTO eredto = tripleConversionPipeline.doEre(truncateDTO);
5 months ago
if (null == eredto){
continue;
}
5 months ago
// 保存实体关系抽取结果
this.saveERE(eredto, truncateDTO.getId());
eredtoList.add(eredto);
5 months ago
}
log.info("命名实体识别完成,耗时:{}秒", timer.intervalSecond("doEre"));
5 months ago
// 合并实体关系抽取结果
log.info("开始合并实体关系抽取结果...");
5 months ago
List<EREDTO> mergedList = tripleConversionPipeline.mergeEreResults(eredtoList);
log.info("合并实体关系抽取结果完成,合并后个数:{}", mergedList.size());
5 months ago
5 months ago
// 保存领域元数据
log.info("开始保存领域元数据...");
5 months ago
for (EREDTO eredto : mergedList) {
5 months ago
List<RelationExtractionDTO> relations = eredto.getRelations();
if (CollUtil.isEmpty(relations)){
continue;
}
for (RelationExtractionDTO relation : relations) {
DomainMetadata domainMetadata = relation.toDomainMetadata();
domainMetadata.setDomainType("1");
domainMetadata.setGenerationType(DomainMetaGenerationEnum.SYSTEM_AUTO_GENERATION.getCode());
domainMetadataService.saveIfNotExists(domainMetadata);
}
}
log.info("保存领域元数据完成....");
5 months ago
5 months ago
// 保存字典
log.info("开始保存字典...");
5 months ago
List<ChineseEnglishWords> allWords = chineseEnglishWordsService.queryAll();
int wordsSize = allWords.size();
5 months ago
for (EREDTO eredto : mergedList) {
List<EntityExtractionDTO> entities = eredto.getEntities();
if (CollUtil.isNotEmpty(entities)){
for (EntityExtractionDTO entityDTO : entities) {
saveWordsIfNecessary(entityDTO.getEntity(), allWords);
if (CollUtil.isNotEmpty(entityDTO.getAttributes())){
for (ERAttributeDTO attribute : entityDTO.getAttributes()) {
saveWordsIfNecessary(attribute.getAttribute(), allWords);
}
}
5 months ago
}
}
List<RelationExtractionDTO> relations = eredto.getRelations();
if (CollUtil.isNotEmpty(relations)){
for (RelationExtractionDTO relationDTO : relations) {
saveWordsIfNecessary(relationDTO.getRelation(), allWords);
if (CollUtil.isNotEmpty(relationDTO.getAttributes())){
for (ERAttributeDTO attribute : relationDTO.getAttributes()) {
saveWordsIfNecessary(attribute.getAttribute(), allWords);
}
}
5 months ago
}
}
}
log.info("保存字典完成,新增字典个数:{}", allWords.size() - wordsSize);
5 months ago
// 生成cypher语句
for (EREDTO eredto : mergedList) {
if (CollUtil.isEmpty(eredto.getEntities()) && CollUtil.isEmpty(eredto.getRelations())){
continue;
}
5 months ago
eredto.setEn(allWords);
tripleToCypherExecutor.saveERE(eredto);
5 months ago
}
5 months ago
}
@Override
@Transactional(rollbackFor = Exception.class)
public void resetGraphData(String documentId) {
log.info("resetGraphData:重置知识图谱数据,documentId:{}", documentId);
List<DocumentTruncation> documentTruncations = documentTruncationService.queryByDocumentId(documentId);
if (CollUtil.isEmpty(documentTruncations)){
log.info("没有找到文档切分数据,documentId:{},不用重置数据...", documentId);
return;
}
// 删除切分数据
documentTruncationService.deleteByDocumentId(documentId);
for (DocumentTruncation documentTruncation : documentTruncations) {
String truncationId = documentTruncation.getId();
// 删除实体数据
truncationEntityExtractionService.deleteByTruncationId(truncationId);
// 删除关系数据
relationExtractionService.deleteByTruncationId(truncationId);
}
log.info("重置知识图谱数据完成,documentId:{}", documentId);
}
@Override
public void submitGenerateTask(String documentId) {
// 提交生成图任务
log.info("submitGenerateTask:提交知识图谱生成任务,documentId:{}", documentId);
KnowledgeGraphGenerateTreadPool.executorService.execute(() -> {
try {
pdfInfoService.pdfToGraphStart(documentId);
generateGraph(documentId);
pdfInfoService.pdfToGraphComplete(documentId);
} catch (Exception e) {
log.error("生成知识图谱失败,documentId:{}", documentId, e);
pdfInfoService.pdfToGraphFail(documentId);
}
});
}
5 months ago
5 months ago
private void saveWordsIfNecessary(String word, List<ChineseEnglishWords> allWords) {
boolean exists = chineseEnglishWordsService.wordsExists(word, allWords);
if (exists){
return;
}
String generate = chinesEsToEnglishGenerator.generate(word);
if (StrUtil.isEmpty(generate)){
log.warn("生成英文名称失败entity:{}", word);
5 months ago
return;
}
ChineseEnglishWords words = new ChineseEnglishWords();
words.setChineseWord(word);
words.setEnglishWord(generate);
chineseEnglishWordsService.saveIfNotExists(words);
allWords.add(words);// 更新缓存
5 months ago
}
@Override
public void queryGraph(String databaseId, String query) {
}
@Override
public void saveERE(EREDTO eredto, String truncationId) {
5 months ago
// 保存实体信息
truncationEntityExtractionService.saveERE(eredto.getEntities());
// 保存关系
relationExtractionService.saveERE(eredto.getRelations());
5 months ago
}
}