Saturday, 15 February 2025

Visualize sentence dependency structure in Python

Python  Text Analysis 

To visualize sentence dependency structure in Python, you can use libraries like spaCy or Stanza. These libraries provide tools for natural language processing (NLP), including dependency parsing. Below is an example using spaCy.

Step 1: Install spaCy

First, install spaCy and download a language model (e.g., en_core_web_sm for English). If you use Spyder (the open-source cross-platform integrated development environment (IDE) for Python), use the commands below in the IPython Console.

pip install spacy

from spacy.cli import download
download("en_core_web_sm")

Step 2: Visualize Dependency Structure

Here’s how you can parse a sentence and visualize its dependency structure:

import spacy
from spacy import displacy

# Load the spaCy model
nlp = spacy.load("en_core_web_sm")

# Input sentence
sentence = "The quick brown fox jumps over the lazy dog."

# Parse the sentence
doc = nlp(sentence)

# Visualize the dependency structure
displacy.serve(doc, style="dep", port=3000)

Explanation:

  1. nlp(sentence): Processes the sentence and creates a Doc object, which contains tokens and their linguistic annotations.
  2. displacy.serve(doc, style="dep", port=3000): Opens a web browser to display the dependency structure visually. The default url is http://localhost:3000

Example Output:

The dependency structure will look something like this:

Sentence dependency structure


Dependency labels

Dependency labels (also known as dependency relations or arc labels) describe the grammatical relationship between a word (the dependent) and its head in a sentence. These labels are language-specific and depend on the annotation scheme used (e.g., Universal Dependencies).

Below is a list of common Universal Dependency (UD) labels used in dependency parsing. These labels are widely used in libraries like spaCy and Stanza.

Universal Dependency Labels (Common Examples)

Core Arguments

  • nsubj: Nominal subject (e.g., "She" in "She runs.")
  • nsubj:pass: Passive nominal subject (e.g., "The book" in "The book was read.")
  • obj: Direct object (e.g., "the ball" in "She kicked the ball.")
  • iobj: Indirect object (e.g., "him" in "She gave him a book.")

Modifiers

  • amod: Adjectival modifier (e.g., "quick" in "the quick fox.")
  • advmod: Adverbial modifier (e.g., "quickly" in "She ran quickly.")
  • nummod: Numeric modifier (e.g., "three" in "three books.")
  • det: Determiner (e.g., "the" in "the cat.")
  • poss: Possessive modifier (e.g., "John's" in "John's book.")

Clausal Relations

  • ccomp: Clausal complement (e.g., "that she won" in "He said that she won.")
  • xcomp: Open clausal complement (e.g., "to leave" in "She wants to leave.")
  • acl: Clausal modifier of a noun (e.g., "who sings" in "the woman who sings.")
  • advcl: Adverbial clause modifier (e.g., "if it rains" in "We will stay if it rains.")

Prepositional and Case Marking

  • case: Case marker (e.g., "of" in "the top of the mountain.")
  • mark: Subordinating conjunction (e.g., "that" in "She said that she was tired.")

Coordination

  • conj: Conjunct (e.g., "apples" and "oranges" in "apples and oranges.")
  • cc: Coordinating conjunction (e.g., "and" in "apples and oranges.")

Miscellaneous

  • root: Root of the sentence (e.g., the main verb in "She runs.")
  • punct: Punctuation (e.g., periods, commas, etc.)
  • appos: Appositional modifier (e.g., "my friend" in "John, my friend, is here.")
  • compound: Compound (e.g., "coffee" in "coffee cup.")
  • fixed: Fixed multiword expression (e.g., "in spite of" in "in spite of the rain.")

Prepositional Relations

  • prep: Prepositional modifier (e.g., "in" in "She is in the house.")
  • pobj: Object of a preposition (e.g., "the house" in "She is in the house.")

Auxiliary and Verb Relations

  • aux: Auxiliary verb (e.g., "is" in "She is running.")
  • aux:pass: Passive auxiliary (e.g., "was" in "The book was read.")
  • cop: Copula (e.g., "is" in "She is happy.")

Nominal Relations

  • nmod: Nominal modifier (e.g., "the roof" in "the roof of the house.")
  • appos: Appositional modifier (e.g., "my friend" in "John, my friend, is here.")

Example with spaCy

Here’s how you can extract dependency labels using spaCy:

import spacy

# Load the spaCy model
nlp = spacy.load("en_core_web_sm")

# Input sentence
sentence = "The quick brown fox jumps over the lazy dog."

# Parse the sentence
doc = nlp(sentence)

# Print tokens and their dependency labels
for token in doc:
    print(f"Token: {token.text}, Dependency Label: {token.dep_}, Head: {token.head.text}")

Output:

Token: The, Dependency Label: det, Head: fox
Token: quick, Dependency Label: amod, Head: fox
Token: brown, Dependency Label: amod, Head: fox
Token: fox, Dependency Label: nsubj, Head: jumps
Token: jumps, Dependency Label: ROOT, Head: jumps
Token: over, Dependency Label: prep, Head: jumps
Token: the, Dependency Label: det, Head: dog
Token: lazy, Dependency Label: amod, Head: dog
Token: dog, Dependency Label: pobj, Head: over
Token: ., Dependency Label: punct, Head: jumps

Notes:

  • The full list of Universal Dependency labels can be found in the Universal Dependencies documentation.
  • Different languages may have additional or slightly different labels.
  • Libraries like spaCy and Stanza map their dependency labels to the Universal Dependencies scheme.


Search