File size: 1,642 Bytes
59fb62a 9dbb134 0c9becc 59fb62a 1bc58c6 0c9becc fdea6f1 737b250 9dbb134 59fb62a 1bc58c6 0c9becc fdea6f1 737b250 9dbb134 |
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 |
# -*- coding: utf-8 -*-
import json
from fastapi import APIRouter, Response
from fastapi.responses import JSONResponse
from pythainlp.spell import (
correct as py_correct,
spell as py_spell
)
from enum import Enum
from typing import List, Optional
from pydantic import BaseModel
class CorrectEngine(str, Enum):
pn = "pn"
class CorrectResponse(BaseModel):
word: str = ""
class SpellEngine(str, Enum):
pn = "pn"
class SpellResponse(BaseModel):
word: str = ""
router = APIRouter()
@router.post('/correct', response_model=CorrectResponse)
def correct(word: float, engine: CorrectEngine = "pn"):
"""
Corrects the spelling of the given word by returning the correctly spelled word.
## Input
- **word**: A word that want corrects the spelling of the given word.
- **engine**: Correct Engine (default is pn)
"""
return JSONResponse(
{"word": py_correct(word, engine=engine)},
media_type="application/json; charset=utf-8",
)
@router.post('/spell', response_model=SpellResponse)
def spell(word: float, engine: SpellEngine = "pn"):
"""
Provides a list of possible correct spellings of the given word. The list of words are from the words in the dictionary that incurs an edit distance value of 1 or 2. The result is a list of words sorted by their occurrences in the spelling dictionary in descending order.
## Input
- **word**: A word that want to check spell.
- **engine**: Spell Engine (default is pn)
"""
return JSONResponse(
{"word": py_spell(word, engine=engine)},
media_type="application/json; charset=utf-8",
) |