Grammalecte  Check-in [31ddfb6e1a]

Overview
Comment:[graphspell] add fr module + [core] code clarification: bShowRuleId
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | core | graphspell | rg
Files: files | file ages | folders
SHA3-256: 31ddfb6e1ac58bc4cedd5cf8d3522b7c0201cd0977460991d2bbaf36f81bad60
User & Date: olr on 2018-06-02 18:58:52
Original Comment: [graphspell] add fr module
Other Links: branch diff | manifest | tags
Context
2018-06-02
20:45
[graphspell] suggestion: ca -> ça check-in: 5fdc97b597 user: olr tags: graphspell, rg
18:58
[graphspell] add fr module + [core] code clarification: bShowRuleId check-in: 31ddfb6e1a user: olr tags: core, graphspell, rg
18:58
[graphspell] default suggestions check-in: 8974c2f6d0 user: olr tags: graphspell, new_feature, rg
Changes

Modified gc_core/py/lang_core/gc_engine.py from [362b18f218] to [d669c0fe4f].

    47     47       "analyses the paragraph sText and returns list of errors"
    48     48       #sText = unicodedata.normalize("NFC", sText)
    49     49       aErrors = None
    50     50       sRealText = sText
    51     51       dDA = {}        # Disambiguisator. Key = position; value = list of morphologies
    52     52       dPriority = {}  # Key = position; value = priority
    53     53       dOpt = _dOptions  if not dOptions  else dOptions
           54  +    bShowRuleId = option('idrule')
    54     55   
    55     56       # parse paragraph
    56     57       try:
    57         -        sNew, aErrors = _proofread(sText, sRealText, 0, True, dDA, dPriority, sCountry, dOpt, bDebug, bContext)
           58  +        sNew, aErrors = _proofread(sText, sRealText, 0, True, dDA, dPriority, sCountry, dOpt, bShowRuleId, bDebug, bContext)
    58     59           if sNew:
    59     60               sText = sNew
    60     61       except:
    61     62           raise
    62     63   
    63     64       # cleanup
    64     65       if " " in sText:
................................................................................
    72     73   
    73     74       # parse sentences
    74     75       for iStart, iEnd in _getSentenceBoundaries(sText):
    75     76           if 4 < (iEnd - iStart) < 2000:
    76     77               dDA.clear()
    77     78               try:
    78     79                   # regex parser
    79         -                _, errs = _proofread(sText[iStart:iEnd], sRealText[iStart:iEnd], iStart, False, dDA, dPriority, sCountry, dOpt, bDebug, bContext)
           80  +                _, errs = _proofread(sText[iStart:iEnd], sRealText[iStart:iEnd], iStart, False, dDA, dPriority, sCountry, dOpt, bShowRuleId, bDebug, bContext)
    80     81                   aErrors.update(errs)
    81     82                   # token parser
    82     83                   oSentence = TokenSentence(sText[iStart:iEnd], sRealText[iStart:iEnd], iStart)
    83         -                _, errs = oSentence.parse(dPriority, sCountry, dOpt, bDebug, bContext)
           84  +                _, errs = oSentence.parse(dPriority, sCountry, dOpt, bShowRuleId, bDebug, bContext)
    84     85                   aErrors.update(errs)
    85     86               except:
    86     87                   raise
    87     88       return aErrors.values() # this is a view (iterable)
    88     89   
    89     90   
    90     91   def _getSentenceBoundaries (sText):
    91     92       iStart = _zBeginOfParagraph.match(sText).end()
    92     93       for m in _zEndOfSentence.finditer(sText):
    93     94           yield (iStart, m.end())
    94     95           iStart = m.end()
    95     96   
    96     97   
    97         -def _proofread (s, sx, nOffset, bParagraph, dDA, dPriority, sCountry, dOptions, bDebug, bContext):
           98  +def _proofread (s, sx, nOffset, bParagraph, dDA, dPriority, sCountry, dOptions, bShowRuleId, bDebug, bContext):
    98     99       dErrs = {}
    99    100       bChange = False
   100         -    bIdRule = option('idrule')
   101         -
   102    101       for sOption, lRuleGroup in _getRules(bParagraph):
   103    102           if not sOption or dOptions.get(sOption, False):
   104    103               for zRegex, bUppercase, sLineId, sRuleId, nPriority, lActions in lRuleGroup:
   105    104                   if sRuleId not in _aIgnoredRules:
   106    105                       for m in zRegex.finditer(s):
   107    106                           bCondMemo = None
   108    107                           for sFuncCond, cActionType, sWhat, *eAct in lActions:
