hydra.utils.hybrid_tree

Hybrid syntactic tree utilities.

class hydra.utils.hybrid_tree.TreeNode(nid: int, category: str | None, terminal: tuple[str, str | None, str | None] | None, is_head: bool, from_index: int | None, to_index: int | None, head_index: int | None = None, attributes: dict[str, Any] | None = None, children: list[TreeNode] | None = None)[source]

Bases: object

A hybrid synactic tree node.

add_child(child: TreeNode) → None[source]

Append as the given node as rightmost child.

get_root() → TreeNode[source]

Return the root of the tree containing this node.

Returns:

the root.

get_yield() → list[TreeNode][source]

Return the list of terminal nodes as read from left to right.

Returns:

the terminals.

is_continuous() → bool[source]

Return True iff all constituents are continuous.

Returns:

a boolean value.

pretty_print(tab: str = '', features: list[str] | None = None) → None[source]

Print the tree’s representation to the standard output.

to_brackets(nonterminal_features: list[str] | None = None, mark_head: bool = True, morph_tags: bool = False, dummy_pre: bool = False) → str[source]

Create an EVALB-style bracketed representation.

Terminals’ order will get scrambled in case of discontinuous trees.

Parameters:
  • nonterminal_features – nonterminal features to include.

  • mark_head – mark head nonterminals with asterisks (‘*’).

  • morph_tags – include inflectional tags.

  • dummy_pre – make all terminals single children by adding dummy preterminals where needed.

Returns:

the bracketed representation.

hydra.utils.hybrid_tree.correct_lemma(token: str, lemma: str, tag: str, morfeusz: Morfeusz) → tuple[str, str][source]

Use Morfeusz to try correcting the model-predicted lemma and inflectional tag.

Returns:

possibly corrected lemma and tag.

hydra.utils.hybrid_tree.drop_rozpodabniacz(lemma: str, tag: str) → str[source]

Remove Morfeusz lemma identifier (‘rozpodabniacz’) if present.

Returns:

the stripped lemma.

hydra.utils.hybrid_tree.get_graph_edges(matrix: NestedIntLabelList, deprel_matrix: NestedStrLabelList) → tuple[NestedIntLabelList, NestedStrLabelList][source]

Read edges from graph adjacency and label matrices.

Returns:

lists of heads and labels for each token.

hydra.utils.hybrid_tree.get_heads(matrix: list[list[float]], mst: str = 'mst') → IntLabelList[source]

Decode a dependency adjacency matrix into a list of token head indices.

Parameters:
  • matrix – edge weights matrix.

  • mst – dependency tree decoding algorithm to use: MST or EAGER.

Returns:

the head indices.

hydra.utils.hybrid_tree.get_tree_dict_yield(tree: dict[str, Any]) → list[dict[str, Any]][source]

Return the list of terminal nodes of a tree’s dictionary representation, as read from left to right.

Returns:

dictionary representation of the terminals.

hydra.utils.hybrid_tree.make_lemma(token: str, case: str, rule: str, legacy_casing: bool = False) → str[source]

Generate a lemma from text form by applying model-predicted rules.

Returns:

the lemma.

hydra.utils.hybrid_tree.make_tree(tokens: TokenList, labels: LabelDict, nonterminal_features: list[str], root_label: str | None = None, force_root_label: bool = False, decompress: bool = True) → tuple[TreeNode, list[str], IntLabelList][source]

Build a hybrid syntactic tree from model-predicted labels.

Parameters:
  • tokens – the sentence being processed.

  • labels – model-prdicted token labels.

  • nonterminal_features – nonterminal features to include.

  • root_label – used by force_root_label.

  • force_root_label – enforce a specific root label.

  • decompress – expand same-category node chains where required by attachment predictions.

Returns:

the constructed tree, info about used heuristics if any, dependency head indices may have been changed wrt. model-predicted labels by heuristics).

hydra.utils.hybrid_tree.tree2dict(tree: TreeNode, metadata: dict[str, Any] | None = None, replace_quotes: bool = False) → dict[str, Any][source]

Create a JSON-serialisable dictionary representation of a tree object .

Parameters:
  • tree – a hybrid syntactic tree.

  • metadata – any metadata to include in the tree dictionary.

  • replace_quotes – replace ‘”’ quotation characters in terminals (text forms and lemmata) with ‘”’.

Returns:

a dictionary representation of the tree.

hydra.utils.hybrid_tree.tree_from_dataset_instance(instance: DatasetInstance, dataset_features: Features) → TreeNode | None[source]

Build a hybrid syntactic tree from a dataset instance encoding it.

Returns:

the constructed tree.