net.sf.okapi.lib.translation

## Class BaseConnector

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

- All Implemented Interfaces:
    java.lang.AutoCloseable, IQuery

      public abstract class BaseConnector
      extends java.lang.Object
      implements IQuery

  Abstract implementation of the `IQuery` interface.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected int` | `current`
    `protected QueryResult` | `result`
    `protected java.lang.String` | `srcCode`
    `protected LocaleId` | `srcLoc`
    `protected java.lang.String` | `trgCode`
    `protected LocaleId` | `trgLoc`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BaseConnector()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `batchLeverage(java.util.List<ITextUnit> tus)` Slow default implementation using leverage(TextUnit).
    `protected void` | `batchLeverageUsingBatchQuery(java.util.List<ITextUnit> tuList)` Call this method inside the overriding `batchLeverage(List)` method of the derived class, if that class offers a fast `batchQuery(List)` method.
    `java.util.List<java.util.List<QueryResult>>` | `batchQuery(java.util.List<TextFragment> fragments)` Slow default implementation using query!! Override to take advantage of servers batch API
    `java.util.List<java.util.List<QueryResult>>` | `batchQueryText(java.util.List<java.lang.String> plainTexts)` Starts a batch query for a given list of plain texts.
    `void` | `clearAttributes()` Removes all attributes from this query engine.
    `int` | `getNoQueryThreshold()` Gets the threshold for when to avoid a query.
    `IParameters` | `getParameters()` Gets the current parameters of this connector.
    `LocaleId` | `getSourceLanguage()` Gets the current source language for this query engine.
    `LocaleId` | `getTargetLanguage()` Gets the current target language for this query engine.
    `int` | `getWeight()` Get the weight for this connector.
    `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.
    `protected void` | `leverageUsingBatchQuery(ITextUnit tu)` Call this method inside the overriding `leverage(ITextUnit)` method of the derived class, if that class offers a fast `batchQuery(List)` method.
    `QueryResult` | `next()` Gets the next hit for the last query.
    `void` | `removeAttribute(java.lang.String name)` Removes a given attribute from this query engine.
    `void` | `setAttribute(java.lang.String name, java.lang.String value)` Sets an attribute for this query engine.
    `void` | `setLanguages(LocaleId sourceLocale, LocaleId targetLocale)` Sets the source and target languages for this query engine.
    `void` | `setNoQueryThreshold(int noQueryThreshold)` Sets the threshold for when to avoid executing a query.
    `void` | `setParameters(IParameters params)` Sets the parameters for opening and querying this connector.
    `void` | `setRootDirectory(java.lang.String rootDir)` Sets the root directory that may be used to replace the available ${rootDir} in the parameters of this object.
    `void` | `setWeight(int weight)` Set the relative weight of this `IQuery` connector as compared to other connectors.
    `protected java.lang.String` | `toInternalCode(LocaleId locId)` Converts a locale identifier to the internal string value for a language/locale code for this connector.

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

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

    - ### Methods inherited from interface net.sf.okapi.common.query.IQuery

      `close, getName, getSettingsDisplay, open, query, query`

