{"record":{"id":"9090a4522d19d919","repo":"stanfordnlp/CoreNLP","slug":"could-not-find-phrase-id-for-phrase-sentence","errorCode":null,"errorMessage":"Could not find phrase id for phrase \" + sentence","messagePattern":"Could not find phrase id for phrase \" \\+ sentence","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sentiment/ReadSentimentDataset.java","lineNumber":271,"sourceCode":"        // overall construction time is still efficient.\n        connect(parentPointers, subtrees, connected, index);\n      }\n    }\n\n    for (int i = 0; i <= maxNode; ++i) {\n      List<Tree> leaves = subtrees[i].getLeaves();\n      List<String> words = CollectionUtils.transformAsList(leaves, TRANSFORM_TREE_TO_WORD);\n      // First we look for a copy of the phrase with -LRB- -RRB-\n      // instead of ().  The sentiment trees sometimes have both, and\n      // the escaped versions seem to have more reasonable scores.\n      // If a particular phrase doesn't have -LRB- -RRB- we fall back\n      // to the unescaped versions.\n      Integer phraseId = phraseIds.get(CollectionUtils.transformAsList(words, TRANSFORM_PARENS));\n      if (phraseId == null) {\n        phraseId = phraseIds.get(words);\n      }\n      if (phraseId == null) {\n        throw new RuntimeException(\"Could not find phrase id for phrase \" + sentence);\n      }\n      // TODO: should we make this an option?  Perhaps we want cases\n      // where the trees have the phrase id and not their class\n      Double score = sentimentScores.get(phraseId);\n      if (score == null) {\n        throw new RuntimeException(\"Could not find sentiment score for phrase id \" + phraseId);\n      }\n\n      int classLabel = Math.round((float) Math.floor(score * (float) 5));\n      if (classLabel > 4 || classLabel < 0) {\n        throw new RuntimeException(\"Unexpected class label: score \" + score + \" became \" + classLabel);\n      }\n      subtrees[i].label().setValue(Integer.toString(classLabel));\n    }\n\n    for (int i = 0; i < sentence.size(); ++i) {\n      Tree leaf = subtrees[i].children()[0];\n      for (Pair<String, String> replacement : singleWordReplacements) {","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sentiment/ReadSentimentDataset.java#L253-L289","documentation":"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.","triggerScenarios":"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.","commonSituations":"Mixed-version Stanford Sentiment Treebank files, edits to sentences without regenerating dictionary.txt, tokenization differences after manual preprocessing.","solutions":["Use matching train/dev/test.txt and dictionary.txt and sentiment_labels.txt from the same SST release","Rebuild dictionary.txt from the sentences file so every phrase is keyed","Normalize tokens (e.g. parentheses -LRB-/-RRB-) consistently in both files"],"exampleFix":"// before: mismatched files\ndictionary.txt from stanfordSentimentTreebank_v1 + sentences from v2\n// after: matched release\nunzip stanfordSentimentTreebank.zip and use all files from the same directory","handlingStrategy":"validation","validationCode":"List<String> key = CollectionUtils.transformAsList(words, TRANSFORM_PARENS);\nif (!phraseIds.containsKey(key) && !phraseIds.containsKey(words)) throw new IllegalStateException(\"Phrase not in dictionary: \" + sentence);","typeGuard":null,"tryCatchPattern":"try { convertTree(...); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Could not find phrase id\")) { logUnmatchedSentence(sentence); } else throw e; }","preventionTips":["Always use dictionary.txt, sentiment_labels.txt, and *.txt from the same SST release","Normalize parentheses to -LRB-/-RRB- consistently","Unit-test conversion on a small sample before full conversion"],"tags":["dataset","lookup-miss"],"backgroundTag":"resource-not-found","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}