Procházet zdrojové kódy

Merge remote-tracking branch 'origin/push-dev' into push-dev-edite

kongwz před 5 roky
rodič
revize
219837f8c3

+ 33 - 0
nlp-web/src/main/java/org/diagbot/nlp/controller/SimilarController.java

@@ -0,0 +1,33 @@
+package org.diagbot.nlp.controller;
+
+import org.diagbot.nlp.similar.SimilarCacheUtil;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @Description:
+ * @Author: HUJING
+ * @Date: 2019/10/25 10:38
+ */
+@Controller
+@RequestMapping("/similar")
+public class SimilarController {
+
+    @RequestMapping(value = "/getsimilar", method = RequestMethod.POST)
+    @ResponseBody
+    public static List<String> getSimilarList(@RequestBody String inputWord) {
+        Map<String, List<String>> similarDictMap = SimilarCacheUtil.getSimilar_dict_map();
+        if (similarDictMap.get(inputWord) != null && similarDictMap.get(inputWord).size() > 0) {
+            return similarDictMap.get(inputWord);
+        }
+        return new ArrayList<>();
+    }
+}

+ 3 - 0
nlp/src/main/java/org/diagbot/nlp/participle/cfg/Configuration.java

@@ -10,5 +10,8 @@ public interface Configuration {
 	public Segment loadMainDict(List<String> dicts);
 	public Map<String, String> loadMapDict(String path);
 	public List<String> readFileContents(String path);
+	public List<String> readDirectFileContents(String path);
+	public List<String> readTargetFileContents(String path);
 	void writeFileContents(List<String> contents, String path);
+
 }

+ 61 - 21
nlp/src/main/java/org/diagbot/nlp/participle/cfg/DefaultConfig.java

