use of org.languagetool.rules.bitext.BitextRule in project languagetool by languagetool-org.
the class Tools method getAllBuiltinBitextRules.
/**
* Use reflection to add bitext rules.
*/
private static List<BitextRule> getAllBuiltinBitextRules(Language language, ResourceBundle messages) {
List<BitextRule> rules = new ArrayList<>();
try {
List<Class<? extends BitextRule>> classes = BitextRule.getRelevantRules();
for (Class class1 : classes) {
Constructor[] constructors = class1.getConstructors();
boolean foundConstructor = false;
for (Constructor constructor : constructors) {
Class[] paramTypes = constructor.getParameterTypes();
if (paramTypes.length == 0) {
rules.add((BitextRule) constructor.newInstance());
foundConstructor = true;
break;
}
if (paramTypes.length == 1 && paramTypes[0].equals(ResourceBundle.class)) {
rules.add((BitextRule) constructor.newInstance(messages));
foundConstructor = true;
break;
}
if (paramTypes.length == 2 && paramTypes[0].equals(ResourceBundle.class) && paramTypes[1].equals(Language.class)) {
rules.add((BitextRule) constructor.newInstance(messages, language));
foundConstructor = true;
break;
}
}
if (!foundConstructor) {
throw new RuntimeException("Unknown constructor type for rule class " + class1.getName() + ", it supports only these constructors: " + Arrays.toString(constructors));
}
}
} catch (Exception e) {
throw new RuntimeException("Failed to load bitext rules", e);
}
return rules;
}
use of org.languagetool.rules.bitext.BitextRule in project languagetool by languagetool-org.
the class Tools method selectBitextRules.
/**
* Enable and disable bitext rules.
* @param bRules List of all bitext rules
* @param disabledRules ids of rules to be disabled
* @param enabledRules ids of rules to be enabled (by default all are enabled)
* @param useEnabledOnly if set to {@code true}, if set to {@code true}, disable all rules except those enabled explicitly.
* @return the list of rules to be used.
* @since 2.8
*/
public static List<BitextRule> selectBitextRules(List<BitextRule> bRules, List<String> disabledRules, List<String> enabledRules, boolean useEnabledOnly) {
List<BitextRule> newBRules = new ArrayList<>(bRules.size());
newBRules.addAll(bRules);
List<BitextRule> rulesToDisable = new ArrayList<>();
if (useEnabledOnly) {
for (String enabledRule : enabledRules) {
for (BitextRule b : bRules) {
if (!b.getId().equals(enabledRule)) {
rulesToDisable.add(b);
}
}
}
} else {
for (String disabledRule : disabledRules) {
for (BitextRule b : newBRules) {
if (b.getId().equals(disabledRule)) {
rulesToDisable.add(b);
}
}
}
}
newBRules.removeAll(rulesToDisable);
return newBRules;
}
use of org.languagetool.rules.bitext.BitextRule in project languagetool by languagetool-org.
the class Tools method checkBitext.
/**
* Checks the bilingual input (bitext).
*
* @param src Source text.
* @param trg Target text.
* @param srcLt Source JLanguageTool (used to analyze the text).
* @param trgLt Target JLanguageTool (used to analyze the text).
* @param bRules Bilingual rules used in addition to target standard rules.
* @return The list of rule matches on the bitext.
* @since 1.0.1
*/
public static List<RuleMatch> checkBitext(String src, String trg, JLanguageTool srcLt, JLanguageTool trgLt, List<BitextRule> bRules) throws IOException {
AnalyzedSentence srcText = srcLt.getAnalyzedSentence(src);
AnalyzedSentence trgText = trgLt.getAnalyzedSentence(trg);
List<Rule> nonBitextRules = trgLt.getAllRules();
for (Rule rule : nonBitextRules) {
rule.reset();
}
List<RuleMatch> ruleMatches = trgLt.checkAnalyzedSentence(JLanguageTool.ParagraphHandling.NORMAL, nonBitextRules, trgText);
for (BitextRule bRule : bRules) {
RuleMatch[] curMatch = bRule.match(srcText, trgText);
if (curMatch != null && curMatch.length > 0) {
// adjust positions for bitext rules
for (RuleMatch match : curMatch) {
if (match.getColumn() < 0) {
match.setColumn(1);
}
if (match.getEndColumn() < 0) {
// we count from 0
match.setEndColumn(trg.length() + 1);
}
if (match.getLine() < 0) {
match.setLine(1);
}
if (match.getEndLine() < 0) {
match.setEndLine(1);
}
ruleMatches.add(match);
}
}
}
return ruleMatches;
}
use of org.languagetool.rules.bitext.BitextRule in project languagetool by languagetool-org.
the class Main method setBitextMode.
private void setBitextMode(Language sourceLang, List<String> disabledRules, List<String> enabledRules, File bitextRuleFile) throws IOException, ParserConfigurationException, SAXException {
bitextMode = true;
Language target = lt.getLanguage();
lt = new MultiThreadedJLanguageTool(target, null);
srcLt = new MultiThreadedJLanguageTool(sourceLang);
Tools.selectRules(lt, disabledRules, enabledRules, true);
Tools.selectRules(srcLt, disabledRules, enabledRules, true);
bRules = Tools.getBitextRules(sourceLang, lt.getLanguage(), bitextRuleFile);
List<BitextRule> bRuleList = new ArrayList<>(bRules);
for (BitextRule bitextRule : bRules) {
for (String disabledRule : disabledRules) {
if (bitextRule.getId().equals(disabledRule)) {
bRuleList.remove(bitextRule);
}
}
}
bRules = bRuleList;
if (enabledRules.size() > 0) {
bRuleList = new ArrayList<>();
for (String enabledRule : enabledRules) {
for (BitextRule bitextRule : bRules) {
if (bitextRule.getId().equals(enabledRule)) {
bRuleList.add(bitextRule);
}
}
}
bRules = bRuleList;
}
}
use of org.languagetool.rules.bitext.BitextRule in project languagetool by languagetool-org.
the class ToolsTest method testBitextCheck.
private void testBitextCheck(ResultCache cache) throws IOException, ParserConfigurationException, SAXException {
Language english = Languages.getLanguageForShortCode("en");
JLanguageTool srcTool = new JLanguageTool(english, null, cache);
Language polish = Languages.getLanguageForShortCode("pl");
JLanguageTool trgTool = new JLanguageTool(polish, null, cache);
List<BitextRule> rules = Tools.getBitextRules(english, polish);
int matchCount = Tools.checkBitext("This is a perfectly good sentence.", "To jest całkowicie prawidłowe zdanie.", srcTool, trgTool, rules).size();
assertEquals(0, matchCount);
List<RuleMatch> matches1 = Tools.checkBitext("This is not actual.", "To nie jest aktualne.", srcTool, trgTool, rules);
assertEquals(1, matches1.size());
assertThat(matches1.get(0).getRule().getId(), is("ACTUAL"));
assertThat(matches1.get(0).getFromPos(), is(12));
assertThat(matches1.get(0).getToPos(), is(20));
List<RuleMatch> matches2 = Tools.checkBitext("A sentence. This is not actual.", "Zdanie. To nie jest aktualne.", srcTool, trgTool, rules);
assertEquals(1, matches2.size());
assertThat(matches2.get(0).getRule().getId(), is("ACTUAL"));
assertThat(matches2.get(0).getFromPos(), is(20));
assertThat(matches2.get(0).getToPos(), is(28));
List<RuleMatch> matches3 = Tools.checkBitext("A new sentence. This is not actual.", "Nowa zdanie. To nie jest aktualne.", srcTool, trgTool, rules);
assertEquals(1, matches3.size());
assertThat(matches3.get(0).getRule().getId(), is("ACTUAL"));
assertThat(matches3.get(0).getFromPos(), is(25));
assertThat(matches3.get(0).getToPos(), is(33));
}
Aggregations