Skip to content

Commit

Permalink
add a cache for better performance in the server use case, where sent…
Browse files Browse the repository at this point in the history
…ences often get checked more than once (e.g. due to corrections of other sentences, which cause a re-check of the whole text)
  • Loading branch information
danielnaber committed Feb 6, 2017
1 parent 969e716 commit a883822
Show file tree
Hide file tree
Showing 15 changed files with 429 additions and 26 deletions.
5 changes: 5 additions & 0 deletions languagetool-core/pom.xml
Expand Up @@ -91,6 +91,11 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>3.5</version> <version>3.5</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency> <dependency>
<groupId>net.java.dev.jna</groupId> <groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId> <artifactId>jna</artifactId>
Expand Down
@@ -0,0 +1,72 @@
/* LanguageTool, a natural language style checker
* Copyright (C) 2017 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool;

import org.languagetool.rules.CategoryId;

import java.util.Objects;
import java.util.Set;

/**
* For internal use only. Used as a key for caching check results.
* @since 3.7
*/
public class InputSentence {

private final String text;
private final Language lang;
private final Language motherTongue;
private final Set<String> disabledRules;
private final Set<CategoryId> disabledRuleCategories;
private final Set<String> enabledRules;
private final Set<CategoryId> enabledRuleCategories;

public InputSentence(String text, Language lang, Language motherTongue,
Set<String> disabledRules, Set<CategoryId> disabledRuleCategories,
Set<String> enabledRules, Set<CategoryId> enabledRuleCategories) {
this.text = Objects.requireNonNull(text);
this.lang = Objects.requireNonNull(lang);
this.motherTongue = motherTongue;
this.disabledRules = disabledRules;
this.disabledRuleCategories = disabledRuleCategories;
this.enabledRules = enabledRules;
this.enabledRuleCategories = enabledRuleCategories;
}

@Override
public boolean equals(Object o) {
if (o == null) return false;
if (o == this) return true;
if (o.getClass() != getClass()) return false;
InputSentence other = (InputSentence) o;
return Objects.equals(text, other.text) &&
Objects.equals(lang, other.lang) &&
Objects.equals(motherTongue, other.motherTongue) &&
Objects.equals(disabledRules, other.disabledRules) &&
Objects.equals(disabledRuleCategories, other.disabledRuleCategories) &&
Objects.equals(enabledRules, other.enabledRules) &&
Objects.equals(enabledRuleCategories, other.enabledRuleCategories);
}

@Override
public int hashCode() {
return Objects.hash(text, lang, motherTongue, disabledRules, disabledRuleCategories, enabledRules, enabledRuleCategories);
}

}
Expand Up @@ -81,6 +81,8 @@ public class JLanguageTool {
/** Name of the message bundle for translations. */ /** Name of the message bundle for translations. */
public static final String MESSAGE_BUNDLE = "org.languagetool.MessagesBundle"; public static final String MESSAGE_BUNDLE = "org.languagetool.MessagesBundle";


private final ResultCache cache;

/** /**
* Returns the build date or {@code null} if not run from JAR. * Returns the build date or {@code null} if not run from JAR.
*/ */
Expand Down Expand Up @@ -140,15 +142,27 @@ public enum ParagraphHandling {
} }


private static final List<File> temporaryFiles = new ArrayList<>(); private static final List<File> temporaryFiles = new ArrayList<>();


/**
* Create a JLanguageTool and setup the built-in rules for the
* given language and false friend rules for the text language / mother tongue pair.
*
* @param lang the language of the text to be checked
* @param motherTongue the user's mother tongue, used for false friend rules, or <code>null</code>.
* The mother tongue may also be used as a source language for checking bilingual texts.
*/
public JLanguageTool(Language lang, Language motherTongue) {
this(lang, motherTongue, null);
}

/** /**
* Create a JLanguageTool and setup the built-in Java rules for the * Create a JLanguageTool and setup the built-in Java rules for the
* given language. * given language.
* *
* @param language the language of the text to be checked * @param language the language of the text to be checked
*/ */
public JLanguageTool(Language language) { public JLanguageTool(Language language) {
this(language, null); this(language, null, null);
} }


