Om Prakash
Om Prakash

Reputation: 2881

StanfordNLP: incompatible types: Object cannot be converted to CoreMap

I am trying Named Entity Recognition using StanfordNLP using this tutorial. I am getting error

incompatible types: Object cannot be converted to CoreMap I tried type casting it to Object but could not get it working.

Partial code throwing error

  // these are all the sentences in this document
  // a CoreMap is essentially a Map that uses class objects as keys and has values with
  // custom types
  List sentences = document.get(SentencesAnnotation.class);
  StringBuilder sb = new StringBuilder();
for (CoreMap sentence : sentences) {
    // traversing the words in the current sentence, "O" is a sensible default to initialise
    // tokens to since we're not interested in unclassified / unknown things..
    String prevNeToken = "O";
    String currNeToken = "O";
    boolean newToken = true;
    for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
      currNeToken = token.get(NamedEntityTagAnnotation.class);
      String word = token.get(TextAnnotation.class);
      // Strip out "O"s completely, makes code below easier to understand
      if (currNeToken.equals("O")) {
        // LOG.debug("Skipping '{}' classified as {}", word, currNeToken);
        if (!prevNeToken.equals("O") && (sb.length() > 0)) {
          handleEntity(prevNeToken, sb, tokens);
          newToken = true;
        }
        continue;
      }

      if (newToken) {
        prevNeToken = currNeToken;
        newToken = false;
        sb.append(word);
        continue;
      }

      if (currNeToken.equals(prevNeToken)) {
        sb.append(" " + word);
      } else {
        // We're done with the current entity - print it out and reset
        // TODO save this token into an appropriate ADT to return for useful processing..
        handleEntity(prevNeToken, sb, tokens);
        newToken = true;
      }
      prevNeToken = currNeToken;
    }
  }

I am a noob with NLP. Thanks in advance.

Upvotes: 0

Views: 149

Answers (1)

Om Prakash
Om Prakash

Reputation: 2881

I managed to solve this issue. Change third line to this

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

Upvotes: 1

Related Questions