................................................................................
   110    109                               try:
   111    110                                   bCondMemo = not sFuncCond or globals()[sFuncCond](s, sx, m, dDA, sCountry, bCondMemo)
   112    111                                   if bCondMemo:
   113    112                                       if cActionType == "-":
   114    113                                           # grammar error
   115    114                                           nErrorStart = nOffset + m.start(eAct[0])
   116    115                                           if nErrorStart not in dErrs or nPriority > dPriority[nErrorStart]:
   117         -                                            dErrs[nErrorStart] = _createRegexError(s, sx, sWhat, nOffset, m, eAct[0], sLineId, sRuleId, bUppercase, eAct[1], eAct[2], bIdRule, sOption, bContext)
          116  +                                            dErrs[nErrorStart] = _createRegexError(s, sx, sWhat, nOffset, m, eAct[0], sLineId, sRuleId, bUppercase, eAct[1], eAct[2], bShowRuleId, sOption, bContext)
   118    117                                               dPriority[nErrorStart] = nPriority
   119    118                                       elif cActionType == "~":
   120    119                                           # text processor
   121    120                                           s = _rewrite(s, sWhat, eAct[0], m, bUppercase)
   122    121                                           bChange = True
   123    122                                           if bDebug:
   124    123                                               echo("~ " + s + "  -- " + m.group(eAct[0]) + "  # " + sLineId)
................................................................................
   137    136                               except Exception as e:
   138    137                                   raise Exception(str(e), "# " + sLineId + " # " + sRuleId)
   139    138       if bChange:
   140    139           return (s, dErrs)
   141    140       return (False, dErrs)
   142    141   
   143    142   
   144         -def _createRegexWriterError (s, sx, sRepl, nOffset, m, iGroup, sLineId, sRuleId, bUppercase, sMsg, sURL, bIdRule, sOption, bContext):
          143  +def _createRegexWriterError (s, sx, sRepl, nOffset, m, iGroup, sLineId, sRuleId, bUppercase, sMsg, sURL, bShowRuleId, sOption, bContext):
   145    144       "error for Writer (LO/OO)"
   146    145       xErr = SingleProofreadingError()
   147    146       #xErr = uno.createUnoStruct( "com.sun.star.linguistic2.SingleProofreadingError" )
   148    147       xErr.nErrorStart = nOffset + m.start(iGroup)
   149    148       xErr.nErrorLength = m.end(iGroup) - m.start(iGroup)
   150    149       xErr.nErrorType = PROOFREADING
   151    150       xErr.aRuleIdentifier = sRuleId
................................................................................
   169    168       # Message
   170    169       if sMsg[0:1] == "=":
   171    170           sMessage = globals()[sMsg[1:]](s, m)
   172    171       else:
   173    172           sMessage = m.expand(sMsg)
   174    173       xErr.aShortComment = sMessage   # sMessage.split("|")[0]     # in context menu
   175    174       xErr.aFullComment = sMessage   # sMessage.split("|")[-1]    # in dialog
   176         -    if bIdRule:
          175  +    if bShowRuleId:
   177    176           xErr.aShortComment += "  # " + sLineId + " # " + sRuleId
   178    177       # URL
   179    178       if sURL:
   180    179           p = PropertyValue()
   181    180           p.Name = "FullCommentURL"
   182    181           p.Value = sURL
   183    182           xErr.aProperties = (p,)
   184    183       else:
   185    184           xErr.aProperties = ()
   186    185       return xErr
   187    186   
   188    187   
   189         -def _createRegexDictError (s, sx, sRepl, nOffset, m, iGroup, sLineId, sRuleId, bUppercase, sMsg, sURL, bIdRule, sOption, bContext):
          188  +def _createRegexDictError (s, sx, sRepl, nOffset, m, iGroup, sLineId, sRuleId, bUppercase, sMsg, sURL, bShowRuleId, sOption, bContext):
   190    189       "error as a dictionary"
   191    190       dErr = {}
   192    191       dErr["nStart"] = nOffset + m.start(iGroup)
   193    192       dErr["nEnd"] = nOffset + m.end(iGroup)
   194    193       dErr["sLineId"] = sLineId
   195    194       dErr["sRuleId"] = sRuleId
   196    195       dErr["sType"] = sOption  if sOption  else "notype"
