Syntax Tree – Natural Language Processing

Natural Language Processing (NLP) is a field of study that deals with understanding, interpreting, and manipulating human spoken languages using computers.
Since most of the significant information is written down in natural languages such as English, French, German, etc. thus, NLP helps computers communicate with humans in their own languages and perform other language-related tasks.
In conclusion, NLP makes it possible for computers to read the text, hear speech, interpret and realize it, understand the sentiment, and identify important parts of a text or speech.

What is Syntax?

A natural language typically follows a hierarchical structure, and contains the following components:

  • Sentences
  • Clauses
  • Phrases
  • Words

Syntax refers to the set of rules, principles, processes that govern the structure of sentences in a natural language. One basic description of syntax is how different words such as Subject, Verbs, Nouns, Noun Phrases, etc. are sequenced in a sentence.

Some of the syntactic categories of a natural language are as follows:



  • Sentence(S)
  • Noun Phrase(NP)
  • Determiner(Det)
  • Verb Phrase(VP)
  • Prepositional Phrase(PP)
  • Verb(V)
  • Noun(N)

Syntax Tree:
A Syntax tree or a parse tree is a tree representation of different syntactic categories of a sentence. It helps us to understand the syntactical structure of a sentence.

Example:

The syntax tree for the sentence given below is as follows:
I drive a car to my college.


Code: Syntax Tree in Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import required libraries
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
from nltk import pos_tag, word_tokenize, RegexpParser
   
# Example text
sample_text = "The quick brown fox jumps over the lazy dog"
   
# Find all parts of speech in above sentence
tagged = pos_tag(word_tokenize(sample_text))
   
#Extract all parts of speech from any text
chunker = RegexpParser("""
                       NP: {<DT>?<JJ>*<NN>}    #To extract Noun Phrases
                       P: {<IN>}               #To extract Prepositions
                       V: {<V.*>}              #To extract Verbs
                       PP: {<P> <NP>}          #To extract Prepostional Phrases
                       VP: {<V> <NP|PP>*}      #To extarct Verb Phrases
                       """)
  
# Print all parts of speech in above sentence
output = chunker.parse(tagged)
print("After Extracting\n", output)

chevron_right


Output:


Code: To draw the syntax free for the above sentence

filter_none

edit
close

play_arrow

link
brightness_4
code

# To draw the parse tree
output.draw()

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.