net.sf.okapi.lib.translation

## Class QueryManager

- java.lang.Object
- - net.sf.okapi.lib.translation.QueryManager

-     public class QueryManager
      extends java.lang.Object

  Provides a wrapper to manage and query several translation resources at the same time. For example, a local TM, a remote TM and a Machine Translation server.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `QueryManager()` Creates a new QueryManager object.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `addAndInitializeResource(IQuery connector, java.lang.String resourceName, IParameters params)` Adds a translation resource to the manager and initializes it with the current source and target language of this manager, as well as any attributes that is set, and the current threshold and maximum hits if it is relevant.
    `int` | `addAndInitializeResource(java.lang.String connectorClass, java.lang.String resourceName, java.lang.ClassLoader loader, java.lang.String connectorParams)` Creates a translation resource and its parameters from their class names, adds it to the manager and initializes it with the current source and target language of this manager, as well as any attributes that is set, and the current threshold and maximum hits if it is relevant.
    `int` | `addAndInitializeResource(java.lang.String connectorClass, java.lang.String resourceName, java.lang.String connectorParams)` Creates a translation resource and its parameters from their class names, adds it to the manager and initializes it with the current source and target language of this manager, as well as any attributes that is set, and the current threshold and maximum hits if it is relevant.
    `int` | `addResource(IQuery connector, java.lang.String name)` Adds a translation resource to the manager.
    `void` | `clearAttributes()` Removes all attributes from this manager and all the translation resources in this manager.
    `void` | `close()` Closes all translation resources in this manager.
    `int` | `getExactBestMatches()` Gets the number of best matches that are exact (100%) since the last call to `resetCounters()`.
    `int` | `getFuzzyBestMatches()` Gets the number of best matches that are fuzzy (less that 100%, more than 0%) since the last call to `resetCounters()`.
    `IQuery` | `getInterface(int resourceId)` Gets the IQuery interface for a given translation resource.
    `java.lang.String` | `getName(int resourceId)` Gets the name for a given translation resource.
    `ResourceItem` | `getResource(int resourceId)` Gets the configuration data for a given translation resource.
    `java.util.Map<java.lang.Integer,ResourceItem>` | `getResources()` Gets the configuration data for all the translation resources in this manager.
    `java.util.List<QueryResult>` | `getResults()` Gets the list of all hit results of the last query.
    `LocaleId` | `getSourceLanguage()` Gets the current source locale for this manager.
    `LocaleId` | `getTargetLanguage()` Gets the current target locale for this manager.
    `int` | `getTotalSegments()` Gets the total number of segments processed since the last call to `resetCounters()`.
    `boolean` | `hasNext()` Indicates of there is a hit available.
    `void` | `leverage(ITextUnit tu)` Leverages a text unit (segmented or not) based on the current settings.
    `QueryResult` | `next()` Gets the next hit for the last query.
    `int` | `query(java.lang.String plainText)` Queries all enabled translation resources for a given plain text.
    `int` | `query(TextFragment text)` Queries all enabled translation resources for a given text fragment.
    `void` | `remove(int resourceId)` Removes a given translation resource.
    `void` | `removeAttribute(java.lang.String name)` Removes a given attribute from this manager and all translation resources in this manager.
    `void` | `resetCounters()` Resets the counters used to calculate the number of segments leveraged.
    `void` | `rewind()` Resets the current result to the first one if there is one.
    `void` | `setAttribute(java.lang.String name, java.lang.String value)` Sets an attribute for this manager and all translation resources in this manager.
    `void` | `setEnabled(int resourceId, boolean enabled)` Enables or disables a given translation resource.
    `void` | `setLanguages(LocaleId sourceLocale, LocaleId targetLocale)` Sets the source and target locales for this manager and for all translation resources in this manager.
    `void` | `setMaximumHits(int max)` Sets the maximum number of hits to return for this query manager and all the relevant translation resources it holds.
    `void` | `setNoQueryThreshold(int noQueryThreshold)` Sets the no-query threshold for this query manager.
    `void` | `setOptions(int thresholdToFill, boolean fillIfTargetIsEmpty, boolean fillIfTargetIsSameAsSource, boolean downgradeIdenticalBestMatches, java.lang.String targetPrefix, int thresholdToPrefix, boolean copySourceOnNoText)` Sets the options for performing the leverage.
    `void` | `setRootDirectory(java.lang.String rootDir)` Sets the root directory for this query manager and all translation resources it holds.
    `void` | `setThreshold(int value)` Sets the threshold for this query manager and all the relevant translation resources it holds.

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

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

