/** * @return the number of rule matches */ public static int profileRulesOnLine(String contents, JLanguageTool lt, Rule rule) throws IOException { int count = 0; for (String sentence : lt.sentenceTokenize(contents)) { count += rule.match(lt.getAnalyzedSentence(sentence)).length ; } return count; }
private boolean match(Rule rule, String sentence, JLanguageTool languageTool) throws IOException { AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(sentence); RuleMatch[] matches = rule.match(analyzedSentence); return matches.length > 0; }
private RuleMatch[] getMatches(Rule rule, String sentence, JLanguageTool languageTool) throws IOException { AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(sentence); RuleMatch[] matches = rule.match(analyzedSentence); return matches; }
/** * This is an internal method that's public only for technical reasons, please use one * of the {@link #check(String)} methods instead. * @since 2.3 */ public List<RuleMatch> checkAnalyzedSentence(ParagraphHandling paraMode, List<Rule> rules, AnalyzedSentence analyzedSentence) throws IOException { List<RuleMatch> sentenceMatches = new ArrayList<>(); for (Rule rule : rules) { if (rule instanceof TextLevelRule) { continue; } if (ignoreRule(rule)) { continue; } if (rule instanceof PatternRule && ((PatternRule)rule).canBeIgnoredFor(analyzedSentence)) { // this is a performance optimization, it should have no effect on matching logic continue; } if (paraMode == ParagraphHandling.ONLYPARA) { continue; } RuleMatch[] thisMatches = rule.match(analyzedSentence); for (RuleMatch elem : thisMatches) { sentenceMatches.add(elem); } } return new SameRuleGroupFilter().filter(sentenceMatches); }
private void assertGood(String input, Rule rule) throws IOException { RuleMatch[] matches = rule.match(lt.getAnalyzedSentence(input)); assertThat("Got unexpected match in: " + input, matches.length, is(0)); }
private void assertMatch(String input, Rule rule) throws IOException { RuleMatch[] matches = rule.match(lt.getAnalyzedSentence(input)); assertThat("Did not find match in: " + input, matches.length, is(1)); }
protected Language acceptedInAlternativeLanguage(String word) throws IOException { for (RuleWithLanguage altRule : altRules) { AnalyzedToken token = new AnalyzedToken(word, null, null); AnalyzedToken sentenceStartToken = new AnalyzedToken("", JLanguageTool.SENTENCE_START_TAGNAME, null); AnalyzedTokenReadings startTokenReadings = new AnalyzedTokenReadings(sentenceStartToken, 0); AnalyzedTokenReadings atr = new AnalyzedTokenReadings(token, 0); RuleMatch[] matches = altRule.getRule().match(new AnalyzedSentence(new AnalyzedTokenReadings[]{startTokenReadings, atr})); if (matches.length == 0) { return altRule.getLanguage(); } else { if (word.endsWith(".")) { Language language = acceptedInAlternativeLanguage(word.substring(0, word.length() - 1)); if (language != null) { return language; } } } } return null; }
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); }
@Test public void testMatchWithMark() throws IOException { JLanguageTool lt = new JLanguageTool(TestTools.getDemoLanguage()); Rule rule = lt.getPatternRulesByIdAndSubId("REGEX_PATTERN_RULE_DEMO_MARK_1", "1").get(0); RuleMatch[] matches2 = rule.match(lt.getAnalyzedSentence("This is foo bar")); assertThat(matches2.length, is(1)); assertThat(matches2[0].getFromPos(), is(8)); assertThat(matches2[0].getToPos(), is(11)); } }
AnalyzedSentence sentence = lt.getAnalyzedSentence(sentenceStr); long startTime = System.currentTimeMillis(); RuleMatch[] matches = spellerRule.match(sentence); totalHunspellComputationTime.addAndGet(System.currentTimeMillis() - startTime); for (RuleMatch match : matches) {
RuleMatch[] originalMatches = spellerRule.match(sentence); System.setProperty("SuggestionsChangesTestAlternativeEnabled", "1"); RuleMatch[] alternativeMatches = spellerRule.match(sentence); assertEquals(originalMatches.length, alternativeMatches.length); RuleMatch[] matches = spellerRule.match(sentence);
@Test public void testMatch() throws IOException { JLanguageTool lt = new JLanguageTool(TestTools.getDemoLanguage()); Rule rule = lt.getPatternRulesByIdAndSubId("REGEX_PATTERN_RULE_DEMO_MARK_0", "1").get(0); RuleMatch[] matches1 = rule.match(lt.getAnalyzedSentence("This is a test")); assertThat(matches1.length, is(0)); RuleMatch[] matches2 = rule.match(lt.getAnalyzedSentence("This is foo bar")); assertThat(matches2.length, is(1)); assertThat(matches2[0].getFromPos(), is(8)); assertThat(matches2[0].getToPos(), is(15)); RuleMatch[] matches3 = rule.match(lt.getAnalyzedSentence("This is foo bar and fou bar")); assertThat(matches3.length, is(2)); assertThat(matches3[0].getFromPos(), is(8)); assertThat(matches3[0].getToPos(), is(15)); assertThat(matches3[0].getMessage(), is("msg: <suggestion>a suggestion foo</suggestion>")); assertThat(matches3[0].getSuggestedReplacements().toString(), is("[a suggestion foo, another suggestion bar]")); assertThat(matches3[1].getFromPos(), is(20)); assertThat(matches3[1].getToPos(), is(27)); assertThat(matches3[1].getMessage(), is("msg: <suggestion>a suggestion fou</suggestion>")); assertThat(matches3[1].getSuggestedReplacements().toString(), is("[a suggestion fou, another suggestion bar]")); }
for(Rule rule : ruleSet) { for(String line : getLine) { String result = rule.match(line); if (result != null) { ...
/** * @return the number of rule matches */ public static int profileRulesOnLine(String contents, JLanguageTool lt, Rule rule) throws IOException { int count = 0; for (String sentence : lt.sentenceTokenize(contents)) { count += rule.match(lt.getAnalyzedSentence(sentence)).length ; } return count; }
@Override public RuleMatch[] match(AnalyzedSentence sentence) throws IOException { List<RuleMatch> ruleMatches = new ArrayList<>(); for (PreferredWordRuleWithSuggestion ruleWithSuggestion : data.get()) { Rule rule = ruleWithSuggestion.rule; RuleMatch[] matches = rule.match(sentence); if (matches.length > 0) { RuleMatch match = matches[0]; String matchedText = sentence.getText().substring(match.getFromPos(), match.getToPos()); //String textFromMatch = sentence.getText().substring(match.getFromPos()); String suggestion = matchedText.replace(ruleWithSuggestion.oldWord, ruleWithSuggestion.newWord); if (!suggestion.equals(matchedText)) { match.setSuggestedReplacement(suggestion); ruleMatches.addAll(Arrays.asList(matches)); } } } return toRuleMatchArray(ruleMatches); }
static List<RuleMatch> computeMatches(AnalyzedSentence sentence, SpellingData data, String[] exceptions) throws IOException { List<RuleMatch> ruleMatches = new ArrayList<>(); for (SpellingRuleWithSuggestion ruleWithSuggestion : data.get()) { Rule rule = ruleWithSuggestion.rule; RuleMatch[] matches = rule.match(sentence); for (RuleMatch match : matches) { String matchedText = sentence.getText().substring(match.getFromPos(), match.getToPos()); String textFromMatch = sentence.getText().substring(match.getFromPos()); boolean isException = false; for (String exception: exceptions) { if (textFromMatch.startsWith(exception)) { isException = true; break; } } if (isException) { continue; } String suggestion = matchedText.replace(ruleWithSuggestion.alternative, ruleWithSuggestion.suggestion); if (!suggestion.equals(matchedText)) { // "Schlüsse" etc. is otherwise considered incorrect (inflected form of "Schluß") match.setSuggestedReplacement(suggestion); ruleMatches.add(match); } } } return ruleMatches; }
/** * This is an internal method that's public only for technical reasons, please use one * of the {@link #check(String)} methods instead. * @since 2.3 */ public List<RuleMatch> checkAnalyzedSentence(ParagraphHandling paraMode, List<Rule> rules, AnalyzedSentence analyzedSentence) throws IOException { List<RuleMatch> sentenceMatches = new ArrayList<>(); for (Rule rule : rules) { if (rule instanceof TextLevelRule) { continue; } if (ignoreRule(rule)) { continue; } if (rule instanceof PatternRule && ((PatternRule)rule).canBeIgnoredFor(analyzedSentence)) { // this is a performance optimization, it should have no effect on matching logic continue; } if (paraMode == ParagraphHandling.ONLYPARA) { continue; } RuleMatch[] thisMatches = rule.match(analyzedSentence); for (RuleMatch elem : thisMatches) { sentenceMatches.add(elem); } } return new SameRuleGroupFilter().filter(sentenceMatches); }
protected Language acceptedInAlternativeLanguage(String word) throws IOException { for (RuleWithLanguage altRule : altRules) { AnalyzedToken token = new AnalyzedToken(word, null, null); AnalyzedToken sentenceStartToken = new AnalyzedToken("", JLanguageTool.SENTENCE_START_TAGNAME, null); AnalyzedTokenReadings startTokenReadings = new AnalyzedTokenReadings(sentenceStartToken, 0); AnalyzedTokenReadings atr = new AnalyzedTokenReadings(token, 0); RuleMatch[] matches = altRule.getRule().match(new AnalyzedSentence(new AnalyzedTokenReadings[]{startTokenReadings, atr})); if (matches.length == 0) { return altRule.getLanguage(); } else { if (word.endsWith(".")) { Language language = acceptedInAlternativeLanguage(word.substring(0, word.length() - 1)); if (language != null) { return language; } } } } return null; }