コード例 #1
0
 /**
  * Create a mock node, to be added to the dependency tree but which is not part of the original
  * sentence.
  *
  * @param toCopy The CoreLabel to copy from initially.
  * @param word The new word to add.
  * @param POS The new part of speech to add.
  * @return A CoreLabel copying most fields from toCopy, but with a new word and POS tag (as well
  *     as a new index).
  */
 @SuppressWarnings("UnusedDeclaration")
 private CoreLabel mockNode(CoreLabel toCopy, String word, String POS) {
   CoreLabel mock = new CoreLabel(toCopy);
   mock.setWord(word);
   mock.setLemma(word);
   mock.setValue(word);
   mock.setNER("O");
   mock.setTag(POS);
   mock.setIndex(sentenceLength + 5);
   return mock;
 }
コード例 #2
0
  /**
   * Parse a CoNLL formatted tree into a SemanticGraph object (along with a list of tokens).
   *
   * @param conll The CoNLL formatted tree.
   * @return A pair of a SemanticGraph and a token list, corresponding to the parse of the sentence
   *     and to tokens in the sentence.
   */
  protected Pair<SemanticGraph, List<CoreLabel>> mkTree(String conll) {
    List<CoreLabel> sentence = new ArrayList<>();
    SemanticGraph tree = new SemanticGraph();
    for (String line : conll.split("\n")) {
      if (line.trim().equals("")) {
        continue;
      }
      String[] fields = line.trim().split("\\s+");
      int index = Integer.parseInt(fields[0]);
      String word = fields[1];
      CoreLabel label = IETestUtils.mkWord(word, index);
      sentence.add(label);
      if (fields[2].equals("0")) {
        tree.addRoot(new IndexedWord(label));
      } else {
        tree.addVertex(new IndexedWord(label));
      }
      if (fields.length > 4) {
        label.setTag(fields[4]);
      }
      if (fields.length > 5) {
        label.setNER(fields[5]);
      }
      if (fields.length > 6) {
        label.setLemma(fields[6]);
      }
    }
    int i = 0;
    for (String line : conll.split("\n")) {
      if (line.trim().equals("")) {
        continue;
      }
      String[] fields = line.trim().split("\\s+");
      int parent = Integer.parseInt(fields[2]);
      String reln = fields[3];
      if (parent > 0) {
        tree.addEdge(
            new IndexedWord(sentence.get(parent - 1)),
            new IndexedWord(sentence.get(i)),
            new GrammaticalRelation(Language.UniversalEnglish, reln, null, null),
            1.0,
            false);
      }
      i += 1;
    }

    return Pair.makePair(tree, sentence);
  }