/** /**
Expand All @@ -158,8 +172,12 @@ public JLanguageTool(Language language) {
* @param language the language of the text to be checked * @param language the language of the text to be checked
* @param motherTongue the user's mother tongue, used for false friend rules, or <code>null</code>. * @param motherTongue the user's mother tongue, used for false friend rules, or <code>null</code>.
* The mother tongue may also be used as a source language for checking bilingual texts. * The mother tongue may also be used as a source language for checking bilingual texts.
* @param cache a cache to speed up checking if the same sentences get checked more than once,
* e.g. when LT is running as a server and texts are re-checked due to changes
* @since 3.7
*/ */
public JLanguageTool(Language language, Language motherTongue) { @Experimental
public JLanguageTool(Language language, Language motherTongue, ResultCache cache) {
this.language = Objects.requireNonNull(language, "language cannot be null"); this.language = Objects.requireNonNull(language, "language cannot be null");
this.motherTongue = motherTongue; this.motherTongue = motherTongue;
ResourceBundle messages = ResourceBundleTools.getMessageBundle(language); ResourceBundle messages = ResourceBundleTools.getMessageBundle(language);
Expand All @@ -171,6 +189,7 @@ public JLanguageTool(Language language, Language motherTongue) {
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Could not activate rules", e); throw new RuntimeException("Could not activate rules", e);
} }
this.cache = cache;
} }


