add speller and translate

This commit is contained in:
ilia 2022-10-22 04:58:10 +03:00
parent bbcf4bd2a7
commit c9ad0a1e7a
2 changed files with 19 additions and 0 deletions

View File

@ -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]

View File

@ -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']])]