api / routers /util.py
wannaphong's picture
Update code
fdea6f1
raw
history blame
1.56 kB
# -*- coding: utf-8 -*-
import json
from fastapi import APIRouter, Response
from fastapi.responses import JSONResponse
from pythainlp.util import (
bahttext as py_bahttext,
normalize as py_normalize,
tone_detector as py_tone_detector,
thaiword_to_num as py_thaiword_to_num
)
router = APIRouter()
@router.post('/bahttext')
def bahttext(number: float):
"""
This api converts a number to Thai text and adds a suffix “บาท” (Baht).
"""
return JSONResponse(
{"bahttext": py_bahttext(number)},
media_type="application/json; charset=utf-8",
)
@router.post('/normalize')
def normalize(text: str):
"""
Normalize and clean Thai text
"""
return JSONResponse(
{"text": py_normalize(text)},
media_type="application/json; charset=utf-8",
)
@router.post('/tone_detector')
def tone_detector(syllable: str):
"""
Thai tone detector for word.
"""
return JSONResponse(
{"tone": py_tone_detector(syllable)},
media_type="application/json; charset=utf-8",
)
@router.post("/thaiword_to_num")
def thaiword_to_num(text: str):
"""
Converts the spelled-out numerals in Thai scripts into an actual integer.
Example: 'สองล้านสามแสนหกร้อยสิบสอง' => 2300612, 'ศูนย์' => 0
## Input
- **text**: Spelled-out numerals in Thai scripts
"""
return JSONResponse(
{"number": py_thaiword_to_num(text)},
media_type="application/json; charset=utf-8",
)