def initialize(self): logging.debug("PropertyManager.initialize: starting") self.__setUiLanguage() VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_IGNORE_DOT, True) VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_NO_UGLY_HYPHENATION, True) # Set these options globally until OOo bug #97945 is resolved. VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_ACCEPT_TITLES_IN_GC, True) VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_ACCEPT_BULLETED_LISTS_IN_GC, True) VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_ACCEPT_UNFINISHED_PARAGRAPHS_IN_GC, True) compContext = uno.getComponentContext() servManager = compContext.ServiceManager self.__linguPropSet = servManager.createInstanceWithContext("com.sun.star.linguistic2.LinguProperties", compContext) self.__linguPropSet.addPropertyChangeListener("IsSpellWithDigits", self) self.__linguPropSet.addPropertyChangeListener("IsSpellUpperCase", self) logging.debug("PropertyManager.initialize: property manager initalized") # synchronize the local settings from global preferences self.__setProperties(self.__linguPropSet) self.readVoikkoSettings() # request that all users of linguistic services run the spellchecker and hyphenator # again with updated settings event = LinguServiceEvent() event.nEvent = SPELL_CORRECT_WORDS_AGAIN | SPELL_WRONG_WORDS_AGAIN | HYPHENATE_AGAIN | PROOFREAD_AGAIN self.__sendLinguEvent(event)
def reloadVoikkoSettings(self): voikko = VoikkoHandlePool.getInstance() event = LinguServiceEvent() event.nEvent = 0 try: hyphWordParts = self.readFromRegistry("/org.puimula.ooovoikko.Config/hyphenator", "hyphWordParts") if hyphWordParts != self.__hyphWordParts: event.nEvent = event.nEvent | HYPHENATE_AGAIN self.__hyphWordParts = hyphWordParts hyphUnknownWords = self.readFromRegistry("/org.puimula.ooovoikko.Config/hyphenator", "hyphUnknownWords") if hyphUnknownWords != self.__hyphUnknownWords: event.nEvent = event.nEvent | HYPHENATE_AGAIN self.__hyphUnknownWords = hyphUnknownWords dictVariant = self.readFromRegistry("/org.puimula.ooovoikko.Config/dictionary", "variant") if dictVariant is None or dictVariant == "": dictVariant = voikko.getPreferredGlobalVariant() if dictVariant != voikko.getPreferredGlobalVariant(): event.nEvent = event.nEvent | SPELL_CORRECT_WORDS_AGAIN | SPELL_WRONG_WORDS_AGAIN | PROOFREAD_AGAIN voikko.setPreferredGlobalVariant(dictVariant) except UnknownPropertyException as e: logging.exception("PropertyManager.reloadVoikkoSettings", e) self.__syncHyphenatorSettings() self.__sendLinguEvent(event)
def setValue(self, value): if value.Name == "IsSpellWithDigits": VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_IGNORE_NUMBERS, not value.Value) elif value.Name == "IsSpellUpperCase": VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_IGNORE_UPPERCASE, not value.Value) elif value.Name == "HyphMinLeading": if value.Value is not None: self.__hyphMinLeading = value.Value self.__syncHyphenatorSettings() elif value.Name == "HyphMinTrailing": if value.Value is not None: self.__hyphMinTrailing = value.Value self.__syncHyphenatorSettings() elif value.Name == "HyphMinWordLength": if value.Value is not None: self.__hyphMinWordLength = value.Value self.__syncHyphenatorSettings()
def isValid(self, word, locale, properties): VoikkoHandlePool.mutex.acquire() try: voikko = VoikkoHandlePool.getInstance().getHandle(locale) if voikko is None: return False PropertyManager.getInstance().setValues(properties) result = voikko.spell(word) PropertyManager.getInstance().resetValues(properties) return result finally: VoikkoHandlePool.mutex.release()
def spell(self, word, locale, properties): # Check if diagnostic message should be returned if word == "VoikkoGetStatusInformation": suggestions = [VoikkoHandlePool.getInstance().getInitializationStatus()] return SpellAlternatives(word, suggestions, locale) VoikkoHandlePool.mutex.acquire() try: voikko = VoikkoHandlePool.getInstance().getHandle(locale) if voikko is None: return None PropertyManager.getInstance().setValues(properties) if voikko.spell(word): PropertyManager.getInstance().resetValues(properties) return None suggestions = voikko.suggest(word) PropertyManager.getInstance().resetValues(properties) return SpellAlternatives(word, suggestions, locale) finally: VoikkoHandlePool.mutex.release()
def spell(self, word, locale, properties): # Check if diagnostic message should be returned if word == "VoikkoGetStatusInformation": suggestions = [ VoikkoHandlePool.getInstance().getInitializationStatus() ] return SpellAlternatives(word, suggestions, locale) VoikkoHandlePool.mutex.acquire() try: voikko = VoikkoHandlePool.getInstance().getHandle(locale) if voikko is None: return None PropertyManager.getInstance().setValues(properties) if voikko.spell(word): PropertyManager.getInstance().resetValues(properties) return None suggestions = voikko.suggest(word) PropertyManager.getInstance().resetValues(properties) return SpellAlternatives(word, suggestions, locale) finally: VoikkoHandlePool.mutex.release()
def hyphenate(self, word, locale, nMaxLeading, properties): logging.debug("Hyphenator.hyphenate") if len(word) > 10000: return None VoikkoHandlePool.mutex.acquire() try: voikko = VoikkoHandlePool.getInstance().getHandle(locale) if voikko is None: return None PropertyManager.getInstance().setValues(properties) minLeading = PropertyManager.getInstance().getHyphMinLeading() minTrailing = PropertyManager.getInstance().getHyphMinTrailing() wlen = len(word) # If the word is too short to be hyphenated, return no hyphenation points if wlen < PropertyManager.getInstance().getHyphMinWordLength() or wlen < minLeading + minTrailing: PropertyManager.getInstance().resetValues(properties) return None hyphenationPoints = voikko.getHyphenationPattern(word) if hyphenationPoints is None: PropertyManager.getInstance().resetValues(properties) return None # find the hyphenation point hyphenPos = -1 i = wlen - minTrailing # The last generally allowed hyphenation point if i > nMaxLeading: i = nMaxLeading # The last allowed point on this line while i >= minLeading and hyphenPos == -1: if word[i] == '\'': i = i - 1 continue if hyphenationPoints[i] == '-' or hyphenationPoints[i] == '=': hyphenPos = i break i = i - 1 # return the result PropertyManager.getInstance().resetValues(properties) if hyphenPos != -1: return HyphenatedWord(word, hyphenPos - 1, locale) else: return None finally: VoikkoHandlePool.mutex.release()
def createPossibleHyphens(self, word, locale, properties): logging.debug("Hyphenator.createPossibleHyphens") wlen = len(word) if wlen > 10000: return None VoikkoHandlePool.mutex.acquire() try: voikko = VoikkoHandlePool.getInstance().getHandle(locale) if voikko is None: return None PropertyManager.getInstance().setValues(properties) # If the word is too short to be hyphenated, return no hyphenation points minLeading = PropertyManager.getInstance().getHyphMinLeading() minTrailing = PropertyManager.getInstance().getHyphMinTrailing() if wlen < PropertyManager.getInstance().getHyphMinWordLength() or wlen < minLeading + minTrailing: PropertyManager.getInstance().resetValues(properties) return None hyphenationPoints = voikko.getHyphenationPattern(word) if hyphenationPoints is None: PropertyManager.getInstance().resetValues(properties) return None hyphenSeq = [] hyphenatedWord = "" for i in range(0, wlen): hyphenatedWord = hyphenatedWord + word[i] if i >= minLeading - 1 and i < wlen - minTrailing and hyphenationPoints[i + 1] == '-': hyphenSeq.append(i) hyphenatedWord = hyphenatedWord + "=" res = PossibleHyphens(word, hyphenatedWord, hyphenSeq, locale) PropertyManager.getInstance().resetValues(properties) return res finally: VoikkoHandlePool.mutex.release()
def __init__(self): self.__messageLanguage = "en_US" VoikkoHandlePool.getInstance().setInstallationPath(self.__getInstallationPath()) logging.debug("PropertyManager.__init__") self.__linguPropSet = None self.__hyphMinLeading = 2 self.__hyphMinTrailing = 2 self.__hyphMinWordLength = 5 self.__hyphWordParts = False self.__hyphUnknownWords = True self.__linguEventListeners = {} try: dictVariant = self.readFromRegistry("/org.puimula.ooovoikko.Config/dictionary", "variant") VoikkoHandlePool.getInstance().setPreferredGlobalVariant(dictVariant) logging.debug("Initial dictionary variant '" + dictVariant + "'") except UnknownPropertyException as e: logging.debug("Setting initial dictionary variant to default") VoikkoHandlePool.getInstance().setPreferredGlobalVariant("") self.initialize()
def __syncHyphenatorSettings(self): if self.__hyphWordParts: VoikkoHandlePool.getInstance().setGlobalIntegerOption(PropertyManager.VOIKKO_MIN_HYPHENATED_WORD_LENGTH, self.__hyphMinWordLength) else: VoikkoHandlePool.getInstance().setGlobalIntegerOption(PropertyManager.VOIKKO_MIN_HYPHENATED_WORD_LENGTH, 2) VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_HYPHENATE_UNKNOWN_WORDS, self.__hyphUnknownWords)
def getLocales(self): return VoikkoHandlePool.getInstance().getSupportedSpellingLocales()
def hasLocale(self, aLocale): return VoikkoHandlePool.getInstance().supportsHyphenationLocale(aLocale)
def hasLocale(self, aLocale): return VoikkoHandlePool.getInstance().supportsSpellingLocale(aLocale)
def hasLocale(self, aLocale): return VoikkoHandlePool.getInstance().supportsGrammarLocale(aLocale)
def getLocales(self): return VoikkoHandlePool.getInstance().getSupportedHyphenationLocales()
def getLocales(self): return VoikkoHandlePool.getInstance().getSupportedGrammarLocales()
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()
def __initAvailableVariants(self): dicts = Voikko.listDicts(VoikkoHandlePool.getInstance().getDictionaryPath()) self.__dictionaryVariantList = [] for vDict in dicts: dictName = vDict.variant + ": " + vDict.description self.__dictionaryVariantList.append(dictName)