................................................................................
   213    212               dErr["aSuggestions"] = m.expand(sRepl).split("|")
   214    213       # Message
   215    214       if sMsg[0:1] == "=":
   216    215           sMessage = globals()[sMsg[1:]](s, m)
   217    216       else:
   218    217           sMessage = m.expand(sMsg)
   219    218       dErr["sMessage"] = sMessage
   220         -    if bIdRule:
          219  +    if bShowRuleId:
   221    220           dErr["sMessage"] += "  # " + sLineId + " # " + sRuleId
   222    221       # URL
   223    222       dErr["URL"] = sURL  if sURL  else ""
   224    223       # Context
   225    224       if bContext:
   226    225           dErr['sUnderlined'] = sx[m.start(iGroup):m.end(iGroup)]
   227    226           dErr['sBefore'] = sx[max(0,m.start(iGroup)-80):m.start(iGroup)]
   228    227           dErr['sAfter'] = sx[m.end(iGroup):m.end(iGroup)+80]
   229    228       return dErr
   230    229   
   231    230   
   232         -def _createTokenWriterError (lToken, sSentence, sSentence0, sRepl, iFirstToken, nStart, nEnd, sLineId, sRuleId, bUppercase, sMsg, sURL, bIdRule, sOption, bContext):
          231  +def _createTokenWriterError (lToken, sSentence, sSentence0, sRepl, iFirstToken, nStart, nEnd, sLineId, sRuleId, bUppercase, sMsg, sURL, bShowRuleId, sOption, bContext):
   233    232       "error for Writer (LO/OO)"
   234    233       xErr = SingleProofreadingError()
   235    234       #xErr = uno.createUnoStruct( "com.sun.star.linguistic2.SingleProofreadingError" )
   236    235       xErr.nErrorStart = nStart
   237    236       xErr.nErrorLength = nEnd - nStart
   238    237       xErr.nErrorType = PROOFREADING
   239    238       xErr.aRuleIdentifier = sRuleId
................................................................................
   257    256       # Message
   258    257       if sMsg[0:1] == "=":
   259    258           sMessage = globals()[sMsg[1:]](lToken)
   260    259       else:
   261    260           sMessage = sMsg
   262    261       xErr.aShortComment = sMessage   # sMessage.split("|")[0]     # in context menu
   263    262       xErr.aFullComment = sMessage   # sMessage.split("|")[-1]    # in dialog
   264         -    if bIdRule:
          263  +    if bShowRuleId:
   265    264           xErr.aShortComment += "  " + sLineId + " # " + sRuleId
   266    265       # URL
   267    266       if sURL:
   268    267           p = PropertyValue()
   269    268           p.Name = "FullCommentURL"
   270    269           p.Value = sURL
   271    270           xErr.aProperties = (p,)
   272    271       else:
   273    272           xErr.aProperties = ()
   274    273       return xErr
   275    274   
   276    275                                                            
   277         -def _createTokenDictError (lToken, sSentence, sSentence0, sRepl, iFirstToken, nStart, nEnd, sLineId, sRuleId, bUppercase, sMsg, sURL, bIdRule, sOption, bContext):
          276  +def _createTokenDictError (lToken, sSentence, sSentence0, sRepl, iFirstToken, nStart, nEnd, sLineId, sRuleId, bUppercase, sMsg, sURL, bShowRuleId, sOption, bContext):
   278    277       "error as a dictionary"
   279    278       dErr = {}
   280    279       dErr["nStart"] = nStart
   281    280       dErr["nEnd"] = nEnd
   282    281       dErr["sLineId"] = sLineId
   283    282       dErr["sRuleId"] = sRuleId
   284    283       dErr["sType"] = sOption  if sOption  else "notype"
................................................................................
   301    300               dErr["aSuggestions"] = sRepl.split("|")
   302    301       # Message
   303    302       if sMsg[0:1] == "=":
   304    303           sMessage = globals()[sMsg[1:]](lToken)
   305    304       else:
   306    305           sMessage = sMsg
   307    306       dErr["sMessage"] = sMessage
   308         -    if bIdRule:
          307  +    if bShowRuleId:
   309    308           dErr["sMessage"] += "  " + sLineId + " # " + sRuleId
   310    309       # URL
   311    310       dErr["URL"] = sURL  if sURL  else ""
   312    311       # Context
   313    312       if bContext:
   314    313           dErr['sUnderlined'] = sSentence0[dErr["nStart"]:dErr["nEnd"]]
   315    314           dErr['sBefore'] = sSentence0[max(0,dErr["nStart"]-80):dErr["nStart"]]
