Exploring Syntactic Dependencies and Token Shapes in NLP

Introduction to Syntactic Dependencies

Hello, welcome to the next step in your linguistic journey! In today's lesson, we'll expand upon your foundational linguistic knowledge and step into the world of syntactic dependencies and token shapes. This knowledge will equip you to delve even deeper into the fascinating realm of Natural Language Processing (NLP).

The first stop in our journey is syntactic dependencies. So, what are they? Simply put, syntactic dependencies are the grammatical relationships between words in a sentence. This could be a subject-verb relationship, an adjective-noun relationship, or other types of grammatical relations. Why are they important in NLP? They help us understand how words relate to each other and how they come together to convey meaning in a sentence.

In Python, with the help of SpaCy, we can extract these dependencies easily. Let's take a look at how to do this with our sample text from the Reuters corpus.

import spacy
from nltk.corpus import reuters

# Let's load the English NLP model
nlp = spacy.load('en_core_web_sm')

# Take a sample text from reuters corpus
sample_text = reuters.raw(reuters.fileids()[0])

# Pass the text to the nlp object
doc = nlp(sample_text)

# For syntactic tokens
print('\nSyntactic Dependencies:\n')
for token in doc:
    print(f"{token.text:<10s} {token.dep_:<10s} {token.head.text:<10s}")

In each line of output, the first word is the token, the second word is the type of syntactic dependency, and the third word is the head of the token. The head of a token is typically the word that governs the relationship between the words. This simple code gives us a depth of insight into the grammatical structure of the text!

Unpacking Syntactic Dependencies Output

Alright, let's take a concrete look at the potential output our syntactic dependencies code could produce.

ASIAN      compound   EXPORTERS 
EXPORTERS  nsubj      FEAR      
FEAR       ccomp      said      
DAMAGE     nsubj      raised    
FROM       prep       DAMAGE    
U.S.-JAPAN compound   friction  

Even at first glance, we can already start to see patterns and relationships emerge from this data. However, to truly gain insights, we must understand what these outcome values mean:

  • ASIAN: Here, "ASIAN" has a compound dependency type. A compound relationship is formed when two nouns come together to form a new noun, such as "ASIAN EXPORTERS".
  • EXPORTERS: The nominal subject (nsubj) of the verb "FEAR" is "EXPORTERS". The nominal subject is typically the "doer" of the action and corresponds to "who" or "what" in the sentence.
  • FEAR: The ccomp in this case stands for clausal complement, referring to "FEAR". These complements are subclauses that provide additional information but usually can't make sense as separate sentences.
  • DAMAGE: It is considered to be the nominal subject (nsubj) for the verb "raised".
  • FROM: Labeled with a prep, which stands for preposition, "FROM" provides a relationship between "U.S.-JAPAN" and another word in the sentence.

Besides these, there are different types of dependencies that you might encounter as well:

  • relcl: It stands for relative clause modifier. They use words like "who" or "which" to provide more detail about the noun.
  • dobj: Denotes direct object. This may be the noun or noun phrase that is receiving the action in the sentence.
  • ROOT: This is the main verb in any given sentence, to which all other words are connected in a manner that is either direct or indirect.
  • nsubjpass: This refers to the nominal subject in a passive sentence. In such sentences, the subject is usually receiving the action of the verb.
  • pobj: Stands for object of a preposition. This is usually the noun coming after the preposition in the sentence.

Finally, remember that understanding these dependencies is vital if you want to dive deeply into the grammatical structure and meaning of a sentence. Now that we've dissected syntactic dependencies output, let's move on to our next interesting segment - the exploration of token shapes.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal