Grammalecte  Diff

Differences From Artifact [3253a2b2ff]:

To Artifact [a5cc9d7be7]:


     6      6   import json
     7      7   import traceback
     8      8   import configparser
     9      9   import time
    10     10   
    11     11   from bottle import Bottle, run, request, response, template, static_file
    12     12   
    13         -import grammalecte.fr as gce
    14         -import grammalecte.fr.lexicographe as lxg
    15         -import grammalecte.fr.textformatter as tf
           13  +import grammalecte
    16     14   import grammalecte.text as txt
    17         -import grammalecte.graphspell.tokenizer as tkz
    18     15   from grammalecte.graphspell.echo import echo
    19     16   
    20     17   
    21     18   HOMEPAGE = """
    22     19   <!DOCTYPE HTML>
    23     20   <html>
    24     21       <head>
................................................................................
   125    122   def genUserId ():
   126    123       i = 0
   127    124       while True:
   128    125           yield str(i)
   129    126           i += 1
   130    127   
   131    128   
   132         -def parseParagraph (iParagraph, sText, oTokenizer, oSpellChecker, dOptions, bDebug=False, bEmptyIfNoErrors=False):
   133         -    aGrammErrs = gce.parse(sText, "FR", bDebug, dOptions)
   134         -    aGrammErrs = list(aGrammErrs)
   135         -    aSpellErrs = []
   136         -    for dToken in oTokenizer.genTokens(sText):
   137         -        if dToken['sType'] == "WORD" and not oSpellChecker.isValidToken(dToken['sValue']):
   138         -            aSpellErrs.append(dToken)
   139         -    if bEmptyIfNoErrors and not aGrammErrs and not aSpellErrs:
   140         -        return ""
   141         -    return "  " + json.dumps({ "iParagraph": iParagraph, "lGrammarErrors": aGrammErrs, "lSpellingErrors": aSpellErrs }, ensure_ascii=False)
   142         -    
   143         -
   144    129   if __name__ == '__main__':
   145    130   
   146         -    gce.load("Server")
          131  +    # initialisation
          132  +    oGrammarChecker = grammalecte.GrammarChecker("fr", "Server")
          133  +    oSpellChecker = oGrammarChecker.getSpellChecker()
          134  +    oLexicographer = oGrammarChecker.getLexicographer()
          135  +    oTextFormatter = oGrammarChecker.getTextFormatter()
          136  +    gce = oGrammarChecker.getGCEngine()
          137  +
   147    138       echo("Grammalecte v{}".format(gce.version))
   148    139       dServerOptions = getServerOptions()
   149    140       dGCOptions = getConfigOptions("fr")
   150    141       if dGCOptions:
   151    142           gce.setOptions(dGCOptions)
   152    143       dServerGCOptions = gce.getOptions()
   153    144       echo("Grammar options:\n" + " | ".join([ k + ": " + str(v)  for k, v in sorted(dServerGCOptions.items()) ]))
   154         -    oSpellChecker = gce.getSpellChecker()
   155         -    oTokenizer = tkz.Tokenizer("fr")
   156         -    oTF = tf.TextFormatter()
   157    145       dUser = {}
   158    146       userGenerator = genUserId()
   159    147   
   160    148       app = Bottle()
   161    149   
   162    150       # GET
   163    151       @app.route("/")
................................................................................
   176    164   
   177    165       # POST
   178    166       @app.route("/gc_text/fr", method="POST")
   179    167       def gcText ():
   180    168           #if len(lang) != 2 or lang != "fr":
   181    169           #    abort(404, "No grammar checker available for lang “" + str(lang) + "”")
   182    170           bComma = False
   183         -        bTF = bool(request.forms.tf)
   184    171           dOptions = None
   185    172           sError = ""
   186    173           if request.cookies.user_id:
   187    174               if request.cookies.user_id in dUser:
   188    175                   dOptions = dUser[request.cookies.user_id].get("gc_options", None)
   189    176                   response.set_cookie("user_id", request.cookies.user_id, path="/", max_age=86400) # we renew cookie for 24h
   190    177               else:
................................................................................
   193    180               try:
   194    181                   dOptions = dict(dServerGCOptions)  if not dOptions  else dict(dOptions)
   195    182                   dOptions.update(json.loads(request.forms.options))
   196    183               except:
   197    184                   sError = "request options not used"
   198    185           sJSON = '{ "program": "grammalecte-fr", "version": "'+gce.version+'", "lang": "'+gce.lang+'", "error": "'+sError+'", "data" : [\n'
   199    186           for i, sText in enumerate(txt.getParagraph(request.forms.text), 1):
   200         -            if bTF:
   201         -                sText = oTF.formatText(sText)
   202         -            sText = parseParagraph(i, sText, oTokenizer, oSpellChecker, dOptions, bEmptyIfNoErrors=True)
          187  +            if bool(request.forms.tf):
          188  +                sText = oTextFormatter.formatText(sText)
          189  +            sText = oGrammarChecker.generateParagraphAsJSON(i, sText, dOptions=dOptions, bEmptyIfNoErrors=True, bReturnText=bool(request.forms.tf))
   203    190               if sText:
   204    191                   if bComma:
   205    192                       sJSON += ",\n"
   206    193                   sJSON += sText
   207    194                   bComma = True
   208    195           sJSON += "\n]}\n"
   209    196           return sJSON
................................................................................
   227    214       def resetOptions ():
   228    215           if request.cookies.user_id and request.cookies.user_id in dUser:
   229    216               del dUser[request.cookies.user_id]
   230    217           return "done"
   231    218   
   232    219       @app.route("/format_text/fr", method="POST")
   233    220       def formatText ():
   234         -        return oTF.formatText(request.forms.text)
          221  +        return oTextFormatter.formatText(request.forms.text)
   235    222   
   236    223       #@app.route('/static/<filepath:path>')
   237    224       #def server_static (filepath):
   238    225       #    return static_file(filepath, root='./views/static')
   239    226   
   240    227       @app.route("/purge_users", method="POST")
   241    228       def purgeUsers ():