本文整理汇总了Python中haystack.utils.Highlighter.highlight方法的典型用法代码示例。如果您正苦于以下问题:Python Highlighter.highlight方法的具体用法?Python Highlighter.highlight怎么用?Python Highlighter.highlight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类haystack.utils.Highlighter
的用法示例。
在下文中一共展示了Highlighter.highlight方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_results_for_page
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def build_results_for_page(games, query):
highlighter = Highlighter(query)
return [{'url': game.url,
'title': highlighter.highlight(game.title),
'intro': highlighter.highlight(game.intro),
'city': highlighter.highlight(game.location.city),
'state': highlighter.highlight(game.location.state)} for game in games]
示例2: no_query_found
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def no_query_found(self):
all_results = SearchQuerySet(self).all()
#sqs = SearchQuerySet().filter(content='foo').highlight()
sqs = SearchQuerySet().filter(content=all_results).highlight()
highlighter = Highlighter(search_query)
result = sqs[0]
result.highlighted['text'][0]
print highlighter.highlight(sqs[0].text)
示例3: _do_search
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def _do_search(self, request, model):
self.method_check(request, allowed=['get'])
self.is_authenticated(request)
self.throttle_check(request)
# Do the query.
query = request.GET.get('q', '')
sqs = SearchQuerySet().models(model).load_all().auto_query(query)
paginator = Paginator(sqs, 20)
try:
page = paginator.page(int(request.GET.get('page', 1)))
except InvalidPage:
raise Http404("Sorry, no results on that page.")
objects = []
for result in page.object_list:
if result:
highlighter = Highlighter(query)
text = highlighter.highlight(result.text)
bundle = self.build_bundle(obj=result.object, request=request)
bundle = self.full_dehydrate(bundle)
bundle.data['text'] = text
objects.append(bundle)
object_list = {
'objects': objects,
}
self.log_throttled_access(request)
return self.create_response(request, object_list)
示例4: get_results
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def get_results(self):
"""
Override get_results to add the value of the field where query was found
Also takes care of highlighting the query.
"""
results = super(FindView, self).get_results()
query = self.query.lower()
highlight = Highlighter(query)
for r in results:
for field in r.get_stored_fields():
value = getattr(r, field)
# assume search index field 'text' is document field
if isinstance(value, string_types) and\
query in value.lower() and\
field != 'text':
# assume search index field name == model field name
try:
name = r.object._meta.get_field(field).verbose_name
except:
name = field
r.context = {
'field': name,
'value': highlight.highlight(value)
}
continue
return results
示例5: _search
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def _search(self, request, model, facets=None, page_size=20,
highlight=True):
"""
`facets`
A list of facets to include with the results
`models`
Limit the search to one or more models
"""
form = FacetedSearchForm(request.GET, facets=facets or [],
models=(model,), load_all=True)
if not form.is_valid():
return self.error_response({'errors': form.errors}, request)
results = form.search()
paginator = Paginator(results, page_size)
try:
page = paginator.page(int(request.GET.get('page', 1)))
except InvalidPage:
raise Http404(ugettext("Sorry, no results on that page."))
objects = []
query = request.GET.get('q', '')
highlighter = Highlighter(query)
for result in page.object_list:
if not result:
continue
text = result.text
if highlight:
text = highlighter.highlight(text)
bundle = self.build_bundle(obj=result.object, request=request)
bundle = self.full_dehydrate(bundle)
bundle.data['text'] = text
objects.append(bundle)
url_template = self._url_template(query,
form['selected_facets'].value())
page_data = {
'number': page.number,
'per_page': paginator.per_page,
'num_pages': paginator.num_pages,
'page_range': paginator.page_range,
'object_count': paginator.count,
'url_template': url_template,
}
if page.has_next():
page_data['url_next'] = url_template.format(
page.next_page_number())
if page.has_previous():
page_data['url_prev'] = url_template.format(
page.previous_page_number())
object_list = {
'page': page_data,
'objects': objects,
}
if facets:
object_list.update({'facets': results.facet_counts()})
return object_list
示例6: search_view
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def search_view(request):
keyword = request.GET['q']
results = SearchQuerySet().filter(content=keyword)
highlighter = Highlighter(keyword)
results_dict = {
'success': True,
'by': 'search',
'list': [{
'title': r.object.title,
'content': highlighter.highlight(r.object.content),
'uri': r.object.abs_uri
} for r in results]
}
return HttpResponse(json.dumps(results_dict), content_type="application/json")
示例7: __init__
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def __init__(self, scholarship_key=None, search_result=None, to_highlight=''):
scholarship_model = search_result.object
self.scholarship_key = scholarship_key
scholarship_model = scholarship_model
highlight = Highlighter(to_highlight, max_length=300)
self.snippet = highlight.highlight(scholarship_model.description)
if scholarship_model is not None:
self.deadline = scholarship_model.deadline
self.source = scholarship_model.organization
self.href = scholarship_model.third_party_url
self.title = scholarship_model.title
self.essay_required = scholarship_model.essay_required
self.gender_restriction = scholarship_model.gender_restriction
safe_title = scholarship_model.title[:100].encode('ascii', 'ignore')
self.vs_href = u'/scholarship/{}?title={}'.format(self.scholarship_key, safe_title)
示例8: index
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def index(request):
#搜索词
words = request.GET['key']
results = SearchQuerySet().filter(name__contains=(words))
#Highlighter(my_query,html_tag='',css_class='',max_length=100)
highlight = Highlighter(words,max_length=100)
#(content=(words))##.facet('name',limit=10)
#输出总条数
counts = results.count()
for r in results:
#设置高亮
r.name = highlight.highlight(r.name)
#unicode -> string unicodestring.endcode('utf-8')
#string -> unicode unicode(utf8string,'utf-8)
return render(request,'search/search.html',{'data':results,'counts':counts})
示例9: test_highlight
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def test_highlight(self):
highlighter = Highlighter('this test')
self.assertEqual(highlighter.highlight(self.document_1), u'<span class="highlighted">This</span> is a <span class="highlighted">test</span> of the highlightable words detection. <span class="highlighted">This</span> is only a <span class="highlighted">test</span>. Were <span class="highlighted">this</span> an actual emergency, your text would have exploded in mid-air.')
self.assertEqual(highlighter.highlight(self.document_2), u'The content of words in no particular order causes nothing to occur.')
self.assertEqual(highlighter.highlight(self.document_3), u'<span class="highlighted">This</span> is a <span class="highlighted">test</span> of the highlightable words detection. <span class="highlighted">This</span> is only a <span class="highlighted">test</span>. Were <span class="highlighted">this</span> an actual emergency, your text would have exploded in mid-air. The content of words in no particular order causes no...')
highlighter = Highlighter('this test', html_tag='div', css_class=None)
self.assertEqual(highlighter.highlight(self.document_1), u'<div>This</div> is a <div>test</div> of the highlightable words detection. <div>This</div> is only a <div>test</div>. Were <div>this</div> an actual emergency, your text would have exploded in mid-air.')
self.assertEqual(highlighter.highlight(self.document_2), u'The content of words in no particular order causes nothing to occur.')
self.assertEqual(highlighter.highlight(self.document_3), u'<div>This</div> is a <div>test</div> of the highlightable words detection. <div>This</div> is only a <div>test</div>. Were <div>this</div> an actual emergency, your text would have exploded in mid-air. The content of words in no particular order causes no...')
highlighter = Highlighter('content detection')
self.assertEqual(highlighter.highlight(self.document_1), u'...<span class="highlighted">detection</span>. This is only a test. Were this an actual emergency, your text would have exploded in mid-air.')
self.assertEqual(highlighter.highlight(self.document_2), u'...<span class="highlighted">content</span> of words in no particular order causes nothing to occur.')
self.assertEqual(highlighter.highlight(self.document_3), u'...<span class="highlighted">detection</span>. This is only a test. Were this an actual emergency, your text would have exploded in mid-air. The <span class="highlighted">content</span> of words in no particular order causes nothing to occur.')
highlighter = Highlighter('content detection', max_length=100)
self.assertEqual(highlighter.highlight(self.document_1), u'...<span class="highlighted">detection</span>. This is only a test. Were this an actual emergency, your text would have exploded in mid-...')
self.assertEqual(highlighter.highlight(self.document_2), u'...<span class="highlighted">content</span> of words in no particular order causes nothing to occur.')
self.assertEqual(highlighter.highlight(self.document_3), u'This is a test of the highlightable words <span class="highlighted">detection</span>. This is only a test. Were this an actual emerge...')
示例10: homeroom
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def homeroom(request):
user = request.user
context = RequestContext(request)
if request.method == 'POST':
query = request.POST['course-search']
results = SearchQuerySet().autocomplete(text=query).models(Course)[:10]
highlighter = Highlighter(query, html_tag='span', css_class='keyword')
courses = []
for result in results:
course = {}
course['object'] = result.object
course['highlight'] = highlighter.highlight(result.text)
courses.append(course)
# courses = Course.objects.filter(institute=user.get_profile().institute, title__icontains=query)
suggestion = None
# suggestion = SearchQuerySet().spelling_suggestion(query)
context['courses'] = courses
context['suggestion'] = suggestion
sections = [assign.section for assign in SectionAssign.objects.filter(user=user).order_by('-section__start_date')]
form = AddCourseForm(request=request)
context['sections'] = sections
context['form'] = form
return render_to_response('homeroom/index.html', context)
示例11: slow_highlight
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def slow_highlight(query, text):
"Invoked only if the search backend does not support highlighting"
highlight = Highlighter(query)
value = highlight.highlight(text)
return value
示例12: highlight_result
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def highlight_result(text, query, length=500):
hl = Highlighter(query, html_tag='strong', max_length=length)
hl = hl.highlight(text)
return hl
示例13: highlighted_persref
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def highlighted_persref(text, query, **kwargs):
highlight = Highlighter(
query, html_tag='strong', css_class='found', max_length=120)
phText = highlight.highlight(text)
parsedText = add_persref_links(phText)
return format_html(parsedText)
示例14: return_search_results_ajax
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def return_search_results_ajax(request):
"""
Process queries issued from the haystack search form and
validate the form. If the form is valid, highlight the queried terms and
return the top hundred search results as highlighted snippets.
NOTE: Due to performance issues, only the top one hundred search results
will be returned at maximum, no matter how many search results have been
found. This shortcoming can be improved by providing more efficient
database queries and adding a sophisticated caching functionality.
"""
haystack_search_form = HaystackSearchForm(request.GET)
response = {}
if haystack_search_form.is_valid():
search_query = haystack_search_form.cleaned_data['search_query']
search_source = haystack_search_form.cleaned_data['search_source']
max_results = haystack_search_form.cleaned_data['max_results']
search_source_to_model_name = {
'venyoo_events': 'event',
'crawled_webpages': 'crawledwebpage'}
highlighter = Highlighter(
search_query,
html_tag='strong',
css_class='highlighted',
max_length=250)
search_results = SearchQuerySet().filter(content=AutoQuery(search_query))
end = int(math.ceil(search_results.count() / 1000))
results = []
webpage_urls = []
highlighted_snippets = []
a, b = 0, 1000
for i in xrange(end):
if search_source in ('venyoo_events', 'crawled_webpages'):
results = results + \
[result for result in search_results[a:b]
if isinstance(result, SearchResult)
and result.model_name ==
search_source_to_model_name[search_source]]
else:
results = results +\
[result for result in search_results[a:b]
if isinstance(result, SearchResult)]
webpage_urls = webpage_urls +\
[result.get_stored_fields()['url'] for result
in results[a:b]]
highlighted_snippets = highlighted_snippets +\
[highlighter.highlight(result.text) for
result in results[a:b]]
a += 1000
b += 1000
results_total = len(results)
response['results_total'] = results_total
response['results_shown'] = max_results if max_results <= results_total else results_total
response['webpage_urls'] = webpage_urls[:max_results]
response['highlighted_snippets'] = highlighted_snippets[:max_results]
return HttpResponse(json.dumps(response), mimetype='application/json')
示例15: highlight
# 需要导入模块: from haystack.utils import Highlighter [as 别名]
# 或者: from haystack.utils.Highlighter import highlight [as 别名]
def highlight(text_block, query, **kwargs):
highlighter = Highlighter(query, **kwargs)
highlighted_text = highlighter.highlight(text_block)
return mark_safe(highlighted_text)