Debian Codegraph @

dictionaries/hu_HU/pythonpath/lightproof_impl_hu_HU.py

raw symbols SCIP
1 # -*- encoding: UTF-8 -*-
2 from __future__ import unicode_literals
3 import uno
4 import re
5 import sys
6 import os
7 import traceback
8 from com.sun.star.text.TextMarkupType import PROOFREADING
9 from com.sun.star.beans import PropertyValue
10
11 pkg = "hu_HU"
12 lang = "hu_HU"
13 locales = {'hu-HU': ['hu', 'HU', '']}
14 version = "1.6.4"
15 author = "László Németh"
16 name = "Lightproof grammar checker (magyar)"
17
18 import lightproof_handler_hu_HU
19
20 # loaded rules (check for Update mechanism of the editor)
21 try:
22 langrule
23 except NameError:
24 langrule = {}
25
26 # ignored rules
27 ignore = {}
28
29 # cache for morphogical analyses
30 analyses = {}
31 stems = {}
32 suggestions = {}
33
34 # assign Calc functions
35 calcfunc = None
36
37 # check settings
38 def option(lang, opt):
39 return lightproof_handler_hu_HU.get_option(lang.Language + "_" + lang.Country, opt)
40
41 # filtering affix fields (ds, is, ts etc.)
42 def onlymorph(st):
43 if st is not None:
44 st = re.sub(r"^.*(st:|po:)", r"\\1", st) # keep last word part
45 st = re.sub(r"\\b(?=[dit][sp]:)","@", st) # and its affixes
46 st = re.sub(r"(?<!@)\\b\w\w:\w+","", st).replace('@','').strip()
47 return st
48
49 # if the pattern matches all analyses of the input word,
50 # return the last matched substring
51 def _morph(rLoc, word, pattern, all, onlyaffix):
52 global analyses
53 if not word:
54 return None
55 if word not in analyses:
56 x = spellchecker.spell(u"<?xml?><query type='analyze'><word>" + word + "</word></query>", rLoc, ())
57 if not x:
58 return None
59 t = x.getAlternatives()
60 if not t:
61 if not analyses: # fix synchronization problem (missing alternatives with unloaded dictionary)
62 return None
63 t = [""]
64 analyses[word] = t[0].split("</a>")[:-1]
65 a = analyses[word]
66 result = None
67 p = re.compile(pattern)
68 for i in a:
69 if onlyaffix:
70 i = onlymorph(i)
71 result = p.search(i)
72 if result:
73 result = result.group(0)
74 if not all:
75 return result
76 elif all:
77 return None
78 return result
79
80 def morph(rLoc, word, pattern, all=True):
81 return _morph(rLoc, word, pattern, all, False)
82
83 def affix(rLoc, word, pattern, all=True):
84 return _morph(rLoc, word, pattern, all, True)
85
86 def spell(rLoc, word):
87 if not word:
88 return None
89 return spellchecker.isValid(word, rLoc, ())
90
91 # get the tuple of the stem of the word or an empty array
92 def stem(rLoc, word):
93 global stems
94 if not word:
95 return []
96 if word not in stems:
97 x = spellchecker.spell(u"<?xml?><query type='stem'><word>" + word + "</word></query>", rLoc, ())
98 if not x:
99 return []
100 t = x.getAlternatives()
101 if not t:
102 t = []
103 stems[word] = list(t)
104 return stems[word]
105
106 # get the tuple of the morphological generation of a word or an empty array
107 def generate(rLoc, word, example):
108 if not word:
109 return []
110 x = spellchecker.spell(u"<?xml?><query type='generate'><word>" + word + "</word><word>" + example + "</word></query>", rLoc, ())
111 if not x:
112 return []
113 t = x.getAlternatives()
114 if not t:
115 t = []
116 return list(t)
117
118 # get suggestions
119 def suggest(rLoc, word):
120 global suggestions
121 if not word:
122 return word
123 if word not in suggestions:
124 x = spellchecker.spell("_" + word, rLoc, ())
125 if not x:
126 return word
127 t = x.getAlternatives()
128 suggestions[word] = "|".join(t)
129 return suggestions[word]
130
131 # get the nth word of the input string or None
132 def word(s, n):
133 a = re.match("(?u)( [-.\\w%%]+){" + str(n-1) + "}( [-.\\w%%]+)", s)
134 if not a:
135 return ''
136 return a.group(2)[1:]
137
138 # get the (-)nth word of the input string or None
139 def wordmin(s, n):
140 a = re.search("(?u)([-.\\w%%]+ )([-.\\w%%]+ ){" + str(n-1) + "}$", s)
141 if not a:
142 return ''
143 return a.group(1)[:-1]
144
145 def calc(funcname, par):
146 global calcfunc
147 global SMGR
148 if calcfunc is None:
149 calcfunc = SMGR.createInstance( "com.sun.star.sheet.FunctionAccess")
150 if calcfunc is None:
151 return None
152 return calcfunc.callFunction(funcname, par)
153
154 def proofread( nDocId, TEXT, LOCALE, nStartOfSentencePos, nSuggestedSentenceEndPos, rProperties ):
155 global ignore
156 aErrs = []
157 s = TEXT[nStartOfSentencePos:nSuggestedSentenceEndPos]
158 for i in get_rule(LOCALE).dic:
159 # 0: regex, 1: replacement, 2: message, 3: condition, 4: ngroup, (5: oldline), 6: case sensitive ?
160 if i[0] and str(i[0]) not in ignore:
161 for m in i[0].finditer(s):
162 try:
163 if not i[3] or eval(i[3]):
164 aErr = uno.createUnoStruct( "com.sun.star.linguistic2.SingleProofreadingError" )
165 aErr.nErrorStart = nStartOfSentencePos + m.start(i[4]) # nStartOfSentencePos
166 aErr.nErrorLength = m.end(i[4]) - m.start(i[4])
167 aErr.nErrorType = PROOFREADING
168 aErr.aRuleIdentifier = str(i[0])
169 iscap = (i[-1] and m.group(i[4])[0:1].isupper())
170 if i[1][0:1] == "=":
171 aErr.aSuggestions = tuple(cap(eval(i[1][1:]).replace('|', "\n").split("\n"), iscap, LOCALE))
172 elif i[1] == "_":
173 aErr.aSuggestions = ()
174 else:
175 aErr.aSuggestions = tuple(cap(m.expand(i[1]).replace('|', "\n").split("\n"), iscap, LOCALE))
176 comment = i[2]
177 if comment[0:1] == "=":
178 comment = eval(comment[1:])
179 else:
180 comment = m.expand(comment)
181 aErr.aShortComment = comment.replace('|', '\n').replace('\\n', '\n').split("\n")[0].strip()
182 aErr.aFullComment = comment.replace('|', '\n').replace('\\n', '\n').split("\n")[-1].strip()
183 if "://" in aErr.aFullComment:
184 p = PropertyValue()
185 p.Name = "FullCommentURL"
186 p.Value = aErr.aFullComment
187 aErr.aFullComment = aErr.aShortComment
188 aErr.aProperties = (p,)
189 else:
190 aErr.aProperties = ()
191 aErrs = aErrs + [aErr]
192 except Exception as e:
193 if len(i) == 7:
194 raise Exception(str(e), i[5])
195 raise
196
197 return tuple(aErrs)
198
199 def cap(a, iscap, rLoc):
200 for i in range(0, len(a)):
201 if a[i][0:6] == "!CASE!":
202 a[i] = a[i][6:]
203 elif iscap:
204 if a[i][0:1] == "i":
205 if rLoc.Language == "tr" or rLoc.Language == "az":
206 a[i] = u"\u0130" + a[i][1:]
207 elif a[i][1:2] == "j" and rLoc.Language == "nl":
208 a[i] = "IJ" + a[i][2:]
209 else:
210 a[i] = "I" + a[i][1:]
211 else:
212 a[i] = a[i].capitalize()
213 return a
214
215 def compile_rules(dic):
216 # compile regular expressions
217 for i in dic:
218 try:
219 if re.compile("[(][?]iu[)]").match(i[0]):
220 i += [True]
221 i[0] = re.sub("[(][?]iu[)]", "(?u)", i[0])
222 else:
223 i += [False]
224 i[0] = re.compile(i[0])
225 except Exception:
226 if 'PYUNO_LOGLEVEL' in os.environ:
227 print("Lightproof: bad regular expression: " + str(traceback.format_exc()))
228 i[0] = None
229
230 def get_rule(loc):
231 try:
232 return langrule[pkg]
233 except Exception:
234 langrule[pkg] = __import__("lightproof_" + pkg)
235 compile_rules(langrule[pkg].dic)
236 return langrule[pkg]
237
238 def get_path():
239 return os.path.join(os.path.dirname(sys.modules[__name__].__file__), __name__ + ".py")
240
241 # [code]
242
243
244 abbrev=re.compile(r"(?i)\\b([a-z\xf6\xfc\xf3\u0151\xfa\xe9\xe1\u0171\xed\xd6\xdc\xd3\u0150\xda\xc9\xc1\u0170\xcd]|\xc1e|\xc1ht|AkH|al|\xe1lt|\xe1pr|aug|Avtv|bek|Bp|br|bt|Btk|cca|ci(i|ii|v|x)?|cl(i|ii|iii|iv|ix|v|vi|vii|viii|x|xi|xii|xiii|xiv|xix|xv|xvi|xvii|xviii|xx|xxi|xxii|xxiii|xxiv|xxix|xxv|xxvi|xxvii|xxviii|xxx|xxxi|xxxii|xxxiii|xxxiv|xxxix|xxxv|xxxvi|xxxvii|xxxviii)?|Co|cv(i|ii|iii)?|cx(c|ci|cii|ciii|civ|cix|cv|cvi|cvii|cviii|i|ii|iii|iv|ix|l|li|lii|liii|liv|lix|lv|lvi|lvii|lviii|v|vi|vii|viii|x|xi|xii|xiii|xiv|xix|xv|xvi|xvii|xviii|xx|xxi|xxii|xxiii|xxiv|xxix|xxv|xxvi|xxvii|xxviii)?|cs|Csjt|Cstv|cs\xfct|dec|dk|dny|dr|du|dz(s)?|egy|\xe9k|\xc9Ksz|em|\xe9ny|\xc9pt|\xe9rk|etc|Etv|e\xfc|ev|\xe9vf|febr|felv|Flt|ford|f\u0151isk|fsz(la|t)?|Ftv|gimn|g\xf6r|gr|Gt|gy|Gyvt|habil|hg|hiv|Hjt|honv|Hpt|hrsz|hsz|Hszt|htb|id|ifj|ig(h)?|ii(i)?|ill|Inc|ind|isk|iv|ix|izr|jan|jegyz|j\xfal|j\xfan|kat|kb|Kbt|ker|kft|kgy|kht|kir|kiv|Kjt|kk(t)?|koll|korm|k\xf6v|kp|Kr|krt|Kt(v)?|ld|li(i|ii|v|x)?|Ltd|ltp|Ltv|luth|lv(i|ii|iii)?|lx(i|ii|iii|iv|ix|v|vi|vii|viii|x|xi|xii|xiii|xiv|xix|xv|xvi|xvii|xviii|xx|xxi|xxii|xxiii|xxiv|xxix|xxv|xxvi|xxvii|xxviii)?|ly|m\xe1j|m\xe1rc|mat|max|mb|megh|megj|MHSz|min|mk|Mo|Mt|NB|nov|ny(\xe1)?|Nyilv|nyrt|okl|okt|olv|op|orsz|ort|ov(h)?|\xf6ssz|\xd6tv|\xf6zv|Pf|pl(d)?|prof|prot|Ptk|pu|ref|rk(p)?|r\xf3m|r\xf6v|rt|sgt|spec|stb|sz(ept|erk)?|Szjt|szoc|Szt(v)?|sz\xfcl|Tbj|tc|tel|tkp|tszf|tvr|ty|ua|ui|\xfam|\xfan|uo|Ve|Vhr|vi(i|ii)?|v\xf6|vsz|Vt(v)?|xc(i|ii|iii|iv|ix|v|vi|vii|viii)?|xi(i|ii|v|x)?|xl(i|ii|iii|iv|ix|v|vi|vii|viii)?|xv(i|ii|iii)?|xx(i|ii|iii|iv|ix|v|vi|vii|viii|x|xi|xii|xiii|xiv|xix|xv|xvi|xvii|xviii)?|zrt)\\.")
245
246 # pattern for paragraph checking
247 paralcap = re.compile(u"(?u)^[a-z\xf6\xfc\xf3\u0151\xfa\xe9\xe1\u0171\xed].*[.?!] [A-Z\xd6\xdc\xd3\u0150\xda\xc9\xc1\u0170\xcd].*[.?!][)\u201d]?$")
248
249
250 foreign=["ab ovo", "ars poetica", "casus belli", "categoricus imperativus",
251 "coming out", "cash flow", "circulus vitiosus", "corpus delicti", "de facto", "de jure",
252 "delirium tremens", "doctor honoris causa", "et cetera", "fait accompli", "fixa idea",
253 "horribile dictu", "hot dog", "in flagranti", "in medias res", "in memoriam", "in vitro",
254 "in vivo", "magna cum laude", "mea culpa", "memento mori", "mountain bike", "nota bene",
255 "persona non grata", "plein air", "pro bono", "salto mortale", "status quo", "tabula rasa",
256 "terminus technicus", "vice versa", "vis maior"]
257
258 def suggest_foreign(word):
259 return "\n".join([i for i in foreign if word == i.split(" ")[0]])
260
261
262 def measurement(mnum, min, mout, mstr):
263 m = calc("CONVERT", (float(mnum.replace(",", ".").replace(u"\u2212", "-")), min, mout))
264 a = list(set([str(calc("ROUND", (m, 0)))[:-2], str(calc("ROUND", (m, 1))), str(calc("ROUND", (m, 2))), str(m)])) # remove duplicated rounded items
265 a.sort(key=lambda x: len(x)) # sort by string length
266 return (mstr + "|").join(a).replace(".", ",").replace("-", u"\u2212") + mstr
267
268