瀏覽代碼

三元组模型更新

louhr 6 年之前
父節點
當前提交
8750fe6668

+ 8 - 2
algorithm/src/main/java/org/algorithm/core/remoteModel/RelationExtractionModelFromHttp.java

@@ -1,10 +1,11 @@
-package org.algorithm.core.remoteModel;
+package org.algorithm.core.cnn.model.impl;
 
 
 import org.algorithm.core.cnn.AlgorithmCNNExecutor;
 import org.algorithm.core.cnn.AlgorithmCNNExecutor;
 import org.algorithm.core.cnn.entity.Lemma;
 import org.algorithm.core.cnn.entity.Lemma;
 import org.algorithm.core.cnn.entity.Triad;
 import org.algorithm.core.cnn.entity.Triad;
 import org.algorithm.util.HttpGetAndPost;
 import org.algorithm.util.HttpGetAndPost;
 import org.diagbot.pub.utils.PropertiesUtil;
 import org.diagbot.pub.utils.PropertiesUtil;
+import org.springframework.util.StringUtils;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
@@ -19,6 +20,9 @@ public class RelationExtractionModelFromHttp extends AlgorithmCNNExecutor {
     public List<Triad> execute(String content, List<Lemma> lemmas) {
     public List<Triad> execute(String content, List<Lemma> lemmas) {
         String url = getUrl();
         String url = getUrl();
         String positions = makePositionsParam(lemmas);
         String positions = makePositionsParam(lemmas);
+        if (StringUtils.isEmpty(positions)) {
+            return new ArrayList<>();
+        }
         String sentence = content;
         String sentence = content;
         String indexPairAndRelations = HttpGetAndPost.sendPost(url,
         String indexPairAndRelations = HttpGetAndPost.sendPost(url,
                 "sentence="+sentence+"&positions="+positions);
                 "sentence="+sentence+"&positions="+positions);
@@ -34,7 +38,9 @@ public class RelationExtractionModelFromHttp extends AlgorithmCNNExecutor {
         String results = "";
         String results = "";
         for(Lemma lm: lemmas)
         for(Lemma lm: lemmas)
             results += lm.getPosition() + "|";  // 形式:1,2|33,45|
             results += lm.getPosition() + "|";  // 形式:1,2|33,45|
-        results = results.substring(0, results.length() - 1);  // 形式:1,2|33,45
+        if (!StringUtils.isEmpty(results)) {
+            results = results.substring(0, results.length() - 1);  // 形式:1,2|33,45
+        }
         return results;
         return results;
     }
     }
 
 

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

@@ -2,7 +2,7 @@ package org.algorithm.test;
 
 
 import org.algorithm.core.cnn.entity.Lemma;
 import org.algorithm.core.cnn.entity.Lemma;
 import org.algorithm.core.cnn.entity.Triad;
 import org.algorithm.core.cnn.entity.Triad;
-import org.algorithm.core.remoteModel.RelationExtractionModelFromHttp;
+import org.algorithm.core.cnn.model.impl.RelationExtractionModelFromHttp;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;

+ 4 - 4
nlp-web/src/main/java/org/diagbot/nlp/controller/RelationExtractionController.java

@@ -31,10 +31,10 @@ public class RelationExtractionController {
     public Response extraction(String content) throws Exception {
     public Response extraction(String content) throws Exception {
         Response response = new Response();
         Response response = new Response();
         response.start();
         response.start();
-//        RelationAnalyze relationAnalyze = new RelationAnalyze();
-//        List<OutputInfo> outputInfos = relationAnalyze.analyze(content, FeatureType.parse("1"));
-        StructureAnalyze structureAnalyze = new StructureAnalyze();
-        List<OutputInfo> outputInfos = structureAnalyze.extract(content);
+        RelationAnalyze relationAnalyze = new RelationAnalyze();
+        List<OutputInfo> outputInfos = relationAnalyze.analyze(content, FeatureType.parse("1"));
+//        StructureAnalyze structureAnalyze = new StructureAnalyze();
+//        List<OutputInfo> outputInfos = structureAnalyze.extract(content);
         response.setData(outputInfos);
         response.setData(outputInfos);
         return response;
         return response;
     }
     }

+ 2 - 1
nlp/src/main/java/org/diagbot/nlp/relation/analyze/RelationAnalyze.java

@@ -3,6 +3,7 @@ package org.diagbot.nlp.relation.analyze;
 import org.algorithm.core.cnn.AlgorithmCNNExecutor;
 import org.algorithm.core.cnn.AlgorithmCNNExecutor;
 import org.algorithm.core.cnn.entity.Lemma;
 import org.algorithm.core.cnn.entity.Lemma;
 import org.algorithm.core.cnn.entity.Triad;
 import org.algorithm.core.cnn.entity.Triad;
+import org.algorithm.core.cnn.model.impl.RelationExtractionModelFromHttp;
 import org.algorithm.core.cnn.model.impl.RelationExtractionModelImpl;
 import org.algorithm.core.cnn.model.impl.RelationExtractionModelImpl;
 import org.diagbot.nlp.feature.FeatureType;
 import org.diagbot.nlp.feature.FeatureType;
 import org.diagbot.nlp.participle.ParticipleUtil;
 import org.diagbot.nlp.participle.ParticipleUtil;
@@ -35,7 +36,7 @@ public class RelationAnalyze {
             List<Lemma> lemmaParticiple = lemmaUtil.lexemeToTriadLemma(lexemes);
             List<Lemma> lemmaParticiple = lemmaUtil.lexemeToTriadLemma(lexemes);
             //调用CNN模型
             //调用CNN模型
             long start = System.currentTimeMillis();
             long start = System.currentTimeMillis();
-            AlgorithmCNNExecutor executor = new RelationExtractionModelImpl();
+            AlgorithmCNNExecutor executor = new RelationExtractionModelFromHttp();
             List<Triad> triads = executor.execute(part_content, lemmaParticiple);
             List<Triad> triads = executor.execute(part_content, lemmaParticiple);
             //删除不作为训练样本集
             //删除不作为训练样本集
             triads = lemmaUtil.findPairTraids(triads);
             triads = lemmaUtil.findPairTraids(triads);