Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Artificial Vision and Language Processing for Robotics

You're reading from   Artificial Vision and Language Processing for Robotics Create end-to-end systems that can power robots with artificial vision and deep learning techniques

Arrow left icon
Product type Paperback
Published in Apr 2019
Publisher Packt
ISBN-13 9781838552268
Length 356 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Gonzalo Molina Gallego Gonzalo Molina Gallego
Author Profile Icon Gonzalo Molina Gallego
Gonzalo Molina Gallego
Unai Garay Maestre Unai Garay Maestre
Author Profile Icon Unai Garay Maestre
Unai Garay Maestre
Álvaro Morena Alberola Álvaro Morena Alberola
Author Profile Icon Álvaro Morena Alberola
Álvaro Morena Alberola
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Artificial Vision and Language Processing for Robotics
Preface
1. Fundamentals of Robotics FREE CHAPTER 2. Introduction to Computer Vision 3. Fundamentals of Natural Language Processing 4. Neural Networks with NLP 5. Convolutional Neural Networks for Computer Vision 6. Robot Operating System (ROS) 7. Build a Text-Based Dialogue System (Chatbot) 8. Object Recognition to Guide a Robot Using CNNs 9. Computer Vision for Robotics Appendix

Chapter 3: Fundamentals of Natural Language Processing


Activity 3: Process a Corpus

Solution

  1. Import the sklearn TfidfVectorizer and TruncatedSVD methods:

    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.decomposition import TruncatedSVD
  2. Load the corpus:

    docs = []
    ndocs = ["doc1", "doc2", "doc3"]
    for n in ndocs:
        aux = open("dataset/"+ n +".txt", "r", encoding="utf8")
        docs.append(aux.read())
  3. With spaCy, let's add some new stop words, tokenize the corpus, and remove the stop words. The new corpus without these words will be stored in a new variable:

    import spacy
    import en_core_web_sm
    from spacy.lang.en.stop_words import STOP_WORDS
    nlp = en_core_web_sm.load()
    nlp.vocab["\n\n"].is_stop = True
    nlp.vocab["\n"].is_stop = True
    nlp.vocab["the"].is_stop = True
    nlp.vocab["The"].is_stop = True
    newD = []
    for d, i in zip(docs, range(len(docs))):
        doc = nlp(d)
        tokens = [token.text for token in doc if not token.is_stop and not token.is_punct]
        newD.append(' '.join(tokens))
  4. Create the TF-IDF matrix. I'm going to add some parameters to improve the results:

    vectorizer = TfidfVectorizer(use_idf=True, 
                                ngram_range=(1,2), 
                                smooth_idf=True,
                                max_df=0.5)
    X = vectorizer.fit_transform(newD)
  5. Perform the LSA algorithm:

    lsa = TruncatedSVD(n_components=100,algorithm='randomized',n_iter=10,random_state=0)
    lsa.fit_transform(X)
  6. With pandas, we are shown a sorted DataFrame with the weights of the terms of each concept and the name of each feature:

    import pandas as pd
    import numpy as np
    dic1 = {"Terms": terms, "Components": lsa.components_[0]}
    dic2 = {"Terms": terms, "Components": lsa.components_[1]}
    dic3 = {"Terms": terms, "Components": lsa.components_[2]}
    f1 = pd.DataFrame(dic1)
    f2 = pd.DataFrame(dic2)
    f3 = pd.DataFrame(dic3)
    f1.sort_values(by=['Components'], ascending=False)
    f2.sort_values(by=['Components'], ascending=False)
    f3.sort_values(by=['Components'], ascending=False)

    The output is as follows:

    Figure 3.26: Output example of the most relevant words in a concept (f1)

    Note

    Do not worry if the keywords are not the same as yours, if the keywords represent a concept, it is a valid result.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image