Package opennlp.tools.langdetect

# Class ThreadSafeLanguageDetectorME

java.lang.Object
opennlp.tools.langdetect.ThreadSafeLanguageDetectorME

All Implemented Interfaces:
  `AutoCloseable`, `LanguageDetector`

@ThreadSafe public class ThreadSafeLanguageDetectorME extends Object implements LanguageDetector, AutoCloseable
A thread-safe version of the `LanguageDetectorME`. Using it is completely transparent. You can use it in a single-threaded context as well, it only incurs a minimal overhead.

Note:
This implementation uses a `ThreadLocal`. Although the implementation is lightweight because the model is not duplicated, if you have many long-running threads, you may run into memory problems.

Be careful when using this in a Jakarta EE application, for example.

The user is responsible for clearing the `ThreadLocal` via calling `close()`.

See Also:
  - `LanguageDetector`
  - `LanguageDetectorME`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ThreadSafeLanguageDetectorME(LanguageDetectorModel model)`
  Initializes a `ThreadSafeLanguageDetectorME` with the specified `model`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  `String[]`
  `getSupportedLanguages()`
  `Language`
  `predictLanguage(CharSequence content)`
  Predicts the `Language` for the full `content` length.
  `Language[]`
  `predictLanguages(CharSequence content)`
  Predicts the `languages` for the full `content` length.

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

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

- ## Constructor Details

  - ### ThreadSafeLanguageDetectorME

    public ThreadSafeLanguageDetectorME(LanguageDetectorModel model)
    Initializes a `ThreadSafeLanguageDetectorME` with the specified `model`.
    Parameters:
      `model` - A valid `LanguageDetectorModel`.

- ## Method Details

  - ### predictLanguages

    public Language[] predictLanguages(CharSequence content)
    Description copied from interface: `LanguageDetector`
    Predicts the `languages` for the full `content` length.
    Specified by:
      `predictLanguages` in interface `LanguageDetector`
    Parameters:
      `content` - The textual content to detect potential `languages` from.
    Returns:
      the predicted languages

  - ### predictLanguage

    public Language predictLanguage(CharSequence content)
    Description copied from interface: `LanguageDetector`
    Predicts the `Language` for the full `content` length.
    Specified by:
      `predictLanguage` in interface `LanguageDetector`
    Parameters:
      `content` - The textual content to detect potential `languages` from.
    Returns:
      the language with the highest confidence

  - ### getSupportedLanguages

    public String[] getSupportedLanguages()
    Specified by:
      `getSupportedLanguages` in interface `LanguageDetector`
    Returns:
      Retrieves an array of language (codes) that are supported by a `LanguageDetector`.

  - ### close

    public void close()
    Specified by:
      `close` in interface `AutoCloseable`