Python Tutorial: Statistical Models

Learn how spaCy's statistical models extract part-of-speech tags, dependencies, and named entities from text. The tutorial loads a pre-trained model, demonstrates tagging the sentence "She ate the pizza," and shows how to identify entities like Apple as ORG or $1 billion as MONEY using spacy.explain.
Chapters
Let's add some more power to the NLP object. In this video you'll learn about spacy. Statistical Models Some of the most interesting things you can analyze are context specific, for example whether a word is a verb or whether a span of text is a person name. Statistical models enable spacy to make predictions in context. This usually includes part of speech tags, syntactic dependencies, and named entities. Models are trained on large datasets of labeled example texts. They can be updated with more examples to fine tune their predictions. For example, to perform better on your specific data, Spacy provides a number of pre trained model packages you can download. For example, the Encore Web SM package is a small English model that supports all core capabilities and is trained on webtext. The spacy load method loads a model package by name and returns an NLP object. The package provides the binary weights that enable spacy to make predictions. It also includes the vocabulary and meta information to tell Spacy which language class to use and how to configure the processing pipeline. Let's take a look at the model's predictions. In this example, we're using spacy to predict part of speech tags the word types in context. First we load the small English model and receive an NLP object. Next, we're processing the text she ate the pizza. For each token in the doc, we can print the text and the POS attribute. The predicted part of speech tag. In spacy attributes that return, strings usually end with an underscore. Attributes without the underscore return an id. Here the model correctly predicted aid as a verb and pizza as a noun. In addition to the part of speech tags, we can also predict how the words are related, for example whether a word is the subject of the sentence or an object. The depattribute returns the predicted dependency label. The head attribute returns the syntactic head token. You can also think of it as the parent token. This word is attached to to describe syntactic dependencies. Spacy uses a standardized label scheme. Here's an example of some common labels. The pronoun she is a nominal subject attached to the verb. In this case to ate. The noun pizza is a direct object attached to the verb ate. It is eaten by the subject she, the determiner. The or also known as an article is attached to the noun pizza. Named entities are real world objects that are assigned a name, for example A person, an organization, or a country. The doc ents property lets you access the named entities predicted by the model. It returns an iterator of span objects so we can print the entity text and the entity label using the labelattribute. In this case, the model is correctly predicting Apple as an organization, UK as a geopolitical entity, and $1 billion as money. A quick to get definitions for the most common tags and labels, you can use the Spacy Explain helper function. For example, GPE for geopolitical entity isn't exactly intuitive, but Spacy Explain can tell you that it refers to countries, cities, and states. The same works for part of speech tags and dependency labels. Now it's your turn. Let's take a look at Spacy's statistical model.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







