stanfordnlp/CoreNLP · error · UnsupportedOperationException

Illegal construction of DistsimFeatureFactory. It must be…

Error message

Illegal construction of DistsimFeatureFactory.  It must be created with a path to a cluster file

What it means

DistsimFeatureFactory has a no-argument constructor that unconditionally throws UnsupportedOperationException. It is deliberately made illegal: the factory requires a path to a word-cluster (distributional similarity) file to build its Distsim lexicon, so the default constructor exists only to block misconfiguration.

Solutions

  1. Call DistsimFeatureFactory(String path) with the path to your cluster file instead
  2. Ensure the cluster file exists and points to a valid Distsim-format lexicon
  3. If using reflection, select the String-argument constructor via getConstructor(String.class)

Example fix

// before
DistsimFeatureFactory factory = DistsimFeatureFactory.class.getDeclaredConstructor().newInstance();

// after
DistsimFeatureFactory factory = new DistsimFeatureFactory("/path/to/word_clusters.txt");
Defensive patterns

Strategy: type-guard

Validate before calling

if (path == null || path.isEmpty()) {
  throw new IllegalArgumentException("DistsimFeatureFactory requires a cluster file path");
}

Try / catch

try {
  factory = ctor.newInstance();
} catch (InvocationTargetException e) {
  throw new IllegalStateException("Use DistsimFeatureFactory(String path), not the no-arg constructor", e.getCause());
}

Prevention

When it happens

Trigger: Calling the package-private no-arg DistsimFeatureFactory() constructor, typically via reflection, serialization frameworks, or factory code that instantiates FeatureFactory subclasses by default constructor.

Common situations: Reflective instantiation of feature factories from config; a deserializer or DI container choosing the no-arg constructor; copy-pasted factory creation code without the cluster file path.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/shiftreduce/DistsimFeatureFactory.java:18

package edu.stanford.nlp.parser.shiftreduce;

import java.util.List;

import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.tagger.maxent.Distsim;

/**
 * Featurizes words based only on their distributional similarity classes.
 * Borrows the Distsim class from the tagger.
 *
 * @author John Bauer
 */
public class DistsimFeatureFactory extends FeatureFactory {
  private final Distsim distsim;

  DistsimFeatureFactory() {
    throw new UnsupportedOperationException("Illegal construction of DistsimFeatureFactory.  It must be created with a path to a cluster file");
  }

  DistsimFeatureFactory(String path) {
    distsim = Distsim.initLexicon(path);
  }

  public void addDistsimFeatures(List<String> features, CoreLabel label, String featureName) {
    if (label == null) {
      return;
    }

    String word = getFeatureFromCoreLabel(label, FeatureComponent.HEADWORD);
    String tag = getFeatureFromCoreLabel(label, FeatureComponent.HEADTAG);

    String cluster = distsim.getMapping(word);

    features.add(featureName + "dis-" + cluster);
    features.add(featureName + "disT-" + cluster + "-" + tag);

View on GitHub (pinned to 1b7edd19c4)