Search in sources :

Example 1 with ProofreadingResult

use of com.sun.star.linguistic2.ProofreadingResult 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 2 with ProofreadingResult

use of com.sun.star.linguistic2.ProofreadingResult in project languagetool by languagetool-org.

the class Main method doProofreading.

/**
   * Runs the grammar checker on paragraph text.
   * 
   * @param docID document ID
   * @param paraText paragraph text
   * @param locale Locale the text Locale
   * @param startOfSentencePos start of sentence position
   * @param nSuggestedBehindEndOfSentencePosition end of sentence position
   * @return ProofreadingResult containing the results of the check.
   */
@Override
public final ProofreadingResult doProofreading(String docID, String paraText, Locale locale, int startOfSentencePos, int nSuggestedBehindEndOfSentencePosition, PropertyValue[] propertyValues) {
    ProofreadingResult paRes = new ProofreadingResult();
    try {
        paRes.nStartOfSentencePosition = startOfSentencePos;
        paRes.xProofreader = this;
        paRes.aLocale = locale;
        paRes.aDocumentIdentifier = docID;
        paRes.aText = paraText;
        paRes.aProperties = propertyValues;
        // since LO 4.3
        int[] footnotePositions = getPropertyValues("FootnotePositions", propertyValues);
        return doGrammarCheckingInternal(paraText, locale, paRes, footnotePositions);
    } catch (Throwable t) {
        showError(t);
        return paRes;
    }
}
Also used : ProofreadingResult(com.sun.star.linguistic2.ProofreadingResult)

Example 3 with ProofreadingResult

use of com.sun.star.linguistic2.ProofreadingResult in project languagetool by languagetool-org.

the class MainTest method testDoProofreading.

@Test
public void testDoProofreading() {
    Main prog = new Main(null);
    Main.setTestMode(true);
    String testString = "To jest trudne zdanie. A to następne.  A to przedostatnie jest. Test ostatniego.";
    Locale plLoc = new Locale("pl", "PL", "");
    PropertyValue[] prop = new PropertyValue[0];
    for (int i = 0; i <= testString.length(); i++) {
        ProofreadingResult paRes = prog.doProofreading("1", testString, plLoc, i, testString.length(), prop);
        assertEquals("1", paRes.aDocumentIdentifier);
        assertTrue(paRes.nStartOfNextSentencePosition >= i);
        if (i < "To jest trudne zdanie. ".length()) {
            assertEquals("To jest trudne zdanie. ".length(), paRes.nStartOfNextSentencePosition);
            assertEquals(0, paRes.nStartOfSentencePosition);
        }
    }
    ProofreadingResult paRes1 = prog.doProofreading("1", testString, plLoc, 0, testString.length(), prop);
    assertEquals("1", paRes1.aDocumentIdentifier);
    assertEquals(23, paRes1.nStartOfNextSentencePosition);
    assertEquals(0, paRes1.nStartOfSentencePosition);
    //that was causing NPE but not anymore:
    String testString2 = "To jest „nowy problem”. A to inny jeszcze( „problem. Co jest „?";
    ProofreadingResult paRes2 = prog.doProofreading("1", testString2, plLoc, 0, testString2.length(), prop);
    assertEquals("1", paRes2.aDocumentIdentifier);
    assertEquals(24, paRes2.nStartOfNextSentencePosition);
    assertEquals(0, paRes2.nStartOfSentencePosition);
}
Also used : Locale(com.sun.star.lang.Locale) PropertyValue(com.sun.star.beans.PropertyValue) ProofreadingResult(com.sun.star.linguistic2.ProofreadingResult) Test(org.junit.Test)

Example 4 with ProofreadingResult

use of com.sun.star.linguistic2.ProofreadingResult in project languagetool by languagetool-org.

the class MainTest method testVariants.

@Test
public void testVariants() {
    Main prog = new Main(null);
    Main.setTestMode(true);
    String testString = "Sigui quina siga la teva intenció. Això és una prova.";
    // LibreOffice config for languages with variants
    Locale cavaLoc = new Locale("qlt", "ES", "ca-ES-valencia");
    PropertyValue[] prop = new PropertyValue[0];
    for (int i = 0; i <= testString.length(); i++) {
        ProofreadingResult paRes = prog.doProofreading("1", testString, cavaLoc, i, testString.length(), prop);
        assertEquals("1", paRes.aDocumentIdentifier);
        assertTrue(paRes.nStartOfNextSentencePosition >= i);
        if (i < "Sigui quina siga la teva intenció. ".length()) {
            assertEquals("Sigui quina siga la teva intenció. ".length(), paRes.nStartOfNextSentencePosition);
            assertEquals(0, paRes.nStartOfSentencePosition);
        //The test result depends on the CONFIG_FILE
        //assertEquals(2, paRes.aErrors.length);
        }
    }
    Locale caLoc = new Locale("ca", "ES", "");
    ProofreadingResult paRes = prog.doProofreading("1", testString, caLoc, 0, testString.length(), prop);
    assertEquals("1", paRes.aDocumentIdentifier);
//assertEquals(1, paRes.aErrors.length);
}
Also used : Locale(com.sun.star.lang.Locale) PropertyValue(com.sun.star.beans.PropertyValue) ProofreadingResult(com.sun.star.linguistic2.ProofreadingResult) Test(org.junit.Test)

Aggregations

ProofreadingResult (com.sun.star.linguistic2.ProofreadingResult)3 PropertyValue (com.sun.star.beans.PropertyValue)2 Locale (com.sun.star.lang.Locale)2 Test (org.junit.Test)2 SingleProofreadingError (com.sun.star.linguistic2.SingleProofreadingError)1 ArrayList (java.util.ArrayList)1 Language (org.languagetool.Language)1 AnnotatedText (org.languagetool.markup.AnnotatedText)1 RuleMatch (org.languagetool.rules.RuleMatch)1