rawTextAPI.py 1.15 KB
Newer Older
1 2 3
from flask_restful import Resource, abort
from flask_restful.reqparse import RequestParser

4 5 6 7 8 9 10 11 12
from mcserver.app.models import AnnisResponse, TextComplexityMeasure, GraphData
from mcserver.app.services import CorpusService, NetworkService, TextComplexityService


class RawTextAPI(Resource):
    """The fill the blank API resource. It creates a fill the blank exercise for a given text."""

    def __init__(self):
        """Initialize possible arguments for calls to the fill the blank REST API."""
13
        self.reqparse: RequestParser = NetworkService.base_request_parser.copy()
14 15 16 17 18 19 20 21 22 23 24 25
        self.reqparse.add_argument("urn", type=str, required=True, default="", help="No URN provided")
        super(RawTextAPI, self).__init__()

    def get(self):
        args = self.reqparse.parse_args()
        urn: str = args["urn"]
        ar: AnnisResponse = CorpusService.get_corpus(cts_urn=urn, is_csm=False)
        if not ar.nodes:
            abort(404)
        gd: GraphData = GraphData(json_dict=ar.__dict__)
        ar.text_complexity = TextComplexityService.text_complexity(TextComplexityMeasure.all.name, urn, False, gd)
        return NetworkService.make_json_response(ar.__dict__)