use of org.languagetool.rules.Rule in project languagetool by languagetool-org.
the class ExampleSentenceCorrectionCreator 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.getAllRules()) {
if (!(rule instanceof PatternRule)) {
continue;
}
List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
for (IncorrectExample incorrectExample : incorrectExamples) {
checkCorrections(rule, incorrectExample, xmlLines, tool);
}
}
System.err.println("Added corrections: " + addedCorrectionsCount);
for (String xmlLine : xmlLines) {
System.out.println(xmlLine);
}
}
use of org.languagetool.rules.Rule in project languagetool by languagetool-org.
the class ExampleSentencePrinter 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);
}
JLanguageTool tool = new JLanguageTool(lang);
System.out.println("<html>");
System.out.println("<head>");
System.out.println(" <title>LanguageTool examples sentences</title>");
System.out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
System.out.println("</head>");
System.out.println("<body>");
int i = 1;
for (Rule rule : tool.getAllRules()) {
List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
if (incorrectExamples.size() > 0) {
String example = incorrectExamples.get(0).getExample().replace("<marker>", "<b>").replace("</marker>", "</b>");
System.out.println(i + ". " + example + " [" + rule.getId() + "]<br>");
i++;
}
}
System.out.println("</body>");
System.out.println("</html>");
}
use of org.languagetool.rules.Rule in project languagetool by languagetool-org.
the class PatternRuleTest method getMatches.
private List<RuleMatch> getMatches(Rule rule, String sentence, JLanguageTool languageTool) throws IOException {
AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(sentence);
RuleMatch[] matches = rule.match(analyzedSentence);
if (CHECK_WITH_SENTENCE_SPLITTING) {
// "real check" with sentence splitting:
for (Rule r : languageTool.getAllActiveRules()) {
languageTool.disableRule(r.getId());
}
languageTool.enableRule(rule.getId());
List<RuleMatch> realMatches = languageTool.check(sentence);
List<String> realMatchRuleIds = new ArrayList<>();
for (RuleMatch realMatch : realMatches) {
realMatchRuleIds.add(realMatch.getRule().getId());
}
for (RuleMatch match : matches) {
String ruleId = match.getRule().getId();
if (!match.getRule().isDefaultOff() && !realMatchRuleIds.contains(ruleId)) {
System.err.println("WARNING: " + languageTool.getLanguage().getName() + ": missing rule match " + ruleId + " when splitting sentences for test sentence '" + sentence + "'");
}
}
}
return Arrays.asList(matches);
}
use of org.languagetool.rules.Rule in project languagetool by languagetool-org.
the class PatternRuleTest method validateRuleIds.
private void validateRuleIds(Language lang, JLanguageTool languageTool) {
List<Rule> allRules = languageTool.getAllRules();
Set<String> ids = new HashSet<>();
Set<Class> ruleClasses = new HashSet<>();
Set<String> categoryIds = new HashSet<>();
for (Rule rule : allRules) {
assertIdUniqueness(ids, ruleClasses, lang, rule);
if (rule.getId().equalsIgnoreCase("ID")) {
System.err.println("WARNING: " + lang.getShortCodeWithCountryAndVariant() + " has a rule with id 'ID', this should probably be changed");
}
Category category = rule.getCategory();
if (category != null && category.getId() != null) {
String catId = category.getId().toString();
if (!catId.matches("[A-Z0-9_-]+") && !categoryIds.contains(catId)) {
System.err.println("WARNING: category id '" + catId + "' doesn't match expected regexp [A-Z0-9_-]+");
categoryIds.add(catId);
}
}
}
}
use of org.languagetool.rules.Rule in project languagetool by languagetool-org.
the class SpellingCheckRuleTest method testIgnorePhrases.
@Test
public void testIgnorePhrases() throws IOException {
JLanguageTool langTool = new JLanguageTool(new AmericanEnglish());
assertThat(langTool.check("A test with myfoo mybar").size(), is(2));
for (Rule rule : langTool.getAllActiveRules()) {
if (rule instanceof SpellingCheckRule) {
((SpellingCheckRule) rule).acceptPhrases(Arrays.asList("myfoo mybar", "Myy othertest"));
} else {
langTool.disableRule(rule.getId());
}
}
assertThat(langTool.check("A test with myfoo mybar").size(), is(0));
// the words on their own are not ignored
assertThat(langTool.check("A test with myfoo and mybar").size(), is(2));
assertThat(langTool.check("myfoo mybar here").size(), is(0));
assertThat(langTool.check("Myfoo mybar here").size(), is(0));
assertThat(langTool.check("MYfoo mybar here").size(), is(2));
assertThat(langTool.check("Myy othertest is okay").size(), is(0));
assertThat(langTool.check("And Myy othertest is okay").size(), is(0));
assertThat(langTool.check("But Myy Othertest is not okay").size(), is(2));
assertThat(langTool.check("But myy othertest is not okay").size(), is(2));
}
Aggregations