コード例 #1
0
def set_leaf_colours(node: Node, parent_label: str, colours: Dict[str, str]):
    """
    Method used to create the text object with colour for the leaves
    :param node: The TextLeaf which needs to get a colour
    :param parent_label: The label of the parent
    :param colours: The colour dictionary for the current environment
    :return: Returns the object needed for the frontend
    """

    if node.is_speculative():
        return ""
    label = parent_label + node.category
    if node.category == "O":
        result_type = "other"
        colour = None
    else:
        result_type = "label"
        colour = colours.get(node.category, FALLBACK_COLOUR)
    return {
        "text": node.text,
        "colour": colour,
        "type": result_type,
        "label": label,
    }
コード例 #2
0
def json_node_template(node: Node, identifier: str, parent_id: str,
                       change: FrontChange, leaf: bool) -> dict:
    """
    Method used to create a json template of any node in the tree
    :param node: The node for which
    :param identifier: The identifier of the node
    :param parent_id: The identifier of the parent
    :param change: the frontend changes for the node
    :param leaf: whether the node is a the end of a branch
    :return:
    """
    alternatives = None
    low_confidence = False
    percentage = None
    if leaf:
        if isinstance(node, LabelNode):
            alternatives = node.options
            text = node.label
            if not node.is_corrected():
                percentage = node.pred_label_conf
            hint = node.text
        else:
            text = node.text
            hint = None
    else:
        text = node.category
        percentage = node.pred_text_conf
        hint = node.hint

    if not text:
        text = "?"
    orgtemplate = "<div class=\"domStyle\"><span>{}</span></div>".format(text)
    template = orgtemplate
    if percentage:
        low_confidence = percentage < 75
        template = orgtemplate + "<span class=\"confidence\">{}%</span>".format(
            percentage)
    if leaf:
        if node.is_corrected():
            template = orgtemplate + "</div></span><span class=\"material-icons\">mode</span>"

    background_color = COLOUR_DICT[leaf][node.is_speculative()]
    outline_color = background_color

    jsonnode = {
        "nodeId": identifier,
        "parentNodeId": parent_id,
        "lowConfidence": low_confidence,
        "width": 347,
        "height": 147,
        "template": template,
        "alternatives": alternatives,
        "hint": hint,
        "text": text,
        "isCorrected": node.is_corrected(),
        "speculative": node.is_speculative(),
        "backgroundColor": background_color,
        "textColor": "#FFFFFF",
        "outlineColor": outline_color,
        "leaf": leaf,
    }

    apply_change(jsonnode, change)
    return jsonnode