dc90841dd541fc3efcdfaec60103291f0e94e636
[wolnelektury.git] / apps / search / views.py
1
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
6
7 from catalogue.utils import get_random_hash
8 from catalogue.models import Book, Tag, TAG_CATEGORIES
9 from catalogue.fields import dumps
10 from catalogue.views import JSONResponse
11 from catalogue import forms
12 from search import MultiSearch, JVM, SearchResult
13 from lucene import StringReader
14
15 import enchant
16
17 dictionary = enchant.Dict('pl_PL')
18
19
20 def did_you_mean(query, tokens):
21     change = {}
22     
23     for t in tokens:
24         print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
25         if not dictionary.check(t):
26             try:
27                 change[t] = dictionary.suggest(t)[0]
28             except IndexError:
29                 pass
30
31     if change == {}:
32         return None
33
34     for frm, to in change.items():
35         query = query.replace(frm, to)
36
37     return query
38
39
40 def category_name(category):
41     try:
42         return filter(lambda c: c[0] == category, TAG_CATEGORIES)[0][1].encode('utf-8')
43     except IndexError:
44         raise KeyError("No category %s" % category)
45
46
47 def hint(request):
48     prefix = request.GET.get('term', '')
49     if len(prefix) < 2:
50         return JSONResponse(dumps({}))
51     JVM.attachCurrentThread()
52     s = MultiSearch()
53     tags = s.hint_tags(prefix)
54     books = s.hint_books(prefix)
55
56     return JSONResponse(
57         [{'label': t.name,
58           'category': category_name(t.category),
59           'id': t.id,
60           'url': t.get_absolute_url()}
61           for t in tags] + \
62           [{'label': b.title,
63             'category': category_name('book'),
64             'id': b.id,
65             'url': b.get_absolute_url()}
66             for b in books])
67
68
69 def main(request):
70     results = {}
71     JVM.attachCurrentThread()  # where to put this?
72     srch = MultiSearch()
73
74     results = None
75     query = None
76     fuzzy = False
77
78     if 'q' in request.GET:
79         tags = request.GET.get('tags', '')
80         try:
81             tag_list = Tag.get_tag_list(tags)
82         except:
83             tag_list = []
84
85             #        tag_filter = srch.
86
87         query = request.GET['q']
88         toks = StringReader(query)
89         fuzzy = 'fuzzy' in request.GET
90         if fuzzy:
91             fuzzy = 0.7
92
93
94         results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy),
95                                          srch.search_perfect_parts(toks, fuzzy=fuzzy),
96                                          srch.search_everywhere(toks, fuzzy=fuzzy))
97         results.sort(reverse=True)
98
99         for r in results:
100             print r.parts
101
102     return render_to_response('newsearch/search.html', {'results': results,
103                                                         'did_you_mean': (query is not None) and 
104                                                         did_you_mean(query, srch.get_tokens(query, field='SIMPLE')),
105                                                         'fuzzy': fuzzy},
106                               context_instance=RequestContext(request))