api / routers /util.py
wannaphong's picture
Update code
737b250
raw
history blame
1.07 kB
# -*- coding: utf-8 -*-
import json
from fastapi import APIRouter, Response
from pythainlp.util import (
bahttext as py_bahttext,
normalize as py_normalize,
tone_detector as py_tone_detector
)
router = APIRouter()
@router.post('/bahttext')
def bahttext(number: float):
"""
This api converts a number to Thai text and adds a suffix “บาท” (Baht).
"""
return Response(
json.dumps({"bahttext": py_bahttext(number)}, ensure_ascii=False),
media_type="application/json; charset=utf-8",
)
@router.post('/normalize')
def normalize(text: str):
"""
Normalize and clean Thai text
"""
return Response(
json.dumps({"text": py_normalize(text)}, ensure_ascii=False),
media_type="application/json; charset=utf-8",
)
@router.post('/tone_detector')
def tone_detector(syllable: str):
"""
Thai tone detector for word.
"""
return Response(
json.dumps({"tone": py_tone_detector(syllable)}, ensure_ascii=False),
media_type="application/json; charset=utf-8",
)