Files
searxng/searx/engines/lingva.py
T

97 lines
3.1 KiB
Python
Raw Normal View History

2022-06-22 14:08:19 +07:00
# SPDX-License-Identifier: AGPL-3.0-or-later
2026-03-02 21:08:34 +01:00
"""`Lingva Translate`_ is an alternative front-end for Google Translate,
serving as a free and open source translator with over a hundred
languages available.
.. _Lingva Translate: https://github.com/thedaviddelta/lingva-translate
"""
import typing as t
2022-06-22 14:08:19 +07:00
from searx.result_types import EngineResults
2026-03-02 21:08:34 +01:00
if t.TYPE_CHECKING:
from searx.extended_types import SXNG_Response
from searx.search.processors.online_dictionary import OnlineDictParams
about: dict[str, t.Any] = {
"website": "https://github.com/thedaviddelta/lingva-translate",
2022-06-22 14:08:19 +07:00
"wikidata_id": None,
2026-03-02 21:08:34 +01:00
"official_api_documentation": "https://github.com/thedaviddelta/lingva-translate#public-apis",
2022-06-22 14:08:19 +07:00
"use_official_api": True,
"require_api_key": False,
2026-03-02 21:08:34 +01:00
"results": "JSON",
2022-06-22 14:08:19 +07:00
}
2026-03-02 21:08:34 +01:00
categories: list[str] = ["general", "translate"]
engine_type = "online_dictionary"
base_url = "https://lingva.ml"
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
def request(_query: str, params: "OnlineDictParams") -> None:
from_lang = params["from_lang"][1]
to_lang = params["to_lang"][1]
query = params["query"]
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
params["url"] = f"{base_url}/api/v1/{from_lang}/{to_lang}/{query}"
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
def response(resp: "SXNG_Response") -> EngineResults:
res = EngineResults()
json_resp = resp.json()
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
params: "OnlineDictParams" = resp.search_params # type: ignore[assignment]
from_lang = params["from_lang"][1]
to_lang = params["to_lang"][1]
query = params["query"]
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
translation = json_resp.get("translation")
if not translation:
return res
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
info: dict[str, t.Any] | None = json_resp.get("info")
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
from_to_prefix = f"{from_lang}-{to_lang} "
2026-03-02 21:08:34 +01:00
translations: list[EngineResults.types.Translations.Item] = []
2026-03-02 21:08:34 +01:00
if info:
if "typo" in info:
res.add(res.types.LegacyResult(suggestion=from_to_prefix + info["typo"]))
for definition in info.get("definitions", []):
for item in definition.get("list", []):
for synonym in item.get("synonyms", []):
res.add(res.types.LegacyResult(suggestion=from_to_prefix + synonym))
translations.append(
EngineResults.types.Translations.Item(
text=translation,
definitions=[item["definition"]] if item.get("definition") else [],
examples=[item["example"]] if item.get("example") else [],
synonyms=item.get("synonyms", []),
)
)
2026-03-02 21:08:34 +01:00
for extra_translation in info.get("extraTranslations", []):
for word in extra_translation.get("list", []):
translations.append(
EngineResults.types.Translations.Item(
text=word["word"],
definitions=word.get("meanings", []),
)
)
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
if not translations:
translations.append(EngineResults.types.Translations.Item(text=translation))
2022-06-22 14:08:19 +07:00
2026-03-02 21:08:34 +01:00
res.add(
res.types.Translations(
translations=translations,
url=f"{base_url}/{from_lang}/{to_lang}/{query}",
)
)
2026-03-02 21:08:34 +01:00
return res