public void testCornerCases() {
   DirectedWeightedMultigraph<Integer, DefaultWeightedEdge> simple =
       new DirectedWeightedMultigraph<>(DefaultWeightedEdge.class);
   simple.addVertex(0);
   simple.addVertex(1);
   DefaultWeightedEdge e = simple.addEdge(0, 1);
   try {
     new EdmondsKarpMFImpl<Integer, DefaultWeightedEdge>(null);
     fail();
   } catch (NullPointerException ex) {
   }
   try {
     new EdmondsKarpMFImpl<>(simple, -0.1);
     fail();
   } catch (IllegalArgumentException ex) {
   }
   try {
     simple.setEdgeWeight(e, -1.0);
     new EdmondsKarpMFImpl<>(simple);
     fail();
   } catch (IllegalArgumentException ex) {
   }
   try {
     simple.setEdgeWeight(e, 1.0);
     MaximumFlowAlgorithm<Integer, DefaultWeightedEdge> solver = new EdmondsKarpMFImpl<>(simple);
     Map<DefaultWeightedEdge, Double> flow = solver.buildMaximumFlow(0, 1).getFlow();
     flow.put(e, 25.0);
     fail();
   } catch (UnsupportedOperationException ex) {
   }
   try {
     MaximumFlowAlgorithm<Integer, DefaultWeightedEdge> solver = new EdmondsKarpMFImpl<>(simple);
     solver.buildMaximumFlow(2, 0);
     fail();
   } catch (IllegalArgumentException ex) {
   }
   try {
     MaximumFlowAlgorithm<Integer, DefaultWeightedEdge> solver = new EdmondsKarpMFImpl<>(simple);
     solver.buildMaximumFlow(1, 2);
     fail();
   } catch (IllegalArgumentException ex) {
   }
   try {
     MaximumFlowAlgorithm<Integer, DefaultWeightedEdge> solver = new EdmondsKarpMFImpl<>(simple);
     solver.buildMaximumFlow(0, 0);
     fail();
   } catch (IllegalArgumentException ex) {
   }
   try {
     MaximumFlowAlgorithm<Integer, DefaultWeightedEdge> solver = new EdmondsKarpMFImpl<>(simple);
     solver.buildMaximumFlow(null, 0);
     fail();
   } catch (IllegalArgumentException ex) {
   }
   try {
     MaximumFlowAlgorithm<Integer, DefaultWeightedEdge> solver = new EdmondsKarpMFImpl<>(simple);
     solver.buildMaximumFlow(0, null);
     fail();
   } catch (IllegalArgumentException ex) {
   }
 }
  public static void step9() throws IOException {
    ADW pipeLine = new ADW();

    double similarityThreshold = 0.8;

    File logFile = new File("step9-output-combineLog.txt");
    PrintStream outputPrintStream = new PrintStream(logFile);
    System.setOut(outputPrintStream);

    HashMap<String, Integer> allPhrases = new HashMap<String, Integer>(); // all
    // salient
    // S/P/O
    HashMap<String, Integer> allPhrases1 = new HashMap<String, Integer>();
    HashMap<String, String> allPhrases2forContributor = new HashMap<String, String>();
    HashMap<String, String> allPhrases2forContributorTriple = new HashMap<String, String>();
    // HashMap<String,String> hashMapReverse = new HashMap<String,String>();

    FileReader reader = new FileReader("step8-out-PyramidForCombination.txt");
    BufferedReader br = new BufferedReader(reader);
    String str = null;
    String nextStr = null;

    str = br.readLine();
    while (str != null) {
      if (str.startsWith("FROM")) {
        final DirectedWeightedMultigraph<String, RelationshipEdge> g =
            new DirectedWeightedMultigraph<String, RelationshipEdge>(RelationshipEdge.class);

        while ((str = br.readLine()) != null && !str.startsWith("FROM")) {
          String text = str;
          String[] splited = text.split("&");
          String twoPhrases = splited[3];

          String text1 = twoPhrases;
          String[] splited1 = text.split("\"");
          String triple1 = "*"; // for the output
          for (String iterator1 : splited1) {
            triple1 = triple1.concat(" " + iterator1);
          }
          triple1 = triple1.replace("* ", "");
          g.addVertex(twoPhrases);
        }

        for (String vertexIterator1 : g.vertexSet()) {
          // g.removeVertex(vertexIterator1);

          for (String vertexIterator2 : g.vertexSet()) {
            if (!vertexIterator2.equals(vertexIterator1)) {
              if (vertexIterator1.contains(vertexIterator2)
                  || vertexIterator2.contains(vertexIterator1)) {
                System.setOut(outputPrintStream);
                System.out.println(
                    "&"
                        + vertexIterator1
                        + "&"
                        + "   "
                        + "&"
                        + vertexIterator2
                        + "&"
                        + "   "
                        + "&"
                        + 1
                        + "&");
              } else {
                LexicalItemType text1Type = LexicalItemType.SURFACE;
                LexicalItemType text2Type = LexicalItemType.SURFACE;

                // measure for comparing semantic signatures
                SignatureComparison measure = new WeightedOverlap();

                // calculate the similarity of text1 and text2
                double similarity =
                    pipeLine.getPairSimilarity(
                        vertexIterator1,
                        vertexIterator2,
                        DisambiguationMethod.ALIGNMENT_BASED,
                        measure,
                        text1Type,
                        text2Type);
                if (similarity >= similarityThreshold) {
                  System.setOut(outputPrintStream);
                  System.out.println(
                      "&"
                          + vertexIterator1
                          + "&"
                          + "   "
                          + "&"
                          + vertexIterator2
                          + "&"
                          + "   "
                          + "&"
                          + similarity
                          + "&");
                }
              }
            } // inner for
          } // if
        } // outer for
      }
    }

    br.close();
  }
