Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a cache for better performance in the server use case, where sent…
…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
1 parent
969e716
commit a883822
Showing
15 changed files
with
429 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
languagetool-core/src/main/java/org/languagetool/InputSentence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
languagetool-core/src/main/java/org/languagetool/ResultCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
} |
48 changes: 48 additions & 0 deletions
48
languagetool-core/src/test/java/org/languagetool/InputSentenceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.