Esempio n. 1
0
    def doProofreading (self, nDocId, rText, rLocale, nStartOfSentencePos, nSuggestedSentenceEndPos, rProperties):
        xRes = ProofreadingResult()
        #xRes = uno.createUnoStruct("com.sun.star.linguistic2.ProofreadingResult")
        xRes.aDocumentIdentifier = nDocId
        xRes.aText = rText
        xRes.aLocale = rLocale
        xRes.nStartOfSentencePosition = nStartOfSentencePos
        xRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos
        xRes.aProperties = ()
        xRes.xProofreader = self
        xRes.aErrors = ()

        # PATCH FOR LO 4
        # Fix for http://nabble.documentfoundation.org/Grammar-checker-Undocumented-change-in-the-API-for-LO-4-td4030639.html
        if nStartOfSentencePos != 0:
            return xRes
        xRes.nStartOfNextSentencePosition = len(rText)
        # END OF PATCH

        # WORKAROUND FOR AVOIDING REPEATED ACTIONS ON HEAVY PARAGRAPHS
        if xRes.nStartOfNextSentencePosition > 3000:
            nHashedVal = hash(rText)
            if nHashedVal in self.dResult:
                return self.dResult[nHashedVal]
        # WORKAROUND ->>>

        try:
            aErrors = tuple(gce.parse(rText, rLocale.Country))
            if aErrors and self.zSpecialChars.search(rText):
                ## Special chars may alter error positioning
                nOffset = self.convertErrorsPosition(rText, aErrors)
                xRes.nStartOfNextSentencePosition += nOffset
            xRes.aErrors = aErrors
            # ->>> WORKAROUND
            if xRes.nStartOfNextSentencePosition > 3000:
                self.dResult[nHashedVal] = xRes
                self.nRes += 1
                if self.nRes > self.nMaxRes:
                    del self.dResult[self.lLastRes.popleft()]
                    self.nRes = self.nMaxRes
                self.lLastRes.append(nHashedVal)
            # END OF WORKAROUND
        except:
            traceback.print_exc()

        xRes.nBehindEndOfSentencePosition = xRes.nStartOfNextSentencePosition
        return xRes
Esempio n. 2
0
    def doProofreading (self, nDocId, rText, rLocale, nStartOfSentencePos, nSuggestedSentenceEndPos, rProperties):
        xRes = ProofreadingResult()
        #xRes = uno.createUnoStruct("com.sun.star.linguistic2.ProofreadingResult")
        xRes.aDocumentIdentifier = nDocId
        xRes.aText = rText
        xRes.aLocale = rLocale
        xRes.nStartOfSentencePosition = nStartOfSentencePos
        xRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos
        xRes.aProperties = ()
        xRes.xProofreader = self
        xRes.aErrors = ()

        # PATCH FOR LO 4
        # Fix for http://nabble.documentfoundation.org/Grammar-checker-Undocumented-change-in-the-API-for-LO-4-td4030639.html
        if nStartOfSentencePos != 0:
            return xRes
        xRes.nStartOfNextSentencePosition = len(rText)
        # END OF PATCH

        # WORKAROUND FOR AVOIDING REPEATED ACTIONS ON HEAVY PARAGRAPHS
        if xRes.nStartOfNextSentencePosition > 3000:
            nHashedVal = hash(rText)
            if nHashedVal in self.dResult:
                return self.dResult[nHashedVal]
        # WORKAROUND ->>>

        xRes.nBehindEndOfSentencePosition = xRes.nStartOfNextSentencePosition

        try:
            xRes.aErrors = gce.parse(rText, rLocale.Country)

            # ->>> WORKAROUND
            if xRes.nStartOfNextSentencePosition > 3000:
                self.dResult[nHashedVal] = xRes
                self.nRes += 1
                if self.nRes > self.nMaxRes:
                    del self.dResult[self.lLastRes.popleft()]
                    self.nRes = self.nMaxRes
                self.lLastRes.append(nHashedVal)
            # END OF WORKAROUND
            
        except Exception as e:
            if sys.version_info.major == 3:
                traceback.print_exc()

        return xRes
