本文整理汇总了Python中haystack.utils.Highlighter类的典型用法代码示例。如果您正苦于以下问题:Python Highlighter类的具体用法?Python Highlighter怎么用?Python Highlighter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Highlighter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _do_search
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)
示例2: get_results
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
示例3: _search
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
示例4: execute_highlighter
def execute_highlighter(query, text_key, results):
highlight = Highlighter(query)
for result in results:
highlight.text_block = result.get_additional_fields().get(text_key, "")
highlight_locations = highlight.find_highlightable_words()
result.highlight_locations = []
for q, locations in highlight_locations.iteritems():
result.highlight_locations.extend([[location, location + len(q)] for location in locations])
示例5: no_query_found
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)
示例6: build_results_for_page
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]
示例7: search_view
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")
示例8: index
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: __init__
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)
示例10: test_find_highlightable_words
def test_find_highlightable_words(self):
highlighter = Highlighter('this test')
highlighter.text_block = self.document_1
self.assertEqual(highlighter.find_highlightable_words(), {'this': [0, 53, 79], 'test': [10, 68]})
# We don't stem for now.
highlighter = Highlighter('highlight tests')
highlighter.text_block = self.document_1
self.assertEqual(highlighter.find_highlightable_words(), {'highlight': [22], 'tests': []})
# Ignore negated bits.
highlighter = Highlighter('highlight -test')
highlighter.text_block = self.document_1
self.assertEqual(highlighter.find_highlightable_words(), {'highlight': [22]})
示例11: homeroom
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)
示例12: test_render_html
def test_render_html(self):
highlighter = Highlighter("this test")
highlighter.text_block = self.document_1
self.assertEqual(
highlighter.render_html({"this": [0, 53, 79], "test": [10, 68]}, 0, 200),
'<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.',
)
highlighter.text_block = self.document_2
self.assertEqual(
highlighter.render_html({"this": [0, 53, 79], "test": [10, 68]}, 0, 200),
"The content of words in no particular order causes nothing to occur.",
)
highlighter.text_block = self.document_3
self.assertEqual(
highlighter.render_html({"this": [0, 53, 79], "test": [10, 68]}, 0, 200),
'<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("content detection")
highlighter.text_block = self.document_3
self.assertEqual(
highlighter.render_html({"content": [151], "detection": [42]}, 42, 242),
'...<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.',
)
self.assertEqual(
highlighter.render_html({"content": [151], "detection": [42]}, 42, 200),
'...<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 no...',
)
示例13: slow_highlight
def slow_highlight(query, text):
"Invoked only if the search backend does not support highlighting"
highlight = Highlighter(query)
value = highlight.highlight(text)
return value
示例14: test_render_html
def test_render_html(self):
highlighter = Highlighter('this test')
highlighter.text_block = self.document_1
self.assertEqual(highlighter.render_html({'this': [0, 53, 79], 'test': [10, 68]}, 0, 200), '<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.')
highlighter.text_block = self.document_2
self.assertEqual(highlighter.render_html({'this': [0, 53, 79], 'test': [10, 68]}, 0, 200), 'The content of words in no particular order causes nothing to occur.')
highlighter.text_block = self.document_3
self.assertEqual(highlighter.render_html({'this': [0, 53, 79], 'test': [10, 68]}, 0, 200), '<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('content detection')
highlighter.text_block = self.document_3
self.assertEqual(highlighter.render_html({'content': [151], 'detection': [42]}, 42, 242), '...<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.')
self.assertEqual(highlighter.render_html({'content': [151], 'detection': [42]}, 42, 200), '...<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 no...')
# One term found within another term.
highlighter = Highlighter('this is')
highlighter.text_block = self.document_1
self.assertEqual(highlighter.render_html({'this': [0, 53, 79], 'is': [2, 5, 55, 58, 81]}, 0, 200), '<span class="highlighted">This</span> <span class="highlighted">is</span> a test of the highlightable words detection. <span class="highlighted">This</span> <span class="highlighted">is</span> only a test. Were <span class="highlighted">this</span> an actual emergency, your text would have exploded in mid-air.')
# Regression for repetition in the regular expression.
highlighter = Highlighter('i++')
highlighter.text_block = 'Foo is i++ in most cases.'
self.assertEqual(highlighter.render_html({'i++': [7]}, 0, 200), 'Foo is <span class="highlighted">i++</span> in most cases.')
highlighter = Highlighter('i**')
highlighter.text_block = 'Foo is i** in most cases.'
self.assertEqual(highlighter.render_html({'i**': [7]}, 0, 200), 'Foo is <span class="highlighted">i**</span> in most cases.')
highlighter = Highlighter('i..')
highlighter.text_block = 'Foo is i.. in most cases.'
self.assertEqual(highlighter.render_html({'i..': [7]}, 0, 200), 'Foo is <span class="highlighted">i..</span> in most cases.')
highlighter = Highlighter('i??')
highlighter.text_block = 'Foo is i?? in most cases.'
self.assertEqual(highlighter.render_html({'i??': [7]}, 0, 200), 'Foo is <span class="highlighted">i??</span> in most cases.')
# Regression for highlighting already highlighted HTML terms.
highlighter = Highlighter('span')
highlighter.text_block = 'A span in spam makes html in a can.'
self.assertEqual(highlighter.render_html({'span': [2]}, 0, 200), 'A <span class="highlighted">span</span> in spam makes html in a can.')
highlighter = Highlighter('highlight')
highlighter.text_block = 'A span in spam makes highlighted html in a can.'
self.assertEqual(highlighter.render_html({'highlight': [21]}, 0, 200), 'A span in spam makes <span class="highlighted">highlight</span>ed html in a can.')
示例15: highlight
def highlight(text_block, query, **kwargs):
highlighter = Highlighter(query, **kwargs)
highlighted_text = highlighter.highlight(text_block)
return mark_safe(highlighted_text)