Class Utf16SpecialistEncodingDetector

java.lang.Object
org.apache.tika.ml.chardetect.Utf16SpecialistEncodingDetector
All Implemented Interfaces:
Serializable, SelfConfiguring, EncodingDetector, StatisticalSpecialist

public class Utf16SpecialistEncodingDetector extends Object implements EncodingDetector, StatisticalSpecialist
UTF-16 specialist detector of the mixture-of-experts charset detection architecture. Uses a tiny dense-feature maxent model paired with Utf16ColumnFeatureExtractor to produce a column-asymmetry-based judgment of UTF-16-LE vs UTF-16-BE.

HTML-immune by construction

The feature set the model consumes (12 per-column byte-range counts) captures the 2-byte alignment asymmetry that UTF-16 content produces and HTML content cannot — HTML has no 2-byte alignment, so any byte range appears with equal expected frequency at even vs odd positions. No amount of HTML markup can fire this specialist. See Utf16ColumnFeatureExtractor for the detailed argument.

Stage 1 of the MoE migration

Runs alongside the existing MojibusterEncodingDetector rather than replacing any piece of it. Emits a single EncodingResult.ResultType.STATISTICAL candidate for the meta arbiter (JunkFilterEncodingDetector) to weigh against the other detectors in the chain. The existing WideUnicodeDetector-based structural UTF-16 detection inside Mojibuster is not removed yet — both can operate in parallel during Stage 1 validation.

Model loading

The default constructor loads a trained model from the classpath at DEFAULT_MODEL_RESOURCE. If the resource is absent or malformed, construction throws IOException — the detector never operates in a no-op state because silent no-ops produce wrong answers without any indication that something's wrong. Deploy the detector only when a trained model is bundled; remove it from the chain otherwise.

Probe size

Reads up to MAX_PROBE_BYTES bytes. UTF-16 column-asymmetry signal stabilises quickly — even ~100 bytes is usually enough for a strong call. Default 512 is generous.

See Also:
  • Field Details

    • DEFAULT_MODEL_RESOURCE

      public static final String DEFAULT_MODEL_RESOURCE
      Default classpath resource for the trained UTF-16 specialist model. Missing resource → detector is a noop (logged once at construction).
      See Also:
    • MAX_PROBE_BYTES

      public static final int MAX_PROBE_BYTES
      Default number of probe bytes read.
      See Also:
    • SPECIALIST_NAME

      public static final String SPECIALIST_NAME
      Specialist name used in SpecialistOutput for provenance.
      See Also:
  • Constructor Details

    • Utf16SpecialistEncodingDetector

      public Utf16SpecialistEncodingDetector() throws IOException
      Load the model from the default classpath location.
      Throws:
      IOException - if the model resource is missing or malformed — the detector does not operate in a no-op state.
  • Method Details

    • provider

      public static Utf16SpecialistEncodingDetector provider()
      ServiceLoader-compatible provider method. Wraps the checked IOException from the no-arg constructor in a ServiceConfigurationError so the arbiter can catch it and skip a specialist whose model is not bundled — without hiding the cause.
    • getName

      public String getName()
      Description copied from interface: StatisticalSpecialist
      Short name: "utf16", "sbcs", etc.
      Specified by:
      getName in interface StatisticalSpecialist
    • score

      public SpecialistOutput score(byte[] probe)
      StatisticalSpecialist entry point: raw per-class logits, or null for a probe too short to evaluate (fewer than 2 bytes) or missing a model. Returning null declines to contribute; an all-low logit vector would muddy the combiner.

      Unlike detect(org.apache.tika.io.TikaInputStream, org.apache.tika.metadata.Metadata, org.apache.tika.parser.ParseContext), this method does not apply a margin threshold — downstream pooling sees raw logits for both classes.

      Specified by:
      score in interface StatisticalSpecialist
    • score

      public SpecialistOutput score(TikaInputStream tis) throws IOException
      Convenience: mark/reset the stream, read a probe, and score it. Returns null if the probe is too short.
      Throws:
      IOException
    • scoreBytes

      @Deprecated public SpecialistOutput scoreBytes(byte[] probe)
      Deprecated.
      use score(byte[]). Kept for existing tests.
    • detect

      public List<EncodingResult> detect(TikaInputStream tis, Metadata metadata, ParseContext parseContext) throws IOException
      Description copied from interface: EncodingDetector
      Detects the character encoding of the given text document.

      Returns an empty list if the encoding cannot be determined. Returns a ranked list of candidates in descending confidence order otherwise. The first entry is always the best guess.

      If the document input stream is not available, then the first argument may be null. Otherwise the detector may read bytes from the start of the stream to help in encoding detection. The given stream is guaranteed to support the mark feature and the detector is expected to mark the stream before reading any bytes from it, and to reset the stream before returning. The stream must not be closed by the detector.

      The given input metadata is only read, not modified, by the detector.

      Specified by:
      detect in interface EncodingDetector
      Parameters:
      tis - text document input stream, or null
      metadata - input metadata for the document
      parseContext - the parse context
      Returns:
      ranked list of encoding results, empty if unknown; never null
      Throws:
      IOException - if the document input stream could not be read
    • detect

      public List<EncodingResult> detect(byte[] probe)
      Byte-array entry point for callers that already hold a probe (e.g. MojibusterEncodingDetector's pipeline). Returns an empty list for probes below MIN_PROBE_BYTES or when the winning class has margin < MIN_LOGIT_MARGIN.