Search in sources :

Example 1 with AnnotatedText

use of org.languagetool.markup.AnnotatedText in project languagetool by languagetool-org.

the class JLanguageToolTest method testAnnotateTextCheckMultipleSentences.

@Test
public void testAnnotateTextCheckMultipleSentences() throws IOException {
    JLanguageTool languageTool = new JLanguageTool(english);
    AnnotatedText annotatedText = new AnnotatedTextBuilder().addMarkup("<b>").addText("here").addMarkup("</b>").addText(" is an error. And ").addMarkup("<i attr='foo'>").addText("here is also").addMarkup("</i>").addText(" a error.").build();
    List<RuleMatch> matches = languageTool.check(annotatedText);
    assertThat(matches.size(), is(2));
    assertThat(matches.get(0).getFromPos(), is(3));
    assertThat(matches.get(0).getToPos(), is(7));
    assertThat(matches.get(1).getFromPos(), is(60));
    assertThat(matches.get(1).getToPos(), is(61));
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) AnnotatedTextBuilder(org.languagetool.markup.AnnotatedTextBuilder) AnnotatedText(org.languagetool.markup.AnnotatedText) Test(org.junit.Test)

Example 2 with AnnotatedText

use of org.languagetool.markup.AnnotatedText in project languagetool by languagetool-org.

the class JLanguageToolTest method testAnnotateTextCheckMultipleSentences2.

@Test
public void testAnnotateTextCheckMultipleSentences2() throws IOException {
    JLanguageTool languageTool = new JLanguageTool(english);
    AnnotatedText annotatedText = new AnnotatedTextBuilder().addText("here").addText(" is an error. And ").addMarkup("<i attr='foo'/>").addText("here is also ").addMarkup("<i>").addText("a").addMarkup("</i>").addText(" error.").build();
    List<RuleMatch> matches = languageTool.check(annotatedText);
    assertThat(matches.size(), is(2));
    assertThat(matches.get(0).getFromPos(), is(0));
    assertThat(matches.get(0).getToPos(), is(4));
    assertThat(matches.get(1).getFromPos(), is(53));
    assertThat(matches.get(1).getToPos(), is(54));
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) AnnotatedTextBuilder(org.languagetool.markup.AnnotatedTextBuilder) AnnotatedText(org.languagetool.markup.AnnotatedText) Test(org.junit.Test)

Example 3 with AnnotatedText

use of org.languagetool.markup.AnnotatedText in project languagetool by languagetool-org.

the class Main method doGrammarCheckingInternal.

