Share E-Book

Make Python Talk Build Apps with Voice Control and Speech Recognition (Mark Liu)(Z-Library)

Author

Rating No ratings yet

Log in to rate

Python
Language English

A project-based book that teaches beginning Python programmers how to build working, useful, and fun voice-controlled applications. This fun, hands-on book will take your basic Python skills to the next level as you build voice-controlled apps to use in your daily life. Starting with a Python refresher and an introduction to speech-recognition/text-to-speech functionalities, you’ll soon ease into more advanced topics, like making your own modules and building working voice-controlled apps. Each chapter scaffolds multiple projects that allow you to see real results from your code at a manageable pace, while end-of-chapter exercises strengthen your understanding of new concepts. You’ll design interactive games, like Connect Four and Tic-Tac-Toe, and create intelligent computer opponents that talk and take commands; you’ll make a real-time language translator, and create voice-activated financial-market apps that track the stocks or cryptocurrencies you are interested in. Finally, you’ll load all of these features into the ultimate virtual personal assistant – a conversational VPA that tells jokes, reads the news, and gives you hands-free control of your email, browser, music player, desktop files, and more. Along the way, you’ll learn how to: • Build Python modules, implement animations, and integrate live data into an app • Use web-scraping skills for voice-controlling podcasts, videos, and web searches • Fine-tune the speech recognition to accept a variety of input • Associate regular tasks like opening files and accessing the web with speech commands • Integrate functionality from other programs into a single VPA with computational knowledge engines to answer almost any question Packed with cross-platform code examples to download, practice activities and exercises, and explainer images, you’ll quickly become proficient in Python coding in general and speech recognition/text to speech in particular.

