<jupyter_start><jupyter_text>How to transform depedency tree nodes as input sequences<jupyter_code>import sys
sys.path.append('..')
from pad_sequences import pad_sequences_adjacency

import torch
print(torch.__version__)

import spacy
%%capture
!python -m spacy download de_core_news_sm<jupyter_output><empty_output><jupyter_text>Simple exampleAssume an adjacency matrix is defined by index pairs `idx_pairs` and the matrix dimension `n_seqlen`<jupyter_code>idx_pairs = [(0, 1), (2, 1), (3, 4), (5, 4), (7, 4), (11, 4), (6, 5), (10, 7), (8, 10), (9, 10)]
n_seqlen = 12
# torch
mat = torch.sparse.FloatTensor(
    torch.LongTensor(idx_pairs).t(),
    torch.FloatTensor([999 for _ in range(len(idx_pairs))]),
    torch.Size([n_seqlen, n_seqlen])
)
mat.to_dense()<jupyter_output><empty_output><jupyter_text>Dependency Relations as Input Sequence<jupyter_code>data = [
    "Die Freisetzung eines Arzneistoffs aus der Arzneiform steht fast vollständig unter pharmazeutisch-technologischer Kontrolle (ist also abhängig von der Galenik und der Formulierung der Arzneiform).",
    "Somit ist die Liberation der Arzneiform nur bedingt dem Bereich der Pharmakokinetik zuzuordnen.",
    "Alle weiteren Vorgänge sind physiologisch gesteuert und in weiten Grenzen variabel.",
    "Freisetzung ist die Grundvoraussetzung für die Wirkung eines Arzneimittels.",
]
import de_core_news_sm
nlp = de_core_news_sm.load()
nlp.disable_pipes(["ner", "tagger"])

# process texts
%time docs = [nlp(s) for s in data]
def get_children_indicies(doc: spacy.tokens.doc.Doc):
    idxpairs = []
    for t in doc:
        idxpairs.extend([(c.i, t.i) for c in t.children])
    return idxpairs

deps_child_idx = [get_children_indicies(doc) for doc in docs]
deps_len = [len(doc) for doc in docs]
deps_child_idx[3]
maxlen = 12
padded = pad_sequences_adjacency(deps_child_idx, deps_len, 
                                 maxlen=maxlen, padding='pre', truncating='pre')
padded[3]
idx_pairs = padded[3]
n_seqlen = deps_len[3]

mat = torch.sparse.FloatTensor(
    torch.LongTensor(idx_pairs).t(),
    torch.FloatTensor([999 for _ in range(len(idx_pairs))]),
    torch.Size([maxlen, maxlen])
)
mat.to_dense()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1143507972665148, 'fraction_numerical': 0.017312072892938495, 'mean_word_length': 5.134078212290503, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '114442', 'license': 'Apache-2.0', 'n_tokens_mistral': 839, 'n_tokens_neox': 812, 'n_words': 202, 'path': 'examples/Pad Dependency Relations as Adjacency Matrices.ipynb', 'repo_name': 'ulf1/pad-sequences', 'size': '2195'}