@@ -86,35 +86,28 @@ public class DefaultConfig implements Configuration {
     }
 
     public List<String> readFileContents(String path) {
-        if (!StringUtils.isEmpty(path_prefix)) path = path_prefix + path;
+        if (!StringUtils.isEmpty(path_prefix)) {
+            path = path_prefix + path;
+        }
+        return readDirectFileContents(path);
+    }
+
+    public List<String> readDirectFileContents(String path) {
         InputStream is = null;
         List<String> fileContents = new ArrayList<String>(10);
         try {
-            if (StringUtils.isEmpty(path)) path = DEFAULT_PATH;
-            is = this.getClass().getClassLoader().getResourceAsStream(path);
+            if (StringUtils.isEmpty(path)) {
+                path = DEFAULT_PATH;
+            }
+            //            is = this.getClass().getClassLoader().getResourceAsStream(path);
             File file = new File(path);
-//            File file = new File(this.getClass().getResource(path).getFile());
+            //            File file = new File(this.getClass().getResource(path).getFile());
             is = new FileInputStream(file);
             if (is == null) {
                 throw new RuntimeException(path + ".......文件未找到!!");
             }
 
-            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);
-
-            EncrypDES encrypDES = new EncrypDES();
-            String theWord = null;
-            int i = 0;
-            do {
-                theWord = br.readLine();
-                if (theWord != null && !"".equals(theWord.trim())) {
-                    if (i == 5) {
-                        logger.info("读取文件第六行解密前:" + theWord.trim());
-                        logger.info("读取文件第六行解密后:" + encrypDES.decryptor(theWord.trim()));
-                    }
-                    fileContents.add(encrypDES.decryptor(theWord.trim()));
-                }
-                i++;
-            } while (theWord != null);
+            addFileContents(is, fileContents);
         } catch (Exception ioe) {
             System.err.println("读取文件" + path + "出错.......................");
             ioe.printStackTrace();
@@ -124,12 +117,59 @@ public class DefaultConfig implements Configuration {
         return fileContents;
     }
 
+    public List<String> readTargetFileContents(String path) {
+        InputStream is = null;
+        List<String> fileContents = new ArrayList<String>(10);
+        try {
+            if (StringUtils.isEmpty(path)) {
+                path = DEFAULT_PATH;
+            }
+            is = this.getClass().getClassLoader().getResourceAsStream(path);
+            //            File file = new File(path);
+            //            File file = new File(this.getClass().getResource(path).getFile());
+            //            is = new FileInputStream(file);
+            if (is == null) {
+                throw new RuntimeException(path + ".......文件未找到!!");
+            }
+
+            addFileContents(is, fileContents);
+        } catch (Exception ioe) {
+            System.err.println("读取文件" + path + "出错.......................");
+            ioe.printStackTrace();
+        } finally {
+            this.close(is);
+        }
+        return fileContents;
+    }
+
+    public void addFileContents(InputStream is, List<String> fileContents) throws Exception {
+        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);
+
+        EncrypDES encrypDES = new EncrypDES();
+        String theWord = null;
+        int i = 0;
+        do {
+            theWord = br.readLine();
+            if (theWord != null && !"".equals(theWord.trim())) {
+                if (i == 5) {
+                    logger.info("读取文件第六行解密前:" + theWord.trim());
+                    logger.info("读取文件第六行解密后:" + encrypDES.decryptor(theWord.trim()));
+                }
+                fileContents.add(encrypDES.decryptor(theWord.trim()));
+            }
+            i++;
+        } while (theWord != null);
+    }
+
+
     public void writeFileContents(List<String> contents, String path) {
         this.writeFileContents(contents, path, "");
     }
 
     public void writeFileContents(List<String> contents, String path, String separator) {
-        if (!StringUtils.isEmpty(path_prefix)) path = path_prefix + path;
+        if (!StringUtils.isEmpty(path_prefix)) {
+            path = path_prefix + path;
+        }
         try {
             FileWriter fw = new FileWriter(path);
             for (String content : contents) {

+ 92 - 0
nlp/src/main/java/org/diagbot/nlp/similar/SimilarCacheFileManager.java

@@ -0,0 +1,92 @@
+package org.diagbot.nlp.similar;
+
+import org.diagbot.pub.jdbc.MysqlJdbc;
+import org.diagbot.pub.utils.PropertiesUtil;
+import org.diagbot.pub.utils.security.EncrypDES;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * @Description:
+ * @Author: HUJING
+ * @Date: 2019/10/16 13:46
+ */
+public class SimilarCacheFileManager {
+    private String user = "root";
+    private String password = "diagbot@20180822";
+    private String url = "jdbc:mysql://192.168.2.235:3306/med-s?useUnicode=true&characterEncoding=UTF-8";
+
+    private String path = "";
+
+    public static void main(String[] args) {
+        SimilarCacheFileManager similarCacheFileManager = new SimilarCacheFileManager();
+        String resourcePath = similarCacheFileManager.getResourcePath();
+//        PropertiesUtil propertiesUtil = new PropertiesUtil("nlp.properties");
+//        String p = propertiesUtil.getProperty("cache.file.dir");
+        File file = new File(resourcePath);
+        if (!file.exists()) {
+            file.mkdirs();
+        }
+        //相似度知识库
+        similarCacheFileManager.createSimilarCacheFile(resourcePath);
+    }
+
+    public String getResourcePath(){
+        return this.getClass().getResource("/").getPath();
+    }
+
+    public SimilarCacheFileManager() {
+//        PropertiesUtil propertiesUtil = new PropertiesUtil("nlp.properties");
+//        String p = propertiesUtil.getProperty("cache.file.dir");
+
+        String resourcePath = getResourcePath();
+        File file = new File(resourcePath);
+        if (!file.exists()) {
+            file.mkdirs();
+        }
+        path = resourcePath;
+    }
+
+    public void createCacheFile() {
+        createSimilarCacheFile(path);
+    }
+
+    public void createSimilarCacheFile(String path) {
+        MysqlJdbc nlpJdbc = new MysqlJdbc(user, password, url);
+        Connection conn = nlpJdbc.connect();
+        Statement st = null;
+        ResultSet rs = null;
+        try {
+            EncrypDES encrypDES = new EncrypDES();
+            //疾病科室
+            String sql = "SELECT similar_1,similar_2 FROM doc_similar";
+            st = conn.createStatement();
+            rs = st.executeQuery(sql);
+            FileWriter fw = new FileWriter(path + "bigdata_similar.dict");
+
+            String r1, r2;
+            while (rs.next()) {
+                r1 = rs.getString(1).trim();
+                r2 = rs.getString(2).trim();
+
+                fw.write(encrypDES.encrytor(r1 + "|" + r2));
+                fw.write("\n");
+            }
+            fw.close();
+        } catch (IOException ioe) {
+            ioe.printStackTrace();
+        } catch (SQLException sqle) {
+            sqle.printStackTrace();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            nlpJdbc.close(rs, st, conn);
+        }
+    }
+}

+ 44 - 0
nlp/src/main/java/org/diagbot/nlp/similar/SimilarCacheUtil.java

@@ -0,0 +1,44 @@
+package org.diagbot.nlp.similar;
+
+import org.diagbot.nlp.participle.cfg.Configuration;
+import org.diagbot.nlp.participle.cfg.DefaultConfig;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @Description:
+ * @Author: HUJING
+ * @Date: 2019/10/16 15:30
+ */
+public class SimilarCacheUtil {
+    //相似度知识库
+    public static Map<String, List<String>> similar_dict_map = null;
+
+    public static Map<String, List<String>> getSimilar_dict_map() {
+        if (similar_dict_map == null) {
+            createSimilar_dict_map();
+        }
+        return similar_dict_map;
+    }
+
+    public static Map<String, List<String>> createSimilar_dict_map() {
+        similar_dict_map = new HashMap<>();
+        List<String> similarList = null;
+        Configuration configuration = new DefaultConfig();
+        List<String> fileContents = configuration.readTargetFileContents("bigdata_similar.dict");
+        for (String line:fileContents) {
+            String[] similarPair = line.split("\\|", -1);
+            if (similar_dict_map.get(similarPair[0]) == null){
+                similarList = new ArrayList<>();
+                similarList.add(similarPair[1]);
+                similar_dict_map.put(similarPair[0],similarList);
+            } else {
+                similar_dict_map.get(similarPair[0]).add(similarPair[1]);
+            }
+        }
+        return similar_dict_map;
+    }
+}

+ 21 - 0
nlp/src/main/java/org/diagbot/nlp/similar/SimilarUtil.java

@@ -0,0 +1,21 @@
+package org.diagbot.nlp.similar;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @Description:
+ * @Author: HUJING
+ * @Date: 2019/10/16 15:59
+ */
+public class SimilarUtil {
+
+    public static List<String> getSimilarList(String inputWord) {
+        Map<String, List<String>> similarDictMap = SimilarCacheUtil.getSimilar_dict_map();
+        if (similarDictMap.get(inputWord) != null && similarDictMap.get(inputWord).size() > 0){
+            return similarDictMap.get(inputWord);
+        }
+        return new ArrayList<>();
+    }
+}

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 57738 - 0
nlp/src/main/resources/bigdata_similar.dict


+ 110 - 0
nlp/src/test/java/org/diagbot/nlp/test/Similar2DBTest.java

@@ -0,0 +1,110 @@
+package org.diagbot.nlp.test;
+
+import org.diagbot.nlp.similar.SimilarUtil;
+import org.diagbot.pub.jdbc.MysqlJdbc;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @Description:
+ * @Author: HUJING
+ * @Date: 2019/10/16 10:11
+ */
+public class Similar2DBTest {
+    private String user = "root";
+    private String password = "diagbot@20180822";
+    private String url = "jdbc:mysql://192.168.2.235:3306/med-s?useUnicode=true&characterEncoding=UTF-8";
+
+    public static void main(String[] args) {
+//        String fileName = "D:/大数据小组/相似度知识库.txt";
+//        Similar2DBTest similar2DBTest = new Similar2DBTest();
+//        Map<String, Set<String>> similarFile = similar2DBTest.readSimilarFile(fileName);
+//        similar2DBTest.write2DB(similarFile);
+        List<String> similarList = SimilarUtil.getSimilarList("表情淡漠");
+        System.out.println(similarList);
+        similarList = SimilarUtil.getSimilarList("右侧乳房");
+        System.out.println(similarList);
+//        SimilarCacheFileManager similarCacheFileManager = new SimilarCacheFileManager();
+//        String resourcePath = similarCacheFileManager.getResourcePath();
+//        System.out.println(resourcePath);
+
+    }
+
+    public Map<String, Set<String>> readSimilarFile(String fileName) {
+        BufferedReader reader = null;
+        Map<String, Set<String>> similarMap = new HashMap<>();
+        Set<String> similarSet = null;
+        try {
+            reader = new BufferedReader(new FileReader(new File(fileName)));
+            String line = "";
+            while ((line = reader.readLine()) != null) {
+                line = line.replaceAll("\'|]|\\[", "");
+                String[] words = line.split(",");
+                if (similarMap.get(words[0]) == null) {
+                    similarSet = new HashSet<>();
+                    similarSet.add(words[1]);
+                    similarMap.put(words[0], similarSet);
+                } else {
+                    similarMap.get(words[0]).add(words[1]);
+                }
+                //                System.out.println(line);
+            }
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                reader.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return similarMap;
+    }
+
+    public void write2DB(Map<String, Set<String>> similarMap) {
+        MysqlJdbc jdbc = new MysqlJdbc(user, password, url);
+        Connection conn = jdbc.connect();
+        PreparedStatement pst = null;
+        String sql = "INSERT INTO doc_similar(similar_1,similar_2) VALUES(?,?)";
+        try {
+            pst = conn.prepareStatement(sql);
+
+            String similar1 = "";
+            int i = 1;
+            for (Map.Entry<String, Set<String>> similarFileMap : similarMap.entrySet()) {
+                //            System.out.println(similarFileMap.getKey());
+                similar1 = similarFileMap.getKey();
+                pst.setString(1, similar1);
+                for (String similar2 : similarFileMap.getValue()) {
+                    pst.setString(2, similar2);
+                    System.out.println(i++);
+                    pst.addBatch();
+                    pst.executeBatch();
+                }
+            }
+        } catch (SQLException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                pst.close();
+                conn.close();
+            } catch (SQLException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}