Browse Source

1- 修改文本长度限制。

bijl 6 years ago
parent
commit
3a37bd07e1

+ 80 - 0
algorithm/src/main/java/org/algorithm/core/remoteModel/RelationExtractionModelFromHttp.java

@@ -0,0 +1,80 @@
+package org.algorithm.core.remoteModel;
+
+import org.algorithm.core.cnn.AlgorithmCNNExecutor;
+import org.algorithm.core.cnn.entity.Lemma;
+import org.algorithm.core.cnn.entity.Triad;
+import org.algorithm.util.HttpGetAndPost;
+import org.diagbot.pub.utils.PropertiesUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Author: bijl
+ * @Date: 2019/5/21 11:02
+ * @Decription: 关系抽取模型接口,从网络中获取
+ */
+public class RelationExtractionModelFromHttp extends AlgorithmCNNExecutor {
+    @Override
+    public List<Triad> execute(String content, List<Lemma> lemmas) {
+        String url = getUrl();
+        String positions = makePositionsParam(lemmas);
+        String sentence = content;
+        String indexPairAndRelations = HttpGetAndPost.sendPost(url,
+                "sentence="+sentence+"&positions="+positions);
+        return mergePredictInfoToTriads(indexPairAndRelations, lemmas);  // 合并信息到三元组中去
+    }
+
+    /**
+     * 用lemma内部属性position组成字符串,按lemma在lemmas中的顺序
+     * @param lemmas 实体list
+     * @return
+     */
+    private String makePositionsParam(List<Lemma> lemmas){
+        String results = "";
+        for(Lemma lm: lemmas)
+            results += lm.getPosition() + "|";  // 形式:1,2|33,45|
+        results = results.substring(0, results.length() - 1);  // 形式:1,2|33,45
+        return results;
+    }
+
+    /**
+     * 获取url
+     * @return
+     */
+    private String getUrl(){
+        PropertiesUtil prop = new PropertiesUtil("/algorithm.properties");
+        return prop.getProperty("relationExtractionUrl");
+    }
+
+    /**
+     * 合并预测信息到三元组中去
+     * @param indexPairAndRelations 预测信息,来自网络 // 形式:1,2:有|3,5:无
+     * @param lemmas 实体
+     * @return 有关系的实体对
+     */
+    private List<Triad> mergePredictInfoToTriads(String indexPairAndRelations, List<Lemma> lemmas){
+        List<Triad> triads = new ArrayList<>();
+        String[] posRelationArray = indexPairAndRelations.split("\\|");
+        String[] info;
+        String[] indexPair;
+        int index1;
+        int index2;
+        for(String posRelation:posRelationArray){
+            info = posRelation.split(":");  // 形式:1,2:有
+            if ("有".equals(info[1])){  // 仅返回有关系的
+                indexPair = info[0].split(","); // 形式:1,2
+                index1 = Integer.parseInt(indexPair[0]);
+                index2 = Integer.parseInt(indexPair[1]);
+                Triad triad = new Triad();
+                triad.setL_1(lemmas.get(index1));
+                triad.setL_2(lemmas.get(index2));
+                triad.setRelation(info[1]);
+                triads.add(triad);
+            }
+        }
+
+        return triads;
+    }
+}
+

+ 21 - 0
algorithm/src/main/java/org/algorithm/test/HttpGetAndPostTest.java

@@ -0,0 +1,21 @@
+package org.algorithm.test;
+import org.algorithm.util.HttpGetAndPost;
+
+/**
+ * @Author: bijl
+ * @Date: 2019/5/20 14:16
+ * @Decription:
+ */
+public class HttpGetAndPostTest {
+    public static void main(String[] args) {
+        //发送 GET 请求
+//        String s=HttpGetAndPostTest.sendGet("http://192.168.3.40:54321/hello", "");
+//        System.out.println(s);
+
+        //发送 POST 请求
+        String sr=HttpGetAndPost.sendPost("http://192.168.3.40:54321/api/re/predict",
+                "sentence=眼眶痛头痛&positions=456");
+        System.out.println(sr);
+
+    }
+}

