Skip to content

Commit

Permalink
[refactor] translation engines: common interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 15, 2024
1 parent 3e87354 commit e2b0fde
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
41 changes: 25 additions & 16 deletions searx/engines/lingva.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Lingva (alternative Google Translate frontend)"""

from json import loads
from searx.utils import render_template

about = {
"website": 'https://lingva.ml',
Expand Down Expand Up @@ -29,7 +29,7 @@ def request(_query, params):
def response(resp):
results = []

result = loads(resp.text)
result = resp.json()
info = result["info"]
from_to_prefix = "%s-%s " % (resp.search_params['from_lang'][1], resp.search_params['to_lang'][1])

Expand All @@ -38,27 +38,36 @@ def response(resp):

if 'definitions' in info: # pylint: disable=too-many-nested-blocks
for definition in info['definitions']:
if 'list' in definition:
for item in definition['list']:
if 'synonyms' in item:
for synonym in item['synonyms']:
results.append({"suggestion": from_to_prefix + synonym})

infobox = ""
for item in definition.get('list', []):
for synonym in item.get('synonyms', []):
results.append({"suggestion": from_to_prefix + synonym})

data = []

for definition in info['definitions']:
for translation in definition['list']:
data.append(
{
'text': result['translation'],
'definitions': [translation['definition']] if translation['definition'] else [],
'examples': [translation['example']] if translation['example'] else [],
'synonyms': translation['synonyms'],
}
)

for translation in info["extraTranslations"]:
for word in translation["list"]:
infobox += f"<dl><dt>{word['word']}</dt>"

for meaning in word["meanings"]:
infobox += f"<dd>{meaning}</dd>"

infobox += "</dl>"
data.append(
{
'text': word['word'],
'definitions': word['meanings'],
}
)

results.append(
{
'infobox': result["translation"],
'content': infobox,
'content': render_template("answerers/translate.html", translations=data),
}
)

Expand Down
25 changes: 11 additions & 14 deletions searx/engines/mozhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import random
import re
import os
from urllib.parse import urlencode
from flask_babel import gettext
from searx.utils import render_template

about = {
"website": 'https://codeberg.org/aryak/mozhi',
Expand Down Expand Up @@ -35,30 +37,25 @@ def request(_query, params):
def response(resp):
translation = resp.json()

infobox = ""
data = {'text': translation['translated-text'], 'definitions': [], 'examples': []}

if translation['target_transliteration'] and not re.match(
re_transliteration_unsupported, translation['target_transliteration']
):
infobox = f"<b>{translation['target_transliteration']}</b>"
data['transliteration'] = translation['target_transliteration']

if translation['word_choices']:
for word in translation['word_choices']:
infobox += f"<dl><dt>{word['word']}: {word['definition']}</dt>"
for word in translation.get('word_choices', []):
if word.get('definition'):
data['definitions'].append(word['definition'])

if word['examples_target']:
for example in word['examples_target']:
infobox += f"<dd>{re.sub(r'<|>', '', example)}</dd>"
infobox += f"<dd>{re.sub(r'<|>', '', example)}</dd>"
for example in word.get('examples_target', []):
data['examples'].append(re.sub(r"<|>", "", example).lstrip('- '))

infobox += "</dl>"

if translation['source_synonyms']:
infobox += f"<dl><dt>{gettext('Synonyms')}: {', '.join(translation['source_synonyms'])}</dt></dl>"
data['synonyms'] = translation.get('source_synonyms', [])

result = {
'infobox': translation['translated-text'],
'content': infobox,
'content': render_template("answerers/translate.html", translations=[data]),
}

return [result]
36 changes: 36 additions & 0 deletions searx/templates/simple/answerers/translate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% for translation in translations %}
<hr />
{% if translations|length > 1 %}
<h2>{{ translation.text }}</h2>
{% endif %}
{% if translation.transliteration %}
<b>translation.transliteration</b>
{% endif %} {% if translation.definitions %}
<dl>
<dt>{{ gettext('Definitions') }}</dt>
<ul>
{% for definition in translation.definitions %}
<li>{{ definition }}</li>
{% endfor %}
<ul>
</dl>
{% endif %} {% if translation.examples %}
<dl>
<dt>{{ gettext('Examples') }}</dt>
<ul>
{% for example in translation.examples %}
<li>{{ example }}</li>
{% endfor %}
</ul>
</dl>
{% endif %} {% if translation.synonyms %}
<dl>
<dt>{{ gettext('Synonyms') }}</dt>
<ul>
{% for synonym in translation.synonyms %}
<li>{{ synonym }}</li>
{% endfor %}
</ul>
</dl>
{% endif %}
{% endfor %}
12 changes: 12 additions & 0 deletions searx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import importlib.util
import json
import types
import os

from typing import Optional, Union, Any, Set, List, Dict, MutableMapping, Tuple, Callable
from numbers import Number
Expand All @@ -19,9 +20,11 @@
from html import escape
from urllib.parse import urljoin, urlparse, parse_qs, urlencode
from markdown_it import MarkdownIt
from jinja2 import Environment, BaseLoader

from lxml import html
from lxml.etree import ElementBase, XPath, XPathError, XPathSyntaxError
from flask_babel import gettext

from searx import settings
from searx.data import USER_AGENTS, data_dir
Expand Down Expand Up @@ -800,3 +803,12 @@ def js_variable_to_python(js_variable):
s = s.replace(chr(1), ':')
# load the JSON and return the result
return json.loads(s)


def render_template(template_path, **args):
templates_path = settings['ui']['templates_path']
with open(os.path.join(templates_path, "simple/" + template_path)) as f:
template_str = f.read()
template = Environment(loader=BaseLoader).from_string(template_str)

return template.render(gettext=gettext, **args)

0 comments on commit e2b0fde

Please sign in to comment.