use of org.languagetool.rules.IncorrectExample in project languagetool by languagetool-org.
the class AfterTheDeadlineEvaluator method run.
private void run(Language lang) throws IOException, InterruptedException {
List<Rule> rules = getRules(lang);
int sentenceCount = 0;
int errorFoundCount = 0;
System.out.println("Starting test for " + lang.getName() + " on " + urlPrefix);
System.out.println("Wait time between HTTP requests: " + WAIT_TIME_MILLIS + "ms");
System.out.println("Starting test on " + rules.size() + " rules");
for (Rule rule : rules) {
if (rule.isDefaultOff()) {
System.out.println("Skipping rule that is off by default: " + rule.getId());
continue;
}
List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
System.out.println("\n" + rule.getId() + ":");
if (incorrectExamples.size() == 0) {
System.out.println(" (no examples)");
continue;
}
for (IncorrectExample example : incorrectExamples) {
boolean match = queryAtDServer(example);
sentenceCount++;
if (match) {
errorFoundCount++;
}
String marker = match ? "+" : "-";
System.out.println(" [" + marker + "] " + example.getExample().replace("<marker>", "<m>").replace("</marker>", "</m>"));
Thread.sleep(WAIT_TIME_MILLIS);
}
//use this to stop: if (sentenceCount > 100) { break; }
}
System.out.println("\nDone.");
System.out.println("Sentence count: " + sentenceCount);
float percentage = (float) errorFoundCount / sentenceCount * 100;
System.out.printf("Expected errors found: " + errorFoundCount + " (%.2f%%)\n", percentage);
}
use of org.languagetool.rules.IncorrectExample in project languagetool by languagetool-org.
the class AfterTheDeadlineEvaluatorTest method testIsExpectedErrorFound.
@Test
public void testIsExpectedErrorFound() throws XPathExpressionException {
AfterTheDeadlineEvaluator evaluator = new AfterTheDeadlineEvaluator("fake");
IncorrectExample example = new IncorrectExample("This <marker>is is</marker> a test");
assertTrue(evaluator.isExpectedErrorFound(example, "<results><error><string>is is</string></error></results>"));
assertFalse(evaluator.isExpectedErrorFound(example, "<results><error><string>This is</string></error></results>"));
assertFalse(evaluator.isExpectedErrorFound(example, "<results></results>"));
assertTrue(evaluator.isExpectedErrorFound(example, "<results>" + "<error><string>foo bar</string></error>" + "<error><string>is is</string></error>" + "</results>"));
}
use of org.languagetool.rules.IncorrectExample in project languagetool by languagetool-org.
the class UselessExampleFinder method run.
private void run(Language lang) throws IOException {
File basePath = new File("/lt/git/languagetool/languagetool-language-modules");
if (!basePath.exists()) {
throw new RuntimeException("basePath does not exist: " + basePath);
}
String langCode = lang.getShortCode();
File xml = new File(basePath, "/" + langCode + "/src/main/resources/org/languagetool/rules/" + langCode + "/grammar.xml");
List<String> xmlLines = IOUtils.readLines(new FileReader(xml));
JLanguageTool tool = new JLanguageTool(lang);
for (Rule rule : tool.getAllActiveRules()) {
if (!(rule instanceof PatternRule)) {
continue;
}
List<CorrectExample> correctExamples = rule.getCorrectExamples();
List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
for (IncorrectExample incorrectExample : incorrectExamples) {
checkCorrections(rule, correctExamples, incorrectExample, xmlLines);
}
}
System.err.println("Useless examples: " + uselessExampleCount);
System.err.println("Removed lines: " + removedLinesCount);
for (String xmlLine : xmlLines) {
System.out.println(xmlLine);
}
}
use of org.languagetool.rules.IncorrectExample in project languagetool by languagetool-org.
the class FalseFriendRuleHandler method endElement.
@Override
public void endElement(String namespaceURI, String sName, String qName) throws SAXException {
switch(qName) {
case RULE:
if (language.equalsConsiderVariantsIfSpecified(textLanguage) && translationLanguage != null && translationLanguage.equalsConsiderVariantsIfSpecified(motherTongue) && language != motherTongue && !translations.isEmpty()) {
formatter.applyPattern(messages.getString("false_friend_hint"));
String tokensAsString = StringUtils.join(patternTokens, " ").replace('|', '/');
Object[] messageArguments = { tokensAsString, messages.getString(textLanguage.getShortCode()), formatTranslations(translations), messages.getString(motherTongue.getShortCode()) };
String description = formatter.format(messageArguments);
PatternRule rule = new FalseFriendPatternRule(id, language, patternTokens, messages.getString("false_friend_desc") + " " + tokensAsString, description, messages.getString("false_friend"));
rule.setCorrectExamples(correctExamples);
rule.setIncorrectExamples(incorrectExamples);
rule.setCategory(Categories.FALSE_FRIENDS.getCategory(messages));
if (defaultOff) {
rule.setDefaultOff();
}
rules.add(rule);
}
if (patternTokens != null) {
patternTokens.clear();
}
break;
case TOKEN:
finalizeTokens();
break;
case PATTERN:
inPattern = false;
break;
case TRANSLATION:
if (currentTranslationLanguage != null && currentTranslationLanguage.equalsConsiderVariantsIfSpecified(motherTongue)) {
// currentTranslationLanguage can be null if the language is not supported
translations.add(translation);
}
if (currentTranslationLanguage != null && currentTranslationLanguage.equalsConsiderVariantsIfSpecified(textLanguage) && language.equalsConsiderVariantsIfSpecified(motherTongue)) {
suggestions.add(translation.toString());
}
translation = new StringBuilder();
inTranslation = false;
currentTranslationLanguage = null;
break;
case EXAMPLE:
if (inCorrectExample) {
correctExamples.add(new CorrectExample(correctExample.toString()));
} else if (inIncorrectExample) {
incorrectExamples.add(new IncorrectExample(incorrectExample.toString()));
}
inCorrectExample = false;
inIncorrectExample = false;
correctExample = new StringBuilder();
incorrectExample = new StringBuilder();
break;
case MESSAGE:
inMessage = false;
break;
case RULEGROUP:
if (!suggestions.isEmpty()) {
List<String> l = new ArrayList<>(suggestions);
suggestionMap.put(id, l);
suggestions.clear();
}
inRuleGroup = false;
break;
}
}
use of org.languagetool.rules.IncorrectExample in project languagetool by languagetool-org.
the class BitextPatternRuleHandler method setExample.
private IncorrectExample setExample() {
IncorrectExample example = null;
if (inCorrectExample) {
example = new IncorrectExample(correctExample.toString());
} else if (inIncorrectExample) {
String[] corrections = exampleCorrection.toString().split("\\|");
if (corrections.length > 0 && corrections[0].length() > 0) {
example = new IncorrectExample(incorrectExample.toString(), Arrays.asList(corrections));
} else {
example = new IncorrectExample(incorrectExample.toString());
}
} else if (inErrorTriggerExample) {
throw new RuntimeException("'triggers_error' is not supported for bitext XML");
}
correctExample = new StringBuilder();
incorrectExample = new StringBuilder();
exampleCorrection = new StringBuilder();
return example;
}
Aggregations