................................................................................
   701    700           if "<re_morph>" in dNode:
   702    701               for sRegex in dNode["<re_morph>"]:
   703    702                   for sMorph in _oSpellChecker.getMorph(dToken["sValue"]):
   704    703                       if re.search(sRegex, sMorph):
   705    704                           #print("morph regex matching: ", sRegex)
   706    705                           yield dGraph[dNode["<re_morph>"][sRegex]]
   707    706   
   708         -    def parse (self, dPriority, sCountry="${country_default}", dOptions=None, bDebug=False, bContext=False):
          707  +    def parse (self, dPriority, sCountry="${country_default}", dOptions=None, bShowRuleId=False, bDebug=False, bContext=False):
   709    708           dErr = {}
   710    709           dPriority = {}  # Key = position; value = priority
   711    710           dOpt = _dOptions  if not dOptions  else dOptions
   712    711           lPointer = []
   713         -        bIdRule = option('idrule')
   714    712           bChange = False
   715    713           for dToken in self.lToken:
   716    714               # check arcs for each existing pointer
   717    715               lNewPointer = []
   718    716               for i, dPointer in enumerate(lPointer):
   719    717                   bValid = False
   720    718                   bFirst = True
................................................................................
   730    728               lPointer.extend(lNewPointer)
   731    729               # check arcs of first nodes
   732    730               for dNode in self._getNextMatchingNodes(dToken, dGraph[0]):
   733    731                   lPointer.append({"nOffset": dToken["i"], "dNode": dNode})
   734    732               # check if there is rules to check for each pointer
   735    733               for dPointer in lPointer:
   736    734                   if "<rules>" in dPointer["dNode"]:
   737         -                    bHasChanged, errs = self._executeActions(dPointer["dNode"]["<rules>"], dPointer["nOffset"], dPriority, dOpt, bIdRule, bContext)
          735  +                    bHasChanged, errs = self._executeActions(dPointer["dNode"]["<rules>"], dPointer["nOffset"], dPriority, dOpt, bShowRuleId, bContext)
   738    736                       dErr.update(errs)
   739    737                       if bHasChanged:
   740    738                           bChange = True
   741    739           if dErr:
   742    740               print(dErr)
   743    741           return (bChange, dErr)
   744    742   
   745         -    def _executeActions (self, dNode, nTokenOffset, dPriority, dOpt, bIdRule, bContext):
          743  +    def _executeActions (self, dNode, nTokenOffset, dPriority, dOpt, bShowRuleId, bContext):
   746    744           #print(locals())
   747    745           dErrs = {}
   748    746           bChange = False
   749    747           for sLineId, nextNodeKey in dNode.items():
   750    748               for sRuleId in dGraph[nextNodeKey]:
   751    749                   print(sRuleId)
   752    750                   bCondMemo = None
................................................................................
   759    757                               # grammar error
   760    758                               print("-")
   761    759                               nTokenErrorStart = nTokenOffset + eAct[0]
   762    760                               nTokenErrorEnd = nTokenOffset + eAct[1]
   763    761                               nErrorStart = self.iStart + self.lToken[nTokenErrorStart]["nStart"]
   764    762                               nErrorEnd = self.iStart + self.lToken[nTokenErrorEnd]["nEnd"]
   765    763                               if nErrorStart not in dErrs or eAct[2] > dPriority[nErrorStart]:
   766         -                                dErrs[nErrorStart] = _createTokenError(self.lToken, self.sSentence, self.sSentence0, sWhat, nTokenErrorStart, nErrorStart, nErrorEnd, sLineId, sRuleId, True, eAct[3], eAct[4], bIdRule, "notype", bContext)
          764  +                                dErrs[nErrorStart] = _createTokenError(self.lToken, self.sSentence, self.sSentence0, sWhat, nTokenErrorStart, nErrorStart, nErrorEnd, sLineId, sRuleId, True, eAct[3], eAct[4], bShowRuleId, "notype", bContext)
   767    765                                   dPriority[nErrorStart] = eAct[2]
   768    766                           elif cActionType == "~":
   769    767                               # text processor
   770    768                               print("~")
   771    769                               self._rewrite(sWhat, nErrorStart, nErrorEnd)
   772    770                           elif cActionType == "@":
   773    771                               # jump

Added graphspell/fr.py version [bcd5d1fc1b].

            1  +# French language
            2  +
            3  +dSugg = {
            4  +    "parce-que": "parce que",
            5  +    "Parce-que": "Parce que"
            6  +}