Skip to content

dataherb.core.search

core.search¤

search_by_ids_in_flora(flora, ids) ¤

search_in_flora finds the herb with the corresponding ids

Parameters:

Name Type Description Default
flora List[Herb]

list of herbs

required
ids Sequence[str]

ids of the herbs to be located

required

Returns:

Type Description
list

herbs that matches the id

Source code in dataherb/core/search.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def search_by_ids_in_flora(flora: List[Herb], ids: Sequence[str]) -> List[dict]:
    """
    search_in_flora finds the herb with the corresponding ids

    :param flora: list of herbs
    :type flora: list
    :param ids: ids of the herbs to be located
    :type ids: list
    :return: herbs that matches the id
    :rtype: list
    """

    if not isinstance(ids, Sequence):
        ids = [ids]

    herbs = []
    for herb in flora:
        if herb.id in ids:
            herb_matched = {"herb": herb, "id": herb.id}
            herbs.append(herb_matched)

    return herbs

search_by_keywords_in_flora(flora, keywords, keys=None, min_score=50) ¤

search_in_flora calculates the match score of each herb and returns the top 10.

Parameters:

Name Type Description Default
flora List[Herb]

list of herbs

required
keywords List[str]

search keywords

required
keys Optional[List[str]]

list of dictionary keys to look into

None
min_score float

minimum score of the dataset, default to 50

50
Source code in dataherb/core/search.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def search_by_keywords_in_flora(
    flora: List[Herb],
    keywords: List[str],
    keys: Optional[List[str]] = None,
    min_score: float = 50,
) -> List[dict]:
    """
    search_in_flora calculates the match score of each herb and returns the top 10.

    :param flora: list of herbs
    :param keywords: search keywords
    :param keys: list of dictionary keys to look into
    :param min_score: minimum score of the dataset, default to 50
    """

    if not isinstance(keywords, List):
        keywords = [keywords]

    herb_scores = []

    for herb in flora:
        herb_search_score = {
            "id": herb.id,
            "herb": herb,
            "score": herb.search_score(keywords),
        }
        herb_scores.append(herb_search_score)

    ranked_herbs = sorted(herb_scores, key=lambda i: i["score"], reverse=True)

    ranked_herbs = [i for i in ranked_herbs if i.get("score", 0) >= min_score]

    return ranked_herbs