Skip to content

Commit

Permalink
[LO extension] extensive optimization to realize full text check; sol…
Browse files Browse the repository at this point in the history
…ves #194
  • Loading branch information
FredKruse committed Jul 2, 2019
1 parent ef48d02 commit 92270c5
Show file tree
Hide file tree
Showing 23 changed files with 428 additions and 56 deletions.
Expand Up @@ -189,4 +189,9 @@ else if ((!isDirectSpeech || minPercent == 0) && !token.isWhitespace() && !token
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 0;
}

}
Expand Up @@ -319,4 +319,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return maxDistanceOfSentences;
}

}
Expand Up @@ -93,4 +93,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return -1;
}

}
Expand Up @@ -86,4 +86,9 @@ public org.languagetool.rules.RuleMatch[] match(List<AnalyzedSentence> sentences
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 1;
}

}
Expand Up @@ -293,4 +293,9 @@ private String findCorrespondingSymbol(String symbol) {
}
}

@Override
public int minToCheckParagraph() {
return -1;
}

}
Expand Up @@ -146,4 +146,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 0;
}

}
Expand Up @@ -93,4 +93,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 0;
}

}
Expand Up @@ -140,5 +140,10 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 1;
}

}

Expand Up @@ -138,4 +138,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 0;
}

}
Expand Up @@ -331,5 +331,10 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(new ArrayList<>());
}
}

@Override
public int minToCheckParagraph() {
return 0;
}

}
Expand Up @@ -88,5 +88,10 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
}
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 0;
}

}
Expand Up @@ -67,5 +67,18 @@ public int estimateContextForSureMatch() {
public final RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
throw new RuntimeException("Not implemented for a text-level rule");
}

/**
* Gives back the minimum number of paragraphs to check to give back a correct result
* only used by LO office extension
* return n;
* n == -1 --> need to check full text (use only if really needed / bad performance)
* examples: AbstractWordCoherencyRule, GenericUnpairedBracketsRule, ...
* n == 0 --> need only to check the current paragraph
* examples: MultipleWhitespaceRule, LongParagraphRule, ...
* n >= 1 --> need only to check n paragraphs around the current paragraph
* examples: ParagraphRepeatBeginningRule (n == 1), WordRepeatBeginningRule (n == 2), ...
*/
public abstract int minToCheckParagraph();

}
Expand Up @@ -188,4 +188,9 @@ private boolean isSentenceEnd(String word) {
private boolean isQuoteStart(String word) {
return StringUtils.equalsAny(word, "\"", "'", "„", "»", "«", "“", "‘");
}

@Override
public int minToCheckParagraph() {
return 0;
}
}
Expand Up @@ -89,4 +89,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 0;
}

}
Expand Up @@ -107,4 +107,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return 2;
}

}
Expand Up @@ -135,4 +135,9 @@ private String getLemma(AnalyzedTokenReadings atr) {
}
}

@Override
public int minToCheckParagraph() {
return -1;
}

}
Expand Up @@ -117,4 +117,9 @@ public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
return toRuleMatchArray(ruleMatches);
}

@Override
public int minToCheckParagraph() {
return -1;
}

}
Expand Up @@ -111,5 +111,10 @@ private String similarName(String nameHere, Set<String> namesSoFar) {
}
return null;
}

@Override
public int minToCheckParagraph() {
return -1;
}

}
Expand Up @@ -594,4 +594,9 @@ private BooleanAndFiniteVerb(boolean verbDoesMatchPersonAndNumber, AnalyzedToken
this.finiteVerb = finiteVerb;
}
}

@Override
public int minToCheckParagraph() {
return 0;
}
}
Expand Up @@ -270,5 +270,9 @@ public String getId() {
public String getDescription() {
return "Test rule";
}
@Override
public int minToCheckParagraph() {
return -1;
}
}
}
Expand Up @@ -34,6 +34,7 @@
import org.languagetool.gui.Configuration;
import org.languagetool.rules.CategoryId;
import org.languagetool.rules.Rule;
import org.languagetool.rules.TextLevelRule;
import org.languagetool.tools.Tools;

