+from catalogue.models import Book, Tag, Fragment
+from catalogue.fields import dumps
+from catalogue.views import JSONResponse
+from search import Search, JVM, SearchResult
+from lucene import StringReader
+from suggest.forms import PublishingSuggestForm
+
+import enchant
+
+dictionary = enchant.Dict('pl_PL')
+
+
+def match_word_re(word):
+ if 'sqlite' in settings.DATABASES['default']['ENGINE']:
+ return r"\b%s\b" % word
+ elif 'mysql' in settings.DATABASES['default']['ENGINE']:
+ return "[[:<:]]%s[[:>:]]" % word
+
+
+def did_you_mean(query, tokens):
+ change = {}
+ for t in tokens:
+ print("%s ok? %s, sug: %s" % (t, dictionary.check(t), dictionary.suggest(t)))
+ authors = Tag.objects.filter(category='author', name__iregex=match_word_re(t))
+ if len(authors) > 0:
+ continue
+
+ if not dictionary.check(t):
+ try:
+ change[t] = dictionary.suggest(t)[0]
+ except IndexError:
+ pass
+
+ if change == {}:
+ return None
+
+ for frm, to in change.items():
+ query = query.replace(frm, to)
+
+ return query
+
+
+def hint(request):
+ prefix = request.GET.get('term', '')
+ if len(prefix) < 2:
+ return JSONResponse([])
+ JVM.attachCurrentThread()
+ s = Search()
+
+ hint = s.hint()
+ try:
+ tags = request.GET.get('tags', '')
+ hint.tags(Tag.get_tag_list(tags))
+ except:
+ pass
+
+ # tagi beda ograniczac tutaj
+ # ale tagi moga byc na ksiazce i na fragmentach
+ # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
+ # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
+
+ tags = s.hint_tags(prefix)
+ books = s.hint_books(prefix)
+
+ # TODO DODAC TU HINTY
+
+ return JSONResponse(
+ [{'label': t.name,
+ 'category': _(t.category),
+ 'id': t.id,
+ 'url': t.get_absolute_url()}
+ for t in tags] + \
+ [{'label': b.title,
+ 'category': _('book'),
+ 'id': b.id,
+ 'url': b.get_absolute_url()}
+ for b in books])