CheckLanguage extends Rule { @Override public List<Advice> evaluate(AnnotatedString s, AnnotatedString original) { List<Advice> out_list = new ArrayList<Advice>(); String s_to_check = s.toString(); List<RuleMatch> matches = null; try { matches = m_languageTool.check(s_to_check); } catch (IOException e) { e.printStackTrace(); } if (matches == null) { return out_list; } for (RuleMatch rm : matches) { String line = ""; Position start_src_pos = Position.NOWHERE, end_src_pos = Position.NOWHERE; Position start_pos = s.getPosition(rm.getFromPos()); if (!start_pos.equals(Position.NOWHERE)) { start_src_pos = s.getSourcePosition(start_pos); } Position end_pos = s.getPosition(rm.getToPos()); if (!end_pos.equals(Position.NOWHERE)) { end_src_pos = s.getSourcePosition(end_pos); } Range r = null; boolean original_range = true; if (start_src_pos.equals(Position.NOWHERE)) { original_range = false; if (start_pos != null) { line = s.getLine(start_pos.getLine()); start_src_pos = start_pos; end_src_pos = end_pos; } r = Range.make(0, 0, 0); } else { line = original.getLine(start_src_pos.getLine()); if (end_src_pos.equals(Position.NOWHERE)) { r = Range.make(start_src_pos.getLine(), start_src_pos.getColumn(), start_src_pos.getColumn()); } else { r = new Range(start_src_pos, end_src_pos); } } if (m_disableUnpaired && rm.getRule().getId().startsWith("EN_UNPAIRED_BRACKETS")) { if (rm.getMessage().contains("{") || rm.getMessage().contains("}")) { continue; } } String clean_line = s.getLine(start_pos.getLine()); int end_p = 0; if (end_pos != null) { end_p = end_pos.getColumn(); } if (end_p > 0) { if (end_p > clean_line.length() - 1) { end_p = clean_line.length() - 1; } } end_p = r.getEnd().getColumn(); if (end_p > line.length() - 1) { end_p = line.length() - 1; } if (rm.getRule().getId().startsWith("FRENCH_WHITESPACE")) { continue; } if (rm.getRule().getId().startsWith("EN_QUOTES") && rm.getMessage().contains("Use a smart opening quote")) { if (line.length() > 0) { String word = line.substring(Math.min(line.length() - 1, r.getStart().getColumn()), end_p).trim(); if (word.contains("``")) { continue; } } } StringBuilder advice_message = new StringBuilder(); advice_message.append(rm.getMessage()); List<String> replacements = rm.getSuggestedReplacements(); if (!replacements.isEmpty()) { advice_message.append(". Suggestions: ").append(replacements.toString()); } advice_message.append(" (").append(rm.getFromPos()).append(")"); Advice ad = new Advice(new CheckLanguageSpecific(rm.getRule().getId(), rm.getRule().getDescription()), r, advice_message.toString(), s.getResourceName(), line, original.getOffset(start_src_pos)); ad.setOriginal(original_range); ad.setShortMessage("LanguageTool rule"); out_list.add(ad); } return out_list; } CheckLanguage(/*@ nullable @*/ Language lang, /*@ non_null @*/ List<String> dictionary);  CheckLanguage(/*@ nullable @*/ Language lang); void handleUserDictionary(); @Override List<Advice> evaluate(AnnotatedString s, AnnotatedString original); void activateLanguageModelRules(File f_ngram_dir); @Override String getDescription();  }
@Test public void test1() throws CheckLanguage.UnsupportedLanguageException { AnnotatedString in_string = AnnotatedString.read(new Scanner(CheckLanguageTest.class.getResourceAsStream("data/test-lt-1.tex"))); Rule r = new CheckLanguage(LanguageFactory.getLanguageFromString("en")); List<Advice> ad_list = r.evaluate(in_string, in_string); assertEquals(0, ad_list.size()); }
@Test public void test2() throws CheckLanguage.UnsupportedLanguageException { AnnotatedString in_string = AnnotatedString.read(new Scanner(CheckLanguageTest.class.getResourceAsStream("data/test-lt-2.tex"))); Rule r = new CheckLanguage(LanguageFactory.getLanguageFromString("en")); List<Advice> ad_list = r.evaluate(in_string, in_string); assertEquals(1, ad_list.size()); }
@Test public void test3() throws CheckLanguage.UnsupportedLanguageException { AnnotatedString in_string = AnnotatedString.read(new Scanner(CheckLanguageTest.class.getResourceAsStream("data/test-lt-2.tex"))); List<String> dict = new ArrayList<String>(2); dict.add("foo"); dict.add("foagzx"); Rule r = new CheckLanguage(LanguageFactory.getLanguageFromString("en"), dict); List<Advice> ad_list = r.evaluate(in_string, in_string); assertEquals(0, ad_list.size()); }