stanfordnlp/CoreNLP · error · IllegalStateException

Unknown sentiment class:

Error message

Unknown sentiment class: 

What it means

Sentence.sentiment() maps the string produced by the sentiment annotator to a SentimentClass enum; a switch on the lowercase labels hits the default branch and throws IllegalStateException when the sentiment string is not one of the five known labels ('very negative','negative','neutral','positive','very positive'). This means the sentiment annotator output was unexpected, typically because the sentiment model wasn't run or produced a different label set.

Solutions

  1. Ensure the pipeline includes 'parse' and 'sentiment' annotators and the default sentiment model
  2. Run the annotator (e.g. document.sentiment() or pipeline.annotate) before reading Sentence.sentiment()
  3. If using a custom sentiment model, make sure its labels match the five canonical sentiment strings
  4. Catch IllegalStateException and treat as missing/unsupported sentiment

Example fix

// before
String label = sentence.sentiment();
// after
String label;
try {
  label = sentence.sentiment();
} catch (IllegalStateException e) {
  label = "neutral"; // sentiment model missing or non-standard labels
}
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure sentiment annotator present and run
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,parse,sentiment");
new StanfordCoreNLP(props).annotate(doc);
if (doc.sentences().isEmpty()) { /* no sentiment yet */ }

Try / catch

try {
  String label = sentence.sentiment();
} catch (IllegalStateException e) {
  if (e.getMessage().startsWith("Unknown sentiment class")) {
    label = "neutral";
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling Sentence.sentiment() when the sentiment annotator did not run (impl.getSentiment() returns an unexpected value), or when a custom/non-standard sentiment model emits labels outside the five canonical classes.

Common situations: Forgetting to add the 'sentiment' annotator (or its required 'parse' annotator) to the pipeline before calling sentiment(); swapping in a custom sentiment model with different label names.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/aaf545f665af384e. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/simple/Sentence.java:1038

   * @param props The properties to pass to the sentiment classifier.
   *
   * @return The {@link SentimentClass} of this sentence, as an enum value.
   */
  public SentimentClass sentiment(Properties props) {
    document.runSentiment(props);
    switch (impl.getSentiment().toLowerCase()) {
      case "very positive":
        return SentimentClass.VERY_POSITIVE;
      case "positive":
        return SentimentClass.POSITIVE;
      case "negative":
        return SentimentClass.NEGATIVE;
      case "very negative":
        return SentimentClass.VERY_NEGATIVE;
      case "neutral":
        return SentimentClass.NEUTRAL;
      default:
        throw new IllegalStateException("Unknown sentiment class: " + impl.getSentiment());
    }
  }

  /**
   * Get the coreference chain for just this sentence.
   * Note that this method is actually fairly computationally expensive to call, as it constructs and prunes
   * the coreference data structure for the entire document.
   *
   * @return A coreference chain, but only for this sentence
   */
  public Map<Integer, CorefChain> coref() {
    // Get the raw coref structure
    Map<Integer, CorefChain> allCorefs = document.coref();
    // Delete coreference chains not in this sentence
    Set<Integer> toDeleteEntirely = new HashSet<>();
    for (Map.Entry<Integer, CorefChain> integerCorefChainEntry : allCorefs.entrySet()) {
      CorefChain chain = integerCorefChainEntry.getValue();
      List<CorefChain.CorefMention> mentions = new ArrayList<>(chain.getMentionsInTextualOrder());

View on GitHub (pinned to 1b7edd19c4)