File size: 1,374 Bytes
1c6c5d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02a3c70
 
1c6c5d7
02a3c70
1c6c5d7
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
39
40
41
42
43
44
45
46
47
from typing import List, Tuple, Dict, Any


def process_reasons(reasons: dict) -> List[Tuple[str, str, str]]:
    """Proprocessing for recommendation reasons data to display in a dataframe.

    Parameters
    ----------
    reasons : dict

    Returns
    -------
    List[Tuple[str, str, str]]
        (attribute type, attribute code, attribute description)
    """

    output = []
    for code, desc in (reasons.get("subject_codes", {}) or {}).items():
        output.append(["PCS subject", code, desc])

    for code, desc in (reasons.get("pop_group_codes", {}) or {}).items():
        output.append(["PCS population", code, desc])

    for code, desc in (reasons.get("geo_ids", {}) or {}).items():
        output.append(["Geography served", code, desc])
    return output


def parse_pcs_descriptions(taxonomy: List[Dict[str, Any]]) -> str:
    reasons = []

    for term in taxonomy:
        facet = (term.get("facet") or "").lower()
        if facet == "subject":
            reasons.append(f"- For <b>{term.get('description')}</b>")
        elif facet == "population":
            reasons.append(f"- Serving <b>{term.get('description')}</b>")
    return '\n'.join(reasons)


def parse_geo_descriptions(geos: List[Dict[str, Any]]) -> str:
    reasons = []

    for geo in geos:
        reasons.append(f"- Serving <b>{geo['name']}</b>")
    return '\n'.join(reasons)