- - ### Field Detail

    - #### srcLoc

          protected LocaleId srcLoc

    - #### srcCode

          protected java.lang.String srcCode

    - #### trgLoc

          protected LocaleId trgLoc

    - #### trgCode

          protected java.lang.String trgCode

    - #### result

          protected QueryResult result

    - #### current

          protected int current

  - ### Constructor Detail

    - #### BaseConnector

          public BaseConnector()

  - ### Method Detail

    - #### getSourceLanguage

          public LocaleId getSourceLanguage()

      Description copied from interface: `IQuery`
      Gets the current source language for this query engine.
      Specified by:
        `getSourceLanguage` in interface `IQuery`
      Returns:
        Code of the source language.

    - #### getTargetLanguage

          public LocaleId getTargetLanguage()

      Description copied from interface: `IQuery`
      Gets the current target language for this query engine.
      Specified by:
        `getTargetLanguage` in interface `IQuery`
      Returns:
        Code of the target language.

    - #### setLanguages

          public void setLanguages(LocaleId sourceLocale,
                          LocaleId targetLocale)

      Description copied from interface: `IQuery`
      Sets the source and target languages for this query engine.
      Specified by:
        `setLanguages` in interface `IQuery`
      Parameters:
        `sourceLocale` - Code of the source locale.
        `targetLocale` - Code of the target locale.

    - #### hasNext

          public boolean hasNext()

      Description copied from interface: `IQuery`
      Indicates of there is a hit available.
      Specified by:
        `hasNext` in interface `IQuery`
      Returns:
        True if a hit is available, false if not.

    - #### next

          public QueryResult next()

      Description copied from interface: `IQuery`
      Gets the next hit for the last query.
      Specified by:
        `next` in interface `IQuery`
      Returns:
        A QueryResult object that holds the source and target text of the hit, or null if there is no more hit.

    - #### clearAttributes

          public void clearAttributes()

      Description copied from interface: `IQuery`
      Removes all attributes from this query engine.
      Specified by:
        `clearAttributes` in interface `IQuery`

    - #### removeAttribute

          public void removeAttribute(java.lang.String name)

      Description copied from interface: `IQuery`
      Removes a given attribute from this query engine.
      Specified by:
        `removeAttribute` in interface `IQuery`
      Parameters:
        `name` - The name of the attribute to remove.

    - #### setAttribute

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

      Description copied from interface: `IQuery`
      Sets an attribute for this query engine.
      Specified by:
        `setAttribute` in interface `IQuery`
      Parameters:
        `name` - name of the attribute.
        `value` - Value of the attribute.

    - #### setRootDirectory

          public void setRootDirectory(java.lang.String rootDir)

      Description copied from interface: `IQuery`
      Sets the root directory that may be used to replace the available ${rootDir} in the parameters of this object.
      Specified by:
        `setRootDirectory` in interface `IQuery`
      Parameters:
        `rootDir` - the root directory.

    - #### getParameters

          public IParameters getParameters()

      Description copied from interface: `IQuery`
      Gets the current parameters of this connector.
      Specified by:
        `getParameters` in interface `IQuery`
      Returns:
        the current parameters of this connector or null if no parameters are used.

    - #### setParameters

          public void setParameters(IParameters params)

      Description copied from interface: `IQuery`
      Sets the parameters for opening and querying this connector.
      Specified by:
        `setParameters` in interface `IQuery`
      Parameters:
        `params` - the parameters to set.

    - #### getWeight

          public int getWeight()

      Description copied from interface: `IQuery`
      Get the weight for this connector.
      Specified by:
        `getWeight` in interface `IQuery`
      Returns:
        the weight for this connector

    - #### setWeight

          public void setWeight(int weight)

      Description copied from interface: `IQuery`
      Set the relative weight of this `IQuery` connector as compared to other connectors. Used to set `QueryResult.weight`.
      Specified by:
        `setWeight` in interface `IQuery`
      Parameters:
        `weight` - the new relative weight.

    - #### batchQueryText

          public java.util.List<java.util.List<QueryResult>> batchQueryText(java.util.List<java.lang.String> plainTexts)

      Description copied from interface: `IQuery`
      Starts a batch query for a given list of plain texts.
      Note that batchQueryText does not use hasNext and next methods. Callers must use the returned list directly.
      Specified by:
        `batchQueryText` in interface `IQuery`
      Parameters:
        `plainTexts` - list of the plain texts to translate.
      Returns:
        a list of lists of query result. Each list corresponds to a source text (in the same order)

    - #### batchQuery

          public java.util.List<java.util.List<QueryResult>> batchQuery(java.util.List<TextFragment> fragments)

      Slow default implementation using query!! Override to take advantage of servers batch API
      Specified by:
        `batchQuery` in interface `IQuery`
      Parameters:
        `fragments` - list of the text fragments to translate.
      Returns:
        a list of lists of query result. Each list corresponds to a source text (in the same order)

    - #### leverage

          public void leverage(ITextUnit tu)

      Description copied from interface: `IQuery`
      Leverages a text unit (segmented or not) based on the current settings. Any options or attributes needed must be set before calling this method.
      Specified by:
        `leverage` in interface `IQuery`
      Parameters:
        `tu` - the text unit to leverage.

    - #### batchLeverage

          public void batchLeverage(java.util.List<ITextUnit> tus)

      Slow default implementation using leverage(TextUnit). Override in sub-class if you want a custom batchLeverage
      Specified by:
        `batchLeverage` in interface `IQuery`
      Parameters:
        `tus` - list of the text units to process.

    - #### setNoQueryThreshold

          public void setNoQueryThreshold(int noQueryThreshold)

      Description copied from interface: `IQuery`
      Sets the threshold for when to avoid executing a query.

      If the entry to leverage has already one candidate with a score equals or above this value, no query is performed. Use 101 to always allow the query.

      Specified by:
        `setNoQueryThreshold` in interface `IQuery`
      Parameters:
        `noQueryThreshold` - no-query threshold (a value between 0 and 101).

    - #### getNoQueryThreshold

          public int getNoQueryThreshold()

      Description copied from interface: `IQuery`
      Gets the threshold for when to avoid a query.
      Specified by:
        `getNoQueryThreshold` in interface `IQuery`
      Returns:
        the current no-query threshold.

    - #### leverageUsingBatchQuery

          protected void leverageUsingBatchQuery(ITextUnit tu)

      Call this method inside the overriding `leverage(ITextUnit)` method of the derived class, if that class offers a fast `batchQuery(List)` method.
      Parameters:
        `tu` - the text unit to leverage.

    - #### batchLeverageUsingBatchQuery

          protected void batchLeverageUsingBatchQuery(java.util.List<ITextUnit> tuList)

      Call this method inside the overriding `batchLeverage(List)` method of the derived class, if that class offers a fast `batchQuery(List)` method.
      Parameters:
        `tuList` - list of the text units to leverage.

    - #### toInternalCode

          protected java.lang.String toInternalCode(LocaleId locId)

      Converts a locale identifier to the internal string value for a language/locale code for this connector. By default, this simply returns the string of the given LocaleId.
      Parameters:
        `locId` - the locale identifier to convert.
      Returns:
        the internal string code for language/locale code for this connector.