{"record":{"id":"8ff23746da0b8725","repo":"stanfordnlp/CoreNLP","slug":"incorrect-function-specification-feature-has-two","errorCode":null,"errorMessage":"Incorrect function specification: Feature has two values at one point: \" + oldVal + \" and \" + vals[i]","messagePattern":"Incorrect function specification: Feature has two values at one point: \" \\+ oldVal \\+ \" and \" \\+ vals\\[i\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/maxent/Feature.java","lineNumber":64,"sourceCode":"\n  protected Index<IntPair> instanceIndex;\n\n  public Feature() {\n  }\n\n\n  /**\n   * This is if we are given an array of double with a value for each training sample in the order of their occurrence.\n   */\n  public Feature(Experiments e, double[] vals, Index<IntPair> instanceIndex) {\n    this.instanceIndex = instanceIndex;\n    Map<Integer, Double> setNonZeros = Generics.newHashMap();\n    for (int i = 0; i < vals.length; i++) {\n      if (vals[i] != 0.0) {\n        Integer in = Integer.valueOf(indexOf(e.get(i)[0], e.get(i)[1]));// new Integer(e.get(i)[0]*e.ySize+e.get(i)[1]);\n        Double oldVal = setNonZeros.put(in, Double.valueOf(vals[i]));\n        if (oldVal != null && oldVal.doubleValue() != vals[i]) {\n          throw new IllegalStateException(\"Incorrect function specification: Feature has two values at one point: \" + oldVal + \" and \" + vals[i]);\n        }\n      }//if\n    }// for\n    \n    indexedValues = new int[setNonZeros.size()];\n    valuesI = new double[indexedValues.length];\n    \n    int i = 0;\n    for (Map.Entry<Integer, Double> entry: setNonZeros.entrySet()) {\n      indexedValues[i] = entry.getKey();\n      valuesI[i] = entry.getValue();\n      i++;\n    }\n    domain = e;\n  }\n\n\n  int indexOf(int x, int y) {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/maxent/Feature.java#L46-L82","documentation":"The Feature constructor builds an internal map from feature index to value, and the same feature index appearing twice with DIFFERENT values makes the feature function ill-defined. The library throws IllegalStateException because the maxent specification is inconsistent.","triggerScenarios":"Constructing a Feature where the same (index) position occurs multiple times in the input pairs e with differing values vals[i] (e.g. index (x,y) appearing at both i=0 and i=3 with vals 0.5 and 0.9).","commonSituations":"Buggy feature-extraction code that emits duplicate (x,y) pairs; concatenating feature lists without deduplication; off-by-one errors in index computation causing accidental collisions.","solutions":["Deduplicate the (index, value) pairs before constructing the Feature, keeping one value per index","Fix the featurizer so each (x,y) index is emitted at most once","If duplicates are intentional aggregation, sum/merge values beforehand instead of passing conflicting ones"],"exampleFix":"// before\nnew Feature(e, vals, ySize); // e contains duplicate (x,y) with different vals\n// after\nMap<Integer, Double> merged = new LinkedHashMap<>();\nfor (int i = 0; i < vals.length; i++) {\n  merged.merge(e.get(i)[0] * ySize + e.get(i)[1], vals[i], Double::sum);\n}\n// build Feature from merged entries","handlingStrategy":"validation","validationCode":"Set<Integer> seen = new HashSet<>();\nfor (int i = 0; i < vals.length; i++) {\n  if (vals[i] != 0.0 && !seen.add(e.get(i)[0] * ySize + e.get(i)[1])) {\n    throw new IllegalStateException(\"Duplicate feature index at \" + i);\n  }\n}","typeGuard":null,"tryCatchPattern":"try { new Feature(e, vals, ySize); } catch (IllegalStateException ex) { /* dedupe pairs and rebuild */ }","preventionTips":["Deduplicate (index,value) pairs before building features","Aggregate duplicate indices with sum/max before construction","Assert uniqueness in featurizer unit tests"],"tags":["java","maxent","duplicate-key","inconsistent-data"],"backgroundTag":"internal-invariant-violation","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"}