File size: 1,929 Bytes
59fb62a 9dbb134 0c9becc 59fb62a 97cd79a 59fb62a 4ae59b6 59fb62a 4ae59b6 59fb62a c1c8471 0c9becc fdea6f1 737b250 9dbb134 59fb62a 4ae59b6 59fb62a c1c8471 0c9becc fdea6f1 737b250 9dbb134 59fb62a 4ae59b6 59fb62a c1c8471 0c9becc fdea6f1 737b250 9dbb134 97cd79a 4ae59b6 97cd79a 0c9becc fdea6f1 97cd79a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# -*- 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
)
from pydantic import BaseModel
router = APIRouter()
class BahttextResponse(BaseModel):
bahttext: str
class NormalizeResponse(BaseModel):
text: str
class ToneResponse(BaseModel):
tone: str
class NumeralsResponse(BaseModel):
number: int
@router.post('/bahttext', response_model=BahttextResponse)
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', response_model=NormalizeResponse)
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', response_model=ToneResponse)
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", response_model=NumeralsResponse)
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",
)
|