Skip to content

Commit 94870b8

Browse files
committed
searching separated into search.py
1 parent 50f4aba commit 94870b8

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

lib/routing.py

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -296,50 +296,7 @@ def _rewrite_section_name_for_q(query):
296296
if not keyword:
297297
return answer
298298

299-
# shorten the answer, because keyword is specified
300-
#
301-
insensitive = 'i' in options
302-
word_boundaries = 'b' in options
303-
304-
paragraphs = _split_paragraphs(answer)
305-
paragraphs = [p for p in paragraphs
306-
if _paragraph_contains(p, keyword,
307-
insensitive=insensitive,
308-
word_boundaries=word_boundaries)]
309-
if paragraphs == []:
310-
return ""
311-
312-
answer = _join_paragraphs(paragraphs)
313-
314-
return answer
315-
316-
def find_answer_by_keyword(directory, keyword, options="", request_options=None):
317-
"""
318-
Search in the whole tree of all cheatsheets or in its subtree `directory`
319-
by `keyword`
320-
"""
321-
322-
recursive = 'r' in options
323-
324-
answer_paragraphs = []
325-
for topic in get_topics_list(skip_internal=True, skip_dirs=True):
326-
327-
if not topic.startswith(directory):
328-
continue
329-
330-
subtopic = topic[len(directory):]
331-
if not recursive and '/' in subtopic:
332-
continue
333-
334-
answer = get_answer(topic, keyword, options=options, request_options=request_options)
335-
if answer:
336-
answer_paragraphs.append(answer)
337-
338-
if len(answer_paragraphs) > MAX_SEARCH_LEN:
339-
answer_paragraphs.append({
340-
'topic_type': 'LIMITED',
341-
'answer': "LIMITED TO %s ANSWERS" % MAX_SEARCH_LEN,
342-
})
343-
break
344-
345-
return answer_paragraphs
299+
# pylint: disable=invalid-name
300+
_ROUTER = Router()
301+
get_topics_list = _ROUTER.get_topics_list
302+
get_answer_dict = _ROUTER.get_answer_dict

lib/search.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Very naive search implementation. Just a placeholder.
3+
4+
Exports:
5+
6+
find_answer_by_keyword()
7+
8+
It should be implemented on the adapter basis:
9+
10+
1. adapter.search(keyword) returns list of matching answers
11+
* maybe with some initial weight
12+
2. ranking is done
13+
3. sorted results are returned
14+
4. eage page are cut by keyword
15+
5. results are paginated
16+
17+
"""
18+
19+
from globals import MAX_SEARCH_LEN
20+
from routing import get_answer_dict, get_topics_list
21+
22+
def _limited_entry():
23+
return {
24+
'topic_type': 'LIMITED',
25+
"topic": "LIMITED",
26+
'answer': "LIMITED TO %s ANSWERS" % MAX_SEARCH_LEN,
27+
'format': "code",
28+
}
29+
30+
def find_answers_by_keyword(directory, keyword, options="", request_options=None):
31+
"""
32+
Search in the whole tree of all cheatsheets or in its subtree `directory`
33+
by `keyword`
34+
"""
35+
36+
recursive = 'r' in options
37+
38+
answers_found = []
39+
for topic in get_topics_list(skip_internal=True, skip_dirs=True):
40+
41+
if not topic.startswith(directory):
42+
continue
43+
44+
subtopic = topic[len(directory):]
45+
if not recursive and '/' in subtopic:
46+
continue
47+
48+
answer = get_answer_dict(topic, request_options=request_options)
49+
50+
if answer and answer.get('answer') and keyword.lower() in answer.get('answer', '').lower():
51+
answers_found.append(answer)
52+
53+
if len(answers_found) > MAX_SEARCH_LEN:
54+
answers_found.append(
55+
_limited_entry()
56+
)
57+
break
58+
59+
return answers_found

0 commit comments

Comments
 (0)