Book Image

Mastering spaCy

By : Duygu Altınok
Book Image

Mastering spaCy

By: Duygu Altınok

Overview of this book

spaCy is an industrial-grade, efficient NLP Python library. It offers various pre-trained models and ready-to-use features. Mastering spaCy provides you with end-to-end coverage of spaCy's features and real-world applications. You'll begin by installing spaCy and downloading models, before progressing to spaCy's features and prototyping real-world NLP apps. Next, you'll get familiar with visualizing with spaCy's popular visualizer displaCy. The book also equips you with practical illustrations for pattern matching and helps you advance into the world of semantics with word vectors. Statistical information extraction methods are also explained in detail. Later, you'll cover an interactive business case study that shows you how to combine all spaCy features for creating a real-world NLP pipeline. You'll implement ML models such as sentiment analysis, intent recognition, and context resolution. The book further focuses on classification with popular frameworks such as TensorFlow's Keras API together with spaCy. You'll cover popular topics, including intent classification and sentiment analysis, and use them on popular datasets and interpret the classification results. By the end of this book, you'll be able to confidently use spaCy, including its linguistic features, word vectors, and classifiers, to create your own NLP apps.
Table of Contents (15 chapters)
1
Section 1: Getting Started with spaCy
4
Section 2: spaCy Features
9
Section 3: Machine Learning with spaCy

Putting it all together

We already extracted the entities and recognized the intent in several ways. We're now ready to put it all together to calculate a semantic representation for a user utterance!

  1. We'll process the example dataset utterance:
    show me flights from denver to philadelphia on tuesday

    We'll hold a dictionary object to hold the result. The result will include the entities and the intent.

  2. Let's extract the entities:
    import spacy
    from spacy.matcher import Matcher  
    nlp = spacy.load("en_core_web_md")
    matcher = Matcher(nlp.vocab)
    pattern = [{"POS": "ADP"}, {"ENT_TYPE": "GPE"}]
    matcher.add("prepositionLocation", [pattern])
    # Location entities
    doc = nlp("show me flights from denver to philadelphia on tuesday")
    matches = matcher(doc)
    for mid, start, end in matches:
        print(doc[start:end])
    ... 
    from denver
    to philadelphia
    # All entities:
    ents = doc...