Esempio n. 3
0
	def doProofreading(self, aDocumentIdentifier, aText, aLocale, nStartOfSentencePos, nSuggestedBehindEndOfSentencePosition, aProperties):
		logging.debug("GrammarChecker.doProofreading")
		result = ProofreadingResult()
		result.aDocumentIdentifier = aDocumentIdentifier
		result.xFlatParagraph = None
		result.aText = aText
		result.aLocale = aLocale
		result.nStartOfSentencePosition = nStartOfSentencePos
		result.nBehindEndOfSentencePosition = nSuggestedBehindEndOfSentencePosition
		result.xProofreader = self

		VoikkoHandlePool.mutex.acquire()
		try:
			voikko = VoikkoHandlePool.getInstance().getHandle(aLocale)
			if voikko is None:
				logging.error("GrammarChecker.doProofreading called without initializing libvoikko")
				return result

			gcErrors = []
			gcI = 0
			vErrorCount = 0
			for vError in voikko.grammarErrors(aText, PropertyManager.getInstance().getMessageLanguage()):
				startPos = vError.startPos
				errorLength = vError.errorLen

				if startPos < result.nStartOfSentencePosition:
					continue
				if startPos >= result.nBehindEndOfSentencePosition:
					break
				if startPos + errorLength > result.nBehindEndOfSentencePosition:
					result.nBehindEndOfSentencePosition = startPos + errorLength

				# we have a real grammar error
				errorCode = vError.errorCode
				ruleIdentifier = str(errorCode)
				if ruleIdentifier in self.__ignoredErrors:
					# ignore this error
					continue

				suggestions = vError.suggestions

				gcError = SingleProofreadingError()
				gcErrors.append(gcError)
				gcError.nErrorStart = startPos
				gcError.nErrorLength = errorLength
				gcError.nErrorType = PROOFREADING
				comment = vError.shortDescription
				gcError.aShortComment = comment
				gcError.aFullComment = comment
				gcError.aRuleIdentifier = ruleIdentifier

				detailUrl = PropertyValue()
				detailUrl.Name = "FullCommentURL"
				detailUrl.Value = "https://voikko.puimula.org/gchelp/fi/" + ruleIdentifier + ".html"
				gcError.aProperties = (detailUrl,)

				# add suggestions
				if len(suggestions) > 0:
					gcError.aSuggestions = tuple(suggestions)

			result.aErrors = tuple(gcErrors)
			result.nStartOfNextSentencePosition = result.nBehindEndOfSentencePosition
			return result
		finally:
			VoikkoHandlePool.mutex.release()
Esempio n. 4
0
	def doProofreading(self, aDocumentIdentifier, aText, aLocale, nStartOfSentencePos, nSuggestedBehindEndOfSentencePosition, aProperties):
		logging.debug("GrammarChecker.doProofreading")
		result = ProofreadingResult()
		result.aDocumentIdentifier = aDocumentIdentifier
		result.xFlatParagraph = None
		result.aText = aText
		result.aLocale = aLocale
		result.nStartOfSentencePosition = nStartOfSentencePos
		result.nBehindEndOfSentencePosition = nSuggestedBehindEndOfSentencePosition
		result.xProofreader = self
		# We read from registry on each check – it probably
		# doesn't change most of the time, but it *can* change
		# if user edited the plugin options, so this is at
		# least safe:
		ignoredRules = readIgnoredRules() # TODO: get from PropertyManager instead, see reloadDivvunSettings

		DivvunHandlePool.mutex.acquire()
		try:
			instance = DivvunHandlePool.getInstance()
			if instance is None:
				logging.error("GrammarChecker.doProofreading could not initialize libdivvun!")
				return result
			divvun = instance.getHandle(aLocale)
			if divvun is None:
				logging.error("GrammarChecker.doProofreading couldn't get an instance for locale %s"%(aLocale,))
				logging.error("DivvunHandlePool.initializationErrors = %s"%(instance.getInitializationStatus(),))
				return result

			gcErrors = []
			logging.info("Checking '%s', nStartOfSentencePos=%d, nSuggestedBehindEndOfSentencePosition=%d",
					aText, nStartOfSentencePos, nSuggestedBehindEndOfSentencePosition)
			for dError in libdivvun.proc_errs_bytes(divvun, aText):
				startPos = dError.beg
				errorLength = dError.end - dError.beg
				logging.info("dError on form=%s at (%d,%d) replacements: %s",
						dError.form, dError.beg, dError.end, dError.rep)
				if dError.beg < result.nStartOfSentencePosition:
					logging.info("beg %d < result.nStartOfSentencePosition %d, continue",
							dError.beg, result.nStartOfSentencePosition)
					continue
				if dError.beg >= result.nBehindEndOfSentencePosition:
					logging.info("beg %d >= result.nBehindEndOfSentencePosition %d, break",
							dError.beg, result.nBehindEndOfSentencePosition)
					break
				if dError.beg + errorLength > result.nBehindEndOfSentencePosition:
					logging.info("dError.beg %d + errorLength %d > result.nBehindEndOfSentencePosition %d, incf",
							dError.beg, errorLength, result.nBehindEndOfSentencePosition)
					result.nBehindEndOfSentencePosition = dError.beg + errorLength
				logging.info("dError on form=%s at (%d,%d) replacements: %s",
						dError.form,
						dError.beg, dError.end,
						dError.rep)
				ruleIdentifier = dError.err
				if ruleIdentifier in ignoredRules:
					logging.debug("Ignored error with rule " + ruleIdentifier)
					continue

				suggestions = dError.rep
				gcError = SingleProofreadingError()
				gcErrors.append(gcError)
				gcError.nErrorStart = startPos
				gcError.nErrorLength = errorLength
				gcError.nErrorType = PROOFREADING
				comment = dError.dsc
				gcError.aShortComment = comment
				gcError.aFullComment = comment
				gcError.aRuleIdentifier = ruleIdentifier

				if False:  # We are not web yet, TODO
				    detailUrl = PropertyValue()
				    detailUrl.Name = "FullCommentURL"
				    detailUrl.Value = "http://divvun.no/gchelp/" + aLocale.Language + "/" + ruleIdentifier + ".html"
				    gcError.aProperties = (detailUrl,)

				# add suggestions
				if len(suggestions) > 0:
					gcError.aSuggestions = tuple(suggestions)

			result.aErrors = tuple(gcErrors)
			result.nStartOfNextSentencePosition = result.nBehindEndOfSentencePosition
			logging.info("return result, errors: %d", len(result.aErrors))
			return result
		finally:
			DivvunHandlePool.mutex.release()