Merge pull request #5 from magnum-opus-tender-hack/speller-translate

add speller and translate
This commit is contained in:
Alexandr Karpov 2022-10-22 06:11:03 +03:00 committed by GitHub
commit 497b930ca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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']])]