示例#1
0
文件: api.py 项目: sultur/Greynir
def nntranslate_api(version=1):
    """ Translate text manually entered by the user, by passing the request
        to a neural network server and returning its result back """
    if not (1 <= version <= 1):
        return better_jsonify(valid=False, reason="Unsupported version")

    try:
        segmented = True
        trnsl_src = None
        if "application/json" in request.headers["Content-Type"]:
            trnsl_src = request.json["pgs"]
            src_lang = request.json["src_lang"]
            tgt_lang = request.json["tgt_lang"]
        else:
            segmented = False
            trnsl_src = text_from_request(request)
            src_lang = request.form.get("src_lang")
            tgt_lang = request.form.get("tgt_lang")
        if not trnsl_src:
            return better_jsonify(valid=False, reason="Invalid request")
    except:
        return better_jsonify(valid=False, reason="Invalid request")

    if segmented:
        result = TranslateClient.request_segmented(trnsl_src, src_lang, tgt_lang)
    else:
        result = TranslateClient.request_text(trnsl_src, src_lang, tgt_lang)
    return better_jsonify(valid=True, result=result)
示例#2
0
文件: api.py 项目: sultur/Greynir
def nnparse_api(version=1):
    """ Analyze text manually entered by the user, by passing the request
        to a neural network server and returning its result back """
    if not (1 <= version <= 1):
        return better_jsonify(valid=False, reason="Unsupported version")

    try:
        text = text_from_request(request)
    except:
        return better_jsonify(valid=False, reason="Invalid request")

    results = ParsingClient.request_sentence(text)
    if results is None:
        return better_jsonify(valid=False, reason="Service unavailable")
    nnTree = results["outputs"]
    scores = results["scores"]
    result = {
        "tree": nnTree.to_dict(),
        "width": nnTree.width(),
        "height": nnTree.height(),
        "scores": scores,
    }

    return better_jsonify(valid=True, result=result)