use of org.languagetool.rules.patterns.bitext.BitextPatternRuleLoader in project languagetool by languagetool-org.
the class BitextPatternRuleTest method testBitextRulesFromXML.
private void testBitextRulesFromXML(Set<Language> ignoredLanguages) throws IOException {
for (Language lang : Languages.getWithDemoLanguage()) {
if (ignoredLanguages != null && ignoredLanguages.contains(lang)) {
continue;
}
BitextPatternRuleLoader ruleLoader = new BitextPatternRuleLoader();
String name = "/" + lang.getShortCode() + "/bitext.xml";
InputStream is;
try {
is = JLanguageTool.getDataBroker().getFromRulesDirAsStream(name);
} catch (RuntimeException ignored) {
// thrown if there is no bitext.xml file
continue;
}
System.out.println("Running tests for " + lang.getName() + "...");
JLanguageTool languageTool = new JLanguageTool(lang);
List<BitextPatternRule> rules = ruleLoader.getRules(is, name);
testBitextRulesFromXML(rules, languageTool, lang);
}
}
use of org.languagetool.rules.patterns.bitext.BitextPatternRuleLoader in project languagetool by languagetool-org.
the class Tools method getBitextRules.
/**
* Gets default bitext rules for a given pair of languages
* @param source Source language.
* @param target Target language.
* @param externalBitextRuleFile external file with bitext rules
* @return List of Bitext rules
* @since 2.9
*/
public static List<BitextRule> getBitextRules(Language source, Language target, File externalBitextRuleFile) throws IOException, ParserConfigurationException, SAXException {
List<BitextRule> bRules = new ArrayList<>();
//try to load the bitext pattern rules for the language...
BitextPatternRuleLoader ruleLoader = new BitextPatternRuleLoader();
String name = "/" + target.getShortCode() + "/bitext.xml";
if (JLanguageTool.getDataBroker().ruleFileExists(name)) {
InputStream is = JLanguageTool.getDataBroker().getFromRulesDirAsStream(name);
if (is != null) {
bRules.addAll(ruleLoader.getRules(is, name));
}
}
if (externalBitextRuleFile != null) {
bRules.addAll(ruleLoader.getRules(new FileInputStream(externalBitextRuleFile), externalBitextRuleFile.getAbsolutePath()));
}
//load the false friend rules in the bitext mode:
FalseFriendsAsBitextLoader fRuleLoader = new FalseFriendsAsBitextLoader();
String falseFriendsFile = "/false-friends.xml";
List<BitextPatternRule> rules = fRuleLoader.getFalseFriendsAsBitext(falseFriendsFile, source, target);
bRules.addAll(rules);
//load Java bitext rules:
bRules.addAll(getAllBuiltinBitextRules(source, null));
return bRules;
}
Aggregations