{"id":23818,"date":"2021-11-29T10:09:03","date_gmt":"2021-11-29T10:09:03","guid":{"rendered":"https:\/\/www.askpython.com\/?p=23818"},"modified":"2021-11-29T10:09:05","modified_gmt":"2021-11-29T10:09:05","slug":"dependency-parsing-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/dependency-parsing-in-python","title":{"rendered":"Introduction to Dependency Parsing in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hey folks! Today in this tutorial, we will be understanding what Dependency Parsing is and how to implement the same using the Python programming language.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is Dependency Parsing?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The technique of assessing the grammatical structure of a sentence based on the dependencies between the words in a sentence is known as <strong><em>Dependency Parsing.<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Various tags in dependency parsing describe the relationship between two words in a sentence. These are the <strong><em>dependencies tags<\/em><\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"230\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-illustration.jpg\" alt=\"Dependency Parsing Illustration\" class=\"wp-image-23825\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-illustration.jpg 574w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-illustration-300x120.jpg 300w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><figcaption>Dependency Parsing Illustration<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Why is Dependency Parsing needed?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency parsing allows us to construct a parsing tree using tags to determine the relationship between words in a sentence rather than using any Grammar rule as in syntactic parsing, which provides a lot of flexibility even when the order of words changes.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing Dependency Parsing in Python <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For implementing Dependency Parsing, we would make use of the <code>spaCy<\/code> module in Python. So, first of all, we import the same into our program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport spacy\nfrom spacy import displacy\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s take some random sentences on which we want to perform Dependency Parsing.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ntext1 = &#039;It took me more than two hours to translate a few pages of English.&#039;\ntext2 = &#039;The old rusted farm equipment surrounded the house predicting its demise.&#039;\ntext3 = &#039;I am a great listener, really good with empathy vs sympathy and all that, but I hate people.&#039;\ntext4 = &#039;The ants enjoyed the barbecue more than the family.&#039;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Next, we use <code>spacy.load<\/code> function which will help to construct a language object and load in the model data and weights, and return it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnlp=spacy.load(&#039;en_core_web_sm&#039;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Finally, we will display the dependency parsing output for the texts mentioned in the previous code snippet.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ndisplacy.render(nlp(text1),jupyter=True)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"246\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-1-1024x246.jpg\" alt=\"Dependency Parsing Output 1\" class=\"wp-image-23834\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-1-1024x246.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-1-300x72.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-1-768x185.jpg 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-1.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dependency Parsing Output 1<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"237\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-2-1024x237.jpg\" alt=\"Dependency Parsing Output 2\" class=\"wp-image-23835\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-2-1024x237.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-2-300x69.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-2-768x178.jpg 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-2.jpg 1155w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dependency Parsing Output 2<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"221\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-3-1024x221.jpg\" alt=\"Dependency Parsing Output 3\" class=\"wp-image-23836\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-3-1024x221.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-3-300x65.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-3-768x166.jpg 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-3.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dependency Parsing Output 3<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"242\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-4-1024x242.jpg\" alt=\"Dependency Parsing Output 4\" class=\"wp-image-23839\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-4-1024x242.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-4-300x71.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-4-768x181.jpg 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Dependency-Parsing-Output-4.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dependency Parsing Output 4<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-d144faf1-1abb-488c-a4bd-8a80520fbf7b\">Congratulations! You just learned how to build a Dependency Parsing chart for sentences using NLP. Hope you enjoyed it! &#x1f607;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-4e73ecff-f7fa-46a5-be6e-35bbb0867ba8\">Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\" id=\"block-39ec4c2c-ab54-45a6-804e-58eb24573b45\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/networkx-package\"><\/a><a href=\"https:\/\/www.askpython.com\/python\/examples\/stemming-and-lemmatization\">Stemming and Lemmatization in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/bag-of-words-model-from-scratch\">Creating Bag of Words Model from Scratch in python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/word-cloud-using-python\">Word Cloud using Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/sentiment-analysis-using-python\">Sentiment Analysis using Python<\/a><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-177e4b87-db13-427b-90fa-d70aac750e1d\">Thank you for taking your time out! Hope you learned something new!! &#x1f604;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hey folks! Today in this tutorial, we will be understanding what Dependency Parsing is and how to implement the same using the Python programming language. What is Dependency Parsing? The technique of assessing the grammatical structure of a sentence based on the dependencies between the words in a sentence is known as Dependency Parsing. Various [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":23850,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-23818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=23818"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23818\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/23850"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=23818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=23818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=23818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}