stanfordnlp/CoreNLP · error · RuntimeException

Could not find phrase id for phrase " + sentence

Error message

Could not find phrase id for phrase " + sentence

What it means

ReadSentimentDataset maps each sentence's word list to a phrase id via the dictionary.txt-derived phraseIds map. If neither the paren-normalized word list nor the raw word list is a key, the phrase cannot be linked to a sentiment score and a RuntimeException is thrown.

Solutions

  1. Use matching train/dev/test.txt and dictionary.txt and sentiment_labels.txt from the same SST release
  2. Rebuild dictionary.txt from the sentences file so every phrase is keyed
  3. Normalize tokens (e.g. parentheses -LRB-/-RRB-) consistently in both files

Example fix

// before: mismatched files
dictionary.txt from stanfordSentimentTreebank_v1 + sentences from v2
// after: matched release
unzip stanfordSentimentTreebank.zip and use all files from the same directory
Defensive patterns

Strategy: validation

Validate before calling

List<String> key = CollectionUtils.transformAsList(words, TRANSFORM_PARENS);
if (!phraseIds.containsKey(key) && !phraseIds.containsKey(words)) throw new IllegalStateException("Phrase not in dictionary: " + sentence);

Try / catch

try { convertTree(...); } catch (RuntimeException e) { if (e.getMessage().startsWith("Could not find phrase id")) { logUnmatchedSentence(sentence); } else throw e; }

Prevention

When it happens

Trigger: Dictionary and sentence files out of sync — e.g. sentences containing tokens (like escaped parens) not present in dictionary.txt, or using an STB dictionary version that does not match the sentences file.

Common situations: Mixed-version Stanford Sentiment Treebank files, edits to sentences without regenerating dictionary.txt, tokenization differences after manual preprocessing.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/sentiment/ReadSentimentDataset.java:271

        // overall construction time is still efficient.
        connect(parentPointers, subtrees, connected, index);
      }
    }

    for (int i = 0; i <= maxNode; ++i) {
      List<Tree> leaves = subtrees[i].getLeaves();
      List<String> words = CollectionUtils.transformAsList(leaves, TRANSFORM_TREE_TO_WORD);
      // First we look for a copy of the phrase with -LRB- -RRB-
      // instead of ().  The sentiment trees sometimes have both, and
      // the escaped versions seem to have more reasonable scores.
      // If a particular phrase doesn't have -LRB- -RRB- we fall back
      // to the unescaped versions.
      Integer phraseId = phraseIds.get(CollectionUtils.transformAsList(words, TRANSFORM_PARENS));
      if (phraseId == null) {
        phraseId = phraseIds.get(words);
      }
      if (phraseId == null) {
        throw new RuntimeException("Could not find phrase id for phrase " + sentence);
      }
      // TODO: should we make this an option?  Perhaps we want cases
      // where the trees have the phrase id and not their class
      Double score = sentimentScores.get(phraseId);
      if (score == null) {
        throw new RuntimeException("Could not find sentiment score for phrase id " + phraseId);
      }

      int classLabel = Math.round((float) Math.floor(score * (float) 5));
      if (classLabel > 4 || classLabel < 0) {
        throw new RuntimeException("Unexpected class label: score " + score + " became " + classLabel);
      }
      subtrees[i].label().setValue(Integer.toString(classLabel));
    }

    for (int i = 0; i < sentence.size(); ++i) {
      Tree leaf = subtrees[i].children()[0];
      for (Pair<String, String> replacement : singleWordReplacements) {

View on GitHub (pinned to 1b7edd19c4)