Format PDF
Size 7.1 MB
4
Views
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
M A R K L I U M A K E P Y T H O N TA L K B U I L D A P P S W I T H V O I C E C O N T R O L A N D S P E E C H R E C O G N I T I O N
Page 2
(This page has no text content)
Page 3
San Francisco M A K E P Y T H O N TA L K B u i l d A p p s w i t h Vo i c e C o n t r o l a n d S p e e c h R e c o g n i t i o n by Mark L iu
Page 4
MAKE PYTHON TALK. Copyright © 2021 by Mark Liu.   All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13: 978-1-7185-0156-0 (print) ISBN-13: 978-1-7185-0157-7 (ebook) Publisher: William Pollock Production Manager: Rachel Monaghan Production Editor: Paula Williamson Developmental Editor: Liz Chadwick Cover Illustrator: Gina Redman Interior Design: Octopod Studios Technical Reviewer: Noah Spahn Copyeditor: Sharon Wilkey Compositor: Jeff Lytle, Happenstance Type-O-Rama Proofreader: Paula Fleming Indexer: JoAnne Burek For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; info@nostarch.com www.nostarch.com Library of Congress Control Number: 2021938060 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
Page 5
Dedicated to Ivey, Andrew, and all MS Finance students (past, present, and future) at the University of Kentucky.
Page 6
(This page has no text content)
Page 7
About the Author Dr. Mark H. Liu is the founding director of the Master of Science in Finance program at the University of Kentucky, where he holds the title of Associate Professor of Finance with tenure. He has a Ph.D. in finance from Boston College, and has been published in top finance journals, including Journal of Financial Economics, Journal of Financial and Quantitative Analysis, and Journal of Corporate Finance. Dr. Liu has more than 20 years of coding experience in C++, SAS, Stata, and Python and runs Python workshops for Finance master students at the University of Kentucky. He has also incorporated Python in finance courses he is teaching. About the Technical Reviewer Noah Spahn is presently enjoying the role of Software Engineer for the Computer Security Group at UCSB (known as SecLab) and their world- famous hacking team: Shellphish. Prior to his current academic role, he ran his own consulting business, allowing him to work on a wide variety of projects, with a broad spectrum and languages, and collaborate with vary- ing teams. Fluency in Python has opened many doors for Spahn, providing the opportunity to contribute to the areas of natural sciences (ocean- ography, ecohydrology, seismology), computer science (network traffic analysis, machine learning, binary analysis), open-source projects (NASA, Kubernetes, and others), as well as many practical applications. Spahn holds a Master of Software Engineering degree from California State University, Fullerton. He has taught courses in Python at the University of California and Santa Barbara’s interdisciplinary Collaboratory, and has taught an upper-division course on the concepts of programming lan- guages at Westmont College. Spahn is glad to teach anyone who is interested in learning.
Page 8
(This page has no text content)
Page 9
B R I E F C O N T E N T S Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix PART I: GETTING STARTED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1: Setting Up Python, Anaconda, and Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Python Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 PART II: LEARNING TO TALK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Chapter 3: Speech Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Chapter 4: Make Python Talk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Chapter 5: Speaking Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Chapter 6: Web Scraping Podcasts, Radios, and Videos . . . . . . . . . . . . . . . . . . . . . . . . 111 Chapter 7: Building a Virtual Personal Assistant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Chapter 8: Know-It-All VPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 PART III: INTERACTIVE GAMES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 Chapter 9: Graphics and Animation with the turtle Module . . . . . . . . . . . . . . . . . . . . . . . 169 Chapter 10: Tic-Tac-Toe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Chapter 11: Connect Four . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Chapter 12: Guess-the-Word Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Chapter 13: Smart Games: Adding Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 PART IV: GOING FURTHER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269 Chapter 14: Financial Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Chapter 15: Stock Market Watch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Chapter 16: Use World Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Page 10
viii   Brief Contents Chapter 17: Ultimate Virtual Personal Assistant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Appendix A: Install Modules to Play Audio Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Appendix B: Suggested Answers to End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . 353 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Page 11
C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS XVII INTRODUCTION XIX PART I: GETTING STARTED 1 1 SETTING UP PYTHON, ANACONDA, AND SPYDER 3 Introducing Anaconda and Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Installing Anaconda and Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Install Anaconda and Spyder in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Install Anaconda and Spyder in macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Install Anaconda and Spyder in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Using Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Write Python in Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Inspect Code in Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Understanding Coding in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Python Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Basic Operations in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2 PYTHON REFRESHER 13 Variables and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Bools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Convert Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Rules for Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Loops and Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Loops in Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Loop Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 String Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 String Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Create a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Access Elements in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Use a List of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Page 12
x   Contents in Detail Add or Multiply Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 List Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Use Built-in Functions with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 list() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Access Values in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Use Dictionary Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 How to Use Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Switch Keys and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Combine Two Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Use Built-in Python Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Define Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Import Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Create Your Own Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Use Third-Party Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Create a Virtual Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Activate the Virtual Environment in Windows . . . . . . . . . . . . . . . . . . . . . . . . 46 Set Up Spyder in the Virtual Environment in Windows . . . . . . . . . . . . . . . . . . 47 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 PART II: LEARNING TO TALK 53 3 SPEECH RECOGNITION 55 Install the SpeechRecognition Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 In Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 In Mac or Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Test and Fine-Tune SpeechRecognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Import SpeechRecognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Test SpeechRecognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Fine-Tune the Speech Recognition Feature . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Perform a Voice-Controlled Web Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Use the webbrowser Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Add Voice Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Perform a Google Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Open Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Use the os and pathlib Modules to Access and Open Files . . . . . . . . . . . . . . . 65 Open Files via Voice Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Create and Import a Local Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Create the Local Module mysr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Import mysr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Page 13
Contents in Detail   xi 4 MAKE PYTHON TALK 73 Install the Text-to-Speech Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Test Your Text-to-Speech Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Repeat After Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Customize the Speech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Retrieve Default Settings in the pyttsx3 Module in Windows . . . . . . . . . . . . . . 81 Adjust Speech Properties in the pyttsx3 Module in Windows . . . . . . . . . . . . . 82 Customize the gTTS Module in Mac or Linux . . . . . . . . . . . . . . . . . . . . . . . . 82 Build the Local mysay Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Create mysay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Import mysay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Build a Voice-Controlled Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Read a File Aloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 5 SPEAKING APPLICATIONS 89 Create Your Self-Made Local Python Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 What’s a Python Package? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Create Your Own Python Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Test Your Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 More on Python Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Interactive Guess the Number Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Speaking Newscast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Scrape the News Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Add the Text-to-Speech Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Voice-Controlled Wikipedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Access Wikipedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Add Speech Recognition and Text to Speech . . . . . . . . . . . . . . . . . . . . . . . 103 Voice-Activated Music Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Traverse Files in a Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Python, Play Selena Gomez . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Python, Play a Country Song . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 6 WEB SCRAPING PODCASTS, RADIOS, AND VIDEOS 111 A Primer on Web Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 What Is HTML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Extract Information with Beautiful Soup . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Scrape Live Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Voice-Activated Podcasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Extract and Play Podcasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Voice-Activate Podcasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Page 14
xii   Contents in Detail Voice-Activated Radio Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Install the selenium Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Control Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Voice-Activate Live Radio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Voice-Activated Videos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 7 BUILDING A VIRTUAL PERSONAL ASSISTANT 133 An Overview of Your VPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Download VPA Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Install the arrow Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Manage the Standby Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Create the Local Module mywakeup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Set Some Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Ask Your VPA to Set a Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Tell the Time with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Build a Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Create the mytimer Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Set the Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Ask Your VPA to Set an Alarm Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Build an Alarm Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Create the Alarm Clock Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Set an Alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Ask Your VPA to Tell a Joke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Create Your Joke List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Create a Joke Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Tell a Joke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Send Hands-Free Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Send Email with Written Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Create the Email Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Add the Email Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 8 KNOW-IT-ALL VPA 155 Get Answers from WolframAlpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Apply for an API Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Retrieve Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Explore Different Areas of Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Add a Know-It-All Functionality to Your VPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 What WolframAlpha Cannot Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Create the myknowall Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 A VPA That Can Answer (Almost) Any Question for You . . . . . . . . . . . . . . . . 165 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Page 15
Contents in Detail   xiii PART III: INTERACTIVE GAMES 167 9 GRAPHICS AND ANIMATION WITH THE TURTLE MODULE 169 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Create a turtle Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Create Movements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Basic Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Use the dot() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Draw Your Own Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Draw Grid Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 How Animation Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Use Multiple Turtles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 10 TIC-TAC-TOE 189 Game Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Draw the Game Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Create the Game Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 How Mouse Clicks Work in turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Convert Mouse Clicks to Cell Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Place Game Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Determine Valid Moves, Wins, and Ties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Voice-Controlled Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 11 CONNECT FOUR 207 Game Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Draw the Game Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 The Mouse-Click Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Drop a Disc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Animate the Falling Discs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Determine Valid Moves, Wins, and Ties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 The Voice-Controlled Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 12 GUESS-THE-WORD GAME 227 Game Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Draw the Game Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 The Text Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Load the Coins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Guess the Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Determine Valid Guesses, Wins, and Losses . . . . . . . . . . . . . . . . . . . . . . . . 234
Page 16
xiv   Contents in Detail The Voice-Controlled Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 13 SMART GAMES: ADDING INTELLIGENCE 241 The Think-Three-Steps-Ahead Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Think One Step Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Think Two Steps Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Implement the Think-Two-Steps-Ahead Strategy . . . . . . . . . . . . . . . . . . . . . . 248 Think Three Steps Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 The Machine-Learning Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Create a Dataset of Simulated Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Apply the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Test the Effectiveness of the Two Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 The Think-Three-Steps-Ahead Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 The Machine-Learning Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Why Doesn’t the Machine-Learning Strategy Work Well in Connect Four? . . . 260 Voice-Controlled Intelligent Connect Four Games . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 A Voice-Controlled Game That Thinks Ahead . . . . . . . . . . . . . . . . . . . . . . . 264 A Voice-Controlled Game Using Machine Learning . . . . . . . . . . . . . . . . . . . 266 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 PART IV: GOING FURTHER 269 14 FINANCIAL APPLICATIONS 271 Python, What’s the Facebook Stock Price? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Obtain the Latest Stock Price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Find Ticker Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Retrieve Stock Prices via Voice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Voice-Controlled Data Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Create Stock Price Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Create Candlestick Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Add Voice Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Voice-Controlled Stock Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Analyze Recent Stock Performance and Risk . . . . . . . . . . . . . . . . . . . . . . . . 289 Add Voice Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 15 STOCK MARKET WATCH 295 Bitcoin Watch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 How to Read JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Page 17
Contents in Detail   xv A Quick Introduction to the tkinter Package . . . . . . . . . . . . . . . . . . . . . . . . 298 A Graphical Bitcoin Watch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 A Talking Bitcoin Watch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 A Talking Stock Market Watch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Apply the Method to Other Financial Markets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 End-of-Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 16 USE WORLD LANGUAGES 309 Text to Speech in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Install Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Convert Text to Speech in Spanish . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Support Text to Speech in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . 311 Convert Text to Speech in World Languages . . . . . . . . . . . . . . . . . . . . . . . . 312 Speech Recognition in Major World Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 A Talking Wikipedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Create Your Own Voice Translator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 A Text-Based Translator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 A Voice-Based Translator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 17 ULTIMATE VIRTUAL PERSONAL ASSISTANT 321 An Overview of the Final VPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 The Chatting Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 The Music Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Create a Music Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Activate the Music Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 The News Brief Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Create a News Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Activate the News Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 The Live Radio Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Create a Radio Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Activate the Radio Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 The Tic-Tac-Toe Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 Create a Tic-Tac-Toe Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 Activate Tic-Tac-Toe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 The Connect Four Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 Create a Connect Four Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Activate Connect Four . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 The Stock Price Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 Create a Stock Market–Tracking Module . . . . . . . . . . . . . . . . . . . . . . . . . . 340 Activate the Stock Market–Tracking Functionalities . . . . . . . . . . . . . . . . . . . . 341 The Voice Translator Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Create a Translator Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Activate the Voice Translator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Page 18
xvi   Contents in Detail APPENDIX A: INSTALL MODULES TO PLAY AUDIO FILES 347 Install the playsound Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Mac 348 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Install the pydub Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Install the pygame Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Mac 349 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Install the vlc Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Sample Scripts to Test the Four Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 The playsound Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 The pydub Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 The pygame Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 The vlc Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 APPENDIX B: SUGGESTED ANSWERS TO END-OF-CHAPTER EXERCISES 353 INDEX 369
Page 19
A C K N O W L E D G M E N T S Many people have helped to make this book a reality. A portion of this book was developed while I was preparing Python workshops for MS Finance students at the University of Kentucky in the past few years. Several Finance master and Ph.D. students helped tremendously in the process: Joe Farizo, James Keyser, Blake Best, and especially my teaching assistants at the time Mike Farrell and Patrick Mullins. I’d like to thank all MS Finance students for keeping me motivated to learn new Python skills in order to show them how useful and interesting coding is. I’d also like to thank Bill Pollock and Barbara Yien at No Starch Press for guiding me through the editorial process. Special thanks to my devel- opmental editor Liz Chadwick, whose pursuit for perfection has greatly improved the book. She helped me find the delicate balance between over- explaining and not enough detail. Many thanks to the technical reviewer of the book Noah Spahn. He doesn’t simply check if the code works correctly, he also makes sure the scripts are as efficient as they can be. A case in point is that he pushed me to find a way to make the self-made modules in different chapters
Page 20
xviii   Acknowledgments consistent. This prompted me to use the self-made Python package in the book. He went way beyond just checking the scripts, and frequently edited the writing to help explain technical details in the book. Last but not least, I’d like to thank my wife Ivey Zhang and my son Andrew Liu for being so supportive in this journey. In the past couple of years, I probably have spent more time with the book than with them over the weekends and holidays. I am indebted to them the most.
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List