[enh] search query highlight in result texts

This commit is contained in:
asciimoo 2013-11-09 18:37:25 +01:00
parent e946752474
commit 14a53e3430

View File

@ -83,6 +83,7 @@ def default_request_params():
return {'method': 'GET', 'headers': {}, 'data': {}, 'url': '', 'cookies': {}} return {'method': 'GET', 'headers': {}, 'data': {}, 'url': '', 'cookies': {}}
def make_callback(engine_name, results, callback, params): def make_callback(engine_name, results, callback, params):
# creating a callback wrapper for the search engine results
def process_callback(response, **kwargs): def process_callback(response, **kwargs):
cb_res = [] cb_res = []
response.search_params = params response.search_params = params
@ -100,6 +101,16 @@ def make_callback(engine_name, results, callback, params):
results[engine_name] = cb_res results[engine_name] = cb_res
return process_callback return process_callback
def highlight_content(content, query):
# ignoring html contents
# TODO better html content detection
if content.find('<') != -1:
return content
for chunk in query.split():
content = content.replace(chunk, '<b>{0}</b>'.format(chunk))
return content
def search(query, request, selected_engines): def search(query, request, selected_engines):
global engines, categories, number_of_searches global engines, categories, number_of_searches
requests = [] requests = []
@ -176,6 +187,8 @@ def search(query, request, selected_engines):
results.append(res) results.append(res)
for result in results: for result in results:
if 'content' in result:
result['content'] = highlight_content(result['content'], query)
for res_engine in result['engines']: for res_engine in result['engines']:
engines[result['engine']].stats['score_count'] += result['score'] engines[result['engine']].stats['score_count'] += result['score']