- - ### Constructor Detail

    - #### QueryManager

          public QueryManager()

      Creates a new QueryManager object.

  - ### Method Detail

    - #### addResource

          public int addResource(IQuery connector,
                        java.lang.String name)

      Adds a translation resource to the manager.
      Parameters:
        `connector` - The translation resource connector to add.
        `name` - Name of the translation resource to add.
      Returns:
        The ID for the added translation resource. This ID can be used later to access specifically the added translation resource.

    - #### addAndInitializeResource

          public int addAndInitializeResource(IQuery connector,
                                     java.lang.String resourceName,
                                     IParameters params)

      Adds a translation resource to the manager and initializes it with the current source and target language of this manager, as well as any attributes that is set, and the current threshold and maximum hits if it is relevant.
      Parameters:
        `connector` - The translation resource connector to add.
        `resourceName` - Name of the translation resource to add.
        `params` - the parameters for this connector.
      Returns:
        The identifier for the added translation resource. This identifier can be used later to access specifically the added translation resource.
      Throws:
        `java.lang.RuntimeException` - if an error occurs.

    - #### addAndInitializeResource

          public int addAndInitializeResource(java.lang.String connectorClass,
                                     java.lang.String resourceName,
                                     java.lang.String connectorParams)

      Creates a translation resource and its parameters from their class names, adds it to the manager and initializes it with the current source and target language of this manager, as well as any attributes that is set, and the current threshold and maximum hits if it is relevant.
      Parameters:
        `connectorClass` - the name of the class for the connector.
        `resourceName` - the name of the translation resource (can be null).
        `connectorParams` - connector parameters stored in a string.
      Returns:
        The identifier for the added translation resource. This identifier can be used later to access specifically the added translation resource.
      Throws:
        `java.lang.RuntimeException` - if an error occurs.

    - #### addAndInitializeResource

          public int addAndInitializeResource(java.lang.String connectorClass,
                                     java.lang.String resourceName,
                                     java.lang.ClassLoader loader,
                                     java.lang.String connectorParams)

      Creates a translation resource and its parameters from their class names, adds it to the manager and initializes it with the current source and target language of this manager, as well as any attributes that is set, and the current threshold and maximum hits if it is relevant.
      Parameters:
        `connectorClass` - the name of the class for the connector.
        `resourceName` - the name of the translation resource (can be null).
        `loader` - class loader from which the connector class must be loaded
        `connectorParams` - connector parameters stored in a string.
      Returns:
        The identifier for the added translation resource. This identifier can be used later to access specifically the added translation resource.
      Throws:
        `java.lang.RuntimeException` - if an error occurs.

    - #### setEnabled

          public void setEnabled(int resourceId,
                        boolean enabled)

      Enables or disables a given translation resource.
      Parameters:
        `resourceId` - ID of the translation resource to enable or disable.
        `enabled` - True to enable the resource, false to disable it.

    - #### remove

          public void remove(int resourceId)

      Removes a given translation resource.
      Parameters:
        `resourceId` - ID of the translation resource to remove.

    - #### getInterface

          public IQuery getInterface(int resourceId)

      Gets the IQuery interface for a given translation resource.
      Parameters:
        `resourceId` - ID of the translation resource to lookup.
      Returns:
        The IQuery interface for the given translation resource, or null if the ID is not found.

    - #### getResource

          public ResourceItem getResource(int resourceId)

      Gets the configuration data for a given translation resource.
      Parameters:
        `resourceId` - ID of the translation resource to lookup.
      Returns:
        A ResourceItem object that contains the configuration data for the given translation resource, or null if the ID is not found.

    - #### getName

          public java.lang.String getName(int resourceId)

      Gets the name for a given translation resource.
      Parameters:
        `resourceId` - ID of the translation resource to lookup.
      Returns:
        The name of the given translation resource, or null if the ID is not found.

    - #### getResources

          public java.util.Map<java.lang.Integer,ResourceItem> getResources()

      Gets the configuration data for all the translation resources in this manager.
      Returns:
        A map of ID+ResourceItem objects pairs that contains the configuration data for each translation resource. the map can be empty.

    - #### close

          public void close()

      Closes all translation resources in this manager.

    - #### getResults

          public java.util.List<QueryResult> getResults()

      Gets the list of all hit results of the last query.
      Returns:
        A list of all hit results of the last query.

    - #### rewind

          public void rewind()

      Resets the current result to the first one if there is one.

    - #### hasNext

          public boolean hasNext()

      Indicates of there is a hit available.
      Returns:
        True if a hit is available, false if not.

    - #### next

          public QueryResult next()

      Gets the next hit for the last query.
      Returns:
        A QueryResult object that holds the source and target text of the hit, or null if there is no more hit.

    - #### query

          public int query(java.lang.String plainText)

      Queries all enabled translation resources for a given plain text.
      Parameters:
        `plainText` - The text to query.
      Returns:
        The number of hits for the given query.

    - #### query

          public int query(TextFragment text)

      Queries all enabled translation resources for a given text fragment.
      Parameters:
        `text` - The text to query.
      Returns:
        The number of hits for the given query.

    - #### setAttribute

          public void setAttribute(java.lang.String name,
                          java.lang.String value)

      Sets an attribute for this manager and all translation resources in this manager.
      Parameters:
        `name` - name of the attribute.
        `value` - Value of the attribute.

    - #### removeAttribute

          public void removeAttribute(java.lang.String name)

      Removes a given attribute from this manager and all translation resources in this manager.
      Parameters:
        `name` - The name of the attribute to remove.

    - #### clearAttributes

          public void clearAttributes()

      Removes all attributes from this manager and all the translation resources in this manager.

    - #### setLanguages

          public void setLanguages(LocaleId sourceLocale,
                          LocaleId targetLocale)

      Sets the source and target locales for this manager and for all translation resources in this manager.
      Parameters:
        `sourceLocale` - Code of the source locale to set.
        `targetLocale` - Code of the target locale to set.

    - #### getSourceLanguage

          public LocaleId getSourceLanguage()

      Gets the current source locale for this manager.
      Returns:
        Code of the current source locale for this manager.

    - #### getTargetLanguage

          public LocaleId getTargetLanguage()

      Gets the current target locale for this manager.
      Returns:
        Code of the current target locale for this manager.

    - #### setThreshold

          public void setThreshold(int value)

      Sets the threshold for this query manager and all the relevant translation resources it holds.
      Parameters:
        `value` - the threshold value to set.

    - #### setMaximumHits

          public void setMaximumHits(int max)

      Sets the maximum number of hits to return for this query manager and all the relevant translation resources it holds.
      Parameters:
        `max` - the maximum value to set.

    - #### setRootDirectory

          public void setRootDirectory(java.lang.String rootDir)

      Sets the root directory for this query manager and all translation resources it holds.
      Parameters:
        `rootDir` - the root directory.

    - #### setNoQueryThreshold

          public void setNoQueryThreshold(int noQueryThreshold)

      Sets the no-query threshold for this query manager. and all the translation resources it holds.
      Parameters:
        `noQueryThreshold` - the value of the no-query threshold (between 0 and 101). Use 101 to always allow the query.

    - #### setOptions

          public void setOptions(int thresholdToFill,
                        boolean fillIfTargetIsEmpty,
                        boolean fillIfTargetIsSameAsSource,
                        boolean downgradeIdenticalBestMatches,
                        java.lang.String targetPrefix,
                        int thresholdToPrefix,
                        boolean copySourceOnNoText)

      Sets the options for performing the leverage.
      Parameters:
        `thresholdToFill` - if the first match has a score equal or above this value, the target text of the match is placed in the target content. To avoid any filling of the target: simply use a high value (e.g. `Integer.MAX_VALUE`).
        `fillIfTargetIsEmpty` - true to fill the target only if its content is currently empty.
        `fillIfTargetIsSameAsSource` - true to fill the target if fillIfTargetIsEmpty is true and the source and target content are the same.
        `downgradeIdenticalBestMatches` - true to reduce the score of best matches when they are identical.
        `targetPrefix` - A prefix to place at the front of the candidate target if it is leveraged into the text unit. Use null to not set a prefix.
        `thresholdToPrefix` - if a target prefix is defined and the score is equal or below this threshold the prefix is added. This parameter is ignored if the target prefix is null.
        `copySourceOnNoText` - true to copy the source content for the target segments that have no text (but they may have codes and/or white spaces).

    - #### leverage

          public void leverage(ITextUnit tu)

      Leverages a text unit (segmented or not) based on the current settings. Any options or attributes needed must be set before calling this method.
      Parameters:
        `tu` - the text unit to leverage.
      See Also:
        `setAttribute(String, String)`, `setLanguages(LocaleId, LocaleId)`, `setMaximumHits(int)`, `setNoQueryThreshold(int)`, `setOptions(int, boolean, boolean, boolean, String, int, boolean)`, `setRootDirectory(String)`, `setThreshold(int)`

    - #### resetCounters

          public void resetCounters()

      Resets the counters used to calculate the number of segments leveraged.
      See Also:
        `getExactBestMatches()`, `getTotalSegments()`

    - #### getTotalSegments

          public int getTotalSegments()

      Gets the total number of segments processed since the last call to `resetCounters()`.
      Returns:
        the total number of segment processed.

    - #### getExactBestMatches

          public int getExactBestMatches()

      Gets the number of best matches that are exact (100%) since the last call to `resetCounters()`.
      Returns:
        the number of best matches that are exact.

    - #### getFuzzyBestMatches

          public int getFuzzyBestMatches()

      Gets the number of best matches that are fuzzy (less that 100%, more than 0%) since the last call to `resetCounters()`.
      Returns:
        the number of best matches that are fuzzy.