-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProofreader.py
158 lines (130 loc) · 5.71 KB
/
Proofreader.py
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
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
from com.sun.star.awt.MessageBoxType import ERRORBOX
from com.sun.star.beans import PropertyValue
from com.sun.star.lang import Locale
from com.sun.star.lang import XServiceInfo, XServiceName, XServiceDisplayName
from com.sun.star.linguistic2 import XProofreader, XSupportedLocales
from com.sun.star.text.TextMarkupType import PROOFREADING
from textwrap import wrap
import sys
import uno
import unohelper
try:
from reynir_correct import check_with_stats
except ImportError:
def check_with_stats(text):
return {"paragraphs": []}
message = """reynir_correct not found, fix with:
{0} -m pip install -U reynir_correct""".format(sys.executable)
try:
import pip
except ImportError:
message = """Download https://bootstrap.pypa.io/get-pip.py and run:
{0} get-pip.py""".format(sys.executable) + "\n\n" + message
ctx = uno.getComponentContext()
sManager = ctx.ServiceManager
toolkit = sManager.createInstance("com.sun.star.awt.Toolkit")
msgbox = toolkit.createMessageBox(None, ERRORBOX, BUTTONS_OK, "Error initializing LibreOffice-GreynirCorrect", message)
msgbox.execute()
ImplName = "libreoffice-greynircorrect"
SupportedServiceNames = ("com.sun.star.linguistic2.Proofreader",)
def check_grammar(text):
errors = []
offset = 0
result = check_with_stats(text)
for pg in result["paragraphs"]:
for sent in pg:
token_idx = []
for t in sent.tokens:
token_idx.append(offset)
offset += len(t.original)
for a in sent.annotations:
aErr = uno.createUnoStruct("com.sun.star.linguistic2.SingleProofreadingError")
aErr.nErrorType = PROOFREADING
aErr.aRuleIdentifier = a.code
aErr.aSuggestions = tuple([a.suggest]) if a.suggest else ()
aErr.aShortComment = a.text
if a.detail:
aErr.aFullComment = aErr.aShortComment + "\n\n" + "\n".join(wrap(a.detail, width=80))
else:
aErr.aFullComment = aErr.aShortComment
aErr.nErrorStart = token_idx[a.start]
while text[aErr.nErrorStart] == " " and (aErr.nErrorStart + 1) < len(text):
aErr.nErrorStart = aErr.nErrorStart + 1
if (a.end + 1) < len(token_idx):
end_idx = token_idx[a.end + 1]
else:
end_idx = len(text)
while end_idx > 0 and text[end_idx - 1] == " ":
end_idx = end_idx - 1
aErr.nErrorLength = end_idx - aErr.nErrorStart
# TODO
# p = PropertyValue()
# p.Name = "FullCommentURL"
# p.Value = "https://ritreglur.arnastofnun.is/#{0}".format(a.references)
# aErr.aProperties = (p,)
aErr.aProperties = ()
errors.append(aErr)
return tuple(errors)
class Impl(unohelper.Base, XProofreader, XServiceInfo, XServiceDisplayName, XServiceName, XSupportedLocales):
def __init__(self, ctx, *args):
self.locales = tuple([Locale('is', 'IS', '')])
self.ignore_rules = set()
# XServiceDisplayName
def getServiceDisplayName(self, aLocale):
return "GreynirCorrect: A spelling and grammar corrector for Icelandic"
# XServiceName
def getServiceName(self):
# TODO: Should this be ServiceName?
return ImplName
# XServiceInfo
def getImplementationName(self):
return ImplName
# XServiceInfo
def supportsService(self, ServiceName):
return (ServiceName in SupportedServiceNames)
# XServiceInfo
def getSupportedServiceNames(self):
return SupportedServiceNames
# XSupportedLocales
def hasLocale(self, aLocale):
return aLocale.Language == "is"
# XSupportedLocales
def getLocales(self):
return self.locales
# XProofreader
def isSpellChecker(self):
return True
# XProofreader
def doProofreading(self, nDocId, rText, rLocale, nStartOfSentencePos, nSuggestedSentenceEndPos, rProperties):
aRes = uno.createUnoStruct("com.sun.star.linguistic2.ProofreadingResult")
aRes.aDocumentIdentifier = nDocId
aRes.aText = rText
aRes.aLocale = rLocale
aRes.nStartOfSentencePosition = nStartOfSentencePos
aRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos
aRes.aProperties = () # TODO: should this be rProperties ?
aRes.xProofreader = self
# PATCH FOR LO 4
# Fix for http://nabble.documentfoundation.org/Grammar-checker-Undocumented-change-in-the-API-for-LO-4-td4030639.html
if nStartOfSentencePos != 0:
return aRes
aRes.nStartOfNextSentencePosition = len(rText)
# END OF PATCH
l = rText[aRes.nStartOfNextSentencePosition:aRes.nStartOfNextSentencePosition+1]
while l == " ":
aRes.nStartOfNextSentencePosition = aRes.nStartOfNextSentencePosition + 1
l = rText[aRes.nStartOfNextSentencePosition:aRes.nStartOfNextSentencePosition+1]
if aRes.nStartOfNextSentencePosition == nSuggestedSentenceEndPos and l!="":
aRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos + 1
aRes.nBehindEndOfSentencePosition = aRes.nStartOfNextSentencePosition
aRes.aErrors = check_grammar(rText)
return aRes
# XProofreader
def ignoreRule(self, rid, aLocale):
self.ignore_rules.add(rid)
# XProofreader
def resetIgnoreRules(self):
self.ignore_rules.clear()
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(Impl, ImplName, SupportedServiceNames)