{"record":{"id":"aaf545f665af384e","repo":"stanfordnlp/CoreNLP","slug":"unknown-sentiment-class","errorCode":null,"errorMessage":"Unknown sentiment class: ","messagePattern":"Unknown sentiment class: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/simple/Sentence.java","lineNumber":1038,"sourceCode":"   * @param props The properties to pass to the sentiment classifier.\n   *\n   * @return The {@link SentimentClass} of this sentence, as an enum value.\n   */\n  public SentimentClass sentiment(Properties props) {\n    document.runSentiment(props);\n    switch (impl.getSentiment().toLowerCase()) {\n      case \"very positive\":\n        return SentimentClass.VERY_POSITIVE;\n      case \"positive\":\n        return SentimentClass.POSITIVE;\n      case \"negative\":\n        return SentimentClass.NEGATIVE;\n      case \"very negative\":\n        return SentimentClass.VERY_NEGATIVE;\n      case \"neutral\":\n        return SentimentClass.NEUTRAL;\n      default:\n        throw new IllegalStateException(\"Unknown sentiment class: \" + impl.getSentiment());\n    }\n  }\n\n  /**\n   * Get the coreference chain for just this sentence.\n   * Note that this method is actually fairly computationally expensive to call, as it constructs and prunes\n   * the coreference data structure for the entire document.\n   *\n   * @return A coreference chain, but only for this sentence\n   */\n  public Map<Integer, CorefChain> coref() {\n    // Get the raw coref structure\n    Map<Integer, CorefChain> allCorefs = document.coref();\n    // Delete coreference chains not in this sentence\n    Set<Integer> toDeleteEntirely = new HashSet<>();\n    for (Map.Entry<Integer, CorefChain> integerCorefChainEntry : allCorefs.entrySet()) {\n      CorefChain chain = integerCorefChainEntry.getValue();\n      List<CorefChain.CorefMention> mentions = new ArrayList<>(chain.getMentionsInTextualOrder());","sourceCodeStart":1020,"sourceCodeEnd":1056,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/simple/Sentence.java#L1020-L1056","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the pipeline includes 'parse' and 'sentiment' annotators and the default sentiment model","Run the annotator (e.g. document.sentiment() or pipeline.annotate) before reading Sentence.sentiment()","If using a custom sentiment model, make sure its labels match the five canonical sentiment strings","Catch IllegalStateException and treat as missing/unsupported sentiment"],"exampleFix":"// before\nString label = sentence.sentiment();\n// after\nString label;\ntry {\n  label = sentence.sentiment();\n} catch (IllegalStateException e) {\n  label = \"neutral\"; // sentiment model missing or non-standard labels\n}","handlingStrategy":"try-catch","validationCode":"// ensure sentiment annotator present and run\nprops.setProperty(\"annotators\", \"tokenize,ssplit,pos,lemma,parse,sentiment\");\nnew StanfordCoreNLP(props).annotate(doc);\nif (doc.sentences().isEmpty()) { /* no sentiment yet */ }","typeGuard":null,"tryCatchPattern":"try {\n  String label = sentence.sentiment();\n} catch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Unknown sentiment class\")) {\n    label = \"neutral\";\n  } else { throw e; }\n}","preventionTips":["Always run the sentiment annotator before calling sentiment()","Include the required parse annotator for sentiment","Avoid custom sentiment models with non-canonical labels"],"tags":["corenlp","sentiment","switch-default"],"backgroundTag":"invalid-enum-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}