Overview
Comment: | [cli][server][graphspell][core] interactive correction with CLI |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk | cli | core | server | graphspell |
Files: | files | file ages | folders |
SHA3-256: |
c8ad079d5789150767bc85ab1558db78 |
User & Date: | olr on 2020-03-05 19:07:35 |
Other Links: | manifest | tags |
Context
2020-03-05
| ||
20:37 | [cli] add comment check-in: 36a06e8ba5 user: olr tags: trunk, cli | |
19:07 | [cli][server][graphspell][core] interactive correction with CLI check-in: c8ad079d57 user: olr tags: trunk, cli, core, server, graphspell | |
2020-03-04
| ||
14:15 | [fx] create custom event on text area when Grammalecte change the text content check-in: 65c3b076ec user: olr tags: trunk, fx | |
Changes
Modified gc_core/py/grammar_checker.py from [f8aade78d9] to [5ca2376e71].
︙ | ︙ | |||
51 52 53 54 55 56 57 | def getParagraphErrors (self, sText, dOptions=None, bContext=False, bSpellSugg=False, bDebug=False): "returns a tuple: (grammar errors, spelling errors)" aGrammErrs = self.gce.parse(sText, "FR", bDebug=bDebug, dOptions=dOptions, bContext=bContext) aSpellErrs = self.oSpellChecker.parseParagraph(sText, bSpellSugg) return aGrammErrs, aSpellErrs | < < < < < < | > > > | > > > | 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 | def getParagraphErrors (self, sText, dOptions=None, bContext=False, bSpellSugg=False, bDebug=False): "returns a tuple: (grammar errors, spelling errors)" aGrammErrs = self.gce.parse(sText, "FR", bDebug=bDebug, dOptions=dOptions, bContext=bContext) aSpellErrs = self.oSpellChecker.parseParagraph(sText, bSpellSugg) return aGrammErrs, aSpellErrs def getParagraphWithErrors (self, sText, dOptions=None, bEmptyIfNoErrors=False, bSpellSugg=False, nWidth=100, bDebug=False): "parse text and return a readable text with underline errors" aGrammErrs, aSpellErrs = self.getParagraphErrors(sText, dOptions, False, bSpellSugg, bDebug) if bEmptyIfNoErrors and not aGrammErrs and not aSpellErrs: return "" return text.generateParagraph(sText, aGrammErrs, aSpellErrs, nWidth) def getTextWithErrors (self, sText, bEmptyIfNoErrors=False, bSpellSugg=False, nWidth=100, bDebug=False): "[todo]" def getParagraphErrorsAsJSON (self, iIndex, sText, dOptions=None, bContext=False, bEmptyIfNoErrors=False, bSpellSugg=False, bReturnText=False, lLineSet=None, bDebug=False): "parse text and return errors as a JSON string" aGrammErrs, aSpellErrs = self.getParagraphErrors(sText, dOptions, bContext, bSpellSugg, bDebug) aGrammErrs = list(aGrammErrs) if bEmptyIfNoErrors and not aGrammErrs and not aSpellErrs: return "" if lLineSet: aGrammErrs, aSpellErrs = text.convertToXY(aGrammErrs, aSpellErrs, lLineSet) return json.dumps({ "lGrammarErrors": aGrammErrs, "lSpellingErrors": aSpellErrs }, ensure_ascii=False) if bReturnText: return json.dumps({ "iParagraph": iIndex, "sText": sText, "lGrammarErrors": aGrammErrs, "lSpellingErrors": aSpellErrs }, ensure_ascii=False) return json.dumps({ "iParagraph": iIndex, "lGrammarErrors": aGrammErrs, "lSpellingErrors": aSpellErrs }, ensure_ascii=False) def getTextErrorsAsJSON (self, sText, bContext=False, bEmptyIfNoErrors=False, bSpellSugg=False, bReturnText=False, bDebug=False): "[todo]" |
Modified gc_core/py/text.py from [0d4a1aba9d] to [df68ef0738].
︙ | ︙ | |||
57 58 59 60 61 62 63 64 65 66 67 68 69 70 | def generateParagraph (sParagraph, aGrammErrs, aSpellErrs, nWidth=100): "Returns a text with readable errors" if not sParagraph: return "" lGrammErrs = sorted(aGrammErrs, key=lambda d: d["nStart"]) lSpellErrs = sorted(aSpellErrs, key=lambda d: d['nStart']) sText = "" nOffset = 0 for sLine in wrap(sParagraph, nWidth): # textwrap.wrap(sParagraph, nWidth, drop_whitespace=False) sText += sLine + "\n" nLineLen = len(sLine) sErrLine = "" nLenErrLine = 0 | > > > | 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | def generateParagraph (sParagraph, aGrammErrs, aSpellErrs, nWidth=100): "Returns a text with readable errors" if not sParagraph: return "" lGrammErrs = sorted(aGrammErrs, key=lambda d: d["nStart"]) lSpellErrs = sorted(aSpellErrs, key=lambda d: d['nStart']) lErrors = sorted(lGrammErrs + lSpellErrs, key=lambda d: d["nStart"]) for n, dErr in enumerate(lErrors, 1): dErr["iError"] = n sText = "" nOffset = 0 for sLine in wrap(sParagraph, nWidth): # textwrap.wrap(sParagraph, nWidth, drop_whitespace=False) sText += sLine + "\n" nLineLen = len(sLine) sErrLine = "" nLenErrLine = 0 |
︙ | ︙ | |||
94 95 96 97 98 99 100 | if nGrammErr: sText += getReadableErrors(lGrammErrs[:nGrammErr], nWidth) del lGrammErrs[0:nGrammErr] if nSpellErr: sText += getReadableErrors(lSpellErrs[:nSpellErr], nWidth, True) del lSpellErrs[0:nSpellErr] nOffset += nLineLen | | | | | | 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 | if nGrammErr: sText += getReadableErrors(lGrammErrs[:nGrammErr], nWidth) del lGrammErrs[0:nGrammErr] if nSpellErr: sText += getReadableErrors(lSpellErrs[:nSpellErr], nWidth, True) del lSpellErrs[0:nSpellErr] nOffset += nLineLen return (sText, lErrors) def getReadableErrors (lErrs, nWidth, bSpellingError=False): "Returns lErrs errors as readable errors" sErrors = "" for dErr in lErrs: if not bSpellingError or "aSuggestions" in dErr: sMsg, *others = getReadableError(dErr, bSpellingError).split("\n") sErrors += "\n".join(textwrap.wrap(sMsg, nWidth, subsequent_indent=" ")) + "\n" for arg in others: sErrors += "\n".join(textwrap.wrap(arg, nWidth, subsequent_indent=" ")) + "\n" if sErrors != "": sErrors += "\n" return sErrors def getReadableError (dErr, bSpellingError=False): "Returns an error dErr as a readable error" try: if bSpellingError: sText = u"* {iError} [{nStart}:{nEnd}] # {sValue}:".format(**dErr) else: sText = u"* {iError} [{nStart}:{nEnd}] # {sLineId} / {sRuleId}:\n".format(**dErr) sText += " " + dErr.get("sMessage", "# error : message not found") if dErr.get("aSuggestions", None): sText += "\n > Suggestions : " + " | ".join(dErr["aSuggestions"]) if dErr.get("URL", None): sText += "\n > URL: " + dErr["URL"] return sText except KeyError: return u"* Non-compliant error: {}".format(dErr) |
︙ | ︙ |
Modified grammalecte-cli.py from [6a3b09f861] to [660294b341].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/usr/bin/env python3 """ Grammalecte CLI (command line interface) """ import sys import os.path import argparse import json import grammalecte import grammalecte.text as txt from grammalecte.graphspell.echo import echo _EXAMPLE = "Quoi ? Racontes ! Racontes-moi ! Bon sangg, parles ! Oui. Il y a des menteur partout. " \ | > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #!/usr/bin/env python3 """ Grammalecte CLI (command line interface) """ import sys import os.path import argparse import json import re import traceback import grammalecte import grammalecte.text as txt from grammalecte.graphspell.echo import echo _EXAMPLE = "Quoi ? Racontes ! Racontes-moi ! Bon sangg, parles ! Oui. Il y a des menteur partout. " \ |
︙ | ︙ | |||
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | except json.JSONDecodeError: print("# Error. File <" + spf + " is not a valid JSON file.") return None return oJSON print("# Error: file <" + spf + "> not found.") return None def main (): "launch the CLI (command line interface)" xParser = argparse.ArgumentParser() xParser.add_argument("-f", "--file", help="parse file (UTF-8 required!) [on Windows, -f is similar to -ff]", type=str) xParser.add_argument("-ff", "--file_to_file", help="parse file (UTF-8 required!) and create a result file (*.res.txt)", type=str) xParser.add_argument("-owe", "--only_when_errors", help="display results only when there are errors", action="store_true") xParser.add_argument("-j", "--json", help="generate list of errors in JSON (only with option --file or --file_to_file)", action="store_true") xParser.add_argument("-cl", "--concat_lines", help="concatenate lines not separated by an empty paragraph (only with option --file or --file_to_file)", action="store_true") xParser.add_argument("-tf", "--textformatter", help="auto-format text according to typographical rules (not with option --concat_lines)", action="store_true") xParser.add_argument("-tfo", "--textformatteronly", help="auto-format text and disable grammar checking (only with option --file or --file_to_file)", action="store_true") xParser.add_argument("-ctx", "--context", help="return errors with context (only with option --json)", action="store_true") xParser.add_argument("-wss", "--with_spell_sugg", help="add suggestions for spelling errors (only with option --file or --file_to_file)", action="store_true") | > > > > > > > > > > > > > > > > > > > > > | 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 | except json.JSONDecodeError: print("# Error. File <" + spf + " is not a valid JSON file.") return None return oJSON print("# Error: file <" + spf + "> not found.") return None def getCommand (): while True: print("COMMANDS: [N]ext paragraph [Q]uit.") print(" [Error number]>[suggestion number]") print(" [Error number] (apply first suggestion)") print(" [Error number]=[replacement]") sCommand = input(" ? ") if sCommand == "q" or sCommand == "Q" or sCommand == "n" or sCommand == "N": return sCommand elif re.match("^([0-9]+)(>[0-9]*|=.*|)$", sCommand): m = re.match("^([0-9]+)(>[0-9]*|=.*|)$", sCommand) nError = int(m.group(1)) - 1 cAction = m.group(2)[0:1] or ">" if cAction == ">": vSugg = int(m.group(2)[1:]) - 1 if m.group(2) else 0 else: vSugg = m.group(2)[1:] return (nError, cAction, vSugg) def main (): "launch the CLI (command line interface)" xParser = argparse.ArgumentParser() xParser.add_argument("-f", "--file", help="parse file (UTF-8 required!) [on Windows, -f is similar to -ff]", type=str) xParser.add_argument("-ff", "--file_to_file", help="parse file (UTF-8 required!) and create a result file (*.res.txt)", type=str) xParser.add_argument("-iff", "--interactive_file_to_file", help="parse file (UTF-8 required!) and create a result file (*.res.txt)", type=str) xParser.add_argument("-owe", "--only_when_errors", help="display results only when there are errors", action="store_true") xParser.add_argument("-j", "--json", help="generate list of errors in JSON (only with option --file or --file_to_file)", action="store_true") xParser.add_argument("-cl", "--concat_lines", help="concatenate lines not separated by an empty paragraph (only with option --file or --file_to_file)", action="store_true") xParser.add_argument("-tf", "--textformatter", help="auto-format text according to typographical rules (not with option --concat_lines)", action="store_true") xParser.add_argument("-tfo", "--textformatteronly", help="auto-format text and disable grammar checking (only with option --file or --file_to_file)", action="store_true") xParser.add_argument("-ctx", "--context", help="return errors with context (only with option --json)", action="store_true") xParser.add_argument("-wss", "--with_spell_sugg", help="add suggestions for spelling errors (only with option --file or --file_to_file)", action="store_true") |
︙ | ︙ | |||
173 174 175 176 177 178 179 | oGrammarChecker.gce.setOptions({ opt:False for opt in xArgs.opt_off }) # disable grammar rules if xArgs.rule_off: for sRule in xArgs.rule_off: oGrammarChecker.gce.ignoreRule(sRule) | > | < > | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | oGrammarChecker.gce.setOptions({ opt:False for opt in xArgs.opt_off }) # disable grammar rules if xArgs.rule_off: for sRule in xArgs.rule_off: oGrammarChecker.gce.ignoreRule(sRule) if xArgs.file or xArgs.file_to_file: # file processing sFile = xArgs.file or xArgs.file_to_file hDst = open(sFile[:sFile.rfind(".")]+".res.txt", "w", encoding="utf-8", newline="\n") if xArgs.file_to_file or sys.platform == "win32" else None bComma = False if xArgs.json: output('{ "grammalecte": "'+oGrammarChecker.gce.version+'", "lang": "'+oGrammarChecker.gce.lang+'", "data" : [\n', hDst) for i, sText, lLineSet in generateParagraphFromFile(sFile, xArgs.concat_lines): if xArgs.textformatter or xArgs.textformatteronly: sText = oTextFormatter.formatText(sText) if xArgs.textformatteronly: output(sText, hDst) continue if xArgs.json: sText = oGrammarChecker.getParagraphErrorsAsJSON(i, sText, bContext=xArgs.context, bEmptyIfNoErrors=xArgs.only_when_errors, \ bSpellSugg=xArgs.with_spell_sugg, bReturnText=xArgs.textformatter, lLineSet=lLineSet) else: sText, _ = oGrammarChecker.getParagraphWithErrors(sText, bEmptyIfNoErrors=xArgs.only_when_errors, bSpellSugg=xArgs.with_spell_sugg, nWidth=xArgs.width) if sText: if xArgs.json and bComma: output(",\n", hDst) output(sText, hDst) bComma = True if hDst: echo("§ %d\r" % i, end="", flush=True) if xArgs.json: output("\n]}\n", hDst) elif xArgs.interactive_file_to_file: sFile = xArgs.interactive_file_to_file hDst = open(sFile[:sFile.rfind(".")]+".res.txt", "w", encoding="utf-8", newline="\n") for i, sText, lLineSet in generateParagraphFromFile(sFile, xArgs.concat_lines): if xArgs.textformatter: sText = oTextFormatter.formatText(sText) while True: sResult, lErrors = oGrammarChecker.getParagraphWithErrors(sText, bEmptyIfNoErrors=False, bSpellSugg=True, nWidth=xArgs.width) print("\n\n============================== Paragraph " + str(i) + " ==============================\n") echo(sResult) print("\n") vCommand = getCommand() if vCommand == "q": # quit hDst.close() exit() elif vCommand == "n": # next paragraph hDst.write(sText) break else: nError, cAction, vSugg = vCommand if 0 <= nError <= len(lErrors) - 1: dErr = lErrors[nError] if cAction == ">" and 0 <= vSugg <= len(dErr["aSuggestions"]) - 1: sSugg = dErr["aSuggestions"][vSugg] sText = sText[0:dErr["nStart"]] + sSugg + sText[dErr["nEnd"]:] elif cAction == "=": sText = sText[0:dErr["nStart"]] + vSugg + sText[dErr["nEnd"]:] else: print("Error. Action not possible.") else: print("Error. This error doesn’t exist.") else: # pseudo-console sInputText = "\n~==========~ Enter your text [/h /q] ~==========~\n" sText = _getText(sInputText) while True: if sText.startswith("?"): for sWord in sText[1:].strip().split(): |
︙ | ︙ | |||
283 284 285 286 287 288 289 | " ".join(dToken.get("lMorph", "")), \ "·".join(dToken.get("aTags", "")) ) ) echo(txt.getReadableErrors(dSentence["lGrammarErrors"], xArgs.width)) else: for sParagraph in txt.getParagraph(sText): if xArgs.textformatter: sText = oTextFormatter.formatText(sParagraph) | | | 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | " ".join(dToken.get("lMorph", "")), \ "·".join(dToken.get("aTags", "")) ) ) echo(txt.getReadableErrors(dSentence["lGrammarErrors"], xArgs.width)) else: for sParagraph in txt.getParagraph(sText): if xArgs.textformatter: sText = oTextFormatter.formatText(sParagraph) sRes, _ = oGrammarChecker.getParagraphWithErrors(sParagraph, bEmptyIfNoErrors=xArgs.only_when_errors, nWidth=xArgs.width, bDebug=xArgs.debug) if sRes: echo("\n" + sRes) else: echo("\nNo error found.") sText = _getText(sInputText) if __name__ == '__main__': main() |
Modified grammalecte-server.py from [38ce8a200e] to [2560b5c98b].
︙ | ︙ | |||
30 31 32 33 34 35 36 | def parseText (sText, dOptions=None, bFormatText=False, sError=""): "parse <sText> and return errors in a JSON format" sJSON = '{ "program": "grammalecte-fr", "version": "'+oGCE.version+'", "lang": "'+oGCE.lang+'", "error": "'+sError+'", "data" : [\n' sDataJSON = "" for i, sParagraph in enumerate(txt.getParagraph(sText), 1): if bFormatText: sParagraph = oTextFormatter.formatText(sParagraph) | | | 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | def parseText (sText, dOptions=None, bFormatText=False, sError=""): "parse <sText> and return errors in a JSON format" sJSON = '{ "program": "grammalecte-fr", "version": "'+oGCE.version+'", "lang": "'+oGCE.lang+'", "error": "'+sError+'", "data" : [\n' sDataJSON = "" for i, sParagraph in enumerate(txt.getParagraph(sText), 1): if bFormatText: sParagraph = oTextFormatter.formatText(sParagraph) sResult = oGrammarChecker.getParagraphErrorsAsJSON(i, sParagraph, dOptions=dOptions, bEmptyIfNoErrors=True, bReturnText=bFormatText) if sResult: if sDataJSON: sDataJSON += ",\n" sDataJSON += sResult sJSON += sDataJSON + "\n]}\n" return sJSON |
︙ | ︙ |