almost there
[wolnelektury.git] / apps / search / views.py
1 # -*- coding: utf-8 -*-
2
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
7
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
15
16 import enchant
17
18 dictionary = enchant.Dict('pl_PL')
19
20
21 def did_you_mean(query, tokens):
22     change = {}
23     # sprawdzić, czy słowo nie jest aby autorem - proste szukanie termu w author!
24     for t in tokens:
25         print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
26         if not dictionary.check(t):
27             try:
28                 change[t] = dictionary.suggest(t)[0]
29             except IndexError:
30                 pass
31
32     if change == {}:
33         return None
34
35     for frm, to in change.items():
36         query = query.replace(frm, to)
37
38     return query
39
40
41 def category_name(category):
42     try:
43         return filter(lambda c: c[0] == category, TAG_CATEGORIES)[0][1].encode('utf-8')
44     except IndexError:
45         raise KeyError("No category %s" % category)
46
47
48 def hint(request):
49     prefix = request.GET.get('term', '')
50     if len(prefix) < 2:
51         return JSONResponse(dumps(None))
52     JVM.attachCurrentThread()
53     s = MultiSearch()
54
55     hint = s.hint()
56     try:
57         tags = request.GET.get('tags', '')
58         hint.tags(Tag.get_tag_list(tags))
59     except:
60         pass
61
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.
66
67     tags = s.hint_tags(prefix)
68     books = s.hint_books(prefix)
69
70     # TODO DODAC TU HINTY
71
72     return JSONResponse(
73         [{'label': t.name,
74           'category': category_name(t.category),
75           'id': t.id,
76           'url': t.get_absolute_url()}
77           for t in tags] + \
78           [{'label': b.title,
79             'category': category_name('book'),
80             'id': b.id,
81             'url': b.get_absolute_url()}
82             for b in books])
83
84
85 def foo(s, q, tag_list=None):
86     hint = s.hint()
87     try:
88         tag_list = Tag.get_tag_list(tag_list)
89         hint.tags(tag_list)
90     except:
91         tag_list = None
92
93     q = StringReader(q)
94     return (q, hint)
95
96
97 def main(request):
98     results = {}
99     JVM.attachCurrentThread()  # where to put this?
100     srch = MultiSearch()
101
102     results = None
103     query = None
104     fuzzy = False
105
106     if 'q' in request.GET:
107         tags = request.GET.get('tags', '')
108         hint = srch.hint()
109         try:
110             tag_list = Tag.get_tag_list(tags)
111         except:
112             tag_list = []
113
114         hint.tags(tag_list)
115
116         query = request.GET['q']
117         toks = StringReader(query)
118         fuzzy = 'fuzzy' in request.GET
119         if fuzzy:
120             fuzzy = 0.7
121
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)
126
127         for r in results:
128             print r.hits
129
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')),
133                                                         'fuzzy': fuzzy},
134                               context_instance=RequestContext(request))