|
@@ -0,0 +1,427 @@
|
|
|
+package com.qizhen.healsphere;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.qizhen.healsphere.common.ai.BaidubceUtil;
|
|
|
+import com.qizhen.healsphere.common.ai.Knowlege;
|
|
|
+import com.qizhen.healsphere.common.ai.QizhenAssistant;
|
|
|
+import com.qizhen.healsphere.repository.neo4j.entity.BaseEntity;
|
|
|
+import com.qizhen.healsphere.service.EntityService;
|
|
|
+import com.qizhen.healsphere.service.RelationshipService;
|
|
|
+import com.qizhen.healsphere.web.vo.CreateEntityVO;
|
|
|
+import com.qizhen.healsphere.web.vo.RelationshipVO;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@ComponentScan(basePackages = {"com.qizhen.healsphere.model","com.qizhen.healsphere.repository"})
|
|
|
+@SpringBootTest
|
|
|
+public class DataWriteTest31 {
|
|
|
+ @Autowired
|
|
|
+ RelationshipService relationshipService;
|
|
|
+ @Autowired
|
|
|
+ EntityService entityService;
|
|
|
+
|
|
|
+ private static String diseaseStr = "风湿性心脏病" +
|
|
|
+ ",原发性高血压" +
|
|
|
+ ",急性心肌梗死" +
|
|
|
+ ",急性冠脉综合征" +
|
|
|
+ ",冠状动脉粥样硬化性心脏病" +
|
|
|
+ ",心力衰竭" +
|
|
|
+ ",心源性休克" +
|
|
|
+ ",酒精中毒" +
|
|
|
+ ",咽炎" +
|
|
|
+ ",急性扁桃体炎" +
|
|
|
+ ",变应性鼻炎" +
|
|
|
+ ",慢性鼻窦炎" +
|
|
|
+ ",鼻出血" +
|
|
|
+ ",急性牙髓炎" +
|
|
|
+ ",牙周病" +
|
|
|
+ ",溃疡性口炎" +
|
|
|
+ ",反流性食管炎" +
|
|
|
+ ",胃溃疡" +
|
|
|
+ ",慢性萎缩性胃炎" +
|
|
|
+ ",功能性消化不良" +
|
|
|
+ ",急性阑尾炎" +
|
|
|
+ ",克罗恩病" +
|
|
|
+ ",肠梗阻" +
|
|
|
+ ",肝硬化" +
|
|
|
+ ",急性胰腺炎" +
|
|
|
+ ",过敏性皮炎" +
|
|
|
+ ",湿疹" +
|
|
|
+ ",银屑病" +
|
|
|
+ ",带状疱疹" +
|
|
|
+ ",类风湿性关节炎" +
|
|
|
+ ",系统性红斑狼疮" +
|
|
|
+ ",慢性肾炎" +
|
|
|
+ ",肾病综合征" +
|
|
|
+ ",慢性肾衰竭" +
|
|
|
+ ",肾结石" +
|
|
|
+ ",输尿管结石" +
|
|
|
+ ",膀胱炎" +
|
|
|
+ ",前列腺增生" +
|
|
|
+ ",子宫内膜异位症" +
|
|
|
+ ",卵巢囊肿" +
|
|
|
+ ",痛经" +
|
|
|
+ ",女性更年期综合征" +
|
|
|
+ ",异位妊娠" +
|
|
|
+ ",妊娠剧吐" +
|
|
|
+ ",鼻咽恶性肿瘤" +
|
|
|
+ ",肝恶性肿瘤" +
|
|
|
+ ",乳房恶性肿瘤" +
|
|
|
+ ",宫颈恶性肿瘤" +
|
|
|
+ ",多发性骨髓瘤" +
|
|
|
+ ",急性白血病" +
|
|
|
+ ",血管瘤" +
|
|
|
+ ",子宫平滑肌瘤" +
|
|
|
+ ",新生儿黄疸" +
|
|
|
+ ",新生儿腹泻" +
|
|
|
+ ",缺铁性贫血" +
|
|
|
+ ",地中海贫血" +
|
|
|
+ ",血友病" +
|
|
|
+ ",甲状腺功能减退症" +
|
|
|
+ ",甲状腺功能亢进症" +
|
|
|
+ ",桥本甲状腺炎" +
|
|
|
+ ",2型糖尿病" +
|
|
|
+ ",糖尿病" +
|
|
|
+ ",甲状旁腺功能减退症" +
|
|
|
+ ",卵巢早衰" +
|
|
|
+ ",坏血病" +
|
|
|
+ ",高脂血症" +
|
|
|
+ ",高尿酸血症" +
|
|
|
+ ",精神分裂症" +
|
|
|
+ ",抑郁症" +
|
|
|
+ ",化脓性脑膜炎" +
|
|
|
+ ",帕金森病" +
|
|
|
+ ",癫痫" +
|
|
|
+ ",偏头痛" +
|
|
|
+ ",脑梗死" +
|
|
|
+ ",脑卒中" +
|
|
|
+ ",阻塞性睡眠呼吸暂停综合征" +
|
|
|
+ ",肺栓塞" +
|
|
|
+ ",慢性肺源性心脏病" +
|
|
|
+ ",急性上呼吸道感染" +
|
|
|
+ ",病毒性肺炎" +
|
|
|
+ ",社区获得性肺炎" +
|
|
|
+ ",支气管肺炎" +
|
|
|
+ ",肺炎" +
|
|
|
+ ",支气管哮喘" +
|
|
|
+ ",呼吸衰竭" +
|
|
|
+ ",肺部感染" +
|
|
|
+ ",急性泪腺炎" +
|
|
|
+ ",急性泪囊炎" +
|
|
|
+ ",巩膜炎" +
|
|
|
+ ",角膜炎" +
|
|
|
+ ",中耳炎" +
|
|
|
+ ",风湿性关节炎" +
|
|
|
+ ",颈椎病" +
|
|
|
+ ",颈肩综合征" +
|
|
|
+ ",坐骨神经痛" +
|
|
|
+ ",肩周炎" +
|
|
|
+ ",骨质疏松" +
|
|
|
+ ",锁骨骨折" +
|
|
|
+ ",肱骨骨折" +
|
|
|
+ ",肩关节脱位";
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void writeNeo4j() {
|
|
|
+ String startLabel = "疾病";
|
|
|
+ String propertyStr = "并发症\t的并发症有哪些疾病";
|
|
|
+ String[] properties = propertyStr.split(",");
|
|
|
+ workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
|
|
|
+ String fileName = "";//
|
|
|
+ HSSFSheet sheet = workbook.createSheet(fileName);
|
|
|
+ int rows = 0;
|
|
|
+ for(String property:properties){
|
|
|
+ String[] split = property.split("\t");
|
|
|
+ if(split.length<2){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String endLabel = split[0];
|
|
|
+ List<Knowlege> data = getData(BaidubceUtil.getAccessToken(), property);
|
|
|
+ if(!CollectionUtils.isEmpty(data)) {
|
|
|
+
|
|
|
+ for (Knowlege temp:data) {
|
|
|
+ HSSFRow row = sheet.createRow(rows++);
|
|
|
+ row.createCell(0).setCellValue(temp.getEntity() == null ? "" : temp.getEntity());
|
|
|
+ row.createCell(1).setCellValue(temp.getProperty() == null ? "" : temp.getProperty());
|
|
|
+ row.createCell(2).setCellValue(temp.getValue() == null ? "" : temp.getValue());
|
|
|
+ row.createCell(3).setCellValue(temp.getQuestion() == null ? "" : temp.getQuestion());
|
|
|
+ row.createCell(4).setCellValue(temp.getAnswer() == null ? "" : temp.getAnswer());
|
|
|
+ row.createCell(5).setCellValue(temp.getChunk() == null ? "" : temp.getChunk());
|
|
|
+ row.createCell(6).setCellValue(temp.getRefenrece() == null ? "" : temp.getRefenrece());
|
|
|
+ }
|
|
|
+ save(fileName);
|
|
|
+
|
|
|
+ for (Knowlege temp:data) {
|
|
|
+ String value = temp.getValue();
|
|
|
+ if(StringUtils.isBlank(value)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(value);
|
|
|
+ BaseEntity startEntity = createNoExists(startLabel, temp.getEntity());
|
|
|
+ long startId = startEntity.getId();
|
|
|
+ List<RelationshipVO> relationshipList = new ArrayList<>();
|
|
|
+ for(int i=0;i<jsonArray.size();i++){
|
|
|
+ String name = jsonArray.getString(i);
|
|
|
+ if(StringUtils.isEmpty(name)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ BaseEntity endEntity = createNoExists(endLabel, name);
|
|
|
+ Long endId = endEntity.getId();
|
|
|
+ RelationshipVO relationshipVO = new RelationshipVO();
|
|
|
+ relationshipVO.setStartId(startId);
|
|
|
+ relationshipVO.setEndId(endId);
|
|
|
+ relationshipVO.setStartLabel(startLabel);
|
|
|
+ relationshipVO.setEndLabel(endLabel);
|
|
|
+ relationshipVO.setRelationshipType(startLabel+"相关"+endLabel);
|
|
|
+ relationshipList.add(relationshipVO);
|
|
|
+ }
|
|
|
+ if(!CollectionUtils.isEmpty(relationshipList)) {
|
|
|
+ System.out.println( relationshipService.createRelationship(relationshipList));
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private BaseEntity createNoExists(String labelName, String name) {
|
|
|
+ BaseEntity nodeByName = entityService.findNodeByName(labelName, name);
|
|
|
+ if(Objects.nonNull(nodeByName)){//节点不存在
|
|
|
+ return nodeByName;
|
|
|
+ }
|
|
|
+ CreateEntityVO createEntity = new CreateEntityVO();
|
|
|
+ createEntity.setName(name);
|
|
|
+ createEntity.setLabel(labelName);
|
|
|
+ return entityService.create(createEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ /*String disease = "咽炎";
|
|
|
+ String relation = "临床表现";
|
|
|
+ String answer = "**咽炎的临床表现为急性咽炎**^[1]^。";
|
|
|
+ String format = "你是专门处理医学领域文本的关系抽取专家。你将在指定的文本中抽取其中“"+disease+"的"+relation+"”。\n" +
|
|
|
+ "\n" +
|
|
|
+ "#要求\n" +
|
|
|
+ "1、抽取的结果将以JSON数组的形式呈现。每个抽取的“"+relation+"”高度简洁、高度概括,不要要描述性的文字,文字尽量保持在12个字符以内!\n" +
|
|
|
+ "\n" +
|
|
|
+ "#示例1\n" +
|
|
|
+ "以抽取“主要症状”为例\n" +
|
|
|
+ "文本:\n" +
|
|
|
+ "臭汗症的主要症状有全身或局部多汗且有臭味^[1][3][4]^。\n" +
|
|
|
+ "\n" +
|
|
|
+ "局部性臭汗症多发于大汗腺所在部位,如腋窝、腹股沟、足部、肛周、外阴、脐部及女性乳房下等处,以足部、腋部臭汗症最为多见。腋窝臭汗症俗称狐臭,是一种特殊的刺鼻臭味。足部臭汗症常与足部多汗伴发,有刺鼻的臭味^[1][4]^。\n" +
|
|
|
+ "\n" +
|
|
|
+ "全身性臭汗症为一种与种族有关的生理现象,也可见于卫生习惯不良者,服食某些食物(如葱、蒜、芥末)或某些药物(如麝香)后,在个别人中可产生臭汗^[4]^。\n" +
|
|
|
+ "\n" +
|
|
|
+ "输出:[\"全身或局部多汗且有臭味\",\"局部性臭汗症\",\"腋窝臭汗症\",\"足部臭汗症常\",\"全身性臭汗症\"]\n" +
|
|
|
+ "\n" +
|
|
|
+ "#示例2\n" +
|
|
|
+ "以抽取“是否传染病”为例\n" +
|
|
|
+ "文本:\n" +
|
|
|
+ "**是**^[2][4][6]^。\n" +
|
|
|
+ "\n" +
|
|
|
+ "输出:[\"是\"]\n\n"+
|
|
|
+ "2、没有可抽取的“"+relation+"”,则返回空json数组。\n" +
|
|
|
+ "\n" +
|
|
|
+ "本次抽取的文本如下:\n\n";
|
|
|
+
|
|
|
+
|
|
|
+ String zhiling = format+ answer;
|
|
|
+ System.out.println(zhiling);
|
|
|
+ System.out.println(BaidubceUtil.getChatResponse(zhiling, BaidubceUtil.getAccessToken()));*/
|
|
|
+
|
|
|
+
|
|
|
+ String accessToken = BaidubceUtil.getAccessToken();
|
|
|
+ String propertyStr = "分期\t的分期有哪些分期";
|
|
|
+
|
|
|
+ saveExel(propertyStr, accessToken,"分期3.1");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ static HSSFWorkbook workbook;
|
|
|
+
|
|
|
+ private static void saveExel(String propertyStr, String accessToken,String fileName) {
|
|
|
+ String[] properties = propertyStr.split(",");
|
|
|
+ workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
|
|
|
+ HSSFSheet sheet = workbook.createSheet(fileName);
|
|
|
+ ;//工作表
|
|
|
+ int rows = 0;
|
|
|
+ for(String property:properties){
|
|
|
+ List<Knowlege> data = getData(accessToken, property);
|
|
|
+ if(!CollectionUtils.isEmpty(data)) {
|
|
|
+ for (Knowlege temp:data) {
|
|
|
+ HSSFRow row = sheet.createRow(rows++);
|
|
|
+ row.createCell(0).setCellValue(temp.getEntity() == null ? "" : temp.getEntity());
|
|
|
+ row.createCell(1).setCellValue(temp.getProperty() == null ? "" : temp.getProperty());
|
|
|
+ row.createCell(2).setCellValue(temp.getValue() == null ? "" : temp.getValue());
|
|
|
+ row.createCell(3).setCellValue(temp.getQuestion() == null ? "" : temp.getQuestion());
|
|
|
+ row.createCell(4).setCellValue(temp.getAnswer() == null ? "" : temp.getAnswer());
|
|
|
+ row.createCell(5).setCellValue(temp.getChunk() == null ? "" : temp.getChunk());
|
|
|
+ row.createCell(6).setCellValue(temp.getRefenrece() == null ? "" : temp.getRefenrece());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ save(fileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static synchronized void save(String fileName) {
|
|
|
+ try {
|
|
|
+ fileName="C:\\Users\\17664\\Desktop\\"+fileName+System.currentTimeMillis()+".xlsx";
|
|
|
+ //文档输出
|
|
|
+ FileOutputStream out = new FileOutputStream(new File(fileName));
|
|
|
+ workbook.write(out);
|
|
|
+ out.close();
|
|
|
+ System.out.println(fileName + "存储完毕");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<Knowlege> getData(String accessToken,String property) {
|
|
|
+ List<Knowlege> list = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ BaidubceUtil baidubceUtil = new BaidubceUtil();
|
|
|
+
|
|
|
+ String[] diseases = diseaseStr.split(",");
|
|
|
+ String appId= "3b615957-f9b1-4811-9dfc-13dcee6e0a37";
|
|
|
+ for (int i=0;i<diseases.length;i++) {
|
|
|
+ try {
|
|
|
+ String disease = diseases[i];
|
|
|
+ disease = disease.trim();
|
|
|
+ String[] split = property.split("\t");
|
|
|
+ if(split.length<2){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String quetionParty = split[1];
|
|
|
+ String relation = split[0];
|
|
|
+ String question = disease + quetionParty + "?";
|
|
|
+
|
|
|
+ Map<String, String> result = QizhenAssistant.getChatResponse(question, QizhenAssistant.getConversationId(appId),appId);
|
|
|
+ String answer = result.get("answer");
|
|
|
+ String references = result.get("references");
|
|
|
+ String defaultReferences = result.get("defaultReferences");
|
|
|
+ String chatResponse = "";
|
|
|
+ System.out.println(answer);
|
|
|
+ if (!("failed".equals(answer) || answer.contains(QizhenAssistant.noAnswer))) {
|
|
|
+ String format = "你是专门处理医学领域文本的关系抽取专家。你将在指定的文本中抽取其中“"+disease+"的"+relation+"”。\n" +
|
|
|
+ "\n" +
|
|
|
+ "#要求\n" +
|
|
|
+ "1、抽取的结果将以JSON数组的形式呈现。每个抽取的“"+relation+"”高度简洁、高度概括,不要要描述性的文字,文字尽量保持在12个字符以内!\n" +
|
|
|
+ "\n" +
|
|
|
+ "#示例1\n" +
|
|
|
+ "以抽取“分期”为例\n" +
|
|
|
+ "文本:\n" +
|
|
|
+ "肱骨骨折如果是**肱骨头坏死**则有Cruess分期,包括I期、Ⅱ期、Ⅲ期、IV期、V期^[1]^。\n" +
|
|
|
+ "\n" +
|
|
|
+ "如果是肱骨近端骨折则有Neer分型和AO分型^[3]^。\n" +
|
|
|
+ "输出:[\"Cruess分期I期\",\"Cruess分期Ⅱ期\",\"Cruess分期Ⅲ期\",\"Cruess分期IV期\",\"Cruess分期V期\"]\n" +
|
|
|
+ "\n" +
|
|
|
+ "#示例2\n" +
|
|
|
+ "以抽取“是否传染病”为例\n" +
|
|
|
+ "文本:\n" +
|
|
|
+ "**是**^[2][4][6]^。\n" +
|
|
|
+ "\n" +
|
|
|
+ "输出:[\"是\"]\n\n"+
|
|
|
+ "2、没有可抽取的“"+relation+"”,则返回空json数组。\n" +
|
|
|
+ "\n" +
|
|
|
+ "本次抽取的文本如下:\n\n";
|
|
|
+
|
|
|
+ String zhiling = format+ answer;
|
|
|
+ System.out.println(zhiling);
|
|
|
+ chatResponse = baidubceUtil.getChatResponse(zhiling, accessToken);
|
|
|
+ chatResponse = filte(chatResponse);
|
|
|
+ JSONArray jsonResult = new JSONArray();
|
|
|
+ if(!StringUtils.isBlank(chatResponse)){
|
|
|
+ try{
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(chatResponse);
|
|
|
+ JSONArray referenceJA = JSONArray.parseArray(references);
|
|
|
+ for(int t=0;t<jsonArray.size();t++){
|
|
|
+ String name = jsonArray.getString(t);
|
|
|
+ JSONObject temp = new JSONObject();
|
|
|
+ temp.put("name", name);
|
|
|
+ JSONArray tempReferenceJA = new JSONArray();
|
|
|
+ for(int r=0;r<referenceJA.size();r++){
|
|
|
+ JSONObject referenceJO = referenceJA.getJSONObject(r);
|
|
|
+ String content = referenceJO.getString("content");
|
|
|
+ String contentFilted = content.replaceAll("\\s+", "");
|
|
|
+ if(contentFilted.contains(name)){
|
|
|
+ //JSONObject clone = referenceJO.clone();
|
|
|
+ tempReferenceJA.add(referenceJO.getString("title"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ temp.put("reference", tempReferenceJA);
|
|
|
+ if(tempReferenceJA.size()<1){
|
|
|
+ temp.put("defaultReferences", defaultReferences);
|
|
|
+ }
|
|
|
+ jsonResult.add(temp);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.println("######"+chatResponse);
|
|
|
+ e.printStackTrace();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ addNode(disease, relation, chatResponse,jsonResult.toJSONString(), answer, question, result, list);
|
|
|
+ }else {
|
|
|
+ addNode(disease, relation, "","", answer, question, result, list);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.println("抽取三元组失败!");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("未知错误!");
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void addNode(String disease, String property, String chatResponse,String refences, String answer, String question, Map<String, String> result, List<Knowlege> list) {
|
|
|
+ Knowlege knowlege = new Knowlege();
|
|
|
+ knowlege.setEntity(disease);
|
|
|
+ knowlege.setProperty(property);
|
|
|
+ knowlege.setValue(chatResponse);
|
|
|
+ knowlege.setAnswer(answer);
|
|
|
+ knowlege.setQuestion(question);
|
|
|
+ knowlege.setChunk(result.get("references"));
|
|
|
+ knowlege.setRefenrece(refences);
|
|
|
+ list.add(knowlege);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String filte(String chatResponse) {
|
|
|
+ if (chatResponse.startsWith("```json")) {
|
|
|
+ chatResponse = chatResponse.substring(7);
|
|
|
+ }
|
|
|
+ if (chatResponse.endsWith("```")) {
|
|
|
+ chatResponse = chatResponse.substring(0, chatResponse.length() - 3);
|
|
|
+ }
|
|
|
+ return chatResponse;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|