250x Filetype PDF File size 3.06 MB Source: markwatson.com
ALispProgrammerLivinginPython-Land:
TheHyProgrammingLanguage
MarkWatson
This book is for sale at http://leanpub.com/hy-lisp-python
This version was published on 2020-10-03
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
manyiterations to get reader feedback, pivot until you have the right book and build traction once
youdo.
©2019-2020MarkWatson
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Setting Up Your Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
WhatisLisp Programming Style? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
HyisPython, But With a Lisp Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
HowThis Book Reflects My Views on Artificial Intelligence and the Future of Society
and Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
About the Book Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
ARequestfromtheAuthor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Introduction to the Hy Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
WeWillOftenUsetheContributedlet Macro in Book Example Code . . . . . . . . . . . . 5
Using Python Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Global vs. Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Using Python Code in Hy Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Using Hy Libraries in Python Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Replacing the Python slice (cut) Notation with the Hy Functional Form . . . . . . . . . . . 10
Iterating Through a List With Index of Each Element . . . . . . . . . . . . . . . . . . . . . . . 11
Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Importing Libraries from Different Directories on Your Laptop . . . . . . . . . . . . . . . . . 13
Using Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
HyLooksLikeClojure: How Similar Are They? . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Plotting Data Using the Numpy and the Matplotlib Libraries . . . . . . . . . . . . . . . . . . 15
Bonus Points: Configuration for macOS and ITerm2 for Generating Plots Inline in a Hy
REPLandShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
WhyLisp? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
I HatedtheWaterfallMethodinthe1970sbutLearnedtoLoveaBottom-UpProgramming
Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
First Introduction to Lisp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Commercial Product Development and Deployment Using Lisp . . . . . . . . . . . . . . . . 21
HyMacrosLetYouExtendtheHyLanguageinYourPrograms. . . . . . . . . . . . . . . . . 21
Performing Bottom Up Development Inside a REPL is a Lifestyle Choice . . . . . . . . . . . 22
CONTENTS
Writing WebApplications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Getting Started With Flask: Using Python Decorators in Hy . . . . . . . . . . . . . . . . . . . 24
Using Jinja2 Templates To Generate HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Handling HTTP Sessions and Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Deploying Hy Language Flask Apps to Google Cloud Platform AppEngine . . . . . . . . . 30
Deploying Hy Language Flask Apps to the Heroku Platform . . . . . . . . . . . . . . . . . . 32
Wrap-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Responsible Web Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Using the Python BeautifulSoup Library in the Hy Language . . . . . . . . . . . . . . . . . . 35
Getting HTML Links from the DemocracyNow.org News Web Site . . . . . . . . . . . . . . 37
Getting Summaries of Front Page from the NPR.org News Web Site . . . . . . . . . . . . . . 39
Using the Microsoft Bing Search APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Getting an Access Key for Microsoft Bing Search APIs . . . . . . . . . . . . . . . . . . . . . . 41
Example Search Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Wrap-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
DeepLearning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Simple Multi-layer Perceptron Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . 46
DeepLearning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Using Keras and TensorFlow to Model The Wisconsin Cancer Data Set . . . . . . . . . . . . 49
Using a LSTM Recurrent Neural Network to Generate English Text Similar to the
Philosopher Nietzsche’s writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Natural Language Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Exploring the spaCy Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Implementing a HyNLP Wrapper for the Python spaCy Library . . . . . . . . . . . . . . . . 68
Coreference (Anaphora Resolution) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Wrap-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Datastores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Sqlite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
RDFDataUsingthe“rdflib” Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Wrap-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Linked Data and the Semantic Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Understanding the Resource Description Framework (RDF) . . . . . . . . . . . . . . . . . . . 89
Resource Namespaces Provided in rdflib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Understanding the SPARQL Query Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Wrapping the Python rdflib Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
KnowledgeGraphCreator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
RecommendedIndustrial Use of Knowledge Graphs . . . . . . . . . . . . . . . . . . . . . . . 95
no reviews yet
Please Login to review.