All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.ling.CoreUtilities Maven / Gradle / Ivy

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.ling;

import java.util.ArrayList;
import java.util.List;

import edu.stanford.nlp.util.CoreMap;

public class CoreUtilities {

  private CoreUtilities() { } // class of static methods


  /**
   * Pieces a List of CoreMaps back together using
   * word and setting a white space between each word
   * TODO: remove this (SentenceUtils.listToString does the same thing - why 2 separate classes)
   */
  public static String toSentence(List sentence) {
    StringBuilder text = new StringBuilder();
    for (int i = 0, sz = sentence.size(); i < sz; i++) {
      CoreMap iw = sentence.get(i);
      text.append(iw.get(CoreAnnotations.TextAnnotation.class));
      if (i < sz - 1) {
        text.append(' ');
      }
    }
    return text.toString();
  }

  public static List deepCopy(List tokens) {
    List copy = new ArrayList<>();
    for (CoreLabel ml : tokens) {
      CoreLabel ml1 = new CoreLabel(ml);  // copy the labels
      copy.add(ml1);
    }
    return copy;
  }

  public static List toCoreLabelList(String... words) {
    List tokens = new ArrayList<>(words.length);
    for (String word : words) {
      CoreLabel cl = new CoreLabel();
      cl.setWord(word);
      tokens.add(cl);
    }
    return tokens;
  }

  public static List toCoreLabelList(List words) {
    List tokens = new ArrayList<>(words.size());
    for (String word : words) {
      CoreLabel cl = new CoreLabel();
      cl.setWord(word);
      tokens.add(cl);
    }
    return tokens;
  }

  public static List toCoreLabelList(String[] words, String[] tags) {
    assert tags.length == words.length;
    List tokens = new ArrayList<>(words.length);
    for (int i = 0, sz = words.length; i < sz; i++) {
      CoreLabel cl = new CoreLabel();
      cl.setWord(words[i]);
      cl.setTag(tags[i]);
      tokens.add(cl);
    }
    return tokens;
  }

  public static List toCoreLabelListWithCharacterOffsets(String[] words, String[] tags) {
    assert tags.length == words.length;
    List tokens = new ArrayList<>(words.length);
    int offset = 0;
    for (int i = 0, sz = words.length; i < sz; i++) {
      CoreLabel cl = new CoreLabel();
      cl.setWord(words[i]);
      cl.setTag(tags[i]);
      cl.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, offset);
      offset += words[i].length();
      cl.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, offset);
      offset++; // assume one space between words :-)
      tokens.add(cl);
    }
    return tokens;
  }

  public static List toCoreLabelList(String[] words,
                                                String[] tags,
                                                String[] answers) {
    assert tags.length == words.length;
    assert answers.length == words.length;
    List tokens = new ArrayList<>(words.length);
    for (int i = 0, sz = words.length; i < sz; i++) {
      CoreLabel cl = new CoreLabel();
      cl.setWord(words[i]);
      cl.setTag(tags[i]);
      cl.set(CoreAnnotations.AnswerAnnotation.class, answers[i]);
      tokens.add(cl);
    }
    return tokens;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy