Ejemplo n.º 1
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()
Ejemplo n.º 2
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()