Extending / Chaining Spell Checker

Guenter Bartsch guenter.bartsch at gmail.com
Sat Jun 1 09:48:57 PDT 2013


(hopefully final) update, for now: using LinguServiceManager I've
implemented a fallback mode which no longer uses hardcoded
hunspell/myspell but instead looks for any other spell checker plugin
in the list of service implementations:

        # http://www.openoffice.org/api/docs/common/ref/com/sun/star/linguistic2/XSpellChecker.html
        # XSpellChecker
        def spell(self, aWord, aLocale, aProperties ):

                try:
                        #print ("Speller: spell aWord: %s, aLocale:
%s, aProperties: %s" % (repr(aWord), repr(aLocale),
repr(aProperties)))

                        if not self.fallback_sc:

                                # look for another spellchecker in the
list of spell check service implementations
                                # that we could ask for proposals

                                ls =
self.service_manager.createInstance("com.sun.star.linguistic2.LinguServiceManager")

                                print ("Speller: lingu service manager
= %s" % repr(ls))

                                available = ls.getAvailableServices
('com.sun.star.linguistic2.SpellChecker', aLocale)

                                print ("Speller: spell services: %s" %
repr(available))

                                for speller in available:

                                        if speller == self.ImplementationName:
                                                continue

                                        self.fallback_sc =
self.service_manager.createInstance (speller)
                                        if self.fallback_sc:
                                                print ("Speller: found
other spell checker: %s" % speller)
                                                break

                                #self.fallback_sc =
self.service_manager.createInstance
("org.openoffice.lingu.MySpellSpellChecker")


                        if self.fallback_sc:
                                return self.fallback_sc.spell (aWord,
aLocale, aProperties)

                except:
                        print ("Speller: spell EXCEPTION CAUGHT %s" %
repr(sys.exc_info()) )


                return None

would still be interested if there is a cleaner way to achieve this.


More information about the LibreOffice mailing list