Search in sources :

Example 1 with SingleProofreadingError

use of com.sun.star.linguistic2.SingleProofreadingError 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 SingleProofreadingError

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

the class Main method createOOoError.

/**
   * Creates a SingleGrammarError object for use in LO/OO.
   */
private SingleProofreadingError createOOoError(RuleMatch ruleMatch, int startIndex) {
    SingleProofreadingError aError = new SingleProofreadingError();
    aError.nErrorType = TextMarkupType.PROOFREADING;
    // the API currently has no support for formatting text in comments
    aError.aFullComment = ruleMatch.getMessage().replaceAll("<suggestion>", "\"").replaceAll("</suggestion>", "\"").replaceAll("([\r]*\n)", " ");
    // not all rules have short comments
    if (!StringTools.isEmpty(ruleMatch.getShortMessage())) {
        aError.aShortComment = ruleMatch.getShortMessage();
    } else {
        aError.aShortComment = aError.aFullComment;
    }
    aError.aShortComment = org.languagetool.gui.Tools.shortenComment(aError.aShortComment);
    int numSuggestions = ruleMatch.getSuggestedReplacements().size();
    String[] allSuggestions = ruleMatch.getSuggestedReplacements().toArray(new String[numSuggestions]);
    if (numSuggestions > MAX_SUGGESTIONS) {
        aError.aSuggestions = Arrays.copyOfRange(allSuggestions, 0, MAX_SUGGESTIONS);
    } else {
        aError.aSuggestions = allSuggestions;
    }
    aError.nErrorStart = ruleMatch.getFromPos() + startIndex;
    aError.nErrorLength = ruleMatch.getToPos() - ruleMatch.getFromPos();
    aError.aRuleIdentifier = ruleMatch.getRule().getId();
    // older version will simply ignore the property:
    if (ruleMatch.getRule().getUrl() != null) {
        aError.aProperties = new PropertyValue[] { new PropertyValue("FullCommentURL", -1, ruleMatch.getRule().getUrl().toString(), PropertyState.DIRECT_VALUE) };
    } else {
        aError.aProperties = new PropertyValue[0];
    }
    return aError;
}
Also used : PropertyValue(com.sun.star.beans.PropertyValue) SingleProofreadingError(com.sun.star.linguistic2.SingleProofreadingError)

Example 3 with SingleProofreadingError

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

the class Main method checkParaRules.

@Nullable
private synchronized SingleProofreadingError[] checkParaRules(String paraText, int startPos, int endPos, String docID) {
    if (startPos == 0) {
        try {
            paragraphMatches = langTool.check(paraText, false, JLanguageTool.ParagraphHandling.ONLYPARA);
            this.docID = docID;
        } catch (Throwable t) {
            showError(t);
        }
    }
    if (paragraphMatches != null && !paragraphMatches.isEmpty() && docID.equals(this.docID)) {
        List<SingleProofreadingError> errorList = new ArrayList<>(paragraphMatches.size());
        for (RuleMatch myRuleMatch : paragraphMatches) {
            int startErrPos = myRuleMatch.getFromPos();
            int endErrPos = myRuleMatch.getToPos();
            if (startErrPos >= startPos && startErrPos < endPos && endErrPos >= startPos && endErrPos < endPos) {
                errorList.add(createOOoError(myRuleMatch, 0));
            }
        }
        if (!errorList.isEmpty()) {
            SingleProofreadingError[] errorArray = errorList.toArray(new SingleProofreadingError[errorList.size()]);
            Arrays.sort(errorArray, new ErrorPositionComparator());
            return errorArray;
        }
    }
    return null;
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) ArrayList(java.util.ArrayList) SingleProofreadingError(com.sun.star.linguistic2.SingleProofreadingError) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

SingleProofreadingError (com.sun.star.linguistic2.SingleProofreadingError)3 ArrayList (java.util.ArrayList)2 RuleMatch (org.languagetool.rules.RuleMatch)2 PropertyValue (com.sun.star.beans.PropertyValue)1 Nullable (org.jetbrains.annotations.Nullable)1 Language (org.languagetool.Language)1 AnnotatedText (org.languagetool.markup.AnnotatedText)1