Browse Source

1- 调整返回类型为组合+阴性词。

bijl 5 years atrás
parent
commit
10d9204df9

+ 121 - 57
algorithm/src/main/java/org/algorithm/core/RelationTreeUtils.java

@@ -17,7 +17,7 @@ public class RelationTreeUtils {
     /**
      * 同名实体(这里也叫词项)归并
      * 规则:
-     *  1- 直接替代为位置最前面的一个
+     * 1- 直接替代为位置最前面的一个
      *
      * @param triads 实体对列表
      */
@@ -55,7 +55,7 @@ public class RelationTreeUtils {
     /**
      * 构建关系树
      * 基本规则:
-     *  1- 两个有关系的实体,前面的为父节点,后面的为子节点
+     * 1- 两个有关系的实体,前面的为父节点,后面的为子节点
      *
      * @param triads 有关系的三元组列表
      */
@@ -64,12 +64,10 @@ public class RelationTreeUtils {
             Lemma l1 = triad.getL_1();
             Lemma l2 = triad.getL_2();
             if (l1.getStartPosition() < l2.getStartPosition()) {  // 在前者为父节点
-                l1.setLeafNode(false);
-                l2.setLeafNode(true);
+                l1.setHaveChildren(true);
                 l2.setParent(l1);
             } else {
-                l1.setLeafNode(true);
-                l2.setLeafNode(false);
+                l2.setHaveChildren(true);
                 l1.setParent(l2);
             }
         }
@@ -78,21 +76,17 @@ public class RelationTreeUtils {
     /**
      * 获取关系树的分枝
      *
-     * @param triads      有关系,并且设置了父子节点关系的三元组
+     * @param triads 有关系,并且设置了父子节点关系的三元组
      */
     public static List<List<String>> getRelationTreeBranches(List<Triad> triads) {
         Map<Lemma, Integer> leafNodeLemmas = new HashMap<>();
 
         for (Triad triad : triads) {
-            if (triad.getL_1().isLeafNode()){
-                if(leafNodeLemmas.get(triad.getL_1()) == null)
-                    leafNodeLemmas.put(triad.getL_1(), 1);
-            }
+            if (!triad.getL_1().isHaveChildren())
+                leafNodeLemmas.putIfAbsent(triad.getL_1(), 1);
 
-            if (triad.getL_2().isLeafNode()){
-                if(leafNodeLemmas.get(triad.getL_2()) == null)
-                    leafNodeLemmas.put(triad.getL_2(), 1);
-            }
+            if (!triad.getL_2().isHaveChildren())
+                leafNodeLemmas.putIfAbsent(triad.getL_2(), 1);
         }
 
         List<List<String>> branches = new ArrayList<>();
@@ -103,17 +97,54 @@ public class RelationTreeUtils {
                 lemma = lemma.getParent();
             }
             aBranch.sort(Comparator.naturalOrder());  // 按位置排序
-            List<String> ordered_names = new ArrayList<>();
-            for (Lemma le: aBranch) {
-                ordered_names.add(le.getText());
-            }
-            branches.add(ordered_names);
+            branches.addAll(handleBranch(aBranch));
         }
 
 
         return branches;
     }
 
+    /**
+     * 处理分枝,要求组合非阴性词,阴性词必须包含
+     * 操作:
+     * 1- 分离阴性词和非阴性词
+     * 2- 组合非阴性词
+     * 3- 添加阴性词到组合结果中
+     *
+     * @param aBranch
+     * @return
+     */
+    private static List<List<String>> handleBranch(List<Lemma> aBranch) {
+        List<Lemma> nonNegativeLemmas = new ArrayList<>();
+        List<Lemma> negativeLemmas = new ArrayList<>();
+        for (Lemma lemma : aBranch) {
+            if ("反意或虚拟".equals(lemma.getProperty()))
+                negativeLemmas.add(lemma);
+            else
+                nonNegativeLemmas.add(lemma);
+        }
+        List<List<Lemma>> nonNegativeLemmaCombinations = new ArrayList<>();
+        if (nonNegativeLemmas.size() > 0) {
+            for (int i = 1; i <= nonNegativeLemmas.size(); i++) {
+                combinerSelect(nonNegativeLemmas, new ArrayList<>(), nonNegativeLemmaCombinations,
+                        nonNegativeLemmas.size(), i);
+            }
+        }
+        List<List<String>> result = new ArrayList<>();
+        for (List<Lemma> lemmaCombination : nonNegativeLemmaCombinations) {
+            List<String> lemmaNames = new ArrayList<>();
+            lemmaCombination.addAll(negativeLemmas);  // 阴性词加入到组合中
+            lemmaCombination.sort(Comparator.naturalOrder());  // 按位置排序
+            for (Lemma lemma : lemmaCombination)  // 取出名称
+                lemmaNames.add(lemma.getText());
+            if (lemmaNames.size() >= 2)
+                result.add(lemmaNames);
+        }
+
+        return result;
+
+    }
+
     /**
      * 从三元组列表到关系树分枝
      *
@@ -121,11 +152,41 @@ public class RelationTreeUtils {
      * @return
      */
     public static List<List<String>> triadsToRelationTreeBranches(List<Triad> triads) {
-        sameTextLemmaMerge(triads);
+//        sameTextLemmaMerge(triads);
         buildRelationTree(triads);
         return getRelationTreeBranches(triads);
     }
 
+    /**
+     * 组合生成器
+     *
+     * @param data      原始数据
+     * @param workSpace 自定义一个临时空间,用来存储每次符合条件的值
+     * @param k         C(n,k)中的k
+     */
+    private static <E> void combinerSelect(List<E> data, List<E> workSpace, List<List<E>> result, int n, int k) {
+        List<E> copyData;
+        List<E> copyWorkSpace = null;
+
+        if (workSpace.size() == k) {
+//            for (E c : workSpace)
+//                System.out.print(c);
+
+            result.add(new ArrayList<>(workSpace));
+//            System.out.println();
+        }
+
+        for (int i = 0; i < data.size(); i++) {
+            copyData = new ArrayList<E>(data);
+            copyWorkSpace = new ArrayList<E>(workSpace);
+
+            copyWorkSpace.add(copyData.get(i));
+            for (int j = i; j >= 0; j--)
+                copyData.remove(j);
+            combinerSelect(copyData, copyWorkSpace, result, n, k);
+        }
+    }
+
     /**
      * 全排列算法
      *
@@ -154,7 +215,7 @@ public class RelationTreeUtils {
                 }
             }
 
-            result = new ArrayList<ArrayList<String>>(current);
+            result = new ArrayList<>(current);
         }
 
         return result;
@@ -167,52 +228,55 @@ public class RelationTreeUtils {
     public static void test() {
 
         List<Triad> triads = new ArrayList<>();
-        Lemma l1_1 = new Lemma();
-        Lemma l1_2 = new Lemma();
-        l1_1.setText("子宫");
-        l1_1.setPosition("0,2");
-
-        l1_2.setText("内膜");
-        l1_2.setPosition("5,8");
+        String[] arr_1 = {"子宫", "0,1", "部位"};
+        String[] arr_2 = {"内膜", "2,3", "结构"};
+        addTriad(arr_1, arr_2, triads);
 
-        Triad triad_1 = new Triad();
-        triad_1.setL_1(l1_1);
-        triad_1.setL_2(l1_2);
-        triads.add(triad_1);
+        String[] arr_1_1 = {"不", "13,13", "反意或虚拟"};
+        String[] arr_2_1 = {"出血", "10,11", "形容词"};
+        addTriad(arr_1_1, arr_2_1, triads);
 
-        Lemma l2_1 = new Lemma();
-        Lemma l2_2 = new Lemma();
-        l2_1.setText("宫颈线");
-        l2_1.setPosition("11,13");
+        String[] arr_1_2 = {"胸部", "15,16", "部位"};
+        String[] arr_2_2 = {"剧烈", "17,18", "程度"};
+        addTriad(arr_1_2, arr_2_2, triads);
 
-        l2_2.setText("很长");
-        l2_2.setPosition("15,18");
+        String[] arr_1_3 = {"疼痛", "17,18", "形容词"};
+        String[] arr_2_3 = {"剧烈", "19,20", "程度"};
+        addTriad(arr_1_3, arr_2_3, triads);
 
-        Triad triad_2 = new Triad();
-        triad_2.setL_1(l2_1);
-        triad_2.setL_2(l2_2);
-        triads.add(triad_2);
+        String[] arr_1_4 = {"内膜", "2,3", "结构"};
+        String[] arr_2_4 = {"出血", "10,11", "形容词"};
+        addTriad(arr_1_4, arr_2_4, triads);
 
+        System.out.println(triads.size());
+        sameTextLemmaMerge(triads);
+        buildRelationTree(triads);
+        List<List<String>> info = getRelationTreeBranches(triads);
 
-        Lemma l3_1 = new Lemma();
-        Lemma l3_2 = new Lemma();
+        System.out.println(info);
+    }
 
-        l3_1.setText("内膜");
-        l3_1.setPosition("5,8");
+    /**
+     * 增加三元组
+     */
+    private static void addTriad(String[] lemma_1, String[] lemma_2, List<Triad> triads) {
+        Lemma lemma1 = new Lemma();
+        lemma1.setText(lemma_1[0]);
+        lemma1.setPosition(lemma_1[1]);
+        lemma1.setProperty(lemma_1[2]);
 
-        l3_2.setText("出血");
-        l3_2.setPosition("9,10");
+        Lemma lemma2 = new Lemma();
+        lemma2.setText(lemma_2[0]);
+        lemma2.setPosition(lemma_2[1]);
+        lemma2.setProperty(lemma_2[2]);
 
-        Triad triad_3 = new Triad();
-        triad_3.setL_1(l3_1);
-        triad_3.setL_2(l3_2);
-        triads.add(triad_3);
+        Triad triad = new Triad();
+        triad.setL_1(lemma1);
+        triad.setL_2(lemma2);
 
-        sameTextLemmaMerge(triads);
-        buildRelationTree(triads);
-        List<List<String>> info = getRelationTreeBranches(triads);
+        triads.add(triad);
 
-        System.out.println(info);
     }
 
+
 }

+ 6 - 7
algorithm/src/main/java/org/algorithm/core/cnn/entity/Lemma.java

@@ -18,17 +18,16 @@ public class Lemma implements Comparable<Lemma> {
 
     private Lemma parent;
 
-    private boolean isLeafNode;
+    private boolean haveChildren = false;
 
-    public boolean isLeafNode() {
-        return isLeafNode;
+    public boolean isHaveChildren() {
+        return haveChildren;
     }
 
-    public void setLeafNode(boolean leafNode) {
-        isLeafNode = leafNode;
+    public void setHaveChildren(boolean haveChildren) {
+        this.haveChildren = haveChildren;
     }
 
-
     public Lemma getParent() {
         return parent;
     }
@@ -37,7 +36,7 @@ public class Lemma implements Comparable<Lemma> {
         this.parent = parent;
     }
 
-    public int getStartPosition(){
+    public int getStartPosition() {
         String[] pos = this.position.split(",");
         return Integer.parseInt(pos[0]);
     }

+ 38 - 48
algorithm/src/main/java/org/algorithm/test/Test.java

@@ -6,60 +6,50 @@ public class Test {
 
 
     public static void main(String[] args) {
-        Set<String> result = new HashSet<String>();
-        Set<String> set1 = new HashSet<String>() {
-            {
-                add("王者荣耀");
-                add("英雄联盟");
-                add("穿越火线");
-                add("地下城与勇士");
-            }
-        };
-
-        Set<String> set2 = new HashSet<String>() {
-            {
-                add("王者荣耀");
-                add("地下城与勇士");
-                add("魔兽世界");
-            }
-        };
-
-        result.clear();
-        result.addAll(set1);
-        result.retainAll(set2);
-        System.out.println("交集:" + result);
+        List<Integer> data = new ArrayList<>();
+        data.add(1);
+        data.add(3);
+        data.add(5);
+        data.add(7);
+        Test t = new Test();
+
+        List<List<Integer>> workSpace = new ArrayList<>();
+        for (int i = 1; i < data.size(); i++) {
+            t.combinerSelect(data, new ArrayList<>(), workSpace, data.size(), i);
+        }
 
-        result.clear();
-        result.addAll(set1);
-        result.removeAll(set2);
-        System.out.println("差集:" + result);
+        System.out.println(workSpace);
 
-        result.clear();
-        result.addAll(set1);
-        result.addAll(set2);
-        System.out.println("并集:" + result);
+    }
 
-        List<Integer> aList = new ArrayList<>();
-        aList.add(1);
-        aList.add(2);
-        aList.add(3);
-        aList.add(4);
-        aList.add(5);
-        aList.add(6);
+    /**
+     * 组合生成器
+     *
+     * @param data      原始数据
+     * @param workSpace 自定义一个临时空间,用来存储每次符合条件的值
+     * @param k         C(n,k)中的k
+     */
+    public <E> void combinerSelect(List<E> data, List<E> workSpace, List<List<E>> result, int n, int k) {
+        List<E> copyData;
+        List<E> copyWorkSpace = null;
+
+        if (workSpace.size() == k) {
+            for (E c : workSpace)
+                System.out.print(c);
+
+            result.add(new ArrayList<>(workSpace));
+            System.out.println();
+        }
 
-        Iterator<Integer> it = aList.iterator();
+        for (int i = 0; i < data.size(); i++) {
+            copyData = new ArrayList<E>(data);
+            copyWorkSpace = new ArrayList<E>(workSpace);
 
-        while (it.hasNext()){
-            int ll = it.next();
-            if (ll % 2 == 0){
-                it.remove();
-            }
+            copyWorkSpace.add(copyData.get(i));
+            for (int j = i; j >= 0; j--)
+                copyData.remove(j);
+            combinerSelect(copyData, copyWorkSpace, result, n, k);
         }
-        System.out.println(aList);
-
-        String xx = "I have an apple, I have a pen.";
-        System.out.println(xx.indexOf("have", 19));
-        System.out.println(xx.substring(19, 22));
     }
 
 }

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

@@ -10,6 +10,6 @@ import org.algorithm.core.RelationTreeUtils;
 public class TestRelationTreeUtils {
 
     public static void main(String[] args) {
-//        RelationTreeUtils.test();
+        RelationTreeUtils.test();
     }
 }