+ 53 - 0
algorithm/src/main/java/org/algorithm/test/RelationExtractionModelFromHttpTest.java

@@ -0,0 +1,53 @@
+package org.algorithm.test;
+
+import org.algorithm.core.cnn.entity.Lemma;
+import org.algorithm.core.cnn.entity.Triad;
+import org.algorithm.core.remoteModel.RelationExtractionModelFromHttp;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Author: bijl
+ * @Date: 2019/5/21 14:03
+ * @Decription:
+ */
+public class RelationExtractionModelFromHttpTest {
+    public static void main(String[] args) {
+
+        String content = "脓涕咽部不适十余天,少量咳嗽,咳黄色痰,眼眶痛头痛2天,无咽痛、发热";
+        List<Lemma> lemmas = new ArrayList<>();
+        Lemma l1 = new Lemma();
+        l1.setText("少量");
+        l1.setPosition("10,11");
+
+        Lemma l2 = new Lemma();
+        l2.setText("咳嗽");
+        l2.setPosition("12,13");
+
+        Lemma l3 = new Lemma();
+        l3.setText("咳");
+        l3.setPosition("15,15");
+
+        Lemma l4 = new Lemma();
+        l4.setText("黄色痰");
+        l4.setPosition("16,18");
+
+        lemmas.add(l1);
+        lemmas.add(l2);
+        lemmas.add(l3);
+        lemmas.add(l4);
+        RelationExtractionModelFromHttp re = new RelationExtractionModelFromHttp();
+        List<Triad> triads = re.execute(content, lemmas);
+        for (Triad triad:triads){
+            System.out.println(triad.getL_1().getText());
+            System.out.println(triad.getL_2().getText());
+            System.out.println(triad.getRelation());
+        }
+//    # "entity_1_position": [12, 13],
+//    # "entity_2_position": [10, 11],
+//        positions_string = '12,13|10,11|15,15|16,18'
+//        public List<Triad> execute(String content, List<Lemma > lemmas) {
+
+    }
+}

+ 1 - 1
algorithm/src/main/java/org/algorithm/test/RelationExtractionModelTest.java

