File size: 1,023 Bytes
89a70e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Readable sector / type of business / niche from a BFF section1 doc.

section1 stores only ids (sectorId, subsectorId, nicheId), plus customNiche when
nicheId is "other". Sub-sector labels come from the exported subsectors.json;
sector and niche ids are snake_case, so they read fine once humanised.
"""

from news.business_types import SUBSECTORS

OTHER_NICHE_ID = "other"
_SUBSECTOR_LABELS = {s["id"]: s["label"] for s in SUBSECTORS}


def _humanise(id_: str) -> str:
    return id_.replace("_", " ").strip().capitalize()


def describe(s1: dict) -> dict:
    """{'sector': ..., 'subsector': ..., 'niche': ...}, '' for anything not set."""
    sub_id = s1.get("subsectorId", "")
    niche_id = s1.get("nicheId", "")
    if niche_id == OTHER_NICHE_ID:
        niche = (s1.get("customNiche") or "").strip()
    else:
        niche = _humanise(niche_id)
    return {
        "sector": _humanise(s1.get("sectorId", "")),
        "subsector": _SUBSECTOR_LABELS.get(sub_id, _humanise(sub_id)),
        "niche": niche,
    }