Grammalecte  Check-in [a25b510c2c]

Overview
Comment:[lo] use LibreOffice API to implement a spellchecker
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | lo
Files: files | file ages | folders
SHA3-256: a25b510c2c104ec0eecc1ccac944714bbcd1533623881e5ec7a27b08891fa5e1
User & Date: olr on 2018-04-04 15:31:57
Other Links: manifest | tags
Context
2018-04-04
16:13
[lo] replace Hunspell by Graphspell + update locales list check-in: e62980249b user: olr tags: trunk, lo
15:31
[lo] use LibreOffice API to implement a spellchecker check-in: a25b510c2c user: olr tags: trunk, lo
15:10
[graphspell] ibdawg: isValid -> True if word is digit check-in: 291494dd43 user: olr tags: trunk, graphspell
Changes

Added gc_lang/fr/oxt/Graphspell/Graphspell.py version [db81597a82].































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Graphspell
#
# Spellchecker based on a DAWG (Direct Acyclic Word Graph)


import uno
import unohelper
import traceback
import re

from grammalecte.graphspell import SpellChecker

from com.sun.star.linguistic2 import XSupportedLocales, XSpellChecker, XSpellAlternatives
from com.sun.star.lang import XServiceInfo, XServiceName, XServiceDisplayName
from com.sun.star.lang import Locale


lLocale = {
    # List of locales in LibreOffice
    # https://cgit.freedesktop.org/libreoffice/core/tree/i18nlangtag/source/isolang/isolang.cxx
    ('la', 'VA', ''),  # Latin (for testing purpose)
    ('fr', 'FR', ''),  # France
    ('fr', 'BE', ''),  # Belgique
    ('fr', 'CA', ''),  # Canada
    ('fr', 'CH', ''),  # Suisse
    ('fr', 'LU', ''),  # Luxembourg
    #('fr', 'MC', ''),  # Monaco
    ('fr', 'BF', ''),  # Burkina Faso
    ('fr', 'BJ', ''),  # Benin
    ('fr', 'CD', ''),  # Congo
    ('fr', 'CI', ''),  # Côte d’Ivoire
    ('fr', 'CM', ''),  # Cameroun
    ('fr', 'MA', ''),  # Maroc
    ('fr', 'ML', ''),  # Mali
    ('fr', 'MU', ''),  # Île Maurice
    ('fr', 'NE', ''),  # Niger
    ('fr', 'RE', ''),  # Réunion
    ('fr', 'SN', ''),  # Sénégal
    ('fr', 'TG', '')   # Togo
}

zElidedWords = re.compile("(?i)^(?:[ldnmtsjcçy]|qu|lorsqu|quoiqu|puisqu|jusqu)['’`‘]")


class Graphspell (unohelper.Base, XSpellChecker, XServiceInfo, XServiceName, XServiceDisplayName, XSupportedLocales):

    def __init__ (self, ctx, *args):
        try:
            self.ctx = ctx
            self.sServiceName = "com.sun.star.linguistic2.SpellChecker"
            self.sImplementationName = "net.grammalecte.graphspell"
            self.tSupportedServiceNames = (self.sServiceName, )
            self.xSvMgr = ctx.ServiceManager
            self.locales = tuple([ Locale(t[0], t[1], t[2])  for t in lLocale ])
            self.oGraphspell = SpellChecker("fr", "fr.bdic")
            self.bHunspell
            self.xHunspell = None
            self.xHunspellLocale = Locale('fr', 'MC', '')
            #self.xHunspellLocale = uno.createUnoStruct('com.sun.star.lang.Locale')
            #self.xHunspellLocale.Language = 'fr'
            #self.xHunspellLocale.Country = 'FR'
            print("init done")
        except:
            print("Graphspell: init")
            traceback.print_exc()
    
    # XServiceName
    def getServiceName (self):
        return self.sImplementationName     #self.sServiceName

    # XServiceInfo
    def getImplementationName (self):
        return self.sImplementationName

    def supportsService (self, sServiceName):
        return (sServiceName in self.tSupportedServiceNames)

    def getSupportedServiceNames (self):
        return self.tSupportedServiceNames

    # XSupportedLocales
    def hasLocale (self, aLocale):
        if aLocale in self.locales:
            return True
        for e in self.locales:
            if aLocale.Language == e.Language and (e.Country == aLocale.Country or e.Country == ""):
                return True
        return False
    
    def getLocales (self):
        return self.locales
    
    # XSpellChecker
    # http://www.openoffice.org/api/docs/common/ref/com/sun/star/linguistic2/XSpellChecker.html
    def isValid (self, aWord, rLocale, aProperties):
        try:
            aWord = zElidedWords.sub("", aWord.rstrip("."), count=1)
            return self.oGraphspell.isValidToken(aWord)
        except:
            traceback.print_exc()
        return False

    def spell (self, aWord, aLocale, aProperties):
        "returns an object SpellAlternatives"
        lSugg = []
        for l in self.oGraphspell.suggest(aWord):
            lSugg.extend(l)
        return SpellAlternatives(aWord, tuple(lSugg))
        try:
            if not self.xHunspell:
                self.xHunspell = self.xSvMgr.createInstance("com.sun.star.linguistic2.SpellChecker")
            if self.xHunspell:
                return self.xHunspell.spell(aWord, self.xHunspellLocale, aProperties)
        except:
            traceback.print_exc()
        return None

    # XServiceDisplayName
    def getServiceDisplayName(self, aLocale):
        return "Graphspell (fr)"


class SpellAlternatives (unohelper.Base, XSpellAlternatives):
    
    def __init__ (self, sWord, lSugg):
        try:
            self.sWord = sWord
            self.lSugg = lSugg
            self.xLocale = Locale('fr', 'FR', '')
        except:
            traceback.print_exc()
    
    # XSpellAlternatives
    # http://www.openoffice.org/api/docs/common/ref/com/sun/star/linguistic2/XSpellAlternatives.html
    def getWord (self):
        return self.sWord
    
    def getLocale (self):
        return self.xLocale
    
    def getFailureType (self):
        return 4
        # IS_NEGATIVE_WORD = 2
        #   The word is a negative one, that is, it should not be used.
        # CAPTION_ERROR = 3
        #   The capitalization of the word is wrong.
        # SPELLING_ERROR = 4
        #   The spelling of the word is wrong (or at least not known to be correct).
        # No difference -> red underline
    
    def getAlternativesCount (self):
        return len(self.lSugg)
    
    def getAlternatives (self):
        return self.lSugg


g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(Graphspell, "net.grammalecte.graphspell", ("com.sun.star.linguistic2.SpellChecker",),)