Class RemoteRule

java.lang.Object
org.languagetool.rules.Rule
org.languagetool.rules.RemoteRule
Direct Known Subclasses:
BERTSuggestionRanking, GRPCRule, TestRemoteRule

public abstract class RemoteRule extends Rule
Since:
4.9
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • shutdownRoutines

      protected static final List<Runnable> shutdownRoutines
    • circuitBreakers

      protected static final ConcurrentMap<String,io.github.resilience4j.circuitbreaker.CircuitBreaker> circuitBreakers
    • serviceConfiguration

      protected final RemoteRuleConfig serviceConfiguration
    • premium

      protected final boolean premium
    • inputLogging

      protected final boolean inputLogging
    • filterMatches

      protected final boolean filterMatches
    • fixOffsets

      protected final boolean fixOffsets
    • whitespaceNormalisation

      protected final boolean whitespaceNormalisation
    • ruleLanguage

      protected final Language ruleLanguage
    • lt

      protected final JLanguageTool lt
    • suppressMisspelledMatch

      protected final Pattern suppressMisspelledMatch
    • suppressMisspelledSuggestions

      protected final Pattern suppressMisspelledSuggestions
  • Constructor Details

  • Method Details

    • shutdown

      public static void shutdown()
    • run

    • prepareRequest

      protected abstract RemoteRule.RemoteRequest prepareRequest(List<AnalyzedSentence> sentences, @Nullable Long textSessionId)
      run local preprocessing steps (or just store sentences)
      Parameters:
      sentences - text to process
      textSessionId - session ID for caching, partial rollout, A/B testing
      Returns:
      parameter for executeRequest/fallbackResults
    • executeRequest

      protected abstract Callable<RemoteRuleResult> executeRequest(RemoteRule.RemoteRequest request, long timeoutMilliseconds) throws TimeoutException
      Parameters:
      request - returned by prepareRequest
      timeoutMilliseconds - 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
    • fallbackResults

      protected abstract RemoteRuleResult fallbackResults(RemoteRule.RemoteRequest request)
      fallback if executeRequest times out or throws an error
      Parameters:
      request - returned by prepareRequest
      Returns:
      local results for this rule
    • createCircuitBreaker

      protected io.github.resilience4j.circuitbreaker.CircuitBreaker createCircuitBreaker(String id)
    • getCircuitBreakerConfig

      @NotNull static io.github.resilience4j.circuitbreaker.CircuitBreakerConfig getCircuitBreakerConfig(RemoteRuleConfig c, String id)
    • isPremium

      public boolean isPremium()
      Overrides:
      isPremium in class Rule
    • run

      public FutureTask<RemoteRuleResult> run(List<AnalyzedSentence> sentences, @Nullable Long textSessionId)
      Parameters:
      sentences - text to check
      textSessionId - ID for texts, should stay constant for a user session; used for A/B tests of experimental rules
      Returns:
      Future with result
    • getTimeout

      static long getTimeout(RemoteRuleConfig serviceConfiguration, long characters)
    • getTimeout

      public long getTimeout(long characters)
    • circuitBreaker

      public io.github.resilience4j.circuitbreaker.CircuitBreaker circuitBreaker()
    • suppressMisspelled

      private List<RuleMatch> suppressMisspelled(List<RuleMatch> sentenceMatches)
    • getId

      public String getId()
      Description copied from class: Rule
      A string used to identify the rule in e.g. configuration files. This string is supposed to be unique and to stay the same in all upcoming versions of LanguageTool. It's supposed to contain only the characters A-Z and the underscore.
      Specified by:
      getId in class Rule
    • match

      public RuleMatch[] match(AnalyzedSentence sentence) throws IOException
      Description copied from class: Rule
      Check whether the given sentence matches this error rule, i.e. whether it contains the error detected by this rule. Note that the order in which this method is called is not always guaranteed, i.e. the sentence order in the text may be different from the order in which you get the sentences (this may be the case when LanguageTool is used as a LibreOffice/OpenOffice add-on, for example). In other words, implementations must be stateless, so that a previous call to this method has no influence on later calls.
      Specified by:
      match in class Rule
      Parameters:
      sentence - a pre-analyzed sentence
      Returns:
      an array of RuleMatch objects
      Throws:
      IOException
    • getServiceConfiguration

      public RemoteRuleConfig getServiceConfiguration()
    • computeOffsetShifts

      static int[] computeOffsetShifts(String s)
    • fixMatchOffsets

      public static void fixMatchOffsets(AnalyzedSentence sentence, List<RuleMatch> matches)
      Adapt match positions so that results from languages that thread emojis, etc. as length 1 work for Java and match the normal offsets we use JavaScript also behaves like Java, so most clients will expect this behavior; but servers used for RemoteRules will often be written in Python (e.g. to access ML frameworks) based on offsetByCodePoints since codePointCount can be confusing, e.g. "👪".codePointCount(0,2) == 1, but length is 2 Java substring methods use this length (which can be >1 for a single character) whereas Python 3 indexing/slicing and len() in strings treat them as a single character so "😁foo".length() == 5, but len("😁foo") == 4; "😁foo".substring(2,5) == "foo" but "😁foo"[1:4] == 'foo'