Practical Artificial Intelligence Programming with Java Fifth Edition (July 2020)
Total Page:16
File Type:pdf, Size:1020Kb
Practical Artificial Intelligence Programming With Java Fifth Edition (July 2020) Mark Watson This book is for sale at http://leanpub.com/javaai This version was published on 2020-07-28 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 many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2013 - 2020 Mark Watson Contents Preface ...................................................... 1 Personal Artificial Intelligence Journey ............................... 2 Maven Setup for Combining Examples in this Book ........................ 2 Software Licenses for Example Programs in this Book ...................... 3 Acknowledgements ........................................... 3 Search ...................................................... 5 Representation of Search State Space and Search Operators ................... 6 Finding Paths in Mazes ......................................... 7 Finding Paths in Graphs ......................................... 14 Adding Heuristics to Breadth-first Search .............................. 21 Heuristic Search and Game Playing: Tic-Tac-Toe and Chess ................... 21 Reasoning .................................................... 44 Logic .................................................... 45 PowerLoom Overview .......................................... 47 Running PowerLoom Interactively .................................. 48 Using the PowerLoom APIs in Java Programs ........................... 51 Suggestions for Further Study ..................................... 55 Anomaly Detection Machine Learning Example ........................... 56 Motivation for Anomaly Detection .................................. 56 Math Primer for Anomaly Detection ................................. 57 AnomalyDetection Utility Class .................................... 57 Example Using the University of Wisconsin Cancer Data .................... 63 Genetic Algorithms ............................................. 69 Theory ................................................... 70 Java Library for Genetic Algorithms ................................. 71 Finding the Maximum Value of a Function ............................. 76 Neural Networks ............................................... 81 Road Map for the Neural Network Example Code ......................... 83 Backpropagation Neural Networks .................................. 85 A Java Class Library for Back Propagation ............................. 88 CONTENTS Adding Momentum to Speed Up Back-Prop Training ....................... 96 Wrap-up for Neural Networks ..................................... 97 Deep Learning Using Deeplearning4j .................................. 98 Feed Forward Classification Networks ................................ 99 Feed Forward Example ......................................... 99 Configuring the Example Using Maven ...............................104 Documentation for Other Types of Deep Learning Layers . 104 Running the DL4J Example Programs and Modifying Them For Your Use . 106 Modifying the Character Generating LSTM Example to Model and Generate CSV Spreadsheet Data ........................................108 Roadmap for the DL4J Model Zoo ...................................110 Deep Learning Wrapup .........................................111 Natural Language Processing ....................................... 113 Overview of the NLP Library and Running the Examples . 113 Tokenizing, Stemming, and Part of Speech Tagging Text . 115 Named Entity Extraction From Text .................................118 Automatically Assigning Categories to Text ............................121 Text Clustering ..............................................124 Wrapup ...................................................127 Natural Language Processing Using OpenNLP ............................ 128 Using OpenNLP Pre-Trained Models .................................130 Training a New Categorization Model for OpenNLP . 134 Using Our New Trained Classification Model ............................136 Using the OpenNLP Parsing Model ..................................141 Combining the WordNet Linguistic Database With OpenNLP .................. 145 Using the WordNet Linguistic Database ...............................145 Installing the Libraries and Linguistic Data for this Example . 148 Implementation ..............................................151 Other Type Relationships Supported by WordNet . 154 Wrap-up and Ideas for Using WordNet ................................154 Information Gathering ........................................... 156 Web Scraping Examples .........................................157 ........................................................160 DBPedia Entity Lookup .........................................163 Client for GeoNames Service ......................................166 Wrap-up for Information Gathering .................................171 Resolve Entity Names to DBPedia References ............................. 172 DBPedia Entities .............................................172 CONTENTS Wrap-up for Resolving Entity Names to DBPedia References . 180 Semantic Web ................................................. 181 Available Tools ..............................................182 Relational Database Model Has Problems Dealing with Rapidly Changing Data Require- ments ...............................................183 RDF: The Universal Data Format ...................................183 Extending RDF with RDF Schema ...................................187 The SPARQL Query Language .....................................189 Using Jena .................................................197 OWL: The Web Ontology Language .................................206 Semantic Web Wrap-up .........................................209 Automatically Generating Data for Knowledge Graphs ...................... 210 Implementation Notes ..........................................210 Generating RDF Data ..........................................212 KGCreator Wrap Up ...........................................216 Knowledge Graph Navigator ....................................... 217 Entity Types Handled by KGN .....................................218 General Design of KGN with Example Output . 218 UML Class Diagram for Example Application . 222 Implementation ..............................................223 Wrap-up ..................................................238 Conclusions .................................................. 239 Preface The latest edition of this book is always available at https://leanpub.com/javaai¹. Currently the latest edition was released in the summer of 2020. It had been seven years since the previous edition and this is largely a rewrite, dropping some material like Drools based expert systems, Weka for machine learning, and the implementation of an RDF server with geolocation support. I am now placing a heavier emphasis on neural networks and deep learning, a greatly expanded discussion of the semantic web and linked data including examples to generate knowledge graphs automatically from text documents and also a system to help navigate public Knowledge Graphs like DBPedia and WikiData. The code and PDF for the 4th edition from 2013 can be found here². I decided which material to keep from old editions and which new material to add based on what my estimation is of which AI technologies are most useful and interesting to Java developers. I have been developing commercial Artificial Intelligence (AI) tools and applications since the 1980s. Mark Watson I wrote this book for both professional programmers and home hobbyists who already know how to program in Java and who want to learn practical AI programming and information processing techniques. I have tried to make this an enjoyable book to work through. In the style of a “cook book,” the chapters can be studied in any order. When an example depends on a library developed in a previous chapter this is stated clearly. Most chapters follow the same pattern: a motivation for learning a technique, some theory for the technique, and a Java example program that you can experiment with. ¹https://leanpub.com/javaai ²https://github.com/mark-watson/Java-AI-Book-Code_4th_edition Preface 2 The code for the example programs is available on github: https://github.com/mark-watson/Java-AI-Book-Code³ My Java code in this book can be used under either or both the LGPL3 and Apache 2 licenses - choose whichever of these two licenses that works best for you. Git pull requests with code improvements will be appreciated by me and the readers of this book. My goal is to introduce you to common AI techniques and to provide you with Java source code to save you some time and effort. Even though I have worked almost exclusively in the field of deep learning in the last six years, I urge you, dear reader, to look at the field of AI as being far broader than machine learning and deep learning in particular. Just as it is wrong to consider the higher level fields of Category Theory or Group Theory to “be” mathematics, there is far more to AI than machine learning. Here we will take a more balanced view of AI, and indeed, my own current research is in hybrid AI, that is, the fusion of deep learning with good old fashioned symbolic AI, probabilistic reasoning, and explainability. This book is released with a Attribution-NonCommercial-NoDerivatives