| 1 |
# -*- encoding: UTF-8 -*- |
| 2 |
import uno |
| 3 |
import re |
| 4 |
import sys |
| 5 |
import os |
| 6 |
import traceback |
| 7 |
from com.sun.star.text.TextMarkupType import PROOFREADING |
| 8 |
from com.sun.star.beans import PropertyValue |
| 9 |
|
| 10 |
pkg = "ru_RU" |
| 11 |
lang = "ru_RU" |
| 12 |
locales = {'ru-RU': ['ru', 'RU', '']} |
| 13 |
version = "0.3.4" |
| 14 |
author = "Yakov Reztsov <yr at myooo dot ru>" |
| 15 |
name = "Lightproof grammar checker (Russian)" |
| 16 |
|
| 17 |
import lightproof_handler_ru_RU |
| 18 |
|
| 19 |
# loaded rules (check for Update mechanism of the editor) |
| 20 |
try: |
| 21 |
langrule |
| 22 |
except NameError: |
| 23 |
langrule = {} |
| 24 |
|
| 25 |
# ignored rules |
| 26 |
ignore = {} |
| 27 |
|
| 28 |
# cache for morphogical analyses |
| 29 |
analyses = {} |
| 30 |
stems = {} |
| 31 |
suggestions = {} |
| 32 |
|
| 33 |
# assign Calc functions |
| 34 |
calcfunc = None |
| 35 |
|
| 36 |
# check settings |
| 37 |
def option(lang, opt): |
| 38 |
return lightproof_handler_ru_RU.get_option(lang.Language + "_" + lang.Country, opt) |
| 39 |
|
| 40 |
# filtering affix fields (ds, is, ts etc.) |
| 41 |
def onlymorph(st): |
| 42 |
if st is not None: |
| 43 |
st = re.sub(r"^.*(st:|po:)", r"\\1", st) # keep last word part |
| 44 |
st = re.sub(r"\\b(?=[dit][sp]:)","@", st) # and its affixes |
| 45 |
st = re.sub(r"(?<!@)\\b\w\w:\w+","", st).replace('@','').strip() |
| 46 |
return st |
| 47 |
|
| 48 |
# if the pattern matches all analyses of the input word, |
| 49 |
# return the last matched substring |
| 50 |
def _morph(rLoc, word, pattern, all, onlyaffix): |
| 51 |
global analyses |
| 52 |
if not word: |
| 53 |
return None |
| 54 |
if word not in analyses: |
| 55 |
x = spellchecker.spell(u"<?xml?><query type='analyze'><word>" + word + "</word></query>", rLoc, ()) |
| 56 |
if not x: |
| 57 |
return None |
| 58 |
t = x.getAlternatives() |
| 59 |
if not t: |
| 60 |
if not analyses: # fix synchronization problem (missing alternatives with unloaded dictionary) |
| 61 |
return None |
| 62 |
t = [""] |
| 63 |
analyses[word] = t[0].split("</a>")[:-1] |
| 64 |
a = analyses[word] |
| 65 |
result = None |
| 66 |
p = re.compile(pattern) |
| 67 |
for i in a: |
| 68 |
if onlyaffix: |
| 69 |
i = onlymorph(i) |
| 70 |
result = p.search(i) |
| 71 |
if result: |
| 72 |
result = result.group(0) |
| 73 |
if not all: |
| 74 |
return result |
| 75 |
elif all: |
| 76 |
return None |
| 77 |
return result |
| 78 |
|
| 79 |
def morph(rLoc, word, pattern, all=True): |
| 80 |
return _morph(rLoc, word, pattern, all, False) |
| 81 |
|
| 82 |
def affix(rLoc, word, pattern, all=True): |
| 83 |
return _morph(rLoc, word, pattern, all, True) |
| 84 |
|
| 85 |
def spell(rLoc, word): |
| 86 |
if not word: |
| 87 |
return None |
| 88 |
return spellchecker.isValid(word, rLoc, ()) |
| 89 |
|
| 90 |
# get the tuple of the stem of the word or an empty array |
| 91 |
def stem(rLoc, word): |
| 92 |
global stems |
| 93 |
if not word: |
| 94 |
return [] |
| 95 |
if word not in stems: |
| 96 |
x = spellchecker.spell(u"<?xml?><query type='stem'><word>" + word + "</word></query>", rLoc, ()) |
| 97 |
if not x: |
| 98 |
return [] |
| 99 |
t = x.getAlternatives() |
| 100 |
if not t: |
| 101 |
t = [] |
| 102 |
stems[word] = list(t) |
| 103 |
return stems[word] |
| 104 |
|
| 105 |
# get the tuple of the morphological generation of a word or an empty array |
| 106 |
def generate(rLoc, word, example): |
| 107 |
if not word: |
| 108 |
return [] |
| 109 |
x = spellchecker.spell(u"<?xml?><query type='generate'><word>" + word + "</word><word>" + example + "</word></query>", rLoc, ()) |
| 110 |
if not x: |
| 111 |
return [] |
| 112 |
t = x.getAlternatives() |
| 113 |
if not t: |
| 114 |
t = [] |
| 115 |
return list(t) |
| 116 |
|
| 117 |
# get suggestions |
| 118 |
def suggest(rLoc, word): |
| 119 |
global suggestions |
| 120 |
if not word: |
| 121 |
return word |
| 122 |
if word not in suggestions: |
| 123 |
x = spellchecker.spell("_" + word, rLoc, ()) |
| 124 |
if not x: |
| 125 |
return word |
| 126 |
t = x.getAlternatives() |
| 127 |
suggestions[word] = "|".join(t) |
| 128 |
return suggestions[word] |
| 129 |
|
| 130 |
# get the nth word of the input string or None |
| 131 |
def word(s, n): |
| 132 |
a = re.match("(?u)( [-.\\w%%]+){" + str(n-1) + "}( [-.\\w%%]+)", s) |
| 133 |
if not a: |
| 134 |
return '' |
| 135 |
return a.group(2)[1:] |
| 136 |
|
| 137 |
# get the (-)nth word of the input string or None |
| 138 |
def wordmin(s, n): |
| 139 |
a = re.search("(?u)([-.\\w%%]+ )([-.\\w%%]+ ){" + str(n-1) + "}$", s) |
| 140 |
if not a: |
| 141 |
return '' |
| 142 |
return a.group(1)[:-1] |
| 143 |
|
| 144 |
def calc(funcname, par): |
| 145 |
global calcfunc |
| 146 |
global SMGR |
| 147 |
if calcfunc is None: |
| 148 |
calcfunc = SMGR.createInstance( "com.sun.star.sheet.FunctionAccess") |
| 149 |
if calcfunc is None: |
| 150 |
return None |
| 151 |
return calcfunc.callFunction(funcname, par) |
| 152 |
|
| 153 |
def proofread( nDocId, TEXT, LOCALE, nStartOfSentencePos, nSuggestedSentenceEndPos, rProperties ): |
| 154 |
global ignore |
| 155 |
aErrs = [] |
| 156 |
s = TEXT[nStartOfSentencePos:nSuggestedSentenceEndPos] |
| 157 |
for i in get_rule(LOCALE).dic: |
| 158 |
# 0: regex, 1: replacement, 2: message, 3: condition, 4: ngroup, (5: oldline), 6: case sensitive ? |
| 159 |
if i[0] and str(i[0]) not in ignore: |
| 160 |
for m in i[0].finditer(s): |
| 161 |
try: |
| 162 |
if not i[3] or eval(i[3]): |
| 163 |
aErr = uno.createUnoStruct( "com.sun.star.linguistic2.SingleProofreadingError" ) |
| 164 |
aErr.nErrorStart = nStartOfSentencePos + m.start(i[4]) # nStartOfSentencePos |
| 165 |
aErr.nErrorLength = m.end(i[4]) - m.start(i[4]) |
| 166 |
aErr.nErrorType = PROOFREADING |
| 167 |
aErr.aRuleIdentifier = str(i[0]) |
| 168 |
iscap = (i[-1] and m.group(i[4])[0:1].isupper()) |
| 169 |
if i[1][0:1] == "=": |
| 170 |
aErr.aSuggestions = tuple(cap(eval(i[1][1:]).replace('|', "\n").split("\n"), iscap, LOCALE)) |
| 171 |
elif i[1] == "_": |
| 172 |
aErr.aSuggestions = () |
| 173 |
else: |
| 174 |
aErr.aSuggestions = tuple(cap(m.expand(i[1]).replace('|', "\n").split("\n"), iscap, LOCALE)) |
| 175 |
comment = i[2] |
| 176 |
if comment[0:1] == "=": |
| 177 |
comment = eval(comment[1:]) |
| 178 |
else: |
| 179 |
comment = m.expand(comment) |
| 180 |
aErr.aShortComment = comment.replace('|', '\n').replace('\\n', '\n').split("\n")[0].strip() |
| 181 |
aErr.aFullComment = comment.replace('|', '\n').replace('\\n', '\n').split("\n")[-1].strip() |
| 182 |
if "://" in aErr.aFullComment: |
| 183 |
p = PropertyValue() |
| 184 |
p.Name = "FullCommentURL" |
| 185 |
p.Value = aErr.aFullComment |
| 186 |
aErr.aFullComment = aErr.aShortComment |
| 187 |
aErr.aProperties = (p,) |
| 188 |
else: |
| 189 |
aErr.aProperties = () |
| 190 |
aErrs = aErrs + [aErr] |
| 191 |
except Exception as e: |
| 192 |
if len(i) == 7: |
| 193 |
raise Exception(str(e), i[5]) |
| 194 |
raise |
| 195 |
|
| 196 |
return tuple(aErrs) |
| 197 |
|
| 198 |
def cap(a, iscap, rLoc): |
| 199 |
if iscap: |
| 200 |
for i in range(0, len(a)): |
| 201 |
if a[i][0:1] == "i": |
| 202 |
if rLoc.Language == "tr" or rLoc.Language == "az": |
| 203 |
a[i] = u"\u0130" + a[i][1:] |
| 204 |
elif a[i][1:2] == "j" and rLoc.Language == "nl": |
| 205 |
a[i] = "IJ" + a[i][2:] |
| 206 |
else: |
| 207 |
a[i] = "I" + a[i][1:] |
| 208 |
else: |
| 209 |
a[i] = a[i].capitalize() |
| 210 |
return a |
| 211 |
|
| 212 |
def compile_rules(dic): |
| 213 |
# compile regular expressions |
| 214 |
for i in dic: |
| 215 |
try: |
| 216 |
if re.compile("[(][?]iu[)]").match(i[0]): |
| 217 |
i += [True] |
| 218 |
i[0] = re.sub("[(][?]iu[)]", "(?u)", i[0]) |
| 219 |
else: |
| 220 |
i += [False] |
| 221 |
i[0] = re.compile(i[0]) |
| 222 |
except Exception: |
| 223 |
if 'PYUNO_LOGLEVEL' in os.environ: |
| 224 |
print("Lightproof: bad regular expression: " + str(traceback.format_exc())) |
| 225 |
i[0] = None |
| 226 |
|
| 227 |
def get_rule(loc): |
| 228 |
try: |
| 229 |
return langrule[pkg] |
| 230 |
except Exception: |
| 231 |
langrule[pkg] = __import__("lightproof_" + pkg) |
| 232 |
compile_rules(langrule[pkg].dic) |
| 233 |
return langrule[pkg] |
| 234 |
|
| 235 |
def get_path(): |
| 236 |
return os.path.join(os.path.dirname(sys.modules[__name__].__file__), __name__ + ".py") |
| 237 |
|
| 238 |
# [code] |
| 239 |
|
| 240 |
|