from aiohttp import web
import utils.web
import utils.text
from extend.kanji_to_romaji import kanji_to_romaji

class Kanji:
    @staticmethod
    def convertToRomaji(self, kanji: str):
        segList = utils.text.splitAscii(kanji)
        sentenceList = []
        for seg in segList:
            if utils.text.isAscii(seg):
                if utils.text.isAsciiPunc(seg):
                    sentenceList.append(seg)
                else:
                    sentenceList.append([seg])
            else:
                romaji = kanji_to_romaji(seg)
                sentenceList.append(romaji.split(" "))
        return sentenceList

    @staticmethod
    async def kanji2romaji(request: web.Request):
        params = await utils.web.get_param(request, {
            "sentence": {
                "required": True,
            },
        })
        sentence = params.get('sentence')

        data = Kanji.convertToRomaji(sentence)
        return await utils.web.api_response(1, data, request=request)