Package net.sf.okapi.lib.translation
Class BaseConnector
- java.lang.Object
-
- net.sf.okapi.lib.translation.BaseConnector
-
- All Implemented Interfaces:
AutoCloseable,Iterator<QueryResult>,IQuery
- Direct Known Subclasses:
ApertiumMTConnector,GoogleAutoMLTranslationConnector,GoogleMTv2Connector,KantanMTv21Connector,Lingo24Connector,MicrosoftMTConnector,MMTConnector,MyMemoryTMConnector,PensieveTMConnector,TDASearchConnector,TranslateToolkitTMConnector
public abstract class BaseConnector extends Object implements IQuery
Abstract implementation of theIQueryinterface.
-
-
Constructor Summary
Constructors Constructor Description BaseConnector()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbatchLeverage(List<ITextUnit> tus)Slow default implementation using leverage(TextUnit).protected voidbatchLeverageUsingBatchQuery(List<ITextUnit> tuList)Call this method inside the overridingbatchLeverage(List)method of the derived class, if that class offers a fastbatchQuery(List)method.List<List<QueryResult>>batchQuery(List<TextFragment> fragments)Slow default implementation using query!! Override to take advantage of servers batch APIList<List<QueryResult>>batchQueryText(List<String> plainTexts)Starts a batch query for a given list of plain texts.voidclearAttributes()Removes all attributes from this query engine.intgetNoQueryThreshold()Gets the threshold for when to avoid a query.IParametersgetParameters()Gets the current parameters of this connector.LocaleIdgetSourceLanguage()Gets the current source language for this query engine.LocaleIdgetTargetLanguage()Gets the current target language for this query engine.intgetWeight()Get the weight for this connector.booleanhasNext()Indicates of there is a hit available.voidleverage(ITextUnit tu)Leverages a text unit (segmented or not) based on the current settings.protected voidleverageUsingBatchQuery(ITextUnit tu)Call this method inside the overridingleverage(ITextUnit)method of the derived class, if that class offers a fastbatchQuery(List)method.QueryResultnext()Gets the next hit for the last query.voidremoveAttribute(String name)Removes a given attribute from this query engine.voidsetAttribute(String name, String value)Sets an attribute for this query engine.voidsetLanguages(LocaleId sourceLocale, LocaleId targetLocale)Sets the source and target languages for this query engine.voidsetNoQueryThreshold(int noQueryThreshold)Sets the threshold for when to avoid executing a query.voidsetParameters(IParameters params)Sets the parameters for opening and querying this connector.voidsetRootDirectory(String rootDir)Sets the root directory that may be used to replace the available ${rootDir} in the parameters of this object.voidsetWeight(int weight)Set the relative weight of thisIQueryconnector as compared to other connectors.protected StringtoInternalCode(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
-
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
-
-
-
Field Detail
-
srcLoc
protected LocaleId srcLoc
-
srcCode
protected String srcCode
-
trgLoc
protected LocaleId trgLoc
-
trgCode
protected String trgCode
-
result
protected QueryResult result
-
current
protected int current
-
-
Method Detail
-
getSourceLanguage
public LocaleId getSourceLanguage()
Description copied from interface:IQueryGets the current source language for this query engine.- Specified by:
getSourceLanguagein interfaceIQuery- Returns:
- Code of the source language.
-
getTargetLanguage
public LocaleId getTargetLanguage()
Description copied from interface:IQueryGets the current target language for this query engine.- Specified by:
getTargetLanguagein interfaceIQuery- Returns:
- Code of the target language.
-
setLanguages
public void setLanguages(LocaleId sourceLocale, LocaleId targetLocale)
Description copied from interface:IQuerySets the source and target languages for this query engine.- Specified by:
setLanguagesin interfaceIQuery- Parameters:
sourceLocale- Code of the source locale.targetLocale- Code of the target locale.
-
hasNext
public boolean hasNext()
Description copied from interface:IQueryIndicates of there is a hit available.- Specified by:
hasNextin interfaceIQuery- Specified by:
hasNextin interfaceIterator<QueryResult>- Returns:
- True if a hit is available, false if not.
-
next
public QueryResult next()
Description copied from interface:IQueryGets the next hit for the last query.- Specified by:
nextin interfaceIQuery- Specified by:
nextin interfaceIterator<QueryResult>- 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:IQueryRemoves all attributes from this query engine.- Specified by:
clearAttributesin interfaceIQuery
-
removeAttribute
public void removeAttribute(String name)
Description copied from interface:IQueryRemoves a given attribute from this query engine.- Specified by:
removeAttributein interfaceIQuery- Parameters:
name- The name of the attribute to remove.
-
setAttribute
public void setAttribute(String name, String value)
Description copied from interface:IQuerySets an attribute for this query engine.- Specified by:
setAttributein interfaceIQuery- Parameters:
name- name of the attribute.value- Value of the attribute.
-
setRootDirectory
public void setRootDirectory(String rootDir)
Description copied from interface:IQuerySets the root directory that may be used to replace the available ${rootDir} in the parameters of this object.- Specified by:
setRootDirectoryin interfaceIQuery- Parameters:
rootDir- the root directory.
-
getParameters
public IParameters getParameters()
Description copied from interface:IQueryGets the current parameters of this connector.- Specified by:
getParametersin interfaceIQuery- Returns:
- the current parameters of this connector or null if no parameters are used.
-
setParameters
public void setParameters(IParameters params)
Description copied from interface:IQuerySets the parameters for opening and querying this connector.- Specified by:
setParametersin interfaceIQuery- Parameters:
params- the parameters to set.
-
getWeight
public int getWeight()
Description copied from interface:IQueryGet the weight for this connector.
-
setWeight
public void setWeight(int weight)
Description copied from interface:IQuerySet the relative weight of thisIQueryconnector as compared to other connectors. Used to setQueryResult.weight.
-
batchQueryText
public List<List<QueryResult>> batchQueryText(List<String> plainTexts)
Description copied from interface:IQueryStarts 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:
batchQueryTextin interfaceIQuery- 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 List<List<QueryResult>> batchQuery(List<TextFragment> fragments)
Slow default implementation using query!! Override to take advantage of servers batch API- Specified by:
batchQueryin interfaceIQuery- 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:IQueryLeverages a text unit (segmented or not) based on the current settings. Any options or attributes needed must be set before calling this method.
-
batchLeverage
public void batchLeverage(List<ITextUnit> tus)
Slow default implementation using leverage(TextUnit). Override in sub-class if you want a custom batchLeverage- Specified by:
batchLeveragein interfaceIQuery- Parameters:
tus- list of the text units to process.
-
setNoQueryThreshold
public void setNoQueryThreshold(int noQueryThreshold)
Description copied from interface:IQuerySets 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:
setNoQueryThresholdin interfaceIQuery- Parameters:
noQueryThreshold- no-query threshold (a value between 0 and 101).
-
getNoQueryThreshold
public int getNoQueryThreshold()
Description copied from interface:IQueryGets the threshold for when to avoid a query.- Specified by:
getNoQueryThresholdin interfaceIQuery- Returns:
- the current no-query threshold.
-
leverageUsingBatchQuery
protected void leverageUsingBatchQuery(ITextUnit tu)
Call this method inside the overridingleverage(ITextUnit)method of the derived class, if that class offers a fastbatchQuery(List)method.- Parameters:
tu- the text unit to leverage.
-
batchLeverageUsingBatchQuery
protected void batchLeverageUsingBatchQuery(List<ITextUnit> tuList)
Call this method inside the overridingbatchLeverage(List)method of the derived class, if that class offers a fastbatchQuery(List)method.- Parameters:
tuList- list of the text units to leverage.
-
toInternalCode
protected 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.
-
-