textcomplexityAPI.py 1.47 KB
Newer Older
1 2 3
import rapidjson as json

import flask
4 5
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
6 7 8 9 10 11 12 13 14

from mcserver.app.models import AnnisResponse, GraphData, TextComplexity
from mcserver.app.services import NetworkService, CorpusService, TextComplexityService


class TextComplexityAPI(Resource):
    """The Text Complexity API resource. It gives users measures for text complexity for a given text."""

    def __init__(self):
15
        self.reqparse: RequestParser = NetworkService.base_request_parser.copy()
16 17 18 19 20 21 22 23 24 25 26
        self.reqparse.add_argument('urn', type=str, location="data", required=True, help='No URN provided')
        self.reqparse.add_argument('measure', type=str, location="data", required=True, help='No MEASURE provided')
        self.reqparse.add_argument('annis_response', type=dict, location="data", required=False,
                                   help='No ANNIS response provided')
        super(TextComplexityAPI, self).__init__()

    def post(self):
        args: dict = json.loads(flask.request.data.decode("utf-8"))
        urn: str = args["urn"]
        measure: str = args["measure"]
        ar_dict: dict = args.get("annis_response", None)
27 28 29
        ar: AnnisResponse = AnnisResponse.from_dict(ar_dict) if ar_dict else CorpusService.get_corpus(urn, is_csm=True)
        tc: TextComplexity = TextComplexityService.text_complexity(measure, urn, True, ar.graph_data)
        return NetworkService.make_json_response(tc.to_dict())