File size: 651 Bytes
ed6bec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# generator/encoder.py

from typing import Optional, Dict, Any

from .fallback import encode_fallback
from .dict_utils import is_defined


def encode_term(term_id: Optional[str], dictionaries: Dict[str, Any]) -> Optional[Dict[str, str]]:
    """
    Returns a dict with:
    - human
    - compact
    - tokens

    If the term is not defined in any dictionary, fallback is used.
    """
    if term_id is None:
        return None

    if not is_defined(term_id, dictionaries):
        return encode_fallback(term_id)

    upper = term_id.upper()

    return {
        "human": term_id,
        "compact": upper,
        "tokens": f"<{upper}>"
    }