<commit_before>""" Adapter for Clova Extensions Kit """
from logging import Logger
from cek import Clova, URL
from minette import Minette
from minette.message import Message, Response
from minette.channel import Adapter


class ClovaAdapter(Adapter):
    """
    Adapter for Clova Extensions Kit

    Attributes
    ----------
    minette : Minette
        Instance of Minette
    clova : Clova
        Clova Extensions Kit API
    logger : Logger
        Logger
    debug : bool
        Debug mode
    """

    def __init__(self, minette, application_id=None,
                 default_language=None, logger=None, debug=False):
        """
        Parameters
        ----------
        minette : Minette
            Instance of Minette
        application_id : str or None, default None
            Application ID for your Clova Skill
        default_language : str or None, default None
            Default language
        logger : Logger, default None
            Logger
        debug : bool, default False
            Debug mode
        """
        super().__init__(minette, logger, debug)
        self.clova = Clova(
            application_id=application_id if application_id else minette.config.get(section="clova_api", key="application_id"),
            default_language=default_language if default_language else minette.config.get(section="clova_api", key="default_language", default="ja"),
            debug_mode=debug)

    def parse_request(self, request_json):
        """
        Parse JSON from clova to Message object

        Parameters
        ----------
        request : dict
            Request from clova

        Returns
        -------
        message : Message
            Request converted into Message object
        """
        if self.debug:
            self.logger.info(request_json)
        msg = Message(
            type=request_json["request"]["type"],
            channel="LINE",
            channel_detail="Clova",
            channel_user_id=request_json["session"]["user"]["userId"],
            channel_message=request_json
        )
        if msg.type == "IntentRequest":
            msg.intent = request_json["request"]["intent"]["name"]
            if request_json["request"]["intent"]["slots"]:
                msg.entities = request_json["request"]["intent"]["slots"]
        return msg

    def format_response(self, response):
        """
        Set Clova formatted response to `for_channel` attribute

        Parameters
        ----------
        response : Response
            Response from chatbot

        Returns
        -------
        response : Response
            Response with Clova formatted response
        """
        if not response.messages:
            response.for_channel = self.clova.response("")
            return response
        res = response.messages[0]
        end_session = res.entities.get("end_session", True)
        if res.type == "url":
            clova_res = self.clova.response(URL(res.text), end_session=end_session)
        else:
            clova_res = self.clova.response(res.text, end_session=end_session)
        response.for_channel = clova_res
        return response

    def chat(self, request_json):
        """
        Interface to chat with Clova Skill

        Parameters
        ----------
        request_json : dict
            JSON(dict) formatted request from Clova

        Returns
        -------
        response : Response
            Response from chatbot. Send back `json` attribute to Clova API
        """
        return super().chat(request_json)<commit_msg>Use CEK in ClovaAdapter for parsing request<commit_after>""" Adapter for Clova Extensions Kit """
from logging import Logger
from cek import Clova, URL, Request
from minette import Minette
from minette.message import Message, Response
from minette.channel import Adapter
from minette.serializer import encode_json


class ClovaAdapter(Adapter):
    """
    Adapter for Clova Extensions Kit

    Attributes
    ----------
    minette : Minette
        Instance of Minette
    application_id : str
        Application ID of Clova Skill
    default_language : str
        Default language of Clova Skill
    clova : Clova
        Clova Extensions Kit API
    logger : Logger
        Logger
    debug : bool
        Debug mode
    """

    def __init__(self, minette, application_id=None,
                 default_language=None, logger=None, debug=False):
        """
        Parameters
        ----------
        minette : Minette
            Instance of Minette
        application_id : str or None, default None
            Application ID for your Clova Skill
        default_language : str or None, default None
            Default language
        logger : Logger, default None
            Logger
        debug : bool, default False
            Debug mode
        """
        super().__init__(minette, logger, debug)
        self.application_id = application_id if application_id else minette.config.get(section="clova_api", key="application_id")
        self.default_language = default_language if default_language else minette.config.get(section="clova_api", key="default_language", default="ja")
        self.clova = Clova(application_id=self.application_id, default_language=self.default_language, debug_mode=debug)

        # handler for all types of request
        @self.clova.handle.default
        def default(clova_request):
            return clova_request

    def parse_request(self, clova_request):
        """
        Parse request from clova to Message object

        Parameters
        ----------
        clova_request : Request
            Request from clova

        Returns
        -------
        message : Message
            Request converted into Message object
        """
        if self.debug:
            self.logger.info(clova_request.__dict__)
        msg = Message(
            type=clova_request.request_type,
            channel="LINE",
            channel_detail="Clova",
            channel_user_id=clova_request.user_id,
            channel_message=clova_request
        )
        if clova_request.is_intent:
            msg.intent = clova_request.intent_name
            # if clova_request.slots_dict: <- Error occures when no slot values
            if clova_request._request["intent"]["slots"]:
                msg.entities = clova_request.slots_dict
        return msg

    def format_response(self, response):
        """
        Set Clova formatted response to `for_channel` attribute

        Parameters
        ----------
        response : Response
            Response from chatbot

        Returns
        -------
        response : Response
            Response with Clova formatted response
        """
        if not response.messages:
            response.for_channel = self.clova.response("")
            return response
        res = response.messages[0]
        end_session = res.entities.get("end_session", True)
        if res.type == "url":
            clova_res = self.clova.response(URL(res.text), end_session=end_session)
        else:
            clova_res = self.clova.response(res.text, end_session=end_session)
        response.for_channel = clova_res
        return response

    def chat(self, request_data, request_headers):
        """
        Interface to chat with Clova Skill

        Parameters
        ----------
        request_data : bytes
            Request data from Clova as bytes
        request_headers : dict
            Request headers from Clova as dict

        Returns
        -------
        response : Response
            Response from chatbot. Send back `json` attribute to Clova API
        """
        clova_request = self.clova.route(request_data, request_headers)
        return super().chat(clova_request)
{'fraction_non_alphanumeric': 0.08836012861736334, 'fraction_numerical': 0.0002572347266881029, 'mean_word_length': 1.933232742361373, 'pattern_counts': {'":': 3, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6240500', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1930, 'n_words': 546}