1 # -*- coding: utf-8 -*-
3 from django.shortcuts import render_to_response, get_object_or_404
4 from django.template import RequestContext
5 from django.contrib.auth.decorators import login_required
6 from django.views.decorators import cache
8 from catalogue.utils import get_random_hash
9 from catalogue.models import Book, Tag, TAG_CATEGORIES
10 from catalogue.fields import dumps
11 from catalogue.views import JSONResponse
12 from catalogue import forms
13 from search import MultiSearch, JVM, SearchResult
14 from lucene import StringReader
18 dictionary = enchant.Dict('pl_PL')
21 def did_you_mean(query, tokens):
23 # sprawdzić, czy słowo nie jest aby autorem - proste szukanie termu w author!
25 print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
26 if not dictionary.check(t):
28 change[t] = dictionary.suggest(t)[0]
35 for frm, to in change.items():
36 query = query.replace(frm, to)
41 def category_name(category):
43 return filter(lambda c: c[0] == category, TAG_CATEGORIES)[0][1].encode('utf-8')
45 raise KeyError("No category %s" % category)
49 prefix = request.GET.get('term', '')
51 return JSONResponse(dumps(None))
52 JVM.attachCurrentThread()
57 tags = request.GET.get('tags', '')
58 hint.tags(Tag.get_tag_list(tags))
62 # tagi beda ograniczac tutaj
63 # ale tagi moga byc na ksiazce i na fragmentach
64 # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
65 # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
67 tags = s.hint_tags(prefix)
68 books = s.hint_books(prefix)
74 'category': category_name(t.category),
76 'url': t.get_absolute_url()}
79 'category': category_name('book'),
81 'url': b.get_absolute_url()}
85 def foo(s, q, tag_list=None):
88 tag_list = Tag.get_tag_list(tag_list)
99 JVM.attachCurrentThread() # where to put this?
106 if 'q' in request.GET:
107 tags = request.GET.get('tags', '')
110 tag_list = Tag.get_tag_list(tags)
116 query = request.GET['q']
117 toks = StringReader(query)
118 fuzzy = 'fuzzy' in request.GET
122 results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy, hint=hint),
123 srch.search_perfect_parts(toks, fuzzy=fuzzy, hint=hint),
124 srch.search_everywhere(toks, fuzzy=fuzzy, hint=hint))
125 results.sort(reverse=True)
130 return render_to_response('newsearch/search.html', {'results': results,
131 'did_you_mean': (query is not None) and
132 did_you_mean(query, srch.get_tokens(query, field='SIMPLE')),
134 context_instance=RequestContext(request))