{"record":{"id":"8c74d91b2e4127cd","repo":"stanfordnlp/CoreNLP","slug":"word-wordcount-token-get-coreannota","errorCode":null,"errorMessage":"Word \" + wordCount + \" (\\\"\" + token.get(CoreAnnotations.TextAnnotation.class) + \"\\\") has a blank answer","messagePattern":"Word \" \\+ wordCount \\+ \" \\(\\\\\"\" \\+ token\\.get\\(CoreAnnotations\\.TextAnnotation\\.class\\) \\+ \"\\\\\"\\) has a blank answer","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFClassifier.java","lineNumber":732,"sourceCode":"\n    int wordCount = 0;\n\n    if (flags.labelDictionaryCutoff > 0) {\n      this.labelDictionary = new LabelDictionary();\n    }\n\n    for (List<IN> doc : ob) {\n      if (flags.useReverse) {\n        Collections.reverse(doc);\n      }\n\n      // create the full set of labels in classIndex\n      // note: update to use addAll later\n      for (IN token : doc) {\n        wordCount++;\n        String ans = token.get(CoreAnnotations.AnswerAnnotation.class);\n        if (ans == null || ans.isEmpty()) {\n          throw new IllegalArgumentException(\"Word \" + wordCount + \" (\\\"\" + token.get(CoreAnnotations.TextAnnotation.class) + \"\\\") has a blank answer\");\n        }\n        classIndex.add(ans);\n        if (labelDictionary != null) {\n          String observation = token.get(CoreAnnotations.TextAnnotation.class);\n          labelDictionary.increment(observation, ans);\n        }\n      }\n\n      for (int j = 0, docSize = doc.size(); j < docSize; j++) {\n        CRFDatum<Collection<String>, CRFLabel> d = makeDatum(doc, j, featureFactories);\n        labelIndex.add(d.label());\n\n        List<Collection<String>> features = d.asFeatures();\n        for (int k = 0, fSize = features.size(); k < fSize; k++) {\n          Collection<String> cliqueFeatures = features.get(k);\n          if (k < 2 && flags.removeBackgroundSingletonFeatures) {\n            String ans = doc.get(j).get(CoreAnnotations.AnswerAnnotation.class);\n            boolean background = ans.equals(flags.backgroundSymbol);","sourceCodeStart":714,"sourceCodeEnd":750,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFClassifier.java#L714-L750","documentation":"During CRF training, every training token must carry a non-null, non-empty AnswerAnnotation label, because the classIndex of possible labels is built from these answers. A token without a label makes the training data ill-formed, so an IllegalArgumentException naming the word index and text is thrown.","triggerScenarios":"Calling train()/trainSeqModel (via the CRFClassifier constructor path shown) on a List<IN> document where any CoreLabel lacks CoreAnnotations.AnswerAnnotation (answer field), or has answer set to \"\".","commonSituations":"Building training documents programmatically and forgetting to setAnswer(); reading CoNLL/TSV files where some rows have no gold label column; filtering that drops labels; sentence boundaries misparsed so unlabeled tokens slip in.","solutions":["Set an answer label on every training token: token.set(CoreAnnotations.AnswerAnnotation.class, label) before training.","Fix the input reader so the gold-label column maps to the 'answer' field (check the column mapping in your training properties, e.g. map=0=word,1=answer).","Pre-validate the training corpus: scan all docs and fail fast on any token with a blank answer, then repair the source data."],"exampleFix":"// before\nfor (CoreLabel tok : doc) { docOut.add(tok); } // answer never set\n// after\nfor (CoreLabel tok : doc) {\n  if (tok.get(CoreAnnotations.AnswerAnnotation.class) == null)\n    tok.set(CoreAnnotations.AnswerAnnotation.class, goldLabel);\n  docOut.add(tok);\n}","handlingStrategy":"validation","validationCode":"for (List<IN> doc : trainingDocs) {\n  int i = 0;\n  for (IN tok : doc) {\n    String ans = tok.get(CoreAnnotations.AnswerAnnotation.class);\n    if (ans == null || ans.isEmpty())\n      throw new IllegalStateException(\"Token \" + i + \" (\" + tok.get(CoreAnnotations.TextAnnotation.class) + \") has blank answer\");\n    i++;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  classifier.train(trainingDocs);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"has a blank answer\")) {\n    log.severe(\"Training data has unlabeled tokens: \" + e.getMessage());\n  }\n  throw e;\n}","preventionTips":["Always call setAnswer()/set(CoreAnnotations.AnswerAnnotation.class, ...) when building training docs programmatically.","Verify the reader's column mapping puts the gold label in the answer field.","Lint the training corpus for null/empty answers before training."],"tags":["java","crf","training-data","missing-label"],"backgroundTag":"empty-required-field","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"}