import com.sun.star.awt.XMenuBar;
Expand Down Expand Up @@ -78,6 +79,7 @@ public class MultiDocumentsHandler {
private final String configFile;
private Configuration config = null;
private LinguisticServices linguServices = null;
private SortedTextRules sortedTextRules;

private XComponentContext xContext; // The context of the document
private List<SingleDocument> documents; // The List of LO documents to be checked
Expand Down Expand Up @@ -367,7 +369,7 @@ private int getNumDoc(String docID) {
}
}
}
documents.add(new SingleDocument(xContext, config, docID, xComponent));
documents.add(new SingleDocument(xContext, config, docID, xComponent, this));
setMenuTextForSwitchOff(xContext);
if (debugMode) {
MessageHandler.printToLogFile("Document " + docNum + " created; docID = " + docID);
Expand Down Expand Up @@ -469,10 +471,12 @@ void initCheck() {
langTool.enableRule(ruleName);
}
}
recheck = false;
sortedTextRules = new SortedTextRules();
setConfigValues(config, langTool);
for (SingleDocument document : documents) {
document.resetCache();
}
recheck = false;
}

/**
Expand Down Expand Up @@ -573,5 +577,128 @@ public boolean toggleSwitchedOff() throws IOException {
return ret;
}

/**
* Returns a list of different numbers of paragraphs to check for text level rules
* (currently only -1 for full text check and n for max number for other text level rules)
*/
public List<Integer> getNumMinToCheckParas() {
return sortedTextRules.minToCheckParagraph;
}

/**
* activate all rules stored under a given index related to the list of getNumMinToCheckParas
* deactivate all other text level rules
*/
public void activateTextRulesByIndex(int index) {
sortedTextRules.activateTextRulesByIndex(index);
}

/**
* reactivate all text level rules
*/
public void reactivateTextRules() {
sortedTextRules.reactivateTextRules();;
}

/**
* class to store all text level rules sorted by the minimum to check paragraphs
* (currently only full text check and all other text level rules)
*
*/
class SortedTextRules {
List<Integer> minToCheckParagraph;
List<List<String>> textLevelRules;
SortedTextRules () {
minToCheckParagraph = new ArrayList<Integer>();
textLevelRules = new ArrayList<List<String>>();
List<Rule> rules = langTool.getAllActiveOfficeRules();
for(Rule rule : rules) {
if(rule instanceof TextLevelRule) {
insertRule(((TextLevelRule) rule).minToCheckParagraph(), rule.getId());
}
}
if(debugMode) {
MessageHandler.printToLogFile("Number different minToCheckParagraph: " + minToCheckParagraph.size());
for( int i = 0; i < minToCheckParagraph.size(); i++) {
MessageHandler.printToLogFile("minToCheckParagraph: " + minToCheckParagraph.get(i));
for (int j = 0; j < textLevelRules.get(i).size(); j++) {
MessageHandler.printToLogFile("RuleId: " + textLevelRules.get(i).get(j));
}
}
}
}

private void insertRule (int minPara, String RuleId) {
if(minPara < 0) {
int n = minToCheckParagraph.indexOf(minPara);
if( n >= 0) {
} else {
minToCheckParagraph.add(minPara);
textLevelRules.add(new ArrayList<String>());
}
textLevelRules.get(textLevelRules.size() - 1).add(new String(RuleId));
} else {
int n = minToCheckParagraph.indexOf(-1);
if( n == 0 || minToCheckParagraph.size() == 0) {
minToCheckParagraph.add(0, minPara);
textLevelRules.add(0, new ArrayList<String>());
} else if(minPara > minToCheckParagraph.get(0)) {
minToCheckParagraph.set(0, minPara);
}
textLevelRules.get(0).add(new String(RuleId));
}
}
/*
* This rule was commented out for performance reasons and replaced by the same named rule before
*
private void insertRule (int minPara, String RuleId) {
int n = minToCheckParagraph.indexOf(minPara);
if( n >= 0) {
textLevelRules.get(n).add(new String(RuleId));
return;
} else {
if(minPara < 0) {
n = minToCheckParagraph.size();
} else {
for (n = 0; n < minToCheckParagraph.size(); n++) {
if(minPara < minToCheckParagraph.get(n) || minToCheckParagraph.get(n) < 0) {
break;
}
}
}
minToCheckParagraph.add(n, minPara);
textLevelRules.add(n, new ArrayList<String>());
textLevelRules.get(n).add(new String(RuleId));
}
}
*/
public List<Integer> getMinToCheckParas() {
return minToCheckParagraph;
}

public void activateTextRulesByIndex(int index) {
for(int i = 0; i < textLevelRules.size(); i++) {
if(i == index) {
for (String ruleId : textLevelRules.get(i)) {
langTool.enableRule(ruleId);
}
} else {
for (String ruleId : textLevelRules.get(i)) {
langTool.disableRule(ruleId);
}
}
}
}

public void reactivateTextRules() {
for(List<String> textRules : textLevelRules) {
for (String ruleId : textRules) {
langTool.enableRule(ruleId);
}
}
}

}


}

0 comments on commit 92270c5

Please sign in to comment.