use of org.languagetool.tools.ContextTools in project languagetool by languagetool-org.
the class Searcher method getContextTools.
private static ContextTools getContextTools(int contextSize) {
ContextTools contextTools = new ContextTools();
contextTools.setEscapeHtml(false);
contextTools.setContextSize(contextSize);
contextTools.setErrorMarkerStart("**");
contextTools.setErrorMarkerEnd("**");
return contextTools;
}
use of org.languagetool.tools.ContextTools in project languagetool by languagetool-org.
the class CommandLineTools method printMatches.
/**
* Displays matches in a simple text format.
* @param ruleMatches Matches from rules.
* @param prevMatches Number of previously found matches.
* @param contents The text that was checked.
* @param contextSize The size of contents displayed.
* @since 1.0.1
*/
private static void printMatches(List<RuleMatch> ruleMatches, int prevMatches, String contents, int contextSize) {
int i = 1;
ContextTools contextTools = new ContextTools();
contextTools.setContextSize(contextSize);
for (RuleMatch match : ruleMatches) {
Rule rule = match.getRule();
String output = i + prevMatches + ".) Line " + (match.getLine() + 1) + ", column " + match.getColumn() + ", Rule ID: " + rule.getId();
if (rule instanceof AbstractPatternRule) {
AbstractPatternRule pRule = (AbstractPatternRule) rule;
if (pRule.getSubId() != null) {
output += "[" + pRule.getSubId() + "]";
}
}
System.out.println(output);
String msg = match.getMessage();
msg = msg.replaceAll("</?suggestion>", "'");
System.out.println("Message: " + msg);
List<String> replacements = match.getSuggestedReplacements();
if (!replacements.isEmpty()) {
System.out.println("Suggestion: " + String.join("; ", replacements));
}
System.out.println(contextTools.getPlainTextContext(match.getFromPos(), match.getToPos(), contents));
if (rule.getUrl() != null) {
System.out.println("More info: " + rule.getUrl());
}
if (i < ruleMatches.size()) {
System.out.println();
}
i++;
}
}
use of org.languagetool.tools.ContextTools in project languagetool by languagetool-org.
the class Searcher method main.
public static void main(String[] args) throws Exception {
ensureCorrectUsageOrExit(args);
long startTime = System.currentTimeMillis();
String[] ruleIds = args[0].split(",");
String languageCode = args[1];
Language language = Languages.getLanguageForShortCode(languageCode);
File indexDir = new File(args[2]);
boolean limitSearch = !(args.length > 3 && "--no_limit".equals(args[3]));
Searcher searcher = new Searcher(new SimpleFSDirectory(indexDir.toPath()));
if (!limitSearch) {
searcher.setMaxHits(100_000);
}
searcher.limitSearch = limitSearch;
ContextTools contextTools = getContextTools(140);
int totalMatches = 0;
for (String ruleId : ruleIds) {
long ruleStartTime = System.currentTimeMillis();
for (PatternRule rule : searcher.getRuleById(ruleId, language)) {
System.out.println("===== " + rule.getFullId() + " =========================================================");
SearcherResult searcherResult = searcher.findRuleMatchesOnIndex(rule, language);
int i = 1;
if (searcherResult.getMatchingSentences().size() == 0) {
System.out.println("[no matches]");
}
for (MatchingSentence ruleMatch : searcherResult.getMatchingSentences()) {
for (RuleMatch match : ruleMatch.getRuleMatches()) {
String context = contextTools.getContext(match.getFromPos(), match.getToPos(), ruleMatch.getSentence());
if (WIKITEXT_OUTPUT) {
ContextTools contextTools2 = getContextTools(0);
String coveredText = contextTools2.getContext(match.getFromPos(), match.getToPos(), ruleMatch.getSentence());
coveredText = coveredText.replaceFirst("^\\.\\.\\.", "").replaceFirst("\\.\\.\\.$", "");
coveredText = coveredText.replaceFirst("^\\*\\*", "").replaceFirst("\\*\\*$", "");
String encodedTextWithQuotes = URLEncoder.encode("\"" + coveredText + "\"", "UTF-8");
String searchLink = "https://de.wikipedia.org/w/index.php?search=" + encodedTextWithQuotes + "&title=Spezial%3ASuche&go=Artikel";
context = context.replaceAll("\\*\\*.*?\\*\\*", "[" + searchLink + " " + coveredText + "]");
String encTitle = URLEncoder.encode(ruleMatch.getTitle(), "UTF-8");
String encodedText = URLEncoder.encode(coveredText, "UTF-8");
System.out.println("# [[" + ruleMatch.getTitle() + "]]: " + context + " ([http://wikipedia.ramselehof.de/wikiblame.php?user_lang=de&lang=de&project=wikipedia&article=" + encTitle + "&needle=" + encodedText + "&skipversions=0&ignorefirst=0&limit=500&searchmethod=int&order=desc&start=Start WikiBlame])");
} else {
System.out.println(i + ": " + context + " [" + ruleMatch.getSource() + "]");
}
}
totalMatches += ruleMatch.getRuleMatches().size();
i++;
}
System.out.println("Time: " + (System.currentTimeMillis() - ruleStartTime) + "ms");
}
}
System.out.println("Total time: " + (System.currentTimeMillis() - startTime) + "ms, " + totalMatches + " matches");
}
use of org.languagetool.tools.ContextTools in project languagetool by languagetool-org.
the class ResultAreaHelper method getRuleMatchHtml.
private void getRuleMatchHtml(List<RuleMatch> ruleMatches, String text) {
ContextTools contextTools = new ContextTools();
StringBuilder sb = new StringBuilder(200);
if (ltSupport.getLanguage().getMaintainedState() != LanguageMaintainedState.ActivelyMaintained) {
sb.append("<p><b>").append(messages.getString("unsupportedWarning")).append("</b></p>\n");
} else {
sb.append(EMPTY_PARA);
}
setMain(sb.toString());
sb.setLength(0);
int i = 0;
for (RuleMatch match : ruleMatches) {
sb.append("<p>");
String output = org.languagetool.tools.Tools.i18n(messages, "result1", i + 1, match.getLine() + 1, match.getColumn());
sb.append(output);
String msg = match.getMessage().replaceAll("<suggestion>", "<b>").replaceAll("</suggestion>", "</b>").replaceAll("<old>", "<b>").replaceAll("</old>", "</b>");
sb.append("<b>").append(messages.getString("errorMessage")).append("</b> ");
sb.append(msg);
RuleLink ruleLink = RuleLink.buildDeactivationLink(match.getRule());
sb.append(" <a href=\"").append(ruleLink).append("\">").append(messages.getString("deactivateRule")).append("</a><br>\n");
if (match.getSuggestedReplacements().size() > 0) {
String replacement = String.join("; ", match.getSuggestedReplacements());
sb.append("<b>").append(messages.getString("correctionMessage")).append("</b> ").append(replacement).append("<br>\n");
}
if (ITSIssueType.Misspelling == match.getRule().getLocQualityIssueType()) {
contextTools.setErrorMarkerStart(SPELL_ERROR_MARKER_START);
} else {
contextTools.setErrorMarkerStart(LT_ERROR_MARKER_START);
}
String context = contextTools.getContext(match.getFromPos(), match.getToPos(), text);
sb.append("<b>").append(messages.getString("errorContext")).append("</b> ").append(context);
if (match.getRule().getUrl() != null && Desktop.isDesktopSupported()) {
sb.append("<br>\n");
sb.append("<b>").append(messages.getString("moreInfo")).append("</b> <a href=\"");
String url = match.getRule().getUrl().toString();
sb.append(url);
String shortUrl = StringUtils.abbreviate(url, 60);
sb.append("\">").append(shortUrl).append("</a>\n");
}
sb.append("</p>");
i++;
appendMain(sb.toString());
sb.setLength(0);
}
sb.append("<p class=\"grayed\">");
sb.append(getDisabledRulesHtml());
String checkDone = org.languagetool.tools.Tools.i18n(messages, "checkDone", ruleMatches.size(), runTime);
sb.append("<br>\n").append(checkDone);
sb.append("<br>\n").append(messages.getString("makeLanguageToolBetter"));
sb.append("<br>\n");
sb.append("</p>");
appendMain(sb.toString());
}
Aggregations