Browse Source

词典库加密

louhr 6 years ago
parent
commit
ffbba03019

+ 3 - 32
bigdata-web/src/main/java/org/diagbot/bigdata/common/ApplicationCacheUtil.java

@@ -6,6 +6,7 @@ import org.diagbot.common.dao.model.StandardInfo;
 import org.diagbot.common.service.StandardInfoService;
 import org.diagbot.nlp.util.Constants;
 import org.diagbot.nlp.util.NegativeEnum;
+import org.diagbot.nlp.util.NlpCache;
 
 import javax.servlet.ServletContext;
 import java.util.*;
@@ -14,16 +15,10 @@ public class ApplicationCacheUtil {
 
     public void putStandardInfoContext(ServletContext servletContext, StandardInfoService standardInfoService) {
         Map<String, Object> map = new HashMap<>(10, 0.8f);
-        //词典疾病名称缓存
-        map.put("categoryId", NegativeEnum.DISEASE.toString());
-        this.putStandardInfoNameContext(servletContext, standardInfoService, map, BigDataConstants.standard_info_diag_cache);
-        //症状名称缓存
-        map.put("categoryId", NegativeEnum.SYMPTOM.toString());
-        this.putStandardInfoNameContext(servletContext, standardInfoService, map, BigDataConstants.standard_info_symptom_cache);
         //同义词缓存
-        this.putStandardInfoRelationNameContext(servletContext, standardInfoService, BigDataConstants.standard_info_relation_type_2, BigDataConstants.standard_info_synonym_cache);
+        servletContext.setAttribute(BigDataConstants.standard_info_synonym_cache, NlpCache.getStandard_info_synonym_map());
         //分类关系
-        this.putStandardInfoRelationNameContext(servletContext, standardInfoService, BigDataConstants.standard_info_relation_type_3, BigDataConstants.standard_info_type_cache);
+        servletContext.setAttribute(BigDataConstants.standard_info_type_cache, NlpCache.getStandard_info_classify_map());
         //分类关系树形结构存储    暂时只支持三级结构
         Map<String, String> standardInfoTypeMap = (Map<String, String>) servletContext.getAttribute(BigDataConstants.standard_info_type_cache);
 
@@ -47,30 +42,6 @@ public class ApplicationCacheUtil {
 
     }
 
-    public void putStandardInfoNameContext(ServletContext servletContext, StandardInfoService standardInfoService, Map<String, Object> map, String key) {
-        Map<String, String> standardInfoMap = new HashMap<>(10, 0.8f);
-        List<StandardInfo> list = standardInfoService.selectList(map);
-        for (StandardInfo standardInfo : list) {
-            standardInfoMap.put(standardInfo.getName(), standardInfo.getName());
-        }
-        servletContext.setAttribute(key, standardInfoMap);
-    }
-
-    public void putStandardInfoRelationNameContext(ServletContext servletContext, StandardInfoService standardInfoService, String relationType, String key) {
-        Map<String, String> standardInfoSynonymMap = new HashMap<>(10, 0.8f);
-        Map<String, Object> relationMap = new HashMap<>(10, 0.3f);
-        if (StringUtils.isNotEmpty(relationType)) {
-            relationMap.put("relationType", relationType);
-        }
-        List<StandardInfo> list = standardInfoService.selectNotNullRelationTerm(relationMap);
-        for (StandardInfo standardInfo : list) {
-            if (StringUtils.isNotEmpty(standardInfo.getRelationName())) {
-                standardInfoSynonymMap.put(standardInfo.getName(), standardInfo.getRelationName());
-            }
-        }
-        servletContext.setAttribute(key, standardInfoSynonymMap);
-    }
-
     private void addChilds(Node root, Map<String, String> standardInfoTypeMap, List<String> hasAddList) {
         for (Node node : root.getChilds()) {
             for (Map.Entry<String, String> entry : standardInfoTypeMap.entrySet()) {

+ 2 - 2
bigdata-web/src/main/java/org/diagbot/bigdata/util/BigDataConstants.java

@@ -22,8 +22,8 @@ public class BigDataConstants {
     public final static String feature_type_history = "7";       //历史
     public final static String feature_type_feature = "9"; //症状描述中的特征信息 如部位、性质等
 
-    public final static String standard_info_diag_cache = "standardInfoDiagMap";                    //词典库疾病缓存
-    public final static String standard_info_symptom_cache = "standardInfoSymptomMap";             //症状信息缓存
+//    public final static String standard_info_diag_cache = "standardInfoDiagMap";                    //词典库疾病缓存
+//    public final static String standard_info_symptom_cache = "standardInfoSymptomMap";             //症状信息缓存
     public final static String standard_info_synonym_cache = "standardInfoSynonymMap";             //同义词缓存
     public final static String standard_info_type_cache = "standardInfoTypeMap";             //大小类关系词缓存
     public final static String standard_info_type_tree_cache = "standardInfoTypeTreeMap";             //大小类关系词缓存-树形结构

+ 21 - 19
nlp-web/src/main/java/org/diagbot/nlp/common/ApplicationCacheUtil.java

@@ -1,42 +1,44 @@
 package org.diagbot.nlp.common;
 
-import org.diagbot.common.dao.model.StandardInfo;
 import org.diagbot.common.service.StandardInfoService;
 import org.diagbot.nlp.util.NegativeEnum;
+import org.diagbot.nlp.util.NlpCache;
 
 import javax.servlet.ServletContext;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 public class ApplicationCacheUtil {
 
     public void putStandardInfoContext(ServletContext servletContext, StandardInfoService standardInfoService) {
-        Map<String, Object> map = new HashMap<>(10, 0.8f);
-        //推送症状名称缓存
-        map.put("status", "1");
+        //推送词典库缓存
+        Map<String, String> standard_info_push_map = NlpCache.getStandard_info_push_map();
         //症状名称缓存
-        map.put("categoryId", NegativeEnum.SYMPTOM.toString());
-        this.putStandardInfoNameContext(servletContext, standardInfoService, map, NlpWebConstants.standard_info_symptom_propel);
+        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.SYMPTOM.toString(), NlpWebConstants.standard_info_symptom_propel);
         //推送诊断名称缓存
-        map.put("categoryId", NegativeEnum.DISEASE.toString());
-        this.putStandardInfoNameContext(servletContext, standardInfoService, map, NlpWebConstants.standard_info_diag_propel);
+        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.DISEASE.toString(), NlpWebConstants.standard_info_diag_propel);
         //推送体征名称缓存
-        map.put("categoryId", NegativeEnum.VITAL_RESULT.toString());
-        this.putStandardInfoNameContext(servletContext, standardInfoService, map, NlpWebConstants.standard_info_vital_propel);
+        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.VITAL_RESULT.toString(), NlpWebConstants.standard_info_vital_propel);
         //推送Lis名称缓存
-        map.put("categoryId", NegativeEnum.LIS_NAME.toString());
-        this.putStandardInfoNameContext(servletContext, standardInfoService, map, NlpWebConstants.standard_info_lis_propel);
+        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.LIS_NAME.toString(), NlpWebConstants.standard_info_lis_propel);
         //推送Pacs名称缓存
-        map.put("categoryId", NegativeEnum.PACS_NAME.toString());
-        this.putStandardInfoNameContext(servletContext, standardInfoService, map, NlpWebConstants.standard_info_pacs_propel);
+        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.PACS_NAME.toString(), NlpWebConstants.standard_info_pacs_propel);
     }
 
-    public void putStandardInfoNameContext(ServletContext servletContext, StandardInfoService standardInfoService, Map<String, Object> map, String key) {
+    public void putStandardInfoNameContext(ServletContext servletContext, Map<String, String> standard_info_push_map, String categoryId, String key) {
         Map<String, String> standardInfoMap = new HashMap<>(10, 0.8f);
-        List<StandardInfo> list = standardInfoService.selectList(map);
-        for (StandardInfo standardInfo : list) {
-            standardInfoMap.put(standardInfo.getName(), standardInfo.getName());
+        String[] categoryIds = null;
+        for (Map.Entry<String, String> entry : standard_info_push_map.entrySet()) {
+            if (categoryId.equals(entry.getValue())) {
+                standardInfoMap.put(entry.getKey(), entry.getKey());
+            } else if (categoryId.indexOf(",") > -1) {
+                categoryIds = categoryId.split(",");
+                for (int i = 0; i < categoryIds.length; i++) {
+                    if (categoryId.equals(categoryIds[i])) {
+                        standardInfoMap.put(entry.getKey(), entry.getKey());
+                    }
+                }
+            }
         }
         servletContext.setAttribute(key, standardInfoMap);
     }

+ 33 - 17
nlp/src/main/java/org/diagbot/nlp/participle/cfg/DefaultConfig.java

@@ -1,6 +1,7 @@
 package org.diagbot.nlp.participle.cfg;
 
 import org.diagbot.nlp.participle.word.Segment;
+import org.diagbot.pub.utils.security.EncrypDES;
 import org.springframework.util.StringUtils;
 
 import java.io.*;
@@ -28,18 +29,24 @@ public class DefaultConfig implements Configuration {
         int length = dicts.size();
 
         segment = new Segment(Character.valueOf('0'));
-        String[] line_string;
-        char[] chars;
-        for (i = 0; i < length; i++) {
-            line_string = org.apache.commons.lang3.StringUtils.split(dicts.get(i), "\\|");
-            chars = line_string[0].toCharArray();
-            if (line_string.length == 3) {
-                segment.fill(segment, chars, 0, chars.length, Float.parseFloat(line_string[1]), line_string[2]);
-            } else if (line_string.length == 1) {
-                segment.fill(segment, chars, 0, chars.length);
-            } else {
-                continue;
+        try {
+            String[] line_string;
+            char[] chars;
+            EncrypDES encrypDES = new EncrypDES();
+            for (i = 0; i < length; i++) {
+                String s = dicts.get(i);
+                line_string = org.apache.commons.lang3.StringUtils.split(new String(encrypDES.decryptor(dicts.get(i))), "\\|");
+                chars = line_string[0].toCharArray();
+                if (line_string.length == 3) {
+                    segment.fill(segment, chars, 0, chars.length, Float.parseFloat(line_string[1]), line_string[2]);
+                } else if (line_string.length == 1) {
+                    segment.fill(segment, chars, 0, chars.length);
+                } else {
+                    continue;
+                }
             }
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         return segment;
     }
@@ -48,13 +55,22 @@ public class DefaultConfig implements Configuration {
         List<String> fileContents = readFileContents(path);
         String[] line_string;
         Map<String, String> map = new HashMap<>(10, 0.8f);
-        for (int i = 0; i < fileContents.size(); i++) {
-            line_string = org.apache.commons.lang3.StringUtils.split(fileContents.get(i), "\\|");
-            if (line_string.length == 2) {
-                map.put(line_string[0], line_string[1]);
-            } else {
-                continue;
+        try {
+            EncrypDES encrypDES = new EncrypDES();
+            for (int i = 0; i < fileContents.size(); i++) {
+                line_string = org.apache.commons.lang3.StringUtils.split(new String(encrypDES.decryptor(fileContents.get(i))), "\\|");
+                if (line_string.length == 2) {
+                    if (map.get(line_string[0]) != null) {
+                        map.put(line_string[0], map.get(line_string[0]) + "," + line_string[1]);
+                    } else {
+                        map.put(line_string[0], line_string[1]);
+                    }
+                } else {
+                    continue;
+                }
             }
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         return map;
     }

+ 14 - 0
nlp/src/main/java/org/diagbot/nlp/util/NlpCache.java

@@ -16,6 +16,8 @@ import java.util.Map;
 public class NlpCache {
     //词典库
     public static Segment segment_cache = null;
+    //推送字典
+    public static Map<String, String> standard_info_push_map = null;
     //词库同义词定义
     public static Map<String, String> standard_info_synonym_map = null;
     //词库大小类定义
@@ -26,6 +28,11 @@ public class NlpCache {
         segment_cache = configuration.loadMainDict("tc.dict");
     }
 
+    public static void createPushCache() {
+        Configuration configuration = new DefaultConfig();
+        standard_info_push_map = configuration.loadMapDict("push-tc.dict");
+    }
+
     public static void createSynonymCache() {
         Configuration configuration = new DefaultConfig();
         standard_info_synonym_map = configuration.loadMapDict("synonym.dict");
@@ -43,6 +50,13 @@ public class NlpCache {
         return segment_cache;
     }
 
+    public static Map<String, String> getStandard_info_push_map() {
+        if (standard_info_push_map == null) {
+            createPushCache();
+        }
+        return standard_info_push_map;
+    }
+
     public static Map<String, String> getStandard_info_synonym_map() {
         if (standard_info_synonym_map == null) {
             createSynonymCache();

File diff suppressed because it is too large
+ 3296 - 3296
nlp/src/main/resources/classify.dict


File diff suppressed because it is too large
+ 333432 - 0
nlp/src/main/resources/push-tc.dict


File diff suppressed because it is too large
+ 8954 - 8960
nlp/src/main/resources/synonym.dict


File diff suppressed because it is too large
+ 338891 - 338896
nlp/src/main/resources/tc.dict


+ 25 - 5
nlp/src/test/java/org/diagbot/nlp/test/LexemeDicTest.java

@@ -1,6 +1,7 @@
 package org.diagbot.nlp.test;
 
 import org.diagbot.pub.jdbc.MysqlJdbc;
+import org.diagbot.pub.utils.security.EncrypDES;
 
 import java.io.FileWriter;
 import java.io.IOException;
@@ -25,17 +26,32 @@ public class LexemeDicTest {
         Statement st = null;
         ResultSet rs = null;
         try {
+            EncrypDES encrypDES = new EncrypDES();
+
             String path = this.getClass().getClassLoader().getResource("").getPath();
             path = path.substring(0, path.indexOf("target"));
-
-            String sql = "select distinct name, category_id from kl_standard_info where category_id != 100 and category_id is not null && category is not null order by name";
+            //所有词典库
+            String sql = "select distinct name, category_id, status from kl_standard_info where category_id != 100 and category_id is not null && category is not null order by name";
             st = conn.createStatement();
             rs = st.executeQuery(sql);
             List<Map.Entry<String, String>> libraryList = rsToMap(rs, true);
 
             FileWriter fw = new FileWriter(path + "src/main/resources/tc.dict");
             for (Map.Entry<String, String> entry : libraryList) {
-                fw.write(entry.getKey() + "|9|" + entry.getValue() + "\n");
+                fw.write(encrypDES.encrytor(entry.getKey() + "|9|" + entry.getValue()));
+                fw.write("\n");
+            }
+            fw.close();
+            //推送词典
+            sql = "select distinct name, category_id from kl_standard_info where status = 1 and category_id != 100 and category_id is not null && category is not null order by name";
+            st = conn.createStatement();
+            rs = st.executeQuery(sql);
+            libraryList = rsToMap(rs, true);
+
+            fw = new FileWriter(path + "src/main/resources/push-tc.dict");
+            for (Map.Entry<String, String> entry : libraryList) {
+                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
+                fw.write("\n");
             }
             fw.close();
 
@@ -47,7 +63,8 @@ public class LexemeDicTest {
 
             fw = new FileWriter(path + "src/main/resources/synonym.dict");
             for (Map.Entry<String, String> entry : libraryList) {
-                fw.write(entry.getKey() + "|" + entry.getValue() + "\n");
+                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
+                fw.write("\n");
             }
             fw.close();
 
@@ -59,7 +76,8 @@ public class LexemeDicTest {
 
             fw = new FileWriter(path + "src/main/resources/classify.dict");
             for (Map.Entry<String, String> entry : libraryList) {
-                fw.write(entry.getKey() + "|" + entry.getValue() + "\n");
+                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
+                fw.write("\n");
             }
             fw.close();
 
@@ -67,6 +85,8 @@ public class LexemeDicTest {
             ioe.printStackTrace();
         } catch (SQLException sqle) {
             sqle.printStackTrace();
+        }  catch (Exception e) {
+            e.printStackTrace();
         } finally {
             nlpJdbc.close(rs, st, conn);
         }

+ 10 - 10
nlp/src/test/java/org/diagbot/nlp/test/ParticipleTest.java

@@ -24,16 +24,16 @@ public class ParticipleTest {
     public static void main(String[] args) {
         try {
             String content = "剑突下痛胀痛1天,";
-            ParticipleTest test = new ParticipleTest();
-            InputStream is = test.getClass().getClassLoader().getResourceAsStream("present.txt");
-            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);
-
-            String line="";
-            StringBuffer  buffer = new StringBuffer();
-            while((line=br.readLine())!=null){
-                buffer.append(line);
-            }
-            content = buffer.toString();
+//            ParticipleTest test = new ParticipleTest();
+//            InputStream is = test.getClass().getClassLoader().getResourceAsStream("present.txt");
+//            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);
+//
+//            String line="";
+//            StringBuffer  buffer = new StringBuffer();
+//            while((line=br.readLine())!=null){
+//                buffer.append(line);
+//            }
+//            content = buffer.toString();
 
             long start = System.currentTimeMillis();
 

+ 73 - 0
public/src/main/java/org/diagbot/pub/utils/security/EncrypDES.java

@@ -0,0 +1,73 @@
+package org.diagbot.pub.utils.security;
+
+import org.apache.commons.codec.binary.Base64;
+
+import javax.crypto.*;
+import javax.crypto.spec.DESKeySpec;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * @ClassName org.diagbot.pub.utils.security.EncrypDES
+ * @Description TODO
+ * @Author fyeman
+ * @Date 2019/2/22/022 14:44
+ * @Version 1.0
+ **/
+public class EncrypDES {
+    //随机加密串
+    private String key = "AUYEJHHH2019";
+    // SecretKey 负责保存对称密钥
+    private SecretKey deskey;
+    // Cipher负责完成加密或解密工作
+    private Cipher c;
+    // 该字节数组负责保存加密的结果
+    private byte[] cipherByte;
+
+    public EncrypDES() throws Exception {
+        DESKeySpec desKey = new DESKeySpec(key.getBytes());
+        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
+        // 生成密钥
+        deskey = keyFactory.generateSecret(desKey);
+        // 生成Cipher对象,指定其支持的DES算法
+        c = Cipher.getInstance("DES");
+    }
+
+    /**
+     * 对字符串加密
+     *
+     * @param str
+     * @return
+     * @throws InvalidKeyException
+     * @throws IllegalBlockSizeException
+     * @throws BadPaddingException
+     */
+    public String encrytor(String str) throws InvalidKeyException,
+            IllegalBlockSizeException, BadPaddingException {
+        // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
+        c.init(Cipher.ENCRYPT_MODE, deskey);
+        byte[] src = str.getBytes();
+        // 加密,结果保存进cipherByte
+        cipherByte = c.doFinal(src);
+        return Base64.encodeBase64String(cipherByte);
+    }
+
+    /**
+     * 对字符串解密
+     *
+     * @param str
+     * @return
+     * @throws InvalidKeyException
+     * @throws IllegalBlockSizeException
+     * @throws BadPaddingException
+     */
+    public byte[] decryptor(String str) throws InvalidKeyException,
+            IllegalBlockSizeException, BadPaddingException {
+        byte[] buff = Base64.decodeBase64(str);
+        // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
+        c.init(Cipher.DECRYPT_MODE, deskey);
+        cipherByte = c.doFinal(buff);
+        return cipherByte;
+    }
+}
+