/** /**
Expand Down Expand Up @@ -935,10 +954,21 @@ public List<RuleMatch> call() throws Exception {
for (AnalyzedSentence analyzedSentence : analyzedSentences) { for (AnalyzedSentence analyzedSentence : analyzedSentences) {
String sentence = sentences.get(i++); String sentence = sentences.get(i++);
try { try {
List<RuleMatch> sentenceMatches = List<RuleMatch> sentenceMatches = null;
checkAnalyzedSentence(paraMode, rules, charCount, lineCount, InputSentence cacheKey = null;
columnCount, sentence, analyzedSentence, annotatedText); if (cache != null) {

cacheKey = new InputSentence(analyzedSentence.getText(), language, motherTongue,
disabledRules, disabledRuleCategories,
enabledRules, enabledRuleCategories);
sentenceMatches = cache.getIfPresent(cacheKey);
}
if (sentenceMatches == null) {
sentenceMatches = checkAnalyzedSentence(paraMode, rules, charCount, lineCount,
columnCount, sentence, analyzedSentence, annotatedText);
}
if (cache != null) {
cache.put(cacheKey, sentenceMatches);
}
ruleMatches.addAll(sentenceMatches); ruleMatches.addAll(sentenceMatches);
charCount += sentence.length(); charCount += sentence.length();
lineCount += countLineBreaks(sentence); lineCount += countLineBreaks(sentence);
Expand Down
15 changes: 15 additions & 0 deletions languagetool-core/src/main/java/org/languagetool/Language.java
Expand Up @@ -463,4 +463,19 @@ public int getPriorityForId(String id) {
return 0; return 0;
} }


/**
* Considers languages as equal if their language code, including the country and variant codes are equal.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Language other = (Language) o;
return Objects.equals(getShortCodeWithCountryAndVariant(), other.getShortCodeWithCountryAndVariant());
}

@Override
public int hashCode() {
return getShortCodeWithCountryAndVariant().hashCode();
}
} }
Expand Up @@ -73,7 +73,26 @@ public MultiThreadedJLanguageTool(Language language, Language motherTongue) {
* @since 2.9 * @since 2.9
*/ */
public MultiThreadedJLanguageTool(Language language, Language motherTongue, int threadPoolSize) { public MultiThreadedJLanguageTool(Language language, Language motherTongue, int threadPoolSize) {
super(language, motherTongue); this(language, motherTongue, threadPoolSize, null);
}

/**
* @see #shutdown()
* @since 3.7
*/
@Experimental
public MultiThreadedJLanguageTool(Language language, Language motherTongue, ResultCache cache) {
this(language, motherTongue, getDefaultThreadCount(), cache);
}

/**
* @see #shutdown()
* @param threadPoolSize the number of concurrent threads
* @since 3.7
*/
@Experimental
public MultiThreadedJLanguageTool(Language language, Language motherTongue, int threadPoolSize, ResultCache cache) {
super(language, motherTongue, cache);
if (threadPoolSize < 1) { if (threadPoolSize < 1) {
throw new IllegalArgumentException("threadPoolSize must be >= 1: " + threadPoolSize); throw new IllegalArgumentException("threadPoolSize must be >= 1: " + threadPoolSize);
} }
Expand Down
71 changes: 71 additions & 0 deletions languagetool-core/src/main/java/org/languagetool/ResultCache.java
@@ -0,0 +1,71 @@
/* LanguageTool, a natural language style checker
* Copyright (C) 2017 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;
import org.languagetool.rules.RuleMatch;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* A cache to speed up text checking for use cases where sentences are checked more than once. This
* typically happens when using LT as a server and texts get re-checked after corrections have been applied
* for some sentences. Use the same cache object for all {@link JLanguageTool} objects <strong>only if
* the JLanguageTool objects all use the same rules.</strong> For example, if you call {@code JLanguageTool.addRule()}
* in different ways for the different instances that you use the same cache for, the cache will return invalid results.
* It is okay however, to use same same cache for {@link JLanguageTool} objects with different languages, as
* cached results are not used for a different language.
* @since 3.7
*/
@Experimental
public class ResultCache {

private final Cache<InputSentence, List<RuleMatch>> cache;

/**
* Create a cache that expires items 5 minutes after the latest read access.
* @param maxSize maximum cache size in number of sentences
*/
public ResultCache(long maxSize) {
cache = CacheBuilder.newBuilder().maximumSize(maxSize).recordStats().expireAfterAccess(5, TimeUnit.MINUTES).build();
}

/**
* @param maxSize maximum cache size in number of sentences
* @param expireAfter time to expire sentences from the cache after last read access
*/
public ResultCache(long maxSize, int expireAfter, TimeUnit timeUnit) {
cache = CacheBuilder.newBuilder().maximumSize(maxSize).recordStats().expireAfterAccess(expireAfter, timeUnit).build();
}

public CacheStats stats() {
return cache.stats();
}

public List<RuleMatch> getIfPresent(InputSentence key) {
return cache.getIfPresent(key);
}

public void put(InputSentence key, List<RuleMatch> sentenceMatches) {
cache.put(key, sentenceMatches);
}
}
@@ -0,0 +1,48 @@
/* LanguageTool, a natural language style checker
* Copyright (C) 2017 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool;

import org.junit.Test;
import org.languagetool.rules.CategoryId;

import java.util.Arrays;
import java.util.HashSet;

import static org.junit.Assert.*;

public class InputSentenceTest {

@Test
public void test() {
Language lang = Languages.getLanguageForShortCode("xx-XX");
InputSentence inputSentence1a = new InputSentence("foo", lang, lang,
new HashSet<>(Arrays.asList("ID1")), new HashSet<>(Arrays.asList(new CategoryId("C1"))),
new HashSet<>(Arrays.asList("ID2")), new HashSet<>(Arrays.asList(new CategoryId("C2"))));
InputSentence inputSentence1b = new InputSentence("foo", lang, lang,
new HashSet<>(Arrays.asList("ID1")), new HashSet<>(Arrays.asList(new CategoryId("C1"))),
new HashSet<>(Arrays.asList("ID2")), new HashSet<>(Arrays.asList(new CategoryId("C2"))));
assertEquals(inputSentence1a, inputSentence1b);
InputSentence inputSentence2 = new InputSentence("foo", lang, null,
new HashSet<>(Arrays.asList("ID1")), new HashSet<>(Arrays.asList(new CategoryId("C1"))),
new HashSet<>(Arrays.asList("ID2")), new HashSet<>(Arrays.asList(new CategoryId("C2"))));
assertNotEquals(inputSentence1a, inputSentence2);
assertNotEquals(inputSentence1b, inputSentence2);
}

}
Expand Up @@ -20,9 +20,7 @@


import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.languagetool.JLanguageTool; import org.languagetool.*;
import org.languagetool.Language;
import org.languagetool.Languages;
import org.languagetool.gui.Configuration; import org.languagetool.gui.Configuration;
import org.languagetool.language.LanguageIdentifier; import org.languagetool.language.LanguageIdentifier;
import org.languagetool.rules.CategoryId; import org.languagetool.rules.CategoryId;
Expand Down Expand Up @@ -58,16 +56,19 @@ abstract class TextChecker {
protected final HTTPServerConfig config; protected final HTTPServerConfig config;


private static final String ENCODING = "UTF-8"; private static final String ENCODING = "UTF-8";
private static final int CACHE_STATS_PRINT = 500; // print cache stats every n cache requests


private final boolean internalServer; private final boolean internalServer;
private final LanguageIdentifier identifier; private final LanguageIdentifier identifier;
private final ExecutorService executorService; private final ExecutorService executorService;
private final ResultCache cache;


TextChecker(HTTPServerConfig config, boolean internalServer) { TextChecker(HTTPServerConfig config, boolean internalServer) {
this.config = config; this.config = config;
this.internalServer = internalServer; this.internalServer = internalServer;
this.identifier = new LanguageIdentifier(); this.identifier = new LanguageIdentifier();
this.executorService = Executors.newCachedThreadPool(); this.executorService = Executors.newCachedThreadPool();
this.cache = new ResultCache(1000);
} }


void shutdownNow() { void shutdownNow() {
Expand Down Expand Up @@ -171,6 +172,12 @@ protected void checkParams(Map<String, String> parameters) {
private List<RuleMatch> getRuleMatches(String text, Map<String, String> parameters, Language lang, private List<RuleMatch> getRuleMatches(String text, Map<String, String> parameters, Language lang,
Language motherTongue, QueryParams params) throws Exception { Language motherTongue, QueryParams params) throws Exception {
String sourceText = parameters.get("srctext"); String sourceText = parameters.get("srctext");
long cacheRequests = cache.stats().requestCount();
if (cacheRequests > 0 && cacheRequests % CACHE_STATS_PRINT == 0) {
double hitRate = cache.stats().hitRate();
String hitPercentage = String.format(Locale.ENGLISH, "%.2f", hitRate * 100.0f);
print("Cache stats: " + hitPercentage + "% hit rate, " + cache.stats());
}
if (sourceText == null) { if (sourceText == null) {
JLanguageTool lt = getLanguageToolInstance(lang, motherTongue, params); JLanguageTool lt = getLanguageToolInstance(lang, motherTongue, params);
return lt.check(text); return lt.check(text);
Expand Down Expand Up @@ -242,7 +249,7 @@ Language detectLanguageOfString(String text, String fallbackLanguage, List<Strin
* @param motherTongue the user's mother tongue or {@code null} * @param motherTongue the user's mother tongue or {@code null}
*/ */
private JLanguageTool getLanguageToolInstance(Language lang, Language motherTongue, QueryParams params) throws Exception { private JLanguageTool getLanguageToolInstance(Language lang, Language motherTongue, QueryParams params) throws Exception {
JLanguageTool lt = new JLanguageTool(lang, motherTongue); JLanguageTool lt = new JLanguageTool(lang, motherTongue, cache);
if (config.getLanguageModelDir() != null) { if (config.getLanguageModelDir() != null) {
lt.activateLanguageModelRules(config.getLanguageModelDir()); lt.activateLanguageModelRules(config.getLanguageModelDir());
} }
Expand Down

0 comments on commit a883822

Please sign in to comment.