Grammalecte  Check-in [7ef0b6ae8a]

Overview
Comment:[lo] convert errors position if special chars are found (Unicode > \U0001000)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | lo
Files: files | file ages | folders
SHA3-256: 7ef0b6ae8a7c33d1b9183d086dbeb3c97eb3d27d0ef8ddff32742e0d6165b108
User & Date: olr on 2021-03-11 13:24:58
Other Links: manifest | tags
Context
2021-03-11
15:41
[fr] ajustements check-in: 98ffeee011 user: olr tags: trunk, fr
13:24
[lo] convert errors position if special chars are found (Unicode > \U0001000) check-in: 7ef0b6ae8a user: olr tags: trunk, lo
2021-03-10
20:22
[fr] faux positif check-in: 0915b69239 user: olr tags: trunk, fr
Changes

Modified gc_core/py/oxt/Grammalecte.py from [d7f4535da4] to [8b3d7a692c].

1
2
3
4
5
6
7
8
9

10





11
12
13
14
15
16
17
# Grammalecte for Writer
# License: MPL 2
# A derivative work of Lightproof from László Németh (http://cgit.freedesktop.org/libreoffice/lightproof/)

import uno
import unohelper
import json
import sys
import traceback

from collections import deque






from com.sun.star.linguistic2 import XProofreader, XSupportedLocales
from com.sun.star.linguistic2 import ProofreadingResult
from com.sun.star.lang import XServiceInfo, XServiceName, XServiceDisplayName
from com.sun.star.lang import Locale

import helpers




|
|
|


>

>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Grammalecte for Writer
# License: MPL 2
# A derivative work of Lightproof from László Németh (http://cgit.freedesktop.org/libreoffice/lightproof/)


import json
import re
import sys
import traceback

from collections import deque
from operator import itemgetter
from bisect import bisect_left

import uno
import unohelper

from com.sun.star.linguistic2 import XProofreader, XSupportedLocales
from com.sun.star.linguistic2 import ProofreadingResult
from com.sun.star.lang import XServiceInfo, XServiceName, XServiceDisplayName
from com.sun.star.lang import Locale

import helpers
41
42
43
44
45
46
47


48
49
50
51
52
53
54
        #opt_handler.load(xContext)
        dOpt = Options.loadOptions("${lang}")
        gce.setOptions(dOpt)
        # dictionaries options
        self.loadUserDictionaries()
        # underlining options
        self.setWriterUnderliningStyle()


        # store for results of big paragraphs
        self.dResult = {}
        self.nMaxRes = 1500
        self.lLastRes = deque(maxlen=self.nMaxRes)
        self.nRes = 0









>
>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        #opt_handler.load(xContext)
        dOpt = Options.loadOptions("${lang}")
        gce.setOptions(dOpt)
        # dictionaries options
        self.loadUserDictionaries()
        # underlining options
        self.setWriterUnderliningStyle()
        # regex for special chars that modify positioning
        self.zSpecialChars = re.compile("[\U00010000-\U0001fbff]")
        # store for results of big paragraphs
        self.dResult = {}
        self.nMaxRes = 1500
        self.lLastRes = deque(maxlen=self.nMaxRes)
        self.nRes = 0


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
        # 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 = tuple(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:
            traceback.print_exc()


        return xRes

    def ignoreRule (self, rid, aLocale):
        gce.ignoreRule(rid)

    def resetIgnoreRules (self):
        gce.resetIgnoreRules()







<
<

|
>
>
>
>
>











>
>







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
        # 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

    def ignoreRule (self, rid, aLocale):
        gce.ignoreRule(rid)

    def resetIgnoreRules (self):
        gce.resetIgnoreRules()
156
157
158
159
160
161
162




















163
164
165
166
167
168
169
            xSettingNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Other/", False)
            xChild = xSettingNode.getByName("o_${lang}")
            sLineType = xChild.getPropertyValue("line_type")
            bMulticolor = bool(xChild.getPropertyValue("line_multicolor"))
            gce.setWriterUnderliningStyle(sLineType, bMulticolor)
        except:
            traceback.print_exc()






















g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(Grammalecte, "org.openoffice.comp.pyuno.Lightproof."+gce.pkg, ("com.sun.star.linguistic2.Proofreader",),)

# g_ImplementationHelper.addImplementation( opt_handler.LightproofOptionsEventHandler, \
#     "org.openoffice.comp.pyuno.LightproofOptionsEventHandler." + gce.pkg, ("com.sun.star.awt.XContainerWindowEventHandler",),)







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
            xSettingNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Other/", False)
            xChild = xSettingNode.getByName("o_${lang}")
            sLineType = xChild.getPropertyValue("line_type")
            bMulticolor = bool(xChild.getPropertyValue("line_multicolor"))
            gce.setWriterUnderliningStyle(sLineType, bMulticolor)
        except:
            traceback.print_exc()

    def convertErrorsPosition (self, sText, aErrors):
        "return list of errors with modified position for Writer"
        # last char position of the last error
        nCheckEnd = 0
        for xErr in aErrors:
            nCheckEnd = max(xErr.nErrorStart + xErr.nErrorLength, nCheckEnd)
        # list thresholds of offsets
        lThresholds = []
        for iCursor in range(nCheckEnd):
            nCharVal = ord(sText[iCursor])
            if nCharVal > 65535:    # \U00010000: each chars beyond this point has a length of 2
                lThresholds.append(iCursor + 1)  # +1 because only chars after are shifted
        # modify errors position according to thresholds
        for xErr in aErrors:
            nErrorEnd = xErr.nErrorStart + xErr.nErrorLength
            xErr.nErrorStart += bisect_left(lThresholds, xErr.nErrorStart)
            nErrorEnd += bisect_left(lThresholds, nErrorEnd)
            xErr.nErrorLength = nErrorEnd - xErr.nErrorStart
        return len(lThresholds)


g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(Grammalecte, "org.openoffice.comp.pyuno.Lightproof."+gce.pkg, ("com.sun.star.linguistic2.Proofreader",),)

# g_ImplementationHelper.addImplementation( opt_handler.LightproofOptionsEventHandler, \
#     "org.openoffice.comp.pyuno.LightproofOptionsEventHandler." + gce.pkg, ("com.sun.star.awt.XContainerWindowEventHandler",),)