2 from django.shortcuts import render_to_response, get_object_or_404
3 from django.template import RequestContext
4 from django.contrib.auth.decorators import login_required
5 from django.views.decorators import cache
7 from catalogue.utils import get_random_hash
8 from catalogue.models import Book, Tag
9 from catalogue import forms
10 from search import MultiSearch, JVM, SearchResult
11 from lucene import StringReader
15 dictionary = enchant.Dict('pl_PL')
18 def did_you_mean(query, tokens):
22 print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
23 if not dictionary.check(t):
25 change[t] = dictionary.suggest(t)[0]
32 for frm, to in change.items():
33 query = query.replace(frm, to)
40 JVM.attachCurrentThread() # where to put this?
46 if 'q' in request.GET:
47 query = request.GET['q']
48 toks = StringReader(query)
49 fuzzy = 'fuzzy' in request.GET
54 results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy),
55 srch.search_perfect_parts(toks, fuzzy=fuzzy),
56 srch.search_everywhere(toks, fuzzy=fuzzy))
57 results.sort(reverse=True)
62 return render_to_response('newsearch/search.html', {'results': results,
63 'did_you_mean': (query is not None) and
64 did_you_mean(query, srch.get_tokens(query, field='SIMPLE')),
66 context_instance=RequestContext(request))