Skip to main content
Version: 9.2.11

dependency_parse

Analyze the grammatical structure and dependencies between words.

!!! note "Requires Deep Learning" This function requires the deep learning dependencies:

pip install "underthesea[deep]"

Usage

from underthesea import dependency_parse

text = "Tối 29/11, Việt Nam thêm 2 ca mắc Covid-19"
result = dependency_parse(text)
print(result)
# [('Tối', 5, 'obl:tmod'),
# ('29/11', 1, 'flat:date'),
# (',', 1, 'punct'),
# ('Việt Nam', 5, 'nsubj'),
# ('thêm', 0, 'root'),
# ('2', 7, 'nummod'),
# ('ca', 5, 'obj'),
# ('mắc', 7, 'nmod'),
# ('Covid-19', 8, 'nummod')]

Function Signature

def dependency_parse(sentence: str) -> list[tuple[str, int, str]]

Parameters

ParameterTypeDescription
sentencestrThe input text to parse

Returns

TypeDescription
list[tuple[str, int, str]]List of (word, head_index, relation) tuples

Each tuple contains:

  • word: The word token
  • head_index: Index of the head word (0 = root)
  • relation: The dependency relation type

Dependency Relations

RelationDescription
rootRoot of the sentence
nsubjNominal subject
objObject
oblOblique nominal
obl:tmodTemporal modifier
amodAdjectival modifier
nmodNominal modifier
nummodNumeric modifier
punctPunctuation
flat:dateFlat date expression
compoundCompound word

Examples

Basic Usage

from underthesea import dependency_parse

text = "Tối 29/11, Việt Nam thêm 2 ca mắc Covid-19"
result = dependency_parse(text)

for i, (word, head, rel) in enumerate(result, 1):
print(f"{i}\t{word}\t{head}\t{rel}")
# 1 Tối 5 obl:tmod
# 2 29/11 1 flat:date
# 3 , 1 punct
# 4 Việt Nam 5 nsubj
# 5 thêm 0 root
# 6 2 7 nummod
# 7 ca 5 obj
# 8 mắc 7 nmod
# 9 Covid-19 8 nummod

Finding the Root

text = "Tôi yêu Việt Nam"
result = dependency_parse(text)

root = [(i, word) for i, (word, head, rel) in enumerate(result, 1) if rel == 'root']
print(f"Root: {root}")
# Root: [(2, 'yêu')]

Finding Subjects and Objects

text = "Sinh viên đọc sách ở thư viện"
result = dependency_parse(text)

subjects = [word for word, head, rel in result if rel == 'nsubj']
objects = [word for word, head, rel in result if rel == 'obj']

print(f"Subjects: {subjects}")
print(f"Objects: {objects}")

Notes

  • This function uses a transformer-based model
  • First call may take longer due to model loading
  • Requires significant memory for the deep learning model