Index: grammalecte-cli.py ================================================================== --- grammalecte-cli.py +++ grammalecte-cli.py @@ -50,11 +50,11 @@ if os.path.isfile(spf): with open(spf, "r", encoding="utf-8") as hSrc: for sLine in hSrc: yield sLine else: - print("# Error: file <" + spf + ">not found.") + print("# Error: file <" + spf + "> not found.") def generateParagraphFromFile (spf, bConcatLines=False): "generator: returns text by tuple of (iParagraph, sParagraph, lLineSet)" if not bConcatLines: @@ -81,10 +81,24 @@ if not hDst: echo(sText, end="") else: hDst.write(sText) + +def loadDictionary (spf): + if os.path.isfile(spf): + sJSON = open(spf, "r", encoding="utf-8").read() + try: + oJSON = json.loads(sJSON) + except: + print("# Error. File <" + spf + " is not a valid JSON file.") + return None + return oJSON + else: + 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) @@ -94,10 +108,11 @@ 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") + xParser.add_argument("-pdi", "--personal_dict", help="load personnal dictionary (JSON file)", type=str) xParser.add_argument("-w", "--width", help="width in characters (40 < width < 200; default: 100)", type=int, choices=range(40,201,10), default=100) xParser.add_argument("-lo", "--list_options", help="list options", action="store_true") xParser.add_argument("-lr", "--list_rules", nargs="?", help="list rules [regex pattern as filter]", const="*") xParser.add_argument("-sug", "--suggest", help="get suggestions list for given word", type=str) xParser.add_argument("-on", "--opt_on", nargs="+", help="activate options") @@ -108,10 +123,14 @@ oGrammarChecker = grammalecte.GrammarChecker("fr") oSpellChecker = oGrammarChecker.getSpellChecker() oLexicographer = oGrammarChecker.getLexicographer() oTextFormatter = oGrammarChecker.getTextFormatter() + if xArgs.personal_dict: + oJSON = loadDictionary(xArgs.personal_dict) + if oJSON: + oSpellChecker.setPersonalDictionary(oJSON) if not xArgs.json: echo("Grammalecte v{}".format(oGrammarChecker.gce.version)) # list options or rules