Index: grammalecte-server.py ================================================================== --- grammalecte-server.py +++ grammalecte-server.py @@ -8,15 +8,12 @@ import configparser import time from bottle import Bottle, run, request, response, template, static_file -import grammalecte.fr as gce -import grammalecte.fr.lexicographe as lxg -import grammalecte.fr.textformatter as tf +import grammalecte import grammalecte.text as txt -import grammalecte.graphspell.tokenizer as tkz from grammalecte.graphspell.echo import echo HOMEPAGE = """ @@ -127,35 +124,26 @@ while True: yield str(i) i += 1 -def parseParagraph (iParagraph, sText, oTokenizer, oSpellChecker, dOptions, bDebug=False, bEmptyIfNoErrors=False): - aGrammErrs = gce.parse(sText, "FR", bDebug, dOptions) - aGrammErrs = list(aGrammErrs) - aSpellErrs = [] - for dToken in oTokenizer.genTokens(sText): - if dToken['sType'] == "WORD" and not oSpellChecker.isValidToken(dToken['sValue']): - aSpellErrs.append(dToken) - if bEmptyIfNoErrors and not aGrammErrs and not aSpellErrs: - return "" - return " " + json.dumps({ "iParagraph": iParagraph, "lGrammarErrors": aGrammErrs, "lSpellingErrors": aSpellErrs }, ensure_ascii=False) - - if __name__ == '__main__': - gce.load("Server") + # initialisation + oGrammarChecker = grammalecte.GrammarChecker("fr", "Server") + oSpellChecker = oGrammarChecker.getSpellChecker() + oLexicographer = oGrammarChecker.getLexicographer() + oTextFormatter = oGrammarChecker.getTextFormatter() + gce = oGrammarChecker.getGCEngine() + echo("Grammalecte v{}".format(gce.version)) dServerOptions = getServerOptions() dGCOptions = getConfigOptions("fr") if dGCOptions: gce.setOptions(dGCOptions) dServerGCOptions = gce.getOptions() echo("Grammar options:\n" + " | ".join([ k + ": " + str(v) for k, v in sorted(dServerGCOptions.items()) ])) - oSpellChecker = gce.getSpellChecker() - oTokenizer = tkz.Tokenizer("fr") - oTF = tf.TextFormatter() dUser = {} userGenerator = genUserId() app = Bottle() @@ -178,11 +166,10 @@ @app.route("/gc_text/fr", method="POST") def gcText (): #if len(lang) != 2 or lang != "fr": # abort(404, "No grammar checker available for lang “" + str(lang) + "”") bComma = False - bTF = bool(request.forms.tf) dOptions = None sError = "" if request.cookies.user_id: if request.cookies.user_id in dUser: dOptions = dUser[request.cookies.user_id].get("gc_options", None) @@ -195,13 +182,13 @@ dOptions.update(json.loads(request.forms.options)) except: sError = "request options not used" sJSON = '{ "program": "grammalecte-fr", "version": "'+gce.version+'", "lang": "'+gce.lang+'", "error": "'+sError+'", "data" : [\n' for i, sText in enumerate(txt.getParagraph(request.forms.text), 1): - if bTF: - sText = oTF.formatText(sText) - sText = parseParagraph(i, sText, oTokenizer, oSpellChecker, dOptions, bEmptyIfNoErrors=True) + if bool(request.forms.tf): + sText = oTextFormatter.formatText(sText) + sText = oGrammarChecker.generateParagraphAsJSON(i, sText, dOptions=dOptions, bEmptyIfNoErrors=True, bReturnText=bool(request.forms.tf)) if sText: if bComma: sJSON += ",\n" sJSON += sText bComma = True @@ -229,11 +216,11 @@ del dUser[request.cookies.user_id] return "done" @app.route("/format_text/fr", method="POST") def formatText (): - return oTF.formatText(request.forms.text) + return oTextFormatter.formatText(request.forms.text) #@app.route('/static/') #def server_static (filepath): # return static_file(filepath, root='./views/static')