Example #1
0
  public static DependencyParse parse(String text) {

    if (pipeline == null) {
      loadModels();
    }

    DependencyParse parse = new DependencyParse();

    Annotation document = new Annotation(text);

    pipeline.annotate(document);

    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    for (CoreMap sentence : sentences) {

      SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);

      IndexedWord root = dependencies.getFirstRoot();

      parse.setHeadNode(root.index());

      List<SemanticGraphEdge> edges = dependencies.edgeListSorted();

      // System.out.println(edges);
      for (SemanticGraphEdge t : edges) {

        String dep = t.getDependent().originalText();
        int depIndex = t.getDependent().index();
        String depPOS = t.getDependent().tag();
        int depStart = t.getDependent().beginPosition();
        int depEnd = t.getDependent().endPosition();

        String gov = t.getGovernor().originalText();
        int govIndex = t.getGovernor().index();
        String govPOS = t.getGovernor().tag();
        int govStart = t.getGovernor().beginPosition();
        int govEnd = t.getGovernor().endPosition();

        parse.addNode(govIndex, gov, govPOS, govStart, govEnd);
        parse.addNode(depIndex, dep, depPOS, depStart, depEnd);

        parse.addEdge(depIndex, govIndex, t.getRelation().getShortName());
      }
    }

    return parse;
  }
Example #2
0
  private DependencyParse parse2(String sentence) {
    DependencyParse parse = new DependencyParse();

    List<TypedDependency> tdl = getDependencies(sentence);
    // System.out.println(tdl);

    for (TypedDependency t : tdl) {

      if (!t.reln().getShortName().equals("root")) {

        String dep = t.dep().originalText();
        int depIndex = t.dep().index() - 1;
        String depPOS = t.dep().tag();
        int depStart = t.dep().beginPosition();
        int depEnd = t.dep().endPosition();

        String gov = t.gov().originalText();
        int govIndex = t.gov().index() - 1;
        String govPOS = t.gov().tag();
        int govStart = t.gov().beginPosition();
        int govEnd = t.gov().endPosition();

        //                if (t.reln().getShortName().equals("amod") ||
        // t.reln().getShortName().equals("nn")) {
        //
        //                    parse.addNode(govIndex, dep + " " + gov, govPOS, depStart, govEnd);
        //                    parse.addEdge(depIndex, govIndex, t.reln().getShortName());
        //
        //                } else {
        parse.addNode(govIndex, gov, govPOS, govStart, govEnd);
        parse.addNode(depIndex, dep, depPOS, depStart, depEnd);

        parse.addEdge(depIndex, govIndex, t.reln().getShortName());
        // }

      } else {
        parse.setHeadNode(t.dep().index() - 1);
      }
    }
    return parse;
  }