duckling.py 701 B

1234567891011121314151617181920212223
  1. from httpx import AsyncClient, URL
  2. from app.core.config import settings
  3. from app.services.service import api_exception
  4. class Duckling:
  5. """
  6. Duckling is a Haskell library that parses text into structured data.
  7. """
  8. def __init__(self, client: AsyncClient, server_settings=settings):
  9. super(Duckling, self).__init__()
  10. self._client = client
  11. self._host = URL(server_settings.DUCKLING_HOST)
  12. @api_exception
  13. async def parse(self, text: str, locale: str = "zh_CN") -> dict:
  14. url = self._host.join("parse")
  15. data = {"locale": locale, "text": text}
  16. raw_response = await self._client.post(url, data=data)
  17. return raw_response.json()