{"id":604,"date":"2021-04-15T19:40:37","date_gmt":"2021-04-15T23:40:37","guid":{"rendered":"https:\/\/www.macloo.com\/ai\/?p=604"},"modified":"2021-04-15T19:40:37","modified_gmt":"2021-04-15T23:40:37","slug":"loving-the-spacy-tutorial-for-nlp","status":"publish","type":"post","link":"https:\/\/www.macloo.com\/ai\/2021\/04\/15\/loving-the-spacy-tutorial-for-nlp\/","title":{"rendered":"Loving the spaCy tutorial for NLP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I got my first look at <a rel=\"noreferrer noopener\" href=\"https:\/\/spacy.io\/\" target=\"_blank\">spaCy<\/a>, a Python library for natural language processing, near the end of 2019. I wanted to learn it but had too many other things to do. Fast-forward to now, almost 14 months into the pandemic, and I recently stumbled across <a rel=\"noreferrer noopener\" href=\"https:\/\/course.spacy.io\/\" target=\"_blank\">spaCy&#8217;s own tutorial<\/a> for learning to use the library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The interactive tutorial includes videos, slides, and code exercises, and there is <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/ines\/spacy-course\" target=\"_blank\">a GitHub repo<\/a>. It is available in English, Deutsch, Espa\u00f1ol, Fran\u00e7ais, Portugu\u00eas, \u65e5\u672c\u8a9e, and \u4e2d\u6587. Today I completed chapter 2. If you already know Python at, say, <em>an intermediate level,<\/em> check it out!<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"417\" src=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities.png\" alt=\"Screenshot from Jupyter Notebook showing named entities\" class=\"wp-image-611\" srcset=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities.png 1024w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities-300x122.png 300w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities-768x313.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>Trying out spaCy&#8217;s displaCy module and named entities.<\/em> <\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In chapter 1 (there are four chapters), I got a handle on part-of-speech tags, syntactic dependencies, and named entities. I learned that we can search on these, and also on words (<strong>tokens<\/strong>) related to <em>combinations<\/em> that we define. I&#8217;ve known about large-scale document searches (where a huge collection of documents is searched programmatically, usually to extract the most meaningful docs for some purpose \u2014 like a journalism investigation), and now I was getting a much better idea of how such searches can be designed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SpaCy provides &#8220;pre-trained <strong>model<\/strong> packages,&#8221; meaning someone else has already done the hard work of machine learning\/training to generate <strong>word vectors.<\/strong> There are <a rel=\"noreferrer noopener\" href=\"https:\/\/spacy.io\/usage\/models\" target=\"_blank\">packages of various sizes and in various languages<\/a>. Loading a model provides various features (the bigger the model, the more features). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I think I was hooked as soon as I saw this and realized you could ask for all the MONEY entities, or all the ORG entities, in a document and evaluate them:<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"402\" src=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities_2.png\" alt=\"\" class=\"wp-image-617\" srcset=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities_2.png 1024w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities_2-300x118.png 300w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_named_entities_2-768x302.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>An example from chapter 1 in the spaCy tutorial.<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Then (still in chapter 1) I learned that I can easily define <em>my own entities<\/em> if the model doesn&#8217;t recognize the ones I need to find. I learned that if I don&#8217;t know what GPE is, I can enter <code>spacy.explain(\"GPE\")<\/code> and spaCy will return <code>'Countries, cities, states'<\/code> \u2014 sweet! <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then I learned about rule-based <strong>matching,<\/strong> and I thought: &#8220;Regular expressions, buh-bye!&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Chapter 1 didn&#8217;t really get deeply into <strong>lemmatization,<\/strong> but it offered this: <\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"749\" src=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_lemma.png\" alt=\"\" class=\"wp-image-621\" srcset=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_lemma.png 900w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_lemma-300x250.png 300w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_lemma-768x639.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><figcaption><em>Lemmatization groups all forms of a word together so they can be analyzed as one item.<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">That was just chapter 1! Chapter 2 went further into creating your own named entities and using parts of speech as part of your search criteria. For example, if you want to find all instances where a particular entity (say, a city) is followed by a verb \u2014 any verb \u2014 you can do that. Or any part of speech. You can construct a complex pattern, mixing specific words, parts of speech, and selected types of entities. The pattern can include as many tokens as you want. (If you&#8217;re familiar with <strong>regex<\/strong> \u2014 all the regex things are available.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can determine whether phrases or sentences are <strong>similar<\/strong> to each other (although imperfectly).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;m not entirely sure how I would use these, but I&#8217;m sure they&#8217;re good for something:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>.root<\/code> \u2014 the token that decides the category of the phrase<\/li><li><code>.head<\/code> \u2014 the syntactic &#8220;parent&#8221; that governs the phrase<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">There <em>is<\/em> an exercise in which I matched country names and their <strong>root head token<\/strong> (<code>span.root.head<\/code>), which gave me a bit of a clue as to how useful that might be in some circumstances.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"783\" src=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_root_head_token.png\" alt=\"\" class=\"wp-image-622\" srcset=\"https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_root_head_token.png 1024w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_root_head_token-300x229.png 300w, https:\/\/www.macloo.com\/ai\/wp-content\/uploads\/2021\/04\/spacy_root_head_token-768x587.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>Example of use of the root head token on a 700-word text.<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Also in chapter 2, I learned how to use an <strong>imported JSON file<\/strong> to add 240 country names as GPE entities \u2014 obviously, the imported terms could be any kind of entity. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, I&#8217;m feeling very excited about spaCy! Halfway through the tutorial!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/4.0\/\"><img decoding=\"async\" alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https:\/\/i.creativecommons.org\/l\/by-nc-nd\/4.0\/88x31.png\"><\/a><br>\n<small><span xmlns:dct=\"http:\/\/purl.org\/dc\/terms\/\" property=\"dct:title\"><strong>AI in Media and Society<\/strong><\/span> by <span xmlns:cc=\"http:\/\/creativecommons.org\/ns#\" property=\"cc:attributionName\">Mindy McAdams<\/span> is licensed under a <a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/4.0\/\">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License<\/a>.<br>\nInclude the author&#8217;s name (Mindy McAdams) and a link to the original post in any reuse of this content.<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got my first look at spaCy, a Python library for natural language processing, near the end of 2019. I wanted to learn it but had too many other things to do. Fast-forward to now, almost 14 months into the pandemic, and I recently stumbled across spaCy&#8217;s own tutorial for learning to use the library.&hellip; <a class=\"more-link\" href=\"https:\/\/www.macloo.com\/ai\/2021\/04\/15\/loving-the-spacy-tutorial-for-nlp\/\">Continue reading <span class=\"screen-reader-text\">Loving the spaCy tutorial for NLP<\/span> <span class=\"meta-nav\" aria-hidden=\"true\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[97,10,118,117],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-nlp","tag-language","tag-python","tag-text-analysis","tag-tutorial"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/comments?post=604"}],"version-history":[{"count":10,"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/posts\/604\/revisions\/624"}],"wp:attachment":[{"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/media?parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/categories?post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.macloo.com\/ai\/wp-json\/wp\/v2\/tags?post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}