@@ -18,7 +18,7 @@ public class RelationExtractionModelTest {
 
     public static void main(String[] args) {
 
-        String sentence = "有双手麻木感,活动后好转,颈部及肩部活动度无殊";
+        String sentence = "病来患者神清,精神软,食欲、睡眠差,小便如常,大便1/日,水样便,近半年体重无明显增减";
         RelationExtractionModel model = new RelationExtractionModelImpl();
 
         List<Lemma> lemmas = new ArrayList<>();

+ 141 - 0
algorithm/src/main/java/org/algorithm/util/HttpGetAndPost.java

@@ -0,0 +1,141 @@
+package org.algorithm.util;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @Author: bijl
+ * @Date: 2019/5/21 10:54
+ * @Decription: http get和post请求
+ */
+public class HttpGetAndPost {
+    /**
+     * 向指定URL发送GET方法的请求
+     *
+     * @param url
+     *            发送请求的URL
+     * @param param
+     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
+     * @return URL 所代表远程资源的响应结果
+     */
+    public static String sendGet(String url, String param) {
+        String result = "";
+        BufferedReader in = null;
+        try {
+            String urlNameString = url + "?" + param;
+            URL realUrl = new URL(urlNameString);
+            // 打开和URL之间的连接
+            URLConnection connection = realUrl.openConnection();
+            // 设置通用的请求属性
+            connection.setRequestProperty("accept", "*/*");
+            connection.setRequestProperty("connection", "Keep-Alive");
+            connection.setRequestProperty("user-agent",
+                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+            // 建立实际的连接
+            connection.connect();
+            // 获取所有响应头字段
+            Map<String, List<String>> map = connection.getHeaderFields();
+            // 遍历所有的响应头字段
+            for (String key : map.keySet()) {
+                System.out.println(key + "--->" + map.get(key));
+            }
+            // 定义 BufferedReader输入流来读取URL的响应
+            in = new BufferedReader(new InputStreamReader(
+                    connection.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null) {
+                result += line;
+            }
+        } catch (Exception e) {
+            System.out.println("发送GET请求出现异常!" + e);
+            e.printStackTrace();
+        }
+        // 使用finally块来关闭输入流
+        finally {
+            try {
+                if (in != null) {
+                    in.close();
+                }
+            } catch (Exception e2) {
+                e2.printStackTrace();
+            }
+        }
+        return result;
+    }
+
+    /**
+     * 向指定 URL 发送POST方法的请求
+     *
+     * @param url
+     *            发送请求的 URL
+     * @param param
+     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
+     * @return 所代表远程资源的响应结果
+     */
+    public static String sendPost(String url, String param) {
+        PrintWriter out = null;
+        BufferedReader in = null;
+        String result = "";
+        try {
+            URL realUrl = new URL(url);
+            // 打开和URL之间的连接
+            URLConnection conn = realUrl.openConnection();
+            // 设置通用的请求属性
+            conn.setRequestProperty("accept", "*/*");
+            conn.setRequestProperty("connection", "Keep-Alive");
+            conn.setRequestProperty("user-agent",
+                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+            // 发送POST请求必须设置如下两行
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+            // 获取URLConnection对象对应的输出流
+            out = new PrintWriter(conn.getOutputStream());
+            // 发送请求参数
+            out.print(param);
+            // flush输出流的缓冲
+            out.flush();
+            // 定义BufferedReader输入流来读取URL的响应
+            in = new BufferedReader(
+                    new InputStreamReader(conn.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null) {
+                result += line;
+            }
+        } catch (Exception e) {
+            System.out.println("发送 POST 请求出现异常!"+e);
+            e.printStackTrace();
+        }
+        //使用finally块来关闭输出流、输入流
+        finally{
+            try{
+                if(out!=null){
+                    out.close();
+                }
+                if(in!=null){
+                    in.close();
+                }
+            }
+            catch(IOException ex){
+                ex.printStackTrace();
+            }
+        }
+        return result;
+    }
+
+    public static void main(String[] args) {
+        //发送 GET 请求
+        String s=HttpGetAndPost.sendGet("http://192.168.3.40:54321/hello", "");
+        System.out.println(s);
+
+        //发送 POST 请求
+        String sr=HttpGetAndPost.sendPost("http://192.168.3.40:54321/hello", "key=123&v=456");
+        System.out.println(sr);
+
+    }
+}

+ 3 - 2
algorithm/src/main/resources/algorithm.properties

@@ -1,8 +1,8 @@
 ################################ model basic url ###################################
 
 #basicPath=E:/git/push/algorithm/src/main/models/model_version_replacement/model
-basicPath=/opt/models/dev/models/model_version_replacement/model
-#basicPath=E:/models/model_version_replacement/model
+#basicPath=/opt/models/dev/models/model_version_replacement/model
+basicPath=E:/xxx/model_version_replacement/model
 
 ############################### current model version ################################
 diagnosisPredict.version=outpatient_556_IOE_1
@@ -18,3 +18,4 @@ diagnosisToVital.version=diagnosis_to_vital_1
 
 ############################ relation extraction ######################################
 relationExtraction=relation_extraction
+relationExtractionUrl=http://192.168.3.40:54321/api/re/predict

+ 1 - 1
push-web/src/main/resources/static/dist/js/push.js

@@ -1,4 +1,4 @@
-var nlp_web_url = "http://192.168.2.234:5002/nlp-web";
+var nlp_web_url = "http://192.168.3.40:5002/nlp-web";
 var bigdata_web_url = "http://192.168.2.234:5001/bigdata-web";
 var graph_web_url = "http://192.168.2.234:5003/graph-web";
 // var push_web_url = "http://192.168.2.234:5008/push-web";