1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-11-26 11:09:22 +03:00

searching separated into search.py

This commit is contained in:
Igor Chubin 2019-02-17 14:10:00 +01:00
parent 50f4abacb0
commit 94870b8192
2 changed files with 63 additions and 47 deletions

View File

@ -296,50 +296,7 @@ def get_answer(topic, keyword, options="", request_options=None): # pylint: disa
if not keyword:
return answer
# shorten the answer, because keyword is specified
#
insensitive = 'i' in options
word_boundaries = 'b' in options
paragraphs = _split_paragraphs(answer)
paragraphs = [p for p in paragraphs
if _paragraph_contains(p, keyword,
insensitive=insensitive,
word_boundaries=word_boundaries)]
if paragraphs == []:
return ""
answer = _join_paragraphs(paragraphs)
return answer
def find_answer_by_keyword(directory, keyword, options="", request_options=None):
"""
Search in the whole tree of all cheatsheets or in its subtree `directory`
by `keyword`
"""
recursive = 'r' in options
answer_paragraphs = []
for topic in get_topics_list(skip_internal=True, skip_dirs=True):
if not topic.startswith(directory):
continue
subtopic = topic[len(directory):]
if not recursive and '/' in subtopic:
continue
answer = get_answer(topic, keyword, options=options, request_options=request_options)
if answer:
answer_paragraphs.append(answer)
if len(answer_paragraphs) > MAX_SEARCH_LEN:
answer_paragraphs.append({
'topic_type': 'LIMITED',
'answer': "LIMITED TO %s ANSWERS" % MAX_SEARCH_LEN,
})
break
return answer_paragraphs
# pylint: disable=invalid-name
_ROUTER = Router()
get_topics_list = _ROUTER.get_topics_list
get_answer_dict = _ROUTER.get_answer_dict

59
lib/search.py Normal file
View File

@ -0,0 +1,59 @@
"""
Very naive search implementation. Just a placeholder.
Exports:
find_answer_by_keyword()
It should be implemented on the adapter basis:
1. adapter.search(keyword) returns list of matching answers
* maybe with some initial weight
2. ranking is done
3. sorted results are returned
4. eage page are cut by keyword
5. results are paginated
"""
from globals import MAX_SEARCH_LEN
from routing import get_answer_dict, get_topics_list
def _limited_entry():
return {
'topic_type': 'LIMITED',
"topic": "LIMITED",
'answer': "LIMITED TO %s ANSWERS" % MAX_SEARCH_LEN,
'format': "code",
}
def find_answers_by_keyword(directory, keyword, options="", request_options=None):
"""
Search in the whole tree of all cheatsheets or in its subtree `directory`
by `keyword`
"""
recursive = 'r' in options
answers_found = []
for topic in get_topics_list(skip_internal=True, skip_dirs=True):
if not topic.startswith(directory):
continue
subtopic = topic[len(directory):]
if not recursive and '/' in subtopic:
continue
answer = get_answer_dict(topic, request_options=request_options)
if answer and answer.get('answer') and keyword.lower() in answer.get('answer', '').lower():
answers_found.append(answer)
if len(answers_found) > MAX_SEARCH_LEN:
answers_found.append(
_limited_entry()
)
break
return answers_found