瀏覽代碼

修改关联本体查找死循环BUG

louhr 5 年之前
父節點
當前提交
9fb377a0dd
共有 1 個文件被更改,包括 20 次插入3 次删除
  1. 20 3
      kernel/src/main/java/com/lantone/qc/kernel/structure/ai/process/EntityProcess.java

+ 20 - 3
kernel/src/main/java/com/lantone/qc/kernel/structure/ai/process/EntityProcess.java

@@ -40,9 +40,10 @@ public class EntityProcess {
         JSONArray relations = annotation.getJSONArray("R");
 
         List<Lemma> allLemmaList = loadAllLemmaList(entitys);
+        List<String> relationIds = new ArrayList<>();
         for (Lemma l : allLemmaList) {
             if (entityType.equals(l.getProperty())) {
-                findRelationLemma(l, allLemmaList, relations);
+                findRelationLemma(l, allLemmaList, relations, relationIds);
                 resultLemmaList.add(l);
             }
         }
@@ -78,19 +79,35 @@ public class EntityProcess {
      * @param allLemmaList
      * @param relations
      */
-    private void findRelationLemma(Lemma lemma, List<Lemma> allLemmaList, JSONArray relations) {
+    private void findRelationLemma(Lemma lemma, List<Lemma> allLemmaList, JSONArray relations, List<String> relationIds) {
         List<Relation> connectEntityIdList = getConnectEntityList(lemma.getId(), relations);
         for (Lemma l : allLemmaList) {
             for (Relation relation : connectEntityIdList) {
                 if (l.getId() == relation.getId()) {
                     l.setRelationName(relation.getRelationName());
-                    findRelationLemma(l, allLemmaList, relations);
+                    if (!hasRelation(l.getId(), lemma.getId(), relationIds)) {
+                        findRelationLemma(l, allLemmaList, relations, relationIds);
+                    }
                     lemma.addRelationLemmas(l);
                 }
             }
         }
     }
 
+    private boolean hasRelation(int a, int b, List<String> relationIds) {
+        String s = "";
+        if (a > b) {
+            s = a + "," + b;
+        } else {
+            s = b + "," + a;
+        }
+        if (relationIds.contains(s)) {
+            return true;
+        }
+        relationIds.add(s);
+        return false;
+    }
+
     /**
      * 处理关系抽取输出的json
      *