Package net.sf.okapi.lib.translation
Class QueryManager
- java.lang.Object
-
- net.sf.okapi.lib.translation.QueryManager
-
- All Implemented Interfaces:
Iterator<QueryResult>
public class QueryManager extends Object implements Iterator<QueryResult>
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 Description QueryManager()Creates a new QueryManager object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddAndInitializeResource(String connectorClass, String resourceName, ClassLoader loader, 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.intaddAndInitializeResource(String connectorClass, String resourceName, 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.intaddAndInitializeResource(IQuery connector, 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.intaddResource(IQuery connector, String name)Adds a translation resource to the manager.static booleancanLeverage(Segment srcSeg, Segment trgSeg, boolean leverageIfTargetIsEmpty, boolean leverageIfTargetIsSameAsSource)Indicates if we can fill a given target segment with leveraged translation based on whether it is empty and whether it is the same as the source segment.static booleancanLeverage(TextContainer srcTc, TextContainer trgTc, boolean leverageIfTargetIsEmpty, boolean leverageIfTargetIsSameAsSource)Indicates if we can fill a given target TextContent with leveraged translation based on whether it is empty and whether it is the same as the source TextContent.voidclearAttributes()Removes all attributes from this manager and all the translation resources in this manager.voidclose()Closes all translation resources in this manager.intgetExactBestMatches()Gets the number of best matches that are exact (100%) since the last call toresetCounters().intgetFuzzyBestMatches()Gets the number of best matches that are fuzzy (less that 100%, more than 0%) since the last call toresetCounters().IQuerygetInterface(int resourceId)Gets the IQuery interface for a given translation resource.StringgetName(int resourceId)Gets the name for a given translation resource.ResourceItemgetResource(int resourceId)Gets the configuration data for a given translation resource.Map<Integer,ResourceItem>getResources()Gets the configuration data for all the translation resources in this manager.List<QueryResult>getResults()Gets the list of all hit results of the last query.LocaleIdgetSourceLanguage()Gets the current source locale for this manager.LocaleIdgetTargetLanguage()Gets the current target locale for this manager.intgetTotalSegments()Gets the total number of segments processed since the last call toresetCounters().booleanhasNext()Indicates of there is a hit available.voidleverage(ITextUnit tu)Leverages a text unit (segmented or not) based on the current settings.QueryResultnext()Gets the next hit for the last query.intquery(String plainText)Queries all enabled translation resources for a given plain text.intquery(TextFragment text)Queries all enabled translation resources for a given text fragment.voidremove(int resourceId)Removes a given translation resource.voidremoveAttribute(String name)Removes a given attribute from this manager and all translation resources in this manager.voidresetCounters()Resets the counters used to calculate the number of segments leveraged.voidrewind()Resets the current result to the first one if there is one.voidsetAttribute(String name, String value)Sets an attribute for this manager and all translation resources in this manager.voidsetEnabled(int resourceId, boolean enabled)Enables or disables a given translation resource.voidsetLanguages(LocaleId sourceLocale, LocaleId targetLocale)Sets the source and target locales for this manager and for all translation resources in this manager.voidsetMaximumHits(int max)Sets the maximum number of hits to return for this query manager and all the relevant translation resources it holds.voidsetNoQueryThreshold(int noQueryThreshold)Sets the no-query threshold for this query manager.voidsetOptions(int thresholdToFill, boolean fillIfTargetIsEmpty, boolean fillIfTargetIsSameAsSource, boolean downgradeIdenticalBestMatches, String targetPrefix, int thresholdToPrefix, boolean copySourceOnNoText)Sets the options for performing the leverage.voidsetRootDirectory(String rootDir)Sets the root directory for this query manager and all translation resources it holds.voidsetThreshold(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
-
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
-
-
-
Method Detail
-
canLeverage
public static boolean canLeverage(Segment srcSeg, Segment trgSeg, boolean leverageIfTargetIsEmpty, boolean leverageIfTargetIsSameAsSource)
Indicates if we can fill a given target segment with leveraged translation based on whether it is empty and whether it is the same as the source segment.- Parameters:
srcSeg- the source segment.trgSeg- the target segment.leverageIfTargetIsEmpty- true if we leverage only if the target is empty.leverageIfTargetIsSameAsSource- true if we leverage if the target is not empty but is the same as the source.- Returns:
- true if we can leverage, false otherwise.
-
canLeverage
public static boolean canLeverage(TextContainer srcTc, TextContainer trgTc, boolean leverageIfTargetIsEmpty, boolean leverageIfTargetIsSameAsSource)
Indicates if we can fill a given target TextContent with leveraged translation based on whether it is empty and whether it is the same as the source TextContent.- Parameters:
srcTc- the source TextContent.trgTc- the target TextContent.leverageIfTargetIsEmpty- true if we leverage only if the target is empty.leverageIfTargetIsSameAsSource- true if we leverage if the target is not empty but is the same as the source.- Returns:
- true if we can leverage, false otherwise.
-
addResource
public int addResource(IQuery connector, 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, 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:
RuntimeException- if an error occurs.
-
addAndInitializeResource
public int addAndInitializeResource(String connectorClass, String resourceName, 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:
RuntimeException- if an error occurs.
-
addAndInitializeResource
public int addAndInitializeResource(String connectorClass, String resourceName, ClassLoader loader, 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 loadedconnectorParams- 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:
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 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 Map<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 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.- Specified by:
hasNextin interfaceIterator<QueryResult>- Returns:
- True if a hit is available, false if not.
-
next
public QueryResult next()
Gets the next hit for the last query.- 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.
-
query
public int query(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(String name, 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(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(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, 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 toresetCounters().- 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 toresetCounters().- 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 toresetCounters().- Returns:
- the number of best matches that are fuzzy.
-
-