Kaynağa Gözat

1、缓存文件生成
2、关系抽取保留关系名称

louhr 5 yıl önce
ebeveyn
işleme
d93257735c

+ 1 - 0
kernel/src/main/java/com/lantone/qc/kernel/structure/ai/model/Lemma.java

@@ -21,6 +21,7 @@ public class Lemma {
     private String position;
     private int len;
     private String property;
+    private String relationName;
     private Lemma parent;
     private boolean haveChildren = false;
     private List<Lemma> relationLemmas = new ArrayList<>();

+ 17 - 0
kernel/src/main/java/com/lantone/qc/kernel/structure/ai/model/Relation.java

@@ -0,0 +1,17 @@
+package com.lantone.qc.kernel.structure.ai.model;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @ClassName : Relation
+ * @Description : 关系信息描述(基于AI)
+ * @Author : 楼辉荣
+ * @Date: 2020-03-11 10:46
+ */
+@Getter
+@Setter
+public class Relation {
+    private int id;
+    private String relationName;
+}

+ 25 - 17
kernel/src/main/java/com/lantone/qc/kernel/structure/ai/process/EntityProcess.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.lantone.qc.kernel.structure.ai.model.CrfOut;
 import com.lantone.qc.kernel.structure.ai.model.EntityEnum;
 import com.lantone.qc.kernel.structure.ai.model.Lemma;
+import com.lantone.qc.kernel.structure.ai.model.Relation;
 import com.lantone.qc.pub.model.entity.DiagInfectious;
 import com.lantone.qc.pub.model.entity.Negative;
 import org.apache.commons.beanutils.BeanUtils;
@@ -76,10 +77,11 @@ public class EntityProcess {
      * @param relations
      */
     private void findRelationLemma(Lemma lemma, List<Lemma> allLemmaList, JSONArray relations) {
-        List<Integer> connectEntityIdList = getConnectEntityIdList(lemma.getId(), relations);
+        List<Relation> connectEntityIdList = getConnectEntityList(lemma.getId(), relations);
         for (Lemma l : allLemmaList) {
-            for (Integer reId : connectEntityIdList) {
-                if (l.getId() == reId) {
+            for (Relation relation : connectEntityIdList) {
+                if (l.getId() == relation.getId()) {
+                    l.setRelationName(relation.getRelationName());
                     findRelationLemma(l, allLemmaList, relations);
                     lemma.addRelationLemmas(l);
                 }
@@ -107,13 +109,13 @@ public class EntityProcess {
             JSONObject entity = entitys.getJSONObject(i);
             if (entityType.equals(entity.getString("name"))) {
                 int id = entity.getIntValue("id");
-                List<Integer> connectEntityIdList = getConnectEntityIdList(id, relations);
-                if (connectEntityIdList.size() == 0) {
+                List<Relation> connectEntityRelationList = getConnectEntityList(id, relations);
+                if (connectEntityRelationList.size() == 0) {
                     connectEntity = new HashMap<>();
                     connectEntity.put(entity.getString("name"), entity.getString("value"));
                     connectEntityList.add(connectEntity);
                 } else {
-                    connectEntity = getConnectEntity(connectEntityIdList, entitys);
+                    connectEntity = getConnectEntity(connectEntityRelationList, entitys);
                     connectEntity.put(entity.getString("name"), entity.getString("value"));
                     connectEntityList.add(connectEntity);
                 }
@@ -129,21 +131,27 @@ public class EntityProcess {
      * @param relations 关系抽取出的关系对
      * @return connectEntityIdList 有关系实体的id列表(List)
      */
-    public List<Integer> getConnectEntityIdList(int entityId, JSONArray relations) {
-        List<Integer> connectEntityIdList = new ArrayList<>();
+    public List<Relation> getConnectEntityList(int entityId, JSONArray relations) {
+        List<Relation> connectEntityList = new ArrayList<>();
         for (int i = 0; i < relations.size(); i++) {
             if (StringUtils.isEmpty(relations.get(i).toString())) {
                 continue;
             }
-            JSONObject relation = relations.getJSONObject(i);
-            if (relation.getIntValue("from") == entityId) {
-                connectEntityIdList.add(relation.getIntValue("to"));
+            JSONObject relationObjs = relations.getJSONObject(i);
+            if (relationObjs.getIntValue("from") == entityId) {
+                Relation relation = new Relation();
+                relation.setId(relationObjs.getIntValue("to"));
+                relation.setRelationName(relationObjs.getString("name"));
+                connectEntityList.add(relation);
             }
-            if (relation.getIntValue("to") == entityId) {
-                connectEntityIdList.add(relation.getIntValue("from"));
+            if (relationObjs.getIntValue("to") == entityId) {
+                Relation relation = new Relation();
+                relation.setId(relationObjs.getIntValue("from"));
+                relation.setRelationName(relationObjs.getString("name"));
+                connectEntityList.add(relation);
             }
         }
-        return connectEntityIdList;
+        return connectEntityList;
     }
 
     /**
@@ -169,15 +177,15 @@ public class EntityProcess {
      * @param entitys             关系抽取的实体列表
      * @return entityRelationPair 实体id列表对应的所有实体类型及实体值
      */
-    public Map<String, String> getConnectEntity(List<Integer> connectEntityIdList, JSONArray entitys) {
+    public Map<String, String> getConnectEntity(List<Relation> connectEntityIdList, JSONArray entitys) {
         Map<String, String> entityRelationPair = new HashMap<>();
-        for (int connectEntityId : connectEntityIdList) {
+        for (Relation connectEntityId : connectEntityIdList) {
             for (int i = 0; i < entitys.size(); i++) {
                 if (StringUtils.isEmpty(entitys.get(i).toString())) {
                     continue;
                 }
                 JSONObject entity = entitys.getJSONObject(i);
-                if (connectEntityId == entity.getIntValue("id")) {
+                if (connectEntityId.getId() == entity.getIntValue("id")) {
                     if (entityRelationPair.containsKey(entity.getString("name"))) {
                         entityRelationPair.put(entity.getString("name"),
                                 entityRelationPair.get(entity.getString("name")) + "," + entity.getString("value"));

+ 24 - 12
kernel/src/main/java/com/lantone/qc/kernel/util/CacheFileManager.java

@@ -2,6 +2,7 @@ package com.lantone.qc.kernel.util;
 
 import com.lantone.qc.pub.jdbc.MysqlJdbc;
 import com.lantone.qc.pub.util.PropertiesUtil;
+import com.lantone.qc.security.util.CryptUtil;
 import com.lantone.qc.security.util.EncrypDES;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -29,12 +30,9 @@ public class CacheFileManager {
 
     public static void main(String[] args) {
         CacheFileManager cacheFileManager = new CacheFileManager();
-        PropertiesUtil propertiesUtil = new PropertiesUtil("nlp.properties");
-        String p = propertiesUtil.getProperty("cache.file.dir");
-        File file = new File(p);
-        if (!file.exists()) {
-            file.mkdirs();
-        }
+        String p = cacheFileManager.getClass().getResource("/").getPath();
+        p = p.substring(0, p.indexOf("/target/")) + "/src/main/resources/cache/";
+        cacheFileManager.createCacheFile(p);
     }
 
     public CacheFileManager() {
@@ -57,14 +55,28 @@ public class CacheFileManager {
         try {
             EncrypDES encrypDES = new EncrypDES();
             //所有词典库 不能用concat_group 大小写不区分
-            String sql = "select l_1.name l_1_name, l_1.type_id type_id, l_2.name l_2_name, kc.lib_name, group_concat(l_1.name) group_name from kl_library_info l_1\n" +
-                    "left join kl_library_info l_2 on l_1.concept_id = l_2.concept_id and l_2.is_concept = 1\n" +
-                    "left join kl_concept kc on l_1.concept_id = kc.id\n" +
-                    "left join kl_disease dis on l_1.concept_id = dis.concept_id\n" +
-                    "where kc.is_deleted = 'N' group by l_1.concept_id";
+            String sql = "select l_1.name l_1_name, l_1.type_id type_id, l_2.name l_2_name, \n" +
+                    "kc.lib_name, dis.is_chronic, dis.is_infections, group_concat(l_1.name) group_name, group_concat(d.feature) feature_name \n" +
+                    "from kl_library_info l_1\n" +
+                    "                    left join kl_library_info l_2 on l_1.concept_id = l_2.concept_id and l_2.is_concept = 1\n" +
+                    "                    left join kl_concept kc on l_1.concept_id = kc.id\n" +
+                    "                    left join kl_disease dis on l_1.concept_id = dis.concept_id\n" +
+                    "left join doc_relevant_feature d on d.diagnose = kc.id\n" +
+                    "                    where kc.is_deleted = 'N' and l_1.type_id = 18 group by l_1.concept_id";
             st = conn.createStatement();
             rs = st.executeQuery(sql);
-            FileWriter fw = new FileWriter(path + "tc.dict");
+            FileWriter fw = new FileWriter(path + "concept_diag_properties.dict");
+            String r1, r2, r3, r4, r5;
+            while (rs.next()) {
+                r1 = CryptUtil.decrypt_char(rs.getString(4));
+                r2 = rs.getString(5);
+                r3 = rs.getString(6);
+                r4 = CryptUtil.decrypt_char(rs.getString(7));
+                r5 = CryptUtil.decrypt_char(rs.getString(8));
+
+                fw.write(r1 + "|" + r2 + "|" + r3 + "|" + r4 + "|" + r5);
+                fw.write("\n");
+            }
             fw.close();
         } catch (IOException ioe) {
             ioe.printStackTrace();

+ 3 - 3
kernel/src/main/resources/kernel.properties

@@ -1,4 +1,4 @@
 ################################ database ###################################
-mysql.user = root
-mysql.password = lantone
-mysql.url = "jdbc:mysql://192.168.2.241:3306/med?useUnicode=true&characterEncoding=UTF-8";
+mysql.user = ujrcgl5RQqE=
+mysql.password = c+7qw+jN+HQ=
+mysql.url = nozAbYFgLmQxh0NOeCJ3+D1MSa7CmEuGIR0pNwPBcJj60CdmTflpxhSZ+f7k3Ld29v6BMAol0r+A+tVobz/zPoVdgRwl7dZPIXkSUxstZ+E=

+ 6 - 0
pom.xml

@@ -212,6 +212,12 @@
             <version>1.16.16</version>
         </dependency>
 
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>${mysql.version}</version>
+        </dependency>
+
         <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
         <dependency>
             <groupId>com.alibaba</groupId>

+ 1 - 1
public/src/main/java/com/lantone/qc/pub/jdbc/MysqlJdbc.java

@@ -10,7 +10,7 @@ import java.util.*;
  * Created by Administrator on 2018/6/8/008.
  */
 public class MysqlJdbc {
-    private String driver = "com.mysql.jdbc.Driver";
+    private String driver = "com.mysql.cj.jdbc.Driver";
 
     private String user;
     private String password;

+ 101 - 0
security/src/main/java/com/lantone/qc/security/util/CryptUtil.java

@@ -0,0 +1,101 @@
+package com.lantone.qc.security.util;
+
+import java.util.List;
+
+/**
+ * @Description: 加解密工具类
+ * @author: gaodm
+ * @time: 2019/12/30 11:09
+ */
+public class CryptUtil {
+
+    private final static char EN_MAX = '\u0080';//128
+    private final static int MOVE_NUM = 2;
+    private final static char DE_MAX = EN_MAX + MOVE_NUM;
+
+    /**
+     * 加密,把一个字符串在原有的基础上+2
+     *
+     * @param data 需要解密的原字符串
+     * @return 返回解密后的新字符串
+     */
+    public static String encrypt_char(String data) {
+        char[] chars = data.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (EN_MAX < chars[i]) {
+                chars[i] += MOVE_NUM;
+            }
+        }
+        return new String(chars);
+    }
+
+    /**
+     * 解密:把一个加密后的字符串在原有基础上-2
+     *
+     * @param data 加密后的字符串
+     * @return 返回解密后的新字符串
+     */
+    public static String decrypt_char(String data) {
+        char[] chars = data.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (DE_MAX < chars[i]) {
+                chars[i] -= MOVE_NUM;
+            }
+        }
+        return new String(chars);
+    }
+
+
+    /**
+     * 对List<String>进行加密
+     *
+     * @param list 加密前的list
+     * @return 加密后的list
+     */
+    public static void encryptList(List<String> list) {
+        if (ListUtil.isNotEmpty(list)) {
+            for (int i = 0; i < list.size(); i++) {
+                list.set(i, CryptUtil.encrypt_char(list.get(i)));
+            }
+        }
+    }
+
+    /**
+     * 对List<String>进行解密
+     * @param list 解密前的list
+     * @return 解密后的list
+     */
+    public static void decryptList(List<String> list) {
+        if (ListUtil.isNotEmpty(list)) {
+            for (int i = 0; i < list.size(); i++) {
+                list.set(i, CryptUtil.decrypt_char(list.get(i)));
+            }
+        }
+    }
+
+
+
+    public static void main(String[] args) {
+        //加密英文
+        String data = "解密后:�dsfaa2132159-4331}~\u007F";
+        String charResult = encrypt_char(data);
+        System.out.println("加密后:" + charResult);
+        //解密
+        String charStr = decrypt_char(charResult);
+        System.out.println("解密后:" + charStr);
+
+
+        //加密中文
+        data = "跳梁小豆tlxd666,z";
+        String result = encrypt_char(data);
+        System.out.println("加密后:" + result);
+        String str1 = decrypt_char(result);
+        System.out.println("解密后:" + str1);
+
+//        int num = 32;
+//        while (num <= 128) {
+//            System.out.println((char) num + "  (Unicode编码对应的数字为:) " + num);
+//            num++;
+//        }
+    }
+}

+ 88 - 0
security/src/main/java/com/lantone/qc/security/util/ListUtil.java

@@ -0,0 +1,88 @@
+package com.lantone.qc.security.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @Description: List 工具类
+ * @author: gaodm
+ * @date: 2017/12/28 15:36
+ * @version: V1.0
+ */
+public class ListUtil {
+    /**
+     * list的第一行
+     */
+    public static final int FIRST = 0;
+
+    private ListUtil() {
+
+    }
+
+    /**
+     * 创建List对象
+     *
+     * @param <E> 泛型,
+     * @return
+     */
+    public static <E> ArrayList<E> newArrayList() {
+        return new ArrayList<>();
+    }
+
+    /**
+     * 判断List是否为空
+     *
+     * @param list
+     * @return
+     */
+    public static boolean isEmpty(List list) {
+        if (null == list) {
+            return Boolean.TRUE;
+        }
+        if (list.isEmpty()) {
+            return Boolean.TRUE;
+        }
+        if (list.size() < 1) {
+            return Boolean.TRUE;
+        }
+        return Boolean.FALSE;
+    }
+
+    /**
+     * 判断List是否为非空
+     *
+     * @param list
+     * @return
+     */
+    public static boolean isNotEmpty(List list) {
+        return !isEmpty(list);
+    }
+
+    /**
+     * 获取List集合中第一个对象,前提是自己先判断这个list不会为空
+     *
+     * @param list
+     * @param <E>
+     * @return
+     */
+    public static <E> E firstEntity(List<E> list) {
+        if (isEmpty(list)) {
+            return null;
+        }
+        return list.get(FIRST);
+    }
+
+    public static <E> ArrayList<E> arrayToList(E[] strArray) {
+        ArrayList<E> arrayList = new ArrayList<>(strArray.length);
+        Collections.addAll(arrayList, strArray);
+        return arrayList;
+    }
+
+    public static void main(String[] args) throws Exception {
+        String[] i ={"A","B"};
+        List<String> o = arrayToList(i);
+        System.out.println("输入参数:"+ i);
+        System.out.println("输出参数:"+ o);
+    }
+}