api / routers /soundex.py
wannaphong's picture
Update code
4ae59b6
raw
history blame contribute delete
911 Bytes
# -*- coding: utf-8 -*-
import json
from fastapi import APIRouter, Response
from fastapi.responses import JSONResponse
from pythainlp.soundex import (
soundex as py_soundex
)
from enum import Enum
from pydantic import BaseModel
class SoundexResponse(BaseModel):
soundex: str = ""
router = APIRouter()
class SoundexEngine(str, Enum):
udom83 = "udom83"
lk82 = "lk82"
metasound = "metasound"
prayut_and_somchaip = "prayut_and_somchaip"
@router.post('/soundex', response_model=SoundexResponse)
def soundex(word: str, engine: SoundexEngine = "udom83"):
"""
This api converts Thai text into phonetic code.
## Input
- **word**: A word that want into phonetic code.
- **engine**: Soundex Engine (default is udom83
"""
return JSONResponse(
{"soundex": py_soundex(text=word, engine=engine)},
media_type="application/json; charset=utf-8",
)