start = None
end = None
totlen = len(text)
- matches_margins = map(lambda (s, e): (max(0, s - margins), min(totlen, e + margins)), matches)
- (start, end) = matches_margins[0]
-
- for (s, e) in matches_margins[1:]:
+ matches_margins = map(lambda (s, e):
+ ((s, e),
+ (max(0, s - margins), min(totlen, e + margins))),
+ matches)
+ (start, end) = matches_margins[0][1]
+ matches = []
+ for (m, (s, e)) in matches_margins[1:]:
if end < s or start > e:
continue
start = min(start, s)
end = max(end, e)
-
+ matches.append(m)
+
snip = text[start:end]
- matches = list(matches)
matches.sort(lambda a, b: cmp(b[0], a[0]))
+
for (s, e) in matches:
off = - start
snip = snip[:e + off] + mark[1] + snip[e + off:]
continue
snip = result.snippets[idx]
# fix some formattting
+ snip = re.subn(r"(^[ \t\n]+|[ \t\n]+$)", u"",
+ re.subn(r"[ \t\n]*\n[ \t\n]*", u"\n", snip))
+
snip = snip.replace("\n", "<br />").replace('---', '—')
hit['snippet'] = snip
'book': book,
'main_link': book.get_absolute_url(),
'request': context.get('request'),
- 'hits': zip(*hits)[1],
+ 'hits': hits and zip(*hits)[1] or [],
'main_link': book.get_absolute_url(),
}
('text', text_phrase),
('text', everywhere)]:
res.sort(reverse=True)
- print "get snips %s, res size %d" % (field, len(res))
for r in res:
- print "Get snippets for %s" % r
search.get_snippets(r, query, field, 3)
- # for r in res:
- # for h in r.hits:
- # h['snippets'] = map(lambda s:
- # re.subn(r"(^[ \t\n]+|[ \t\n]+$)", u"",
- # re.subn(r"[ \t\n]*\n[ \t\n]*", u"\n", s)[0])[0], h['snippets'])
- # suggestion = did_you_mean(query, search.get_tokens(toks, field="SIMPLE"))
suggestion = u''
def ensure_exists(r):