Esempio n. 3
0
  private static DirectedWeightedMultigraph<Node, LabeledLink> buildGraphsFromStatements2(
      List<Statement> statements) {

    if (statements == null || statements.size() == 0) return null;

    DirectedWeightedMultigraph<Node, LabeledLink> graph =
        new DirectedWeightedMultigraph<Node, LabeledLink>(LabeledLink.class);

    // Assumption: there is only one rdf:type for each URI
    HashMap<String, Node> uri2Classes = new HashMap<String, Node>();
    for (Statement st : statements) {

      String subjStr = st.getSubject();
      String predicateStr = st.getPredicate();
      String objStr = st.getObject();

      subjStr = getUri(subjStr);
      predicateStr = getUri(predicateStr);
      objStr = getUri(objStr);

      if (predicateStr.equalsIgnoreCase(typePredicate)) {

        Node classNode = new InternalNode(objStr, new Label(objStr));
        uri2Classes.put(subjStr, classNode);
        graph.addVertex(classNode);
      }
    }

    //		int countOfLiterals = 0;
    String id;
    for (Statement st : statements) {

      String subjStr = st.getSubject();
      String predicateStr = st.getPredicate();
      String objStr = st.getObject();

      subjStr = getUri(subjStr);
      predicateStr = getUri(predicateStr);
      objStr = getUri(objStr);

      if (predicateStr.equalsIgnoreCase(typePredicate)) continue;

      Node subj = uri2Classes.get(subjStr);
      if (subj == null) {
        subj = new InternalNode(subjStr, new Label(subjStr));
        graph.addVertex(subj);
      }

      Node obj = uri2Classes.get(objStr);
      if (obj == null) {
        if (objStr.startsWith(attPrefix)) {
          id = new RandomGUID().toString();
          obj = new ColumnNode(id, objStr, objStr, null, null);
          SemanticType semanticType =
              new SemanticType(
                  ((ColumnNode) obj).getHNodeId(),
                  new Label(predicateStr),
                  subj.getLabel(),
                  Origin.User,
                  1.0,
                  false);
          ((ColumnNode) obj).setUserSelectedSemanticType(semanticType);

        } else if (objStr.indexOf(":") == -1 && objStr.indexOf("\"") != -1) {
          //					String literalId = "lit:" + serviceId + "_l" + String.valueOf(countOfLiterals);
          obj = new LiteralNode(objStr, objStr, null);
          //					countOfLiterals ++;
        } else obj = new InternalNode(objStr, new Label(objStr));

        graph.addVertex(obj);
      }

      LabeledLink e;
      if (obj instanceof InternalNode)
        e =
            new ObjectPropertyLink(
                LinkIdFactory.getLinkId(predicateStr, subj.getId(), obj.getId()),
                new Label(predicateStr),
                ObjectPropertyType.None);
      else
        e =
            new DataPropertyLink(
                LinkIdFactory.getLinkId(predicateStr, subj.getId(), obj.getId()),
                new Label(predicateStr));
      graph.addEdge(subj, obj, e);
    }

    return graph;
  }