class DDParserTask(task, model, tree=True, prob=False, use_pos=False, use_cuda=False, batch_size=1, return_visual=False, **kwargs)[source]#

Bases: Task

DDParser task to analyze the dependency relationship between words in a sentence :param task: The name of task. :type task: string :param model: The model name in the task. :type model: string :param tree: Ensure the output conforms to the tree structure. :type tree: bool :param prob: Whether to return the probability of predicted heads. :type prob: bool :param use_pos: Whether to return the postag. :type use_pos: bool :param batch_size: Numbers of examples a batch. :type batch_size: int :param return_visual: If True, the result will contain the dependency visualization. :type return_visual: bool :param kwargs: Additional keyword arguments passed along to the specific task. :type kwargs: dict, optional

pad_sequence(sequences, padding_value=0, fix_len=None)[source]#

Fill sequences(np.ndarray) into a fixed-length matrix.

decode(arc_preds, rel_preds, s_arc, mask, tree)[source]#
eisner(scores, mask)[source]#

Eisner algorithm is a general dynamic programming decoding algorithm for bilexical grammar.


scores: Adjacency matrix,shape=(batch, seq_len, seq_len) mask: mask matrix,shape=(batch, sql_len)


output,shape=(batch, seq_len),the index of the parent node corresponding to the token in the query

fill_diagonal(x, value, offset=0, dim1=0, dim2=1)[source]#

Fill value into the diagoanl of x that offset is ${offset} and the coordinate system is (dim1, dim2).

backtrack(p_i, p_c, heads, i, j, complete)[source]#

Backtrack the position matrix of eisner to generate the tree

stripe(x, n, w, offset=(0, 0), dim=1)[source]#

Returns a diagonal stripe of the tensor.

  • x (Tensor) – the input tensor with 2 or more dims.

  • n (int) – the length of the stripe.

  • w (int) – the width of the stripe.

  • offset (tuple) – the offset of the first two dims.

  • dim (int) – 0 if returns a horizontal stripe; 1 else.

Example: >>> x = np.arange(25).reshape(5, 5) >>> x tensor([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])

>>> stripe(x, 2, 3, (1, 1))
tensor([[ 6,  7,  8],
        [12, 13, 14]])
>>> stripe(x, 2, 3, dim=0)
tensor([[ 0,  5, 10],
        [ 6, 11, 16]])
class Node(id=None, parent=None)[source]#

Bases: object

Node class

class DepTree(sentence)[source]#

Bases: object

DepTree class, used to check whether the prediction result is a project Tree. A projective tree means that you can project the tree without crossing arcs.


Build the tree

add(parent, child)[source]#

Add a child node

Determine whether it is a project tree


Inorder traversal


Is the sequence a project tree