.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.query.LanguageParser Class Reference
+ Inheritance diagram for searx.query.LanguageParser:
+ Collaboration diagram for searx.query.LanguageParser:

Public Member Functions

 __call__ (self, raw_value)
 
- Public Member Functions inherited from searx.query.QueryPartParser
 __init__ (self, raw_text_query, enable_autocomplete)
 

Static Public Member Functions

 check (raw_value)
 

Protected Member Functions

 _parse (self, value)
 
 _autocomplete (self, value)
 
- Protected Member Functions inherited from searx.query.QueryPartParser
 _add_autocomplete (self, value)
 

Additional Inherited Members

- Public Attributes inherited from searx.query.QueryPartParser
 raw_text_query
 
 enable_autocomplete
 

Detailed Description

Definition at line 72 of file query.py.

Member Function Documentation

◆ __call__()

searx.query.LanguageParser.__call__ ( self,
raw_value )
Try to parse raw_value: set the self.raw_text_query properties

return True if raw_value has been parsed

self.raw_text_query.autocomplete_list is also modified
if self.enable_autocomplete is True

Reimplemented from searx.query.QueryPartParser.

Definition at line 77 of file query.py.

77 def __call__(self, raw_value):
78 value = raw_value[1:].lower().replace('_', '-')
79 found = self._parse(value) if len(value) > 0 else False
80 if self.enable_autocomplete and not found:
81 self._autocomplete(value)
82 return found
83

References searx.query.TimeoutParser._autocomplete(), searx.query.ExternalBangParser._autocomplete(), searx.query.BangParser._autocomplete(), searx.query.LanguageParser._autocomplete(), searx.query.TimeoutParser._parse(), searx.query.LanguageParser._parse(), searx.query.ExternalBangParser._parse(), searx.query.BangParser._parse(), and searx.query.QueryPartParser.enable_autocomplete.

+ Here is the call graph for this function:

◆ _autocomplete()

searx.query.LanguageParser._autocomplete ( self,
value )
protected

Definition at line 118 of file query.py.

118 def _autocomplete(self, value):
119 if not value:
120 # show some example queries
121 if len(settings['search']['languages']) < 10:
122 for lang in settings['search']['languages']:
123 self.raw_text_query.autocomplete_list.append(':' + lang)
124 else:
125 for lang in [":en", ":en_us", ":english", ":united_kingdom"]:
126 self.raw_text_query.autocomplete_list.append(lang)
127 return
128
129 for lc in sxng_locales:
130 if lc[0] not in settings['search']['languages']:
131 continue
132 lang_id, lang_name, country, english_name, _flag = map(str.lower, lc)
133
134 # check if query starts with language-id
135 if lang_id.startswith(value):
136 if len(value) <= 2:
137 self._add_autocomplete(':' + lang_id.split('-')[0])
138 else:
139 self._add_autocomplete(':' + lang_id)
140
141 # check if query starts with language name
142 if lang_name.startswith(value) or english_name.startswith(value):
143 self._add_autocomplete(':' + lang_name)
144
145 # check if query starts with country
146 # here "new_zealand" is "new-zealand" (see __call__)
147 if country.startswith(value.replace('-', ' ')):
148 self._add_autocomplete(':' + country.replace(' ', '_'))
149
150

References searx.query.QueryPartParser._add_autocomplete(), and searx.query.QueryPartParser.raw_text_query.

Referenced by searx.query.TimeoutParser.__call__(), searx.query.LanguageParser.__call__(), searx.query.ExternalBangParser.__call__(), and searx.query.BangParser.__call__().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _parse()

searx.query.LanguageParser._parse ( self,
value )
protected

Definition at line 84 of file query.py.

84 def _parse(self, value):
85 found = False
86 # check if any language-code is equal with
87 # declared language-codes
88 for lc in sxng_locales:
89 lang_id, lang_name, country, english_name, _flag = map(str.lower, lc)
90
91 # if correct language-code is found
92 # set it as new search-language
93
94 if (
95 value == lang_id or value == lang_name or value == english_name or value.replace('-', ' ') == country
96 ) and value not in self.raw_text_query.languages:
97 found = True
98 lang_parts = lang_id.split('-')
99 if len(lang_parts) == 2:
100 self.raw_text_query.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
101 else:
102 self.raw_text_query.languages.append(lang_id)
103 # to ensure best match (first match is not necessarily the best one)
104 if value == lang_id:
105 break
106
107 # user may set a valid, yet not selectable language
108 if VALID_LANGUAGE_CODE.match(value) or value == 'auto':
109 lang_parts = value.split('-')
110 if len(lang_parts) > 1:
111 value = lang_parts[0].lower() + '-' + lang_parts[1].upper()
112 if value not in self.raw_text_query.languages:
113 self.raw_text_query.languages.append(value)
114 found = True
115
116 return found
117

References searx.query.QueryPartParser.raw_text_query.

Referenced by searx.query.TimeoutParser.__call__(), searx.query.LanguageParser.__call__(), searx.query.ExternalBangParser.__call__(), and searx.query.BangParser.__call__().

+ Here is the caller graph for this function:

◆ check()

searx.query.LanguageParser.check ( raw_value)
static
Check if raw_value can be parsed

Reimplemented from searx.query.QueryPartParser.

Definition at line 74 of file query.py.

74 def check(raw_value):
75 return raw_value[0] == ':'
76

The documentation for this class was generated from the following file: