Package opennlp.tools.ml.libsvm.doccat

# Class SvmDoccatModel

java.lang.Object
opennlp.tools.ml.libsvm.doccat.SvmDoccatModel

All Implemented Interfaces:
  `Serializable`

public class SvmDoccatModel extends Object implements Serializable
A model for SVM-based document categorization. This model wraps a zlibsvm `SvmModel` together with the feature vocabulary, category label mappings, corpus statistics, and configuration required for classification.

Persistence uses Java object serialization via `serialize(OutputStream)` and `deserialize(InputStream)`. Reads are guarded by an `ObjectInputFilter` that allow-lists only the classes reachable from a legitimate `SvmDoccatModel` graph and bounds graph depth, references, and array length. Foreign payloads are rejected with `InvalidClassException` before being materialised.

Treat the filter as defense-in-depth, not as a license to deserialize from untrusted sources: callers should still ensure the input stream originates from a location they trust.

See Also:
  - `DocumentCategorizerSVM`
  - Serialized Form

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final record`
  `SvmDoccatModel.DeserializationLimits`
  Resource limits applied to the `ObjectInputFilter` used by `deserialize(InputStream, DeserializationLimits)`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static SvmDoccatModel`
  `deserialize(InputStream in)`
  Deserializes a `SvmDoccatModel` from the given `InputStream` using `default` resource limits.
  `static SvmDoccatModel`
  `deserialize(InputStream in, SvmDoccatModel.DeserializationLimits limits)`
  Deserializes a `SvmDoccatModel` from the given `InputStream` using the supplied `resource limits`.
  `Map<String,Integer>`
  `getCategoryToIndex()`
  `SvmDoccatConfiguration`
  `getConfiguration()`
  `Map<Integer,Double>`
  `getFeatureMaxValues()`
  `Map<Integer,Double>`
  `getFeatureMinValues()`
  `Map<String,Integer>`
  `getFeatureVocabulary()`
  `Map<String,Double>`
  `getIdfValues()`
  `Map<Integer,String>`
  `getIndexToCategory()`
  `String`
  `getLanguageCode()`
  `int`
  `getNumberOfCategories()`
  `de.hhn.mi.domain.SvmModel`
  `getSvmModel()`
  `void`
  `serialize(OutputStream out)`
  Serializes this model to the given `OutputStream` using Java object serialization.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getSvmModel

    public de.hhn.mi.domain.SvmModel getSvmModel()
    Returns:
      The underlying `SvmModel`.

  - ### getFeatureVocabulary

    public Map<String,Integer> getFeatureVocabulary()
    Returns:
      An unmodifiable mapping from feature strings to their numeric indices.

  - ### getIndexToCategory

    public Map<Integer,String> getIndexToCategory()
    Returns:
      An unmodifiable mapping from numeric category labels to category names.

  - ### getCategoryToIndex

    public Map<String,Integer> getCategoryToIndex()
    Returns:
      An unmodifiable mapping from category names to numeric labels.

  - ### getIdfValues

    public Map<String,Double> getIdfValues()
    Returns:
      An unmodifiable mapping from feature strings to their IDF values.

  - ### getFeatureMinValues

    public Map<Integer,Double> getFeatureMinValues()
    Returns:
      An unmodifiable mapping from feature index to its minimum value in the training corpus.

  - ### getFeatureMaxValues

    public Map<Integer,Double> getFeatureMaxValues()
    Returns:
      An unmodifiable mapping from feature index to its maximum value in the training corpus.

  - ### getConfiguration

    public SvmDoccatConfiguration getConfiguration()
    Returns:
      The `SvmDoccatConfiguration` used for this model.

  - ### getLanguageCode

    public String getLanguageCode()
    Returns:
      The ISO language code associated with this model.

  - ### getNumberOfCategories

    public int getNumberOfCategories()
    Returns:
      The number of categories in this model.

  - ### serialize

    public void serialize(OutputStream out) throws IOException
    Serializes this model to the given `OutputStream` using Java object serialization. The resulting stream can be read back with `deserialize(InputStream)`.
    Parameters:
      `out` - The `OutputStream` to write to. Must not be `null`.
    Throws:
      `IOException` - Thrown if IO errors occurred during serialization.
      `IllegalArgumentException` - if `out` is `null`.

  - ### deserialize

    public static SvmDoccatModel deserialize(InputStream in) throws IOException, ClassNotFoundException
    Deserializes a `SvmDoccatModel` from the given `InputStream` using `default` resource limits.

    The stream is filtered via an `ObjectInputFilter` that allow-lists only the classes required to reconstruct an `SvmDoccatModel`, plus resource limits on graph depth, references, and array length. Foreign payloads are rejected with `InvalidClassException` before `ObjectInputStream.readObject()` returns.

    Callers should still treat this method as defense-in-depth: only invoke it on streams from trusted sources.

    If the default limits are too tight for an unusually large model — for example, a model with more than 10000000L entries in a single map — use `deserialize(InputStream, DeserializationLimits)` to supply higher limits. The class allow-list is intentionally not configurable; loosening it would defeat the purpose of the filter.

    Parameters:
      `in` - The `InputStream` to read from. Must not be `null`.
    Returns:
      A valid `SvmDoccatModel` instance.
    Throws:
      `IOException` - Thrown if IO errors occurred during deserialization, including `InvalidClassException` when the stream contains a class outside the allow-list or exceeds a resource limit.
      `ClassNotFoundException` - Thrown if required classes are not found.
      `IllegalArgumentException` - if `in` is `null`.

  - ### deserialize

    public static SvmDoccatModel deserialize(InputStream in, SvmDoccatModel.DeserializationLimits limits) throws IOException, ClassNotFoundException
    Deserializes a `SvmDoccatModel` from the given `InputStream` using the supplied `resource limits`.

    Use this overload when the `default limits` reject a legitimate model — for example, models trained with very large feature vocabularies. The class allow-list applied to the stream is the same as for `deserialize(InputStream)`; only the numeric limits change.

    Parameters:
      `in` - The `InputStream` to read from. Must not be `null`.
      `limits` - The `SvmDoccatModel.DeserializationLimits` to apply. Must not be `null`.
    Returns:
      A valid `SvmDoccatModel` instance.
    Throws:
      `IOException` - Thrown if IO errors occurred during deserialization, including `InvalidClassException` when the stream contains a class outside the allow-list or exceeds one of the supplied limits.
      `ClassNotFoundException` - Thrown if required classes are not found.
      `IllegalArgumentException` - if `in` or `limits` is `null`.