{"record":{"id":"d1eb0df15ae0900f","repo":"stanfordnlp/CoreNLP","slug":"not-sure-if-rvfdataset-runs-correctly-in-this-meth","errorCode":null,"errorMessage":"Not sure if RVFDataset runs correctly in this method. Please update this code if it does.","messagePattern":"Not sure if RVFDataset runs correctly in this method\\. Please update this code if it does\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/classify/NaiveBayesClassifierFactory.java","lineNumber":348,"sourceCode":"//\n//    }\n//    System.out.println(\"Unconstrained conditional likelihood no prior :\");\n//    for (int j = 0; j < 100; j++) {\n//      NaiveBayesClassifier<String, Integer> classifier = new NaiveBayesClassifierFactory<String, Integer>(0.1, 0.01, 0.6, LogPrior.LogPriorType.NULL.ordinal(), NaiveBayesClassifierFactory.UCL).trainClassifier(train);\n//      classifier.print();\n//      //now classify\n//\n//      float accTrain = classifier.accuracy(train.iterator());\n//      log.info(\"training accuracy \" + accTrain);\n//      float accTest = classifier.accuracy(test.iterator());\n//      log.info(\"test accuracy \" + accTest);\n//    }\n//  }\n\n  @Override\n  public NaiveBayesClassifier<L, F> trainClassifier(GeneralDataset<L, F> dataset) {\n    if(dataset instanceof RVFDataset){\n      throw new RuntimeException(\"Not sure if RVFDataset runs correctly in this method. Please update this code if it does.\");\n    }\n    return trainClassifier(dataset.getDataArray(), dataset.labels, dataset.numFeatures(),\n        dataset.numClasses(), dataset.labelIndex, dataset.featureIndex);\n  }\n\n}\n","sourceCodeStart":330,"sourceCodeEnd":355,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/classify/NaiveBayesClassifierFactory.java#L330-L355","documentation":"NaiveBayesClassifierFactory.trainClassifier(GeneralDataset) only supports count-based (CDF-style) datasets, not ones holding real-valued feature counts. When handed an RVFDataset it refuses to proceed because the training path assumes binary/presence features and has not been verified for real values. The library throws this as a guard so silent mis-training cannot happen.","triggerScenarios":"Calling NaiveBayesClassifierFactory.trainClassifier(dataset) where dataset is an RVFDataset (created with RVFDataset API, from real-valued features, or via scaleDatum/scaleFeatures). Any pipeline that mixes RVF datums with this factory hits it immediately.","commonSituations":"Developers converting code from LogisticRegressionClassifier (which accepts RVF datasets) to NaiveBayes, or loading SVMLight/real-valued data and reusing the same GeneralDataset reference with a NaiveBayes trainer. Also common after reading datasets from files that produce RVFDataset by default.","solutions":["Convert the RVFDataset to a count-based dataset before training, e.g. new Dataset(labelIndex, featureIndex, data) or dataset as classic Dataset via RVFDataset -> binarize thresholding each count to presence","Use a classifier that supports real values (e.g. LogisticRegressionClassifier or SVMLight via trainClassifier with weights) instead of this NaiveBayes factory method","If NaiveBayes on counts is acceptable, build your data as Dataset (not RVFDataset) from the start with add(new ClassicDatum...)/add(presence features)","If you verified it works with RVF data, edit the source to remove the check and re-test accuracy"],"exampleFix":"// before\nRVFDataset<L,F> ds = loadRealValuedData();\nNaiveBayesClassifier<L,F> clf = new NaiveBayesClassifierFactory<L,F>().trainClassifier(ds); // throws\n// after\nDataset<L,F> binarized = new Dataset<>(ds.size(), ds.labelIndex(), ds.featureIndex());\nfor (RVFDatum<L,F> d : ds) binarized.add(new BasicDatum<>(d.asFeatures(), d.label()));\nNaiveBayesClassifier<L,F> clf = new NaiveBayesClassifierFactory<L,F>().trainClassifier(binarized);","handlingStrategy":"validation","validationCode":"if (dataset instanceof RVFDataset) {\n  throw new IllegalArgumentException(\"NaiveBayesClassifierFactory requires a count-based Dataset; convert the RVFDataset first\");\n}","typeGuard":"boolean isCountBased(GeneralDataset<?,?> ds) { return !(ds instanceof RVFDataset); }","tryCatchPattern":"try {\n  NaiveBayesClassifier<L,F> clf = factory.trainClassifier(dataset);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"RVFDataset\")) {\n    dataset = binarizeDataset((RVFDataset<L,F>) dataset);\n    clf = factory.trainClassifier(dataset);\n  } else throw e;\n}","preventionTips":["Keep count-based data in Dataset and real-valued data in RVFDataset as separate pipeline stages","Check dataset type before passing to any trainer","Prefer classifiers documented to accept RVF data when your features are real-valued"],"tags":["java","stanford-nlp","machine-learning","unsupported-input"],"backgroundTag":"unsupported-operation","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"}