Package org.languagetool.rules
Class GRPCRule
java.lang.Object
org.languagetool.rules.Rule
org.languagetool.rules.RemoteRule
org.languagetool.rules.GRPCRule
Base class fur rules running on external servers;
see gRPC service definition in languagetool-core/src/main/proto/ml_server.proto
See #create(Language, ResourceBundle, RemoteRuleConfig, boolean, String, String, Map) for an easy way to add rules; return rule in Language::getRelevantRemoteRules
add it like this:
public List<Rule> getRelevantRemoteRules(ResourceBundle messageBundle, List<RemoteRuleConfig> configs, GlobalConfig globalConfig, UserConfig userConfig, Language motherTongue, List<Language> altLanguages) throws IOException {
List<Rule> rules = new ArrayList<>(super.getRelevantRemoteRules(
messageBundle, configs, globalConfig, userConfig, motherTongue, altLanguages));
Rule exampleRule = GRPCRule.create(messageBundle,
RemoteRuleConfig.getRelevantConfig("EXAMPLE_ID", configs),
"EXAMPLE_ID", "example_rule_id",
Collections.singletonMap("example_match_id", "example_rule_message"));
rules.add(exampleRule);
return rules;
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classstatic classstatic classInternal rule to create rule matches with IDs based on Match Sub-IDsprotected static classNested classes/interfaces inherited from class org.languagetool.rules.RemoteRule
RemoteRule.RemoteRequest -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intstatic final Stringprivate final GRPCRule.Connectionprivate static final intprivate static final Stringprivate static final org.slf4j.Loggerprivate intprivate final booleanprivate static final com.google.common.cache.LoadingCache<RemoteRuleConfig, GRPCRule.Connection> static final PatternFields inherited from class org.languagetool.rules.RemoteRule
circuitBreakers, filterMatches, fixOffsets, inputLogging, lt, premium, ruleLanguage, serviceConfiguration, shutdownRoutines, suppressMisspelledMatch, suppressMisspelledSuggestions, whitespaceNormalisation -
Constructor Summary
ConstructorsConstructorDescriptionGRPCRule(Language language, ResourceBundle messages, RemoteRuleConfig config, boolean inputLogging) -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringstatic GRPCRulecreate(Language language, ResourceBundle messages, RemoteRuleConfig config, boolean inputLogging, String id, String descriptionKey, Map<String, String> messagesByID) Helper method to create instances of RemoteMLRulestatic GRPCRulecreate(Language language, RemoteRuleConfig config, boolean inputLogging, String id, String description, Map<String, String> messagesByID) Helper method to create instances of RemoteMLRulecreateAll(Language language, List<RemoteRuleConfig> configs, boolean inputLogging) createAll(Language language, List<RemoteRuleConfig> configs, boolean inputLogging, String prefix, String defaultDescription) protected Callable<RemoteRuleResult> executeRequest(RemoteRule.RemoteRequest requestArg, long timeoutMilliseconds) protected RemoteRuleResultfallbackResults(RemoteRule.RemoteRequest request) fallback if executeRequest times out or throws an errorprotected abstract StringgetMessage(MLServerProto.Match match, AnalyzedSentence sentence) messages can be provided by the ML server or the Java client fill them in here or leave this empty if the server takes care of itgetRuleMatches(List<AnalyzedSentence> sentences, List<MLServerProto.MatchResponse> responses) private static Stringprotected RemoteRule.RemoteRequestprepareRequest(List<AnalyzedSentence> sentences, Long textSessionId) run local preprocessing steps (or just store sentences)Methods inherited from class org.languagetool.rules.RemoteRule
circuitBreaker, computeOffsetShifts, createCircuitBreaker, fixMatchOffsets, getCircuitBreakerConfig, getId, getServiceConfiguration, getTimeout, getTimeout, isPremium, match, run, run, shutdownMethods inherited from class org.languagetool.rules.Rule
addExamplePair, addTags, addToneTags, cacheAntiPatterns, estimateContextForSureMatch, getAntiPatterns, getCategory, getCorrectExamples, getDescription, getDistanceTokens, getErrorTriggeringExamples, getFullId, getIncorrectExamples, getLocQualityIssueType, getMinPrevMatches, getPriority, getRuleOptions, getSentenceWithImmunization, getSourceFile, getSubId, getTags, getToneTags, getUrl, hasTag, hasToneTag, isDefaultOff, isDefaultTempOff, isDictionaryBasedSpellingRule, isGoalSpecific, isIncludedInHiddenMatches, isOfficeDefaultOff, isOfficeDefaultOn, makeAntiPatterns, setCategory, setCorrectExamples, setDefaultOff, setDefaultOn, setDefaultTempOff, setDistanceTokens, setErrorTriggeringExamples, setExamplePair, setGoalSpecific, setIncludedInHiddenMatches, setIncorrectExamples, setLocQualityIssueType, setMinPrevMatches, setOfficeDefaultOff, setOfficeDefaultOn, setPremium, setPriority, setTags, setToneTags, setUrl, supportsLanguage, toRuleMatchArray, useInOffice
-
Field Details
-
CONFIG_TYPE
- See Also:
-
logger
private static final org.slf4j.Logger logger -
DEFAULT_BATCH_SIZE
private static final int DEFAULT_BATCH_SIZE- See Also:
-
WHITESPACE_REGEX
-
DEFAULT_DESCRIPTION
- See Also:
-
servers
private static final com.google.common.cache.LoadingCache<RemoteRuleConfig,GRPCRule.Connection> servers -
conn
-
batchSize
private final int batchSize -
sendAnalyzedData
private final boolean sendAnalyzedData -
maxSentenceLength
private int maxSentenceLength
-
-
Constructor Details
-
GRPCRule
public GRPCRule(Language language, ResourceBundle messages, RemoteRuleConfig config, boolean inputLogging)
-
-
Method Details
-
cleanID
-
prepareRequest
protected RemoteRule.RemoteRequest prepareRequest(List<AnalyzedSentence> sentences, @Nullable Long textSessionId) Description copied from class:RemoteRulerun local preprocessing steps (or just store sentences)- Specified by:
prepareRequestin classRemoteRule- Parameters:
sentences- text to processtextSessionId- session ID for caching, partial rollout, A/B testing- Returns:
- parameter for executeRequest/fallbackResults
-
nonEmpty
-
executeRequest
protected Callable<RemoteRuleResult> executeRequest(RemoteRule.RemoteRequest requestArg, long timeoutMilliseconds) throws TimeoutException - Specified by:
executeRequestin classRemoteRule- Parameters:
requestArg- returned by prepareRequesttimeoutMilliseconds- timeout for this operation, <=0 -> unlimited- Returns:
- callable that sends request, parses and returns result for this remote rule
- Throws:
TimeoutException- if timeout was exceeded
-
getRuleMatches
private List<RuleMatch> getRuleMatches(List<AnalyzedSentence> sentences, List<MLServerProto.MatchResponse> responses) -
getMessage
messages can be provided by the ML server or the Java client fill them in here or leave this empty if the server takes care of it -
fallbackResults
Description copied from class:RemoteRulefallback if executeRequest times out or throws an error- Specified by:
fallbackResultsin classRemoteRule- Parameters:
request- returned by prepareRequest- Returns:
- local results for this rule
-
create
public static GRPCRule create(Language language, ResourceBundle messages, RemoteRuleConfig config, boolean inputLogging, String id, String descriptionKey, Map<String, String> messagesByID) Helper method to create instances of RemoteMLRule- Parameters:
language- rule languagemessages- for i18n; = JLanguageTool.getMessageBundle(lang)config- configuration for remote rule server; options: secure, clientKey, clientCertificate, rootCertificate use RemoteRuleConfig.getRelevantConfig(id, configs) to load this in Language::getRelevantRemoteRulesid- ID of ruledescriptionKey- key in MessageBundle.properties for rule descriptionmessagesByID- mapping match.sub_id -> key in MessageBundle.properties for RuleMatch's message- Returns:
- instance of RemoteMLRule
-
create
public static GRPCRule create(Language language, RemoteRuleConfig config, boolean inputLogging, String id, String description, Map<String, String> messagesByID) Helper method to create instances of RemoteMLRule- Parameters:
language- rule languageconfig- configuration for remote rule server; options: secure, clientKey, clientCertificate, rootCertificate use RemoteRuleConfig.getRelevantConfig(id, configs) to load this in Language::getRelevantRemoteRulesid- ID of ruledescription- rule descriptionmessagesByID- mapping match.sub_id to RuleMatch's message- Returns:
- instance of RemoteMLRule
-
createAll
-
createAll
public static List<GRPCRule> createAll(Language language, List<RemoteRuleConfig> configs, boolean inputLogging)
-