private synchronized ProofreadingResult doGrammarCheckingInternal(String paraText, Locale locale, ProofreadingResult paRes, int[] footnotePositions) {
    if (!StringTools.isEmpty(paraText) && hasLocale(locale)) {
        Language langForShortName = getLanguage(locale);
        if (!langForShortName.equals(docLanguage) || langTool == null || recheck) {
            docLanguage = langForShortName;
            initLanguageTool();
        }
        Set<String> disabledRuleIds = config.getDisabledRuleIds();
        if (disabledRuleIds != null) {
            // copy as the config thread may access this as well
            List<String> list = new ArrayList<>(disabledRuleIds);
            for (String id : list) {
                langTool.disableRule(id);
            }
        }
        Set<String> disabledCategories = config.getDisabledCategoryNames();
        if (disabledCategories != null) {
            // copy as the config thread may access this as well
            List<String> list = new ArrayList<>(disabledCategories);
            for (String categoryName : list) {
                langTool.disableCategory(categoryName);
            }
        }
        Set<String> enabledRuleIds = config.getEnabledRuleIds();
        if (enabledRuleIds != null) {
            // copy as the config thread may access this as well
            List<String> list = new ArrayList<>(enabledRuleIds);
            for (String ruleName : list) {
                langTool.enableRule(ruleName);
            }
        }
        try {
            String sentence = getSentence(paraText, paRes.nStartOfSentencePosition);
            paRes.nStartOfSentencePosition = position;
            paRes.nStartOfNextSentencePosition = position + sentence.length();
            paRes.nBehindEndOfSentencePosition = paRes.nStartOfNextSentencePosition;
            if (!StringTools.isEmpty(sentence)) {
                AnnotatedText annotatedText = getAnnotatedText(sentence, footnotePositions, paRes);
                List<RuleMatch> ruleMatches = langTool.check(annotatedText, false, JLanguageTool.ParagraphHandling.ONLYNONPARA);
                SingleProofreadingError[] pErrors = checkParaRules(paraText, paRes.nStartOfSentencePosition, paRes.nStartOfNextSentencePosition, paRes.aDocumentIdentifier);
                int pErrorCount = 0;
                if (pErrors != null) {
                    pErrorCount = pErrors.length;
                }
                if (!ruleMatches.isEmpty()) {
                    SingleProofreadingError[] errorArray = new SingleProofreadingError[ruleMatches.size() + pErrorCount];
                    int i = 0;
                    for (RuleMatch myRuleMatch : ruleMatches) {
                        errorArray[i] = createOOoError(myRuleMatch, paRes.nStartOfSentencePosition);
                        i++;
                    }
                    // add para matches
                    if (pErrors != null) {
                        for (SingleProofreadingError paraError : pErrors) {
                            if (paraError != null) {
                                errorArray[i] = paraError;
                                i++;
                            }
                        }
                    }
                    Arrays.sort(errorArray, new ErrorPositionComparator());
                    paRes.aErrors = errorArray;
                } else {
                    if (pErrors != null) {
                        paRes.aErrors = pErrors;
                    }
                }
            }
        } catch (Throwable t) {
            showError(t);
            paRes.nBehindEndOfSentencePosition = paraText.length();
        }
    }
    return paRes;
}
Also used : AnnotatedText(org.languagetool.markup.AnnotatedText) ArrayList(java.util.ArrayList) SingleProofreadingError(com.sun.star.linguistic2.SingleProofreadingError) RuleMatch(org.languagetool.rules.RuleMatch) Language(org.languagetool.Language)

Example 4 with AnnotatedText

use of org.languagetool.markup.AnnotatedText in project languagetool by languagetool-org.

the class JLanguageToolTest method testAnnotateTextCheckPlainText.

@Test
public void testAnnotateTextCheckPlainText() throws IOException {
    JLanguageTool languageTool = new JLanguageTool(english);
    AnnotatedText annotatedText = new AnnotatedTextBuilder().addText("A good sentence. But here's a error.").build();
    List<RuleMatch> matches = languageTool.check(annotatedText);
    assertThat(matches.size(), is(1));
    assertThat(matches.get(0).getFromPos(), is(28));
    assertThat(matches.get(0).getToPos(), is(29));
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) AnnotatedTextBuilder(org.languagetool.markup.AnnotatedTextBuilder) AnnotatedText(org.languagetool.markup.AnnotatedText) Test(org.junit.Test)

Example 5 with AnnotatedText

use of org.languagetool.markup.AnnotatedText in project languagetool by languagetool-org.

the class JLanguageToolTest method testAnnotateTextCheck.

@Test
public void testAnnotateTextCheck() throws IOException {
    JLanguageTool languageTool = new JLanguageTool(english);
    AnnotatedText annotatedText = new AnnotatedTextBuilder().addMarkup("<b>").addText("here").addMarkup("</b>").addText(" is an error").build();
    List<RuleMatch> matches = languageTool.check(annotatedText);
    assertThat(matches.size(), is(1));
    assertThat(matches.get(0).getFromPos(), is(3));
    assertThat(matches.get(0).getToPos(), is(7));
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) AnnotatedTextBuilder(org.languagetool.markup.AnnotatedTextBuilder) AnnotatedText(org.languagetool.markup.AnnotatedText) Test(org.junit.Test)

Aggregations

AnnotatedText (org.languagetool.markup.AnnotatedText)5 RuleMatch (org.languagetool.rules.RuleMatch)5 Test (org.junit.Test)4 AnnotatedTextBuilder (org.languagetool.markup.AnnotatedTextBuilder)4 SingleProofreadingError (com.sun.star.linguistic2.SingleProofreadingError)1 ArrayList (java.util.ArrayList)1 Language (org.languagetool.Language)1