From c9ad0a1e7a2708328e94e1b373b94814379a9b0e Mon Sep 17 00:00:00 2001 From: ilia Date: Sat, 22 Oct 2022 04:58:10 +0300 Subject: [PATCH] add speller and translate --- app/search/services/spell_check.py | 6 ++++++ app/search/services/translate.py | 13 +++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 app/search/services/spell_check.py create mode 100644 app/search/services/translate.py diff --git a/app/search/services/spell_check.py b/app/search/services/spell_check.py new file mode 100644 index 0000000..120c0d2 --- /dev/null +++ b/app/search/services/spell_check.py @@ -0,0 +1,6 @@ +import requests as r + + +def spell_check(word: str) -> str: + res = r.get(f'https://speller.yandex.net/services/spellservice.json/checkText?text={word}') + return res.json()[0]['s'][0] diff --git a/app/search/services/translate.py b/app/search/services/translate.py new file mode 100644 index 0000000..8fa8f39 --- /dev/null +++ b/app/search/services/translate.py @@ -0,0 +1,13 @@ +import requests as r +from conf.settings.base import YANDEX_DICT_API_KEY +from itertools import chain +from typing import List + + +def translate_ru_en(word: str) -> List[str]: + res = r.get(f"https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key={YANDEX_DICT_API_KEY}&lang=ru-en&text={word}") + return [i['text'] for i in chain(*[j['tr']for j in res.json()['def']])] + +def translate_en_ru(word: str) -> List[str]: + res = r.get(f"https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key={YANDEX_DICT_API_KEY}&lang=en-ru&text={word}") + return [i['text'] for i in chain(*[j['tr']for j in res.json()['def']])]