diff options
author | Markus Heiser <markus.heiser@darmarit.de> | 2023-01-31 12:40:23 +0100 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarit.de> | 2023-04-15 22:23:33 +0200 |
commit | d5ecda99303f1555dc34d824f9dca662bbdda24e (patch) | |
tree | 558a23031685ad06864e4458ac4c261a8154e28d /searx/search | |
parent | c03b0ea650e0a052e422881c6278a5c9a7cca60c (diff) | |
download | searxng-d5ecda99303f1555dc34d824f9dca662bbdda24e.tar.gz searxng-d5ecda99303f1555dc34d824f9dca662bbdda24e.zip |
[mod] move language recognition to get_search_query_from_webapp
To set the language from language recognition and hold the value selected by the
client, the previous implementation creates a copy of the SearchQuery object and
manipulates the SearchQuery object by calling function replace_auto_language().
This patch tries to implement a similar functionality in a more central place,
in function get_search_query_from_webapp() when the SearchQuery object is build
up.
Additional this patch uses the language preferred by the client, if language
recognition does not have a match / the existing implementation does not care
about client preferences and uses 'all' in case of no match.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/search')
-rw-r--r-- | searx/search/__init__.py | 43 |
1 files changed, 2 insertions, 41 deletions
diff --git a/searx/search/__init__.py b/searx/search/__init__.py index e5465880c..77121c426 100644 --- a/searx/search/__init__.py +++ b/searx/search/__init__.py @@ -22,7 +22,6 @@ from searx.network import initialize as initialize_network, check_network_config from searx.metrics import initialize as initialize_metrics, counter_inc, histogram_observe_time from searx.search.processors import PROCESSORS, initialize as initialize_processors from searx.search.checker import initialize as initialize_checker -from searx.utils import detect_language logger = logger.getChild('search') @@ -40,57 +39,19 @@ def initialize(settings_engines=None, enable_checker=False, check_network=False, initialize_checker() -def replace_auto_language(search_query: SearchQuery): - """ - Do nothing except if `search_query.lang` is "auto". - In this case: - * the value "auto" is replaced by the detected language of the query. - The default value is "all" when no language is detected. - * `search_query.locale` is updated accordingly - - Use :py:obj:`searx.utils.detect_language` with `only_search_languages=True` to keep - only languages supported by the engines. - """ - if search_query.lang != 'auto': - return - - detected_lang = detect_language(search_query.query, threshold=0.3, only_search_languages=True) - if detected_lang is None: - # fallback to 'all' if no language has been detected - search_query.lang = 'all' - search_query.locale = None - return - search_query.lang = detected_lang - try: - search_query.locale = babel.Locale.parse(search_query.lang) - except babel.core.UnknownLocaleError: - search_query.locale = None - - class Search: """Search information container""" __slots__ = "search_query", "result_container", "start_time", "actual_timeout" def __init__(self, search_query: SearchQuery): - """Initialize the Search - - search_query is copied - """ + """Initialize the Search""" # init vars super().__init__() + self.search_query = search_query self.result_container = ResultContainer() self.start_time = None self.actual_timeout = None - self.search_query = copy(search_query) - self.update_search_query(self.search_query) - - def update_search_query(self, search_query: SearchQuery): - """Update search_query. - - call replace_auto_language to replace the "auto" language - """ - replace_auto_language(search_query) def search_external_bang(self): """ |