100% found this document useful (6 votes)
25 views

Immediate download Advanced Data Analytics Using Python: With Architectural Patterns, Text and Image Classification, and Optimization Techniques [2nd Edition] Sayan Mukhopadhyay ebooks 2024

Analytics

Uploaded by

huzaimrehaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (6 votes)
25 views

Immediate download Advanced Data Analytics Using Python: With Architectural Patterns, Text and Image Classification, and Optimization Techniques [2nd Edition] Sayan Mukhopadhyay ebooks 2024

Analytics

Uploaded by

huzaimrehaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Download Full Version ebook - Visit ebookmeta.

com

Advanced Data Analytics Using Python: With


Architectural Patterns, Text and Image
Classification, and Optimization Techniques [2nd
Edition] Sayan Mukhopadhyay
https://ebookmeta.com/product/advanced-data-analytics-using-
python-with-architectural-patterns-text-and-image-
classification-and-optimization-techniques-2nd-edition-
sayan-mukhopadhyay/

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmeta.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

Advanced Data Analytics Using Python: With Architectural


Patterns, Text and Image Classification, and Optimization
Techniques [2nd Edition] Sayan Mukhopadhyay
https://ebookmeta.com/product/advanced-data-analytics-using-python-
with-architectural-patterns-text-and-image-classification-and-
optimization-techniques-2nd-edition-sayan-mukhopadhyay/
ebookmeta.com

Advanced Analytics with Transact-SQL: Exploring Hidden


Patterns and Rules in Your Data 1st Edition Dejan Sarka

https://ebookmeta.com/product/advanced-analytics-with-transact-sql-
exploring-hidden-patterns-and-rules-in-your-data-1st-edition-dejan-
sarka/
ebookmeta.com

Advanced Analytics With Transact-SQL: Exploring Hidden


Patterns and Rules in Your Data 1st Edition Dejan Sarka

https://ebookmeta.com/product/advanced-analytics-with-transact-sql-
exploring-hidden-patterns-and-rules-in-your-data-1st-edition-dejan-
sarka-2/
ebookmeta.com

Time Travel For Love And Profit: A Time Travel Harem


Adventure 1st Edition Simon Archer

https://ebookmeta.com/product/time-travel-for-love-and-profit-a-time-
travel-harem-adventure-1st-edition-simon-archer/

ebookmeta.com
Goldie Lox And Her Trio Of Bears Goldie Lox 1 1st Edition
Amy Star

https://ebookmeta.com/product/goldie-lox-and-her-trio-of-bears-goldie-
lox-1-1st-edition-amy-star/

ebookmeta.com

Foundations of Python Network Programming 3rd Edition


Brandon Rhodes John Goerzen

https://ebookmeta.com/product/foundations-of-python-network-
programming-3rd-edition-brandon-rhodes-john-goerzen-2/

ebookmeta.com

Master Web Design with HTML CSS JavaScript and jQuery


Create Stunning Interactive Websites Frahaan Hussain

https://ebookmeta.com/product/master-web-design-with-html-css-
javascript-and-jquery-create-stunning-interactive-websites-frahaan-
hussain/
ebookmeta.com

Good Families of Barcelona A Social History of Power in


the Industrial Era Gary Wray Mcdonogh

https://ebookmeta.com/product/good-families-of-barcelona-a-social-
history-of-power-in-the-industrial-era-gary-wray-mcdonogh/

ebookmeta.com

Looking Within Finding an Environmental Justice and Global


Citizenship Lens 1st Edition Karen Druffel

https://ebookmeta.com/product/looking-within-finding-an-environmental-
justice-and-global-citizenship-lens-1st-edition-karen-druffel/

ebookmeta.com
How Small Social Systems Work From Soccer Teams to Jazz
Trios and Families Yair Neuman

https://ebookmeta.com/product/how-small-social-systems-work-from-
soccer-teams-to-jazz-trios-and-families-yair-neuman/

ebookmeta.com
Advanced Data
Analytics Using
Python
With Architectural Patterns,
Text and Image Classification,
and Optimization Techniques
Second Edition

Sayan Mukhopadhyay
Pratip Samanta
Advanced Data Analytics Using Python: With Architectural Patterns, Text
and Image Classification, and Optimization Techniques

Sayan Mukhopadhyay Pratip Samanta


Kolkata, West Bengal, India Kolkota, West Bengal, India

ISBN-13 (pbk): 978-1-4842-8004-1 ISBN-13 (electronic): 978-1-4842-8005-8


https://doi.org/10.1007/978-1-4842-8005-8
Copyright © 2023 by Sayan Mukhopadhyay, Pratip Samanta
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: James Markham
Coordinating Editor: Mark Powers
Copyeditor: Kim Wimpsett
Cover designed by eStudioCalamar
Cover image by David Clode on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY
10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected],
or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance
Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for
reprint, paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub (github.com/apress). For more detailed information, please visit
www.apress.com/source-­code.
Printed on acid-free paper
The reason for the success of this book is that it has original
research, so I dedicate it to the person from whom I learned
how to do research: Dr. Debnath Pal, IISc.
—Sayan Mukhopadhyay
Table of Contents
About the Authors��������������������������������������������������������������������������������xi

About the Technical Reviewer�����������������������������������������������������������xiii


Acknowledgments������������������������������������������������������������������������������xv
Introduction��������������������������������������������������������������������������������������xvii

Chapter 1: A Birds Eye View to AI System��������������������������������������������1


OOP in Python�������������������������������������������������������������������������������������������������������1
Calling Other Languages in Python���������������������������������������������������������������������13
Exposing the Python Model as a Microservice���������������������������������������������������14
High-Performance API and Concurrent Programming����������������������������������������17
Choosing the Right Database������������������������������������������������������������������������������21
Summary������������������������������������������������������������������������������������������������������������22

Chapter 2: ETL with Python����������������������������������������������������������������23


MySQL�����������������������������������������������������������������������������������������������������������������24
How to Install MySQLdb?�������������������������������������������������������������������������������24
Database Connection�������������������������������������������������������������������������������������25
INSERT Operation������������������������������������������������������������������������������������������26
READ Operation���������������������������������������������������������������������������������������������26
DELETE Operation������������������������������������������������������������������������������������������27
UPDATE Operation�����������������������������������������������������������������������������������������28
COMMIT Operation�����������������������������������������������������������������������������������������28
ROLL-BACK Operation�����������������������������������������������������������������������������������28

v
Table of Contents

Normal Forms�����������������������������������������������������������������������������������������������������31
First Normal Form�����������������������������������������������������������������������������������������31
Second Normal Form�������������������������������������������������������������������������������������32
Third Normal Form����������������������������������������������������������������������������������������33
Elasticsearch�������������������������������������������������������������������������������������������������������35
Connection Layer API�������������������������������������������������������������������������������������38
Neo4j Python Driver��������������������������������������������������������������������������������������������39
neo4j-rest-client�������������������������������������������������������������������������������������������������39
In-Memory Database������������������������������������������������������������������������������������������40
MongoDB (Python Edition)����������������������������������������������������������������������������������40
Import Data into the Collection����������������������������������������������������������������������41
Create a Connection Using pymongo�������������������������������������������������������������42
Access Database Objects������������������������������������������������������������������������������42
Insert Data�����������������������������������������������������������������������������������������������������43
Update Data���������������������������������������������������������������������������������������������������43
Remove Data�������������������������������������������������������������������������������������������������43
Cloud Databases�������������������������������������������������������������������������������������������������43
Pandas����������������������������������������������������������������������������������������������������������������44
ETL with Python (Unstructured Data)������������������������������������������������������������������45
Email Parsing�������������������������������������������������������������������������������������������������45
Topical Crawling��������������������������������������������������������������������������������������������48
Summary������������������������������������������������������������������������������������������������������������52

Chapter 3: Feature Engineering and Supervised Learning�����������������53


Dimensionality Reduction with Python���������������������������������������������������������������54
Correlation Analysis���������������������������������������������������������������������������������������55
Principal Component Analysis�����������������������������������������������������������������������57
Mutual Information����������������������������������������������������������������������������������������60
Classifications with Python���������������������������������������������������������������������������������64

vi
Table of Contents

Semi-Supervised Learning���������������������������������������������������������������������������������65
Decision Tree�������������������������������������������������������������������������������������������������������66
Which Attribute Comes First?������������������������������������������������������������������������66
Random Forest Classifier������������������������������������������������������������������������������68
Naïve Bayes Classifier�����������������������������������������������������������������������������������������68
Support Vector Machine��������������������������������������������������������������������������������������69
Nearest Neighbor Classifier��������������������������������������������������������������������������������71
Sentiment Analysis���������������������������������������������������������������������������������������������71
Image Recognition����������������������������������������������������������������������������������������������73
Regression with Python���������������������������������������������������������������������������������74
Least Square Estimation��������������������������������������������������������������������������������75
Logistic Regression���������������������������������������������������������������������������������������76
Classification and Regression�����������������������������������������������������������������������������76
Intentionally Bias the Model to Over-Fit or Under-Fit������������������������������������������77
Dealing with Categorical Data�����������������������������������������������������������������������������78
Summary������������������������������������������������������������������������������������������������������������79

Chapter 4: Unsupervised Learning: Clustering�����������������������������������81


K-Means Clustering��������������������������������������������������������������������������������������������82
Choosing K: The Elbow Method���������������������������������������������������������������������������86
Silhouette Analysis����������������������������������������������������������������������������������������������86
Distance or Similarity Measure���������������������������������������������������������������������������88
Properties������������������������������������������������������������������������������������������������������89
General and Euclidean Distance��������������������������������������������������������������������89
Squared Euclidean Distance��������������������������������������������������������������������������91
Distance Between String-Edit Distance��������������������������������������������������������91
Similarity in the Context of a Document�������������������������������������������������������������93
Types of Similarity�����������������������������������������������������������������������������������������94

vii
Table of Contents

Example of K-Means in Images��������������������������������������������������������������������������95


Preparing the Cluster�������������������������������������������������������������������������������������97
Thresholding�������������������������������������������������������������������������������������������������������99
Time to Cluster��������������������������������������������������������������������������������������������100
Revealing the Current Cluster����������������������������������������������������������������������102
Hierarchical Clustering��������������������������������������������������������������������������������������104
Bottom-Up Approach�����������������������������������������������������������������������������������104
Distance Between Clusters�������������������������������������������������������������������������105
Top-Down Approach������������������������������������������������������������������������������������107
Graph Theoretical Approach������������������������������������������������������������������������111
How Do You Know If the Clustering Result Is Good?�����������������������������������112
Summary����������������������������������������������������������������������������������������������������������113

Chapter 5: Deep Learning and Neural Networks�������������������������������115


Backpropagation�����������������������������������������������������������������������������������������������116
Backpropagation Approach�������������������������������������������������������������������������116
Other Algorithms�����������������������������������������������������������������������������������������������119
TensorFlow��������������������������������������������������������������������������������������������������������120
Network Architecture and Regularization Techniques���������������������������������124
Updatable Model and Transfer Learning������������������������������������������������������124
Recurrent Neural Network��������������������������������������������������������������������������������133
LSTM�����������������������������������������������������������������������������������������������������������134
Reinforcement Learning�����������������������������������������������������������������������������������138
TD0��������������������������������������������������������������������������������������������������������������139
TD�������������������������������������������������������������������������������������������������������������145
Example of Dialectic Learning���������������������������������������������������������������������146
Convolution Neural Networks���������������������������������������������������������������������������156
Summary����������������������������������������������������������������������������������������������������������159

viii
Table of Contents

Chapter 6: Time Series���������������������������������������������������������������������161


Classification of Variation����������������������������������������������������������������������������������161
Analyzing a Series Containing a Trend��������������������������������������������������������������161
Curve Fitting������������������������������������������������������������������������������������������������163
Removing Trends from a Time Series����������������������������������������������������������163
Analyzing a Series Containing Seasonality�������������������������������������������������������164
Removing Seasonality from a Time Series��������������������������������������������������������165
By Filtering��������������������������������������������������������������������������������������������������165
By Differencing��������������������������������������������������������������������������������������������166
Transformation��������������������������������������������������������������������������������������������������166
To Stabilize the Variance�����������������������������������������������������������������������������167
To Make the Seasonal Effect Additive���������������������������������������������������������167
To Make the Data Distribution Normal���������������������������������������������������������167
Stationary Time Series��������������������������������������������������������������������������������������168
Stationary Process��������������������������������������������������������������������������������������168
Autocorrelation and the Correlogram����������������������������������������������������������169
Estimating Autocovariance and Autocorrelation Functions�������������������������170
Time-Series Analysis with Python���������������������������������������������������������������������171
Useful Methods��������������������������������������������������������������������������������������������171
Autoregressive Processes���������������������������������������������������������������������������173
Estimating Parameters of an AR Process����������������������������������������������������175
Mixed ARMA Models�����������������������������������������������������������������������������������������177
Integrated ARMA Models�����������������������������������������������������������������������������������179
The Fourier Transform���������������������������������������������������������������������������������������181
An Exceptional Scenario�����������������������������������������������������������������������������������182
Missing Data�����������������������������������������������������������������������������������������������������183
Summary����������������������������������������������������������������������������������������������������������184

ix
Table of Contents

Chapter 7: Analytics at Scale�����������������������������������������������������������185


Hadoop��������������������������������������������������������������������������������������������������������������185
MapReduce Programming���������������������������������������������������������������������������185
Partitioning Function�����������������������������������������������������������������������������������186
Combiner Function��������������������������������������������������������������������������������������187
HDFS File System����������������������������������������������������������������������������������������197
MapReduce Design Pattern�������������������������������������������������������������������������197
A Notes on Functional Programming����������������������������������������������������������������203
Spark�����������������������������������������������������������������������������������������������������������������204
PySpark�������������������������������������������������������������������������������������������������������������207
Updatable Machine Learning and Spark Memory Model����������������������������������209
Analytics in the Cloud���������������������������������������������������������������������������������������221
Internet of Things����������������������������������������������������������������������������������������������231
Essential Architectural Patterns for Data Scientists������������������������������������231
Scenario 1: Hot Potato Anti-Pattern�������������������������������������������������������������232
Scenario 2: Proxy and Layering Patterns�����������������������������������������������������236
Thank You����������������������������������������������������������������������������������������������������������241

Index�������������������������������������������������������������������������������������������������243

x
About the Authors
Sayan Mukhopadhyay has more than 13 years
of industry experience and has been associated
with companies such as Credit Suisse, PayPal,
CA Technologies, CSC, and Mphasis. He has
a deep understanding of applications for
data analysis in domains such as investment
banking, online payments, online advertising,
IT infrastructure, and retail. His area of
expertise is in applying high-performance
computing in distributed and data-driven
environments such as real-time analysis, high-
frequency trading, and so on.
He earned his engineering degree in electronics and instrumentation
from Jadavpur University and his master’s degree in research in
computational and data science from IISc in Bangalore.

Pratip Samanta is a principal AI engineer/


researcher with more than 11 years of
experience. He has worked for several software
companies and research institutions. He has
published conference papers and has been
granted patents in AI and natural language
processing. He is also passionate about
gardening and teaching.

xi
About the Technical Reviewer
Joos Korstanje is a data scientist with more
than five years of industry experience in
developing machine learning tools, of which a
large part is forecasting models. He currently
works at Disneyland Paris where he develops
machine learning for a variety of tools.

xiii
Acknowledgments
Thanks to Labonic Chakraborty (Ripa) and Soumili Chakraborty.

xv
Introduction
We are living in the data science/artificial intelligence era. To thrive in
this environment, where data drives decision-making in everything from
business to government to sports and entertainment, you need the skills
to manage and analyze huge amounts of data. Together we can use this
data to make the world better for everyone. In fact, humans have yet to find
everything we can do using this data. So, let us explore!
Our objective for this book is to empower you to become a leader
in this data-transformed era. With this book you will learn the skills to
develop AI applications and make a difference in the world.
This book is intended for advanced user, because we have incorporated
some advanced analytics topics. Important machine learning models and
deep learning models are explained with coding exercises and real-world
examples.
All the source code used in this book is available for download at
https://github.com/apress/advanced-­data-­analytics-­python-­2e.
Happy reading!

xvii
CHAPTER 1

A Birds Eye View to


AI System
In this book, we assume that you are familiar with Python programming.
In this introductory chapter, we explain why a data scientist should choose
Python as a programming language. Then we highlight some situations
where Python may not be the ideal choice. Finally, we describe some best
practices for application development and give some coding examples that
a data scientist may need in their day-to-day job.

OOP in Python
In this section, we explain some features of object-oriented programming
(OOP) in a Python context.
The most basic element of any modern application is an object. To
a programmer or architect, the world is a collection of objects. Objects
consist of two types of members: attributes and methods. Members can be
private, public, or protected. Classes are data types of objects. Every object
is an instance of a class. A class can be inherited in child classes. Two
classes can be associated using composition.
Python has no keywords for public, private, or protected, so
encapsulation (hiding a member from the outside world) is not implicit in
Python. Like C++, it supports multilevel and multiple inheritance. Like Java,
it has an abstract keyword. Classes and methods both can be abstract.
© Sayan Mukhopadhyay, Pratip Samanta 2023 1
S. Mukhopadhyay and P. Samanta, Advanced Data Analytics Using Python,
https://doi.org/10.1007/978-1-4842-8005-8_1
Chapter 1 A Birds Eye View to AI System

In the following code, we are describing an object-oriented question-­


answering system without any machine learning. The program’s input is a
set of dialogs in input.txt, as shown here:

glob is I
prok is V
pish is X
tegj is L
glob glob Silver is 34 Credits
glob prok Gold is 57800 Credits
pish pish Iron is 3910 Credits
how much is pish tegj glob glob ?
how many Credits is glob prok Silver ?
how many Credits is glob prok Gold ?
how many Credits is glob prok Iron ?
how much wood could a woodchuck chuck if a woodchuck could
chuck wood?
Program has a knowledge base in config.txt.
I,1,roman
V,5,roman
X,10,roman
L,50,roman
C,100,roman
D,500,roman
M,1000,roman

Based on this input and the configuration program, the answer to the
question is given in input.txt in standard output, as shown here:

pish tegj glob glob is 42


glob prok Silver is 68 Credits
glob prok Gold is 57800 Credits
glob prok Iron is 782 Credits
I have no idea what you are talking about

2
Chapter 1 A Birds Eye View to AI System

The parsing logic is in the Observer class.

import operator

#this class verify the validity of input


class Observer(object):
      #store frequecy of symbols
      length = {}
      #most frequent symbol
      symbol = ''
      #count of most frequent symbol
      count = 0
      #calling class
      compiler = None

      def __init__(self,cmpiler):
            self.compiler = cmpiler

      def initialize(self, arr):


            for i in range(len(arr)):
                  self.length[arr[i]] = 0

      #increase count for each occurence of symbol


      def increment(self,symbol):
            self.length[symbol] = self.length[symbol] + 1

      #claculate most frequent symbol and it's count


      def calculate(self):
            self.symbol,self.count = max(self.length.items(),
key=operator.itemgetter(1))

      #verify if wrong symbol is subtracted ie ( V, ..


      def verifySubstract(self, current):
            while current % 10 != 0:
                  current = current / 10

3
Chapter 1 A Birds Eye View to AI System

            if current == 5:
                  raise Exception("Wrong Substraction")

      def evaluate(self):
            #check mximum repeatation is crossing the limit
            if self.count > 3:
                  raise Exception("Repeat more than 3")
            #symbol is proper or not
            if self.symbol not in self.compiler.symbol_map:
                  raise Exception("Wrong Symbol")
            #check if wrong symbol is repeated ie (V, ..
            self.symbol,unit = self.compiler.
evaluateSymbol(self.symbol)
            while self.symbol % 10 != 0:
                  self.symbol = self.symbol / 10
            if self.count > 1 and  self.symbol == 5:
                  raise Exception("Wrong Symbol repeated")

      #checking if input sentence is proper or not


      def evaluateSentence(self, line):
            if "is" not in line:
                  return "I have no idea what you are
talking about"

The compilation logic is in the compiler class, as shown here:

import sys

from observer import Observer

class compilerTrader(object):
    #store mapping of symbols with score and unit
    symbol_map = {}
    #store the list of valid symbol
    valid_values = []

4
Chapter 1 A Birds Eye View to AI System

    #read the config and initialize the class member


    def __init__(self, config_path):
        with open(config_path) as f:
           for line in f:
               if ',' in line :
                   symbol, value, type = line.strip().
split(',')
                   self.symbol_map[symbol] = float(value), type
                   self.valid_values.append(float(value))
           f.close()

    #evaluate the ultimate numerical score with unit for


a symbol
    def evaluateSymbol(self, symbol):
        while symbol not in self.valid_values:
              symbol,unit = self.symbol_map[symbol]
        return float(symbol), unit

    #compiling the info in line


    def compile_super(self, line):
        obs = Observer(self)
        if 'is' in line:
            fields = line.split(' is ')
            value = fields[-1]
            var = fields[0]
            #if one symbol and one value
            if ' ' not in var:
                if value in self.symbol_map:
                   self.symbol_map[var] = int(self.symbol_
map[value][0]) ,'roman'

5
Chapter 1 A Birds Eye View to AI System

                else:
                    #logic for value with unit
                    if ' ' in value:
                        fields = value.split(' ')
                        user_unit = fields[-1]
                        if ' ' not in var:
                            self.symbol_map[var] =
int(fields[0]), user_unit
                        else:
                            #logic for multiple symbols
in input
                            total = int(fields[0])
                            factor = 0
                            arr = var.split(' ')
                            obs.initialize(arr)
                            for i in range(len(arr)):
                                obs.increment(arr[i])
                                if arr[i] in self.symbol_map
and arr[i+1] in self.symbol_map
and i < len(arr) -1:
                                    current, current_
unit = self.
evaluateSymbol(
[arr[i]][0])
                                    next, next_unit = self.
evaluateSymbol(
[arr[i+1]][0])
                                    if current >= next:
                                       factor =  factor
+ current

6
Chapter 1 A Birds Eye View to AI System

                                    else:
                                        obs.verifySubstract(
current)
                                        factor = factor -
current
                                    else:
                                        if arr[i] in self.
symbol_map:
                                        current, current_
unit = self.
evaluateSymbol(
[arr[i]][0])
                                        factor = factor +
current
                                            else:
                                                self.
symbol_map[
arr[i]] =
total/factor,
user_unit
                                                self.valid_
values.append
(total/factor)
                               obs.calculate()
                               obs.evaluate()

The answering logic is in the answer layer, which calls Observer and
compiler. The answering class inherits the compiler class.

import sys

from observer import Observer


from compiler import compilerTrader

7
Chapter 1 A Birds Eye View to AI System

class answeringTrader(compilerTrader):

      def __init__(self, config_path):


            super().__init__(config_path)

      #compiling info in line


      def compile(self, line):
            super().compile_super(line)

      #answering query in line


      def answer(self, line):
            obs = Observer(super())
            if 'is' in line:
                  values = line.split(' is ')[-1]
                  ans = 0
                  arr = values.split(' ')
                  unit = ''
                  obs.initialize(arr)
                  for i in range(len(arr)):
                        if arr[i] in "?.,!;":
                              continue
                        obs.increment(arr[i])
                        if i < len(arr)-2:
                              if arr[i] in super().symbol_
map and arr[i+1] in super().
symbol_map:
                                    current, current_
unit = super().
evaluateSymbol([arr[i]][0])
                                    next, next_unit = super().
evaluateSymbol(
[arr[i+1]][0])

8
Chapter 1 A Birds Eye View to AI System

                                    if current >= next:


                                          ans = ans + current
                                    else:
                                          if next_unit ==
'roman':
                                                obs.verify
Substract(
current)
                                                ans = ans -
current
                                          else:
                                                ans = ans
+ current
                        else:
                              if arr[i] in super().symbol_map:
                                    current,unit = super().
evaluateSymbol([arr[i]][0])
                                    if  unit != 'roman':
                                          ans = ans * current
                                    else:
                                          ans = ans + current

                  obs.calculate()
                  obs.evaluate()
                  values = values.replace("?" , "is ")
                  if unit == 'roman':
                        unit = ''
                  return(values + str(ans) + ' ' + unit)

Finally, the main program calls the answering class and the observer,
and then it performs the task and does unit testing on the logic.

9
Chapter 1 A Birds Eye View to AI System

import sys
import unittest

sys.path.append('./answerLayer')
sys.path.append('./compilerLayer')
sys.path.append('./utilityLayer')

from answer import answeringTrader


from observer import Observer

#client interface for the framework


class ClientTrader(object):
      trader = None
      def __init__(self, config_path):
            self.trader = answeringTrader(config_path)

      #processing an input string


      def process(self, input_string):
            obs = Observer(self.trader)
            valid = obs.evaluateSentence(input_string)
            if valid is not None:
                  return valid
            if input_string.strip()[-1] == '?' :
                  return self.trader.answer(input_string)
            else:
                  return self.trader.compile(input_string)

#unit test cases


class TestTrader(unittest.TestCase):
      trader = None

      def setUp(self):
            pass

10
Chapter 1 A Birds Eye View to AI System

      #test case for non-roman symbol      unit other


than roman
      def test_answer_unit(self):
            ans = self.trader.process("how many Credits is glob
prok Silver ?")
            self.assertEqual(ans.strip(), "glob prok Silver is
68.0 Credits")

      #test case with only roman symbol in unit case


      def test_answer_roman(self):
            ans = self.trader.process("how much is pish tegj
glob glob ?")
            self.assertEqual(ans.strip(), "pish tegj glob glob
is 42.0")

      #test case if repeatation of symbol is exceed max


limit (3)
      def test_exception_over_repeat(self):
            with self.assertRaises(Exception) as context:
                  ans = self.trader.process("how much is pish
tegj glob glob glob glob ?")
                  self.assertTrue("Repeat more than 3" in
context.exception)

      #test case if wrong symbol repeated ie (V ..


      def test_exception_unproper_repeat(self):
            with self.assertRaises(Exception) as context:
                  ans = self.trader.process("how much is pish
tegj D D ?")
                  self.assertTrue("Repeat more than 3" in
context.exception)

11
Chapter 1 A Birds Eye View to AI System

      #test case if wrong symbol substracted ie (V ...


      def test_wrong_substraction(self):
            with self.assertRaises(Exception) as context:
                  ans = self.trader.process("how much
is V X ?")
                  self.assertTrue("Wrong Substraction" in
context.exception)

      #test case if query is not properly formatted


      def test_wrong_format_query(self):
            ans = self.trader.process("how much wood could a
woodchuck chuck if a woodchuck could chuck wood ?")
            self.assertEqual(ans.strip(), "I have no idea what
you are talking about")

if __name__ == '__main__':
      if len(sys.argv) != 3:
            print("Usage is : " + sys.argv[0] + " <intput file
path>  <config file path>")
            exit(0)
      tr = ClientTrader(sys.argv[2])
      f = open(sys.argv[1])
      for line in f:
            response = tr.process(line.strip())
            if response is not None:
                  print(response)

      TestTrader.trader = tr
      unittest.main(argv = [sys.argv[0]], exit = False)

You can run this program with the following command:

python client.py input.txt config.txt

12
Chapter 1 A Birds Eye View to AI System

Calling Other Languages in Python


Now we will describe how to use other languages in Python. There are two
examples here. The first is calling R code from Python. R code is required
for some use cases. For example, if you want a ready-made function for the
Holt-Winters method in a time series, it is difficult to perform in Python,
but it is available in R. So, you can call R code from Python using the rpy2
module, as shown here:

import rpy2.robjects as ro
ro.r('data(input)')
ro.r('x <-HoltWinters(input data frame)')

(You can use example data given in time series chapter.)


Sometimes you need to call Java code from Python. For example, say
you are working on a name-entity recognition problem in the field of
natural language processing (NLP); some text is given as input, and you
have to recognize the names in the text. Python’s NLTK package does have
a name-entity recognition function, but its accuracy is not good. Stanford
NLP is a better choice here, but it is written in Java. You can solve this
problem in two ways.

• You can call Java at the command line using Python


code. You need to install Java with the yum/at-get
install java command before calling it.

• For Windows, it is recommended that you install


the JRE from https://adoptium.net/temurin/
releases/?version=8. You can also install the JRE
from another distribution. The installation will
automatically create JAVA_HOME. If it does not, you
need to set JAVA_HOME as the system variable, and the
value should be the location of Java installation folder,
for example, JAVA_HOME=C:\Program Files\Eclipse
Adoptium\jdk-8.0.345.1-hotspot\.
13
Chapter 1 A Birds Eye View to AI System

import subprocess
subprocess.call(['java','-cp','*','edu.stanford.nlp.sentiment.
SentimentPipeline','-file','foo.txt'])

Please place foo.txt in the same folder where you run the
Python code.

• You can expose Stanford NLP as a web service and call


it as a service. (Before running this code, you’ll need to
download the Stanford nlp JAR file available with the
book’s source code.)

nlp = StanfordCoreNLP('http://127.0.0.1:9000')
output = nlp.annotate(sentence, properties={
"annotators": "tokenize,ssplit,parse,sentiment",
"outputFormat": "json",
# Only split the sentence at End Of Line. We assume that this
method only takes in one single sentence.
"ssplit.eolonly": "true",
# Setting enforceRequirements to skip some annotators and make
the process faster
"enforceRequirements": "false"
})

You will see a more detailed example of Stanford NLP in Chapter 2.

E xposing the Python Model


as a Microservice
You can expose the Python model as a microservice in the same way that
your Python model can be used by others to write their own code. The best
way to do this is to expose your model as a web service. As an example, the
following code exposes a deep learning model using Flask:

14
Chapter 1 A Birds Eye View to AI System

from flask import Flask, request, g


from flask_cors import CORS
import tensorflow as tf
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
import pygeoip
from pymongo import MongoClient
import json
import datetime as dt
import ipaddress
import math
app = Flask(__name__)
CORS(app)
@app.before_request
def before():
       db = create_engine('sqlite:///score.db')
       metadata = MetaData(db)
       g.scores = Table('scores', metadata, autoload=True)
       Session = sessionmaker(bind=db)
       g.session = Session()
       client = MongoClient()
       g.db = client.frequency
       g.gi = pygeoip.GeoIP('GeoIP.dat')
       sess = tf.Session()
       new_saver = tf.train.import_meta_graph('model.obj.meta')
       new_saver.restore(sess, tf.train.latest_
checkpoint('./'))
       all_vars = tf.get_collection('vars')
       g.dropped_features = str(sess.run(all_vars[0]))
       g.b = sess.run(all_vars[1])[0]
       return

15
Chapter 1 A Birds Eye View to AI System

def get_hour(timestamp):
       return dt.datetime.utcfromtimestamp(timestamp /
1e3).hour
def get_value(session, scores, feature_name, feature_value):
       s = scores.select((scores.c.feature_name == feature_
name) & (scores.c.feature_value == feature_value))
       rs = s.execute()
       row = rs.fetchone()
       if row is not None:
             return float(row['score'])
       else:
             return 0.0
@app.route('/predict', methods=['POST'])
def predict():
       input_json = request.get_json(force=True)
       features = ['size','domain','client_time','device','ad_
position','client_size', 'ip','root']
       predicted = 0
       feature_value = ''
       for f in features:
             if f not in g.dropped_features:
                    if f == 'ip':
                          feature_value = str(ipaddress.
IPv4Address(ipaddress.ip_
address(unicode(request.remote_
addr))))
                    else:
                          feature_value = input_json.get(f)
                    if f == 'ip':
                          if 'geo' not in g.dropped_features:

16
Discovering Diverse Content Through
Random Scribd Documents
and his hands trembling, for many weird tales had been told of this
same Señor Zorro and his brutality, none of which was true.
"I am sure that you will pardon me," Señor Zorro said, "when I ask
you to sit at the far end of the room. As I take each bite, I must
raise the bottom of my mask, for I have no wish to become known. I
put the pistol before me on the table, so, to discourage treachery.
And now, Don Carlos Pulido, I shall do justice to the meal you have
furnished."
Don Carlos and his daughter sat where they had been directed, and
the bandit ate with evident relish. Now and then he stopped to talk
to them, and once he had Don Carlos send out for more wine,
declaring it to be the best he had tasted for a year.
Don Carlos was only too glad to oblige him. He was playing to gain
time. He knew the horse the native rode, and judged that he had
reached the presidio at Reina de Los Angeles before this, and that
the soldiers were on their way. If he could hold this Señor Zorro until
they arrived!
"I am having some food prepared for you to carry with you, señor,"
he said. "You will pardon me while I get it? My daughter will
entertain you."
Señor Zorro bowed, and Don Carlos hurried from the room. But Don
Carlos had made a mistake in his eagerness. It was an unusual thing
for a girl to be left alone in the company of a man in such fashion,
especially with a man known to be an outlaw. Señor Zorro guessed
at once that he was being delayed purposely. For, again, it was an
unusual thing for a man like Don Carlos to go for the package of
food himself when there were servants that could be called by a
mere clapping of the hands. Don Carlos, in fact, had gone into the
other room to listen at a window for sounds of galloping horses.
"Señor!" Lolita whispered across the room.
"What is it, señorita?"
"You must go—at once. I am afraid that my father has sent for the
soldiers."
"And you are kind enough to warn me?"
"Do I wish to see you taken here? Do I wish to see fighting and
bloodshed?" she asked.
"That is the only reason, señorita?"
"Will you not go, señor?"
"I am loath to rush away from such a charming presence, señorita.
May I come again at the next siesta hour?"
"By the saints—no! This must end, Señor Zorro! Go your way—and
take care! You have done some things that I admire, hence I would
not see you captured. Go north, as far as San Francisco de Asis, and
turn honest, señor. It is the better way."
"Little priest!" he said.
"Shall you go, señor?"
"But your father has gone to fetch food for me. And could I depart
without thanking him for this meal?"
Don Carlos came back into the room then, and Señor Zorro knew by
the expression on his face that the soldiers were coming up the trail.
The don put a package on the table.
"Some food to carry with you, señor," he said. "And we would relish
more of your reminiscences before you start on your perilous
journey."
"I have spoken too much of myself already, señor, and it ill becomes
a caballero to do that. It were better that I thank you and leave you
now."
"At least, señor, drink another mug of wine."
"I fear," said Señor Zorro, "that the soldiers are much too close, Don
Carlos."
The face of the don went white at that, for the highwayman was
picking up his pistol, and Don Carlos feared he was about to pay the
price for his treacherous hospitality. But Señor Zorro made no move
to fire.
"I forgive you this breach of hospitality, Don Carlos, because I am an
outlaw and there has been a price put upon my head," he said.
"And, also, I hold you no ill will because of it. Buenas noches,
señorita! Señor, á Dios!"
Then a terrified servant who knew little concerning the events of the
evening rushed in at the door.
"Master! The soldiers are here!" he cried. "They are surrounding the
house!"
CHAPTER IX
THE CLASH OF BLADES

On the table, near its middle, was an imposing candelero in which


half a score of candles burned brightly. Señor Zorro sprang toward it
now, and with one sweep of his hand dashed it to the floor,
extinguishing all the candles in an instant and plunging the room in
darkness.
He evaded the wild rush of Don Carlos, springing across the room so
lightly that his soft boots made not the slightest noise to give news
of his whereabouts. For an instant the Señorita Lolita felt a man's
arm around her waist, gently squeezing it, felt a man's breath on her
cheek, and heard a man's whisper in her ear:
"Until later, señorita!"
Don Carlos was bellowing like a bull to direct the soldiers to the
scene; and already some of them were pounding at the front door.
Señor Zorro rushed from the room and into the one adjoining, which
happened to be the kitchen. The native servants fled before him as if
he had been a ghost, and he quickly extinguished all the candles
that burned there.
Then he ran to the door that opened into the patio, and raised his
voice, and gave a call that was half moan and half shriek, a peculiar
call, the like of which none at the Pulido hacienda had heard before.
As the soldiers rushed in at the front door, and as Don Carlos called
for a brand with which to light the candles again, the sound of
galloping hoofs was heard from the rear of the patio. Some powerful
horse was getting under way there, the soldiers guessed
immediately.
The sound of hoofs died away in the distance, but the soldiers had
noted the direction in which the horse was traveling.
"The fiend escapes!" Sergeant Gonzales shrieked, he being in charge
of the squad. "To horse, and after him! I give the man who
overtakes him one-third of all the reward!"
The big sergeant rushed from the house, the men at his heels, and
they tumbled into their saddles and rode furiously through the
darkness, following the sound of the beating hoofs.
"Lights! Lights!" Don Carlos was shrieking inside the house.
A servant came with a brand, and the candles were lighted again.
Don Carlos stood in the middle of the room, shaking his fists in
impotent rage. Señorita Lolita crouched in a corner, her eyes wide
with fear. Doña Catalina, fully recovered now from her fainting spell,
came from her own room to ascertain the cause of the commotion.
"The rascal got away!" Don Carlos said. "It is to be hoped that the
soldiers capture him."
"At least, he is clever and brave," Señorita Lolita said.
"I grant him that, but he is a highwayman and a thief!" Don Carlos
roared. "Why should he torment me by visiting my house?"
Señorita Lolita thought she knew, but she would be the last one to
explain to her parents. There was a faint blush on her face yet
because of the arm that had squeezed her and the words that had
been whispered in her ear.
Don Carlos threw the front door open wide and stood in it, listening.
To his ears came the sound of galloping hoofs once more.
"My sword!" he cried to a servant. "Some one comes—it may be the
rascal returning! It is but one rider, by the saints!"
The galloping stopped; a man made his way across the veranda and
hurried through the door into the room.
"Thank the good saints!" Don Carlos gasped.
It was not the highwayman returned; it was Captain Ramón,
comandante of the presidio at Reina de Los Angeles.
"Where are my men?" the captain cried.
"Gone, señor! Gone after that pig of a highwayman!" Don Carlos
informed him.
"He escaped?"
"He did, with your men surrounding the house. He dashed the
candles to the floor, ran through the kitchen—"
"The men took after him?"
"They are upon his heels, señor."
"Ha! It is to be hoped that they catch this pretty bird. He is a thorn
in the side of the soldiery. We do not catch him, and because we do
not the governor sends sarcastic letters by his courier. This Señor
Zorro is a clever gentleman, but he will be captured yet!"
And then Captain Ramón walked further into the room, and
perceived the ladies, and swept off his cap and bowed before them.
"You must pardon my bold entrance," he said. "When an officer is on
duty—"
"The pardon is granted freely," said Doña Catalina. "You have met
my daughter?"
"I have not had the honor."
The doña presented them, and Lolita retreated to her corner again
and observed the soldier. He was not ill to look at—tall and straight
and in a brilliant uniform, and with sword dangling at his side. As for
the captain, he never had set eyes upon Señorita Lolita before, for
he had been at the post at Reina de Los Angeles but a month,
having been transferred there from Santa Barbara.
But now that he had looked at her once he looked a second time,
and a third. There was a sudden light in his eyes that pleased Doña
Catalina. If Lolita could not look with favor upon Don Diego Vega,
perhaps she would look with favor upon this Captain Ramón, and to
have her wedded to an officer would mean that the Pulido family
would have some protection.
"I could not find my men now in the darkness," the captain said,
"and so, if it is not presuming too much, I shall remain here and
await their return."
"By all means," Don Carlos said. "Be seated, señor, and I'll have a
servant fetch wine."
"This Señor Zorro has about had his run," the captain said, after the
wine had been tasted and found excellent. "Now and then a man of
his sort pops up and endures for a little day, but he never lasts long.
In the end he meets the fate."
"That is true," said Don Carlos. "The fellow was boasting to us to-
night of his accomplishments."
"I was comandante at Santa Barbara when he made his famous visit
there," the captain explained. "I was visiting at one of the houses at
the time else there might have been a different story. And to-night,
when the alarm came, I was not at the presidio, but at the residence
of a friend. That is why I did not ride out with the soldiers. As soon
as I was notified I came. It appears that this Señor Zorro has some
knowledge of my whereabouts and is careful that I am not in a
position to clash with him. I hope one day to do so."
"You think you could conquer him, señor?" Doña Catalina asked.
"Undoubtedly! I understand he really is an ordinary hand with a
blade. He made a fool of my sergeant, but that is a different
proposition—and I believe he held a pistol in one hand while he
fenced, too. I should make short work of the fellow."
There was a closet in one corner of the room, and now its door was
opened a crack.
"The fellow should die the death!" Captain Ramón went on to say.
"He is brutal in his dealings with men. He kills wantonly, I have
heard. They say he caused a reign of terror in the north, in the
vicinity of San Francisco de Asis. He slew men regardless, insulted
women—"
The closet door was hurled open—and Señor Zorro stepped into the
room.
"I shall take you to task for that statement, señor, since it is a
falsehood!" the highwayman cried.
Don Carlos whirled around and gasped his surprise. Doña Catalina
felt suddenly weak in the knees and collapsed on a chair. Señorita
Lolita felt some pride in the man's statement, and a great deal of
fear for him.
"I—I thought you had escaped," Don Carlos gasped.
"Ha! It was but a trick! My horse escaped—but I did not!"
"Then there shall be no escape for you now!" Captain Ramón cried,
drawing his blade.
"Back, señor!" Zorro cried, exhibiting a pistol suddenly. "I shall fight
you gladly, but the fight must be fair. Don Carlos, gather your wife
and daughter beneath your arms and retire to the corner while I
cross blades with this teller of falsehoods. I do not intend to have a
warning given out that I still am here!"
"I thought—you escaped!" Don Carlos gasped again, seemingly
unable to think of anything else, and doing as Señor Zorro
commanded.
"A trick!" the highwayman repeated, laughing. "It is a noble horse I
have. Perhaps you heard a peculiar cry from my lips? My beast is
trained to act at that cry. He gallops away wildly, making
considerable noise, and the soldiers follow him. And when he has
gone some distance he turns aside and stops, and after the pursuit
has passed he returns to await my bidding. No doubt he is behind
the patio now. I shall punish this captain, and then mount and ride
away!"
"With a pistol in your hand!" Ramón cried.
"I put the pistol upon the table—so! There it remains if Don Carlos
stays in the corner with the ladies. Now, captain!"
Señor Zorro extended his blade, and with a glad cry Captain Ramón
crossed it with his own. Captain Ramón had some reputation as a
master of fence, and Señor Zorro evidently knew it, for he was
cautious at first, leaving no opening, on defense rather than attack.
The captain pressed him back, his blade flashing like streaks of
lightning in a troubled sky. Now Señor Zorro was almost against the
wall near the kitchen door, and in the captain's eyes the light of
triumph already was beginning to burn. He fenced rapidly, giving the
highwayman no rest, standing his ground and keeping his antagonist
against the wall.
And then Señor Zorro chuckled! For now he had solved the other's
manner of combat, and knew that all would be well. The captain
gave ground a little as the defense turned into an attack that
puzzled him. Señor Zorro began laughing lightly.
"'Twere a shame to kill you," he said. "You are an excellent officer, I
have heard, and the army needs a few such. But you have spoken
falsehood regarding me, and so must pay a price. Presently I shall
run you through, but in such manner that your life will not emerge
when I withdraw my blade."
"Boaster!" the captain snarled.
"As to that we shall see presently. Ha! I almost had you there, my
captain. You are more clever than your big sergeant, but not half
clever enough. Where do you prefer to be touched—the left side or
the right?"
"If you are so certain run me through the right shoulder," the captain
said.
"Guard it well, my captain, for I shall do as you say! Ha!"
The captain circled, trying to get the light of the candles in the
highwayman's eyes, but Señor Zorro was too clever for that. He
caused the captain to circle back, forced him to retreat, fought him
to a corner.
"Now, my captain!" he cried.
And so he ran him through the right shoulder, as the captain had
said, and twisted the blade a bit as he brought it out. He had struck
a little low, and Captain Ramón dropped to the floor, a sudden
weakness upon him.
Señor Zorro stepped back and sheathed his blade.
"I ask the pardon of the ladies for this scene," he said. "And I assure
you that this time I am, indeed, going away. You will find that the
captain is not badly injured, Don Carlos. He may return to his
presidio within the day."
He removed his sombrero and bowed low before them, while Don
Carlos sputtered and failed to think of anything to say that would be
mean and cutting enough. His eyes, for a moment, met those of the
Señorita Lolita, and he was glad to find that in hers there was no
repugnance.
"Buenas noches!" he said and laughed again.
And then he dashed through the kitchen and into the patio, and
found the horse awaiting him there, as he had said it would be, and
was quick to mount and ride away.
CHAPTER X
A HINT AT JEALOUSY

Within the space of half an hour Captain Ramón's wounded shoulder


had been cleansed of blood and bandaged, and the captain was
sitting at one end of the table, sipping wine and looking very white
in the face and tired.
Doña Catalina and Señorita Lolita had shown much sympathy,
though the latter could scarcely refrain from smiling when she
remembered the captain's boast regarding what he purposed doing
to the highwayman, and compared it to what had happened. Don
Carlos was outdoing himself to make the captain feel at home since
it was well to seek influence with the army, and already had urged
upon the officer that he remain at the hacienda a few days until his
wound had healed.
Having looked into the eyes of the Señorita Lolita, the captain had
answered that he would be glad to remain at least for a day, and
despite his wound was attempting polite and witty conversation, yet
failing miserably.
Once more there could be heard the drumming of a horse's hoofs,
and Don Carlos sent a servant to the door to open it so that the light
would shine out, for they supposed that it was one of the soldiers
returning.
The horseman came nearer, and presently stopped before the
house, and the servant hurried out to care for the beast.
There passed a moment during which those inside the house heard
nothing at all, and then there were steps on the veranda, and Don
Diego Vega hurried through the door.
"Ha!" he cried, as if in relief. "I am rejoiced that you all are alive and
well!"
"Don Diego!" the master of the house exclaimed. "You have ridden
out from the pueblo a second time in one day?"
"No doubt I shall be ill because of it," Don Diego said. "Already I am
feeling stiff and my back aches. Yet I felt that I must come. There
was an alarm in the pueblo, and it was noised abroad that this Señor
Zorro, the highwayman, had paid a visit to the hacienda. I saw the
soldiers ride furiously in this direction, and fear came into my heart.
You understand, Don Carlos, I feel sure."
"I understand, caballero," Don Carlos replied, beaming upon him and
glancing once at Señorita Lolita.
"I—er—felt it my duty to make the journey. And now I find that it
has been made for naught—you all are alive and well. How does it
happen?"
Lolita sniffed, but Don Carlos was quick to make reply.
"The fellow was here, but he made his escape after running Captain
Ramón through the shoulder."
"Ha!" Don Diego said, collapsing into a chair. "So you have felt his
steel; eh, captain? That should feed your desire for vengeance. Your
soldiers are after the rogue?"
"They are," the captain replied shortly, for he did not like to have it
said that he had been defeated in combat. "And they will continue to
be after him until he is captured. I have a big sergeant, Gonzales—I
think he is a friend of yours, Don Diego—who is eager to make the
arrest and earn the governor's reward. I shall instruct him, when he
returns, to take his squad and pursue this highwayman until he has
been dealt with properly."
"Let me express the hope that the soldiers will be successful, señor.
The rogue has annoyed Don Carlos and the ladies—and Don Carlos
is my friend. I would have all men know it!"
Don Carlos beamed, and Doña Catalina smiled bewitchingly, but the
Señorita Lolita fought to keep her pretty upper lip from curling with
scorn.
"A mug of your refreshing wine, Don Carlos," Don Diego Vega
continued. "I am fatigued. Twice to-day have I ridden here from
Reina de Los Angeles, and it is about all a man can endure."
"'Tis not much of a journey—four miles," said the captain.
"Possibly not for a rough soldier," Don Diego replied, "but it is for a
caballero."
"May not a soldier be a caballero?" Ramón asked, nettled somewhat
at the other's words.
"It has happened before now, but we come across it rarely," Don
Diego said. He glanced at Lolita as he spoke, intending that she
should take notice of his words, for he had seen the manner in
which the captain glanced at her, and jealousy was beginning to
burn in his heart.
"Do you mean to insinuate, señor, that I am not of good blood?"
Captain Ramón asked.
"I cannot reply as to that, señor, having seen none of it. No doubt
this Señor Zorro could tell me. He saw the color of it, I understand."
"By the saints!" Captain Ramón cried, "you would taunt me?"
"Never be taunted by the truth," Don Diego observed. "He ran you
through the shoulder, eh? 'Tis a mere scratch, I doubt not. Should
you not be at the presidio instructing your soldiers?"
"I await their return here," the captain replied. "Also, it is a fatiguing
journey from here to the presidio, according to your own ideas,
señor."
"But a soldier is inured to hardship, señor."
"True, there are many pests he must encounter," the captain said,
glancing at Don Diego with meaning.
"You term me a pest, señor?"
"Did I say as much?"
This was perilous ground, and Don Carlos had no mind to let an
officer of the army and Don Diego Vega have trouble in his
hacienda, for fear he would get into greater difficulties.
"More wine, señores!" he exclaimed in a loud voice, and stepping
between their chairs in utter disregard of proper breeding. "Drink,
my captain, for your wound has made you weak. And you, Don
Diego, after your wild ride—"
"I doubt its wildness," Captain Ramón observed.
Don Diego accepted the proffered wine mug and turned his back
upon the captain. He glanced across at Señorita Lolita and smiled.
He got up deliberately and picked up his chair, and carried it across
the room to set it down beside her.
"And did the rogue frighten you, señorita?" he asked.
"Suppose he did, señor? Would you avenge the matter? Would you
put blade at your side and ride abroad until you found him, and then
punish him as he deserves?"
"By the saints, were it necessary, I might do as much. But I am able
to employ a raft of strong fellows who would like nothing better than
to run down the rogue. Why should I risk my own neck?"
"Oh!" she exclaimed, exasperated.
"Let us not talk further of this bloodthirsty Señor Zorro," he begged.
"There are other things fit for conversation. Have you been thinking,
señorita, on the object of my visit earlier in the day?"
Señorita Lolita thought of it now. She remembered again what the
marriage would mean to her parents and their fortunes, and she
recalled the highwayman, too, and remembered his dash and spirit,
and wished that Don Diego could be such a man. And she could not
say the word that would make her the betrothed of Don Diego Vega.
"I—I have scarcely had time to think of it, caballero," she replied.
"I trust you will make up your mind soon," he said.
"You are so eager?"
"My father was at me again this afternoon. He insists that I should
take a wife as soon as possible. It is rather a nuisance, of course,
but a man must please his father."
Lolita bit her lips because of her quick anger. Was ever girl so
courted before? she wondered.
"I shall make up my mind as soon as possible, señor," she said
finally.
"Does this Captain Ramón remain long at the hacienda?"
A little hope came into Lolita's breast. Could it be possible that Don
Diego Vega was jealous? If that were true, possibly there might be
stuff in the man, after all. Perhaps he would awaken, and love and
passion come to him, and he would be as other young men.
"My father has asked him to remain until he is able to travel to the
presidio," she replied.
"He is able to travel now. A mere scratch!"
"You will not return to-night?" she asked.
"It probably will make me ill, but I must return. There are certain
things that must engage my interest early in the morning. Business
is such a nuisance!"
"Perhaps my father will offer to send you in the carriage."
"Ha! It were kindness if he does. A man may doze a bit in a
carriage."
"But, if this highwayman should stop you?"
"I need not fear, señorita. Have I not wealth? Could I not purchase
my release?"
"You would pay ransom rather than fight him, señor?"
"I have lots of money, but only one life, señorita. Would I be a wise
man to risk having my blood let out?"
"It would be the manly part, would it not?" she asked.
"Any male can be manly at times, but it takes a clever man to be
sagacious," he said.
Don Diego laughed lightly, as if it cost him an effort, and bent
forward to speak in lower tones.
On the other side of the room, Don Carlos was doing his best to
make Captain Ramón comfortable, and was glad that he and Don
Diego remained apart for the time being.
"Don Carlos," the captain said, "I come from a good family, and the
governor is friendly toward me, as no doubt you have heard. I am
but twenty-three years of age, else I would hold a higher office. But
my future is assured."
"I am rejoiced to learn it, señor."
"I never set eyes upon your daughter until this evening, but she has
captivated me, señor. Never have I seen such grace and beauty,
such flashing eyes! I ask your permission, señor, to pay my
addresses to the señorita."
CHAPTER XI
THREE SUITORS

Here was a fix! Don Carlos had no wish to anger Don Diego Vega or
a man who stood high in the governor's regard. And how was he to
evade it? If Lolita could not force her heart to accept Don Diego,
perhaps she could learn to love Captain Ramón. After Don Diego, he
was the best potential son-in-law in the vicinity.
"Your answer, señor?" the captain was asking.
"I trust you will not misunderstand me, señor," Don Carlos said, in
lower tones. "I must make a simple explanation."
"Proceed, señor."
"But this morning Don Diego Vega asked me the same question."
"Ha!"
"You know his blood and his family, señor. Could I refuse him? Of
rights I could not. But I may tell you this—the señorita weds no man
unless it is her wish. So Don Diego has my permission to pay his
addresses, but if he fails to touch her heart—"
"Then I may try?" the captain asked.
"You have my permission, señor. Of course, Don Diego has great
wealth, but you have a dashing way with you, and Don Diego—that
is—he is rather—"
"I understand perfectly, señor," the captain said, laughing. "He is not
exactly a brave and dashing caballero. Unless your daughter prefers
wealth to a genuine man—"
"My daughter will follow the dictates of her heart, señor!" Don Carlos
said proudly.
"Then the affair is between Don Diego Vega and myself?"
"So long as you use discretion, señor. I would have nothing happen
that would cause enmity between the Vega family and mine."
"Your interests shall be protected, Don Carlos," Captain Ramón
declared.
As Don Diego talked, the Señorita Lolita observed her father and
Captain Ramón, and guessed what was being said. It pleased her, of
course, that a dashing officer should enter the lists for her hand, and
yet she had felt no thrill when first she looked into his eyes.
Señor Zorro, now, had thrilled her to the tips of her tiny toes, and
merely because he had talked to her, and touched the palm of her
hand with his lips. If Don Diego Vega were only more like the
highwayman! If some man appeared who combined Vega's wealth
with the rogue's spirit and dash and courage!
There was a sudden tumult outside, and into the room strode the
soldiers, Sergeant Gonzales at their head. They saluted their captain,
and the big sergeant looked with wonder at his wounded shoulder.
"The rogue escaped us," Gonzales reported. "We followed him for a
distance of three miles or so, as he made his way into the hills,
where we came upon him."
"Well?" Ramón questioned.
"He has allies."
"What is this?"
"Fully ten men were waiting for him there, my captain. They set
upon us before we were aware of their presence. We fought them
well, and three of them we wounded, but they made their escape
and took their comrades with them. We had not been expecting a
band, of course, and so rode into their ambush."
"Then we have to contend with a band of them!" Captain Ramón
said. "Sergeant, you will select a score of men in the morning, and
have command over them. You will take the trail of this Señor Zorro,
and you will not stop until he is either captured or slain. I will add a
quarter's wages to the reward of his excellency, the governor, if you
are successful."
"Ha! It is what I have wished!" Sergeant Gonzales cried. "Now we
shall run this coyote to earth in short order! I shall show you the
color of his blood—"
"'Twould be no more than right, since he has seen the color of the
captain's," Don Diego put in.
"What is this, Don Diego, my friend? Captain, you have crossed
blades with the rogue?"
"I have," the captain assented. "You but followed a tricky horse, my
sergeant. The fellow was here, in a closet, and came out after I had
entered. So it must have been some other man you met with his
companions up in the hills. This Señor Zorro treated me much as he
treated you in the tavern—had a pistol handy in case I should prove
too expert with the blade."
Captain and sergeant looked at each other squarely, each wondering
how much the other had been lying; while Don Diego chuckled
faintly and tried to press the Señorita Lolita's hand, and failed.
"This thing can be settled only in blood!" Gonzales declared. "I shall
pursue the rascal until he is run to earth. I have permission to select
my men?"
"You may take any at the presidio," the captain said.
"Sergeant Gonzales, I should like to go with you," Don Diego said
suddenly.
"By the saints! It would kill you, caballero! Day and night in the
saddle, up hill and down hill, through dust and heat, and with a
chance at fighting!"
"Well, perhaps it were best for me to remain in the pueblo," Don
Diego admitted. "But he has annoyed this family, of which I am a
true friend. At least, you will keep me informed? You will tell me how
he escapes if he dodges you? I at least may know that you are on
his trail, and where you are riding, so I may be with you in spirit?"
"Certainly, caballero—certainly!" Sergeant Gonzales replied. "I shall
give you the chance of looking upon the rogue's dead face. I swear
it!"
"'Tis a terrible oath, my sergeant. Suppose it should come to pass—"
"I mean, if I slay the rascal, caballero. My captain, do you return this
night to the presidio?"
"Yes," Ramón replied. "Despite my wound, I can ride a horse."
He glanced toward Don Diego as he spoke, and there was almost a
sneer upon his lips.
"What magnificent grit!" Don Diego said. "I, too, shall return to
Reina de Los Angeles, if Don Carlos will be as good as to have his
carriage around. I can tie my horse to the rear of it. To ride
horseback the distance again this day would be the death of me!"
Gonzales laughed and led the way from the house. Captain Ramón
paid his respects to the ladies, glowered at Don Diego, and followed.
The caballero faced Señorita Lolita again as her parents escorted the
captain to the door.
"You will think of the matter?" he asked. "My father will be at me
again within a few days, and I shall escape censure if I am able to
tell him that it is all settled. If you decide to wed me, have your
father send me word by a servant. Then I shall put my house in
order against the wedding day."
"I shall think of it," the girl said.
"We could be married at the mission of San Gabriel, only we should
have to make the confounded journey there. Fray Felipe, of the
mission, has been my friend from the days of my boyhood, and I
would have him say the words, unless you prefer otherwise. He
could come to Reina de Los Angeles and read the ceremony in the
little church on the plaza there."
"I shall think of it," the girl said again.
"Perhaps I may come out again to see you within a few days, if I
survive this night. Buenas noches, señorita! I suppose I should—er—
kiss your hand?"
"You need not take the trouble," Señorita Lolita replied. "It might
fatigue you."
"Ah—thank you! You are thoughtful, I see. I am fortunate if I get me
a thoughtful wife."
Don Diego sauntered to the door. Señorita Lolita rushed into her
own room and beat at her breasts with her hands, and tore at her
hair a bit, too angry, too enraged to weep. Kiss her hand, indeed!
Señor Zorro had not suggested it—he had done it. Señor Zorro had
dared death to visit her! Señor Zorro had laughed as he fought, and
then had escaped by a trick! Ah, if Don Diego Vega were half the
man this highwayman appeared!
She heard the soldiers gallop away, and after a little time she heard
Don Diego Vega depart in her father's carriage. And then she went
out into the great room again to her parents.
"My father, it is impossible that I wed with Don Diego Vega," she
said.
"What has caused your decision, my daughter?"
"I scarcely can tell, except that he is not the sort of man I wish for
my husband. He is lifeless; existence with him would be a continual
torment."
"Captain Ramón also has asked permission to pay you his
addresses," Doña Catalina said.
"And he is almost as bad. I do not like the look in his eyes," the girl
replied.
"You are too particular," Don Carlos told her. "If the persecution
continues another year we shall be beggars. Here is the best catch
in the country seeking you, and you would refuse him. And you do
not like a high army officer because you do not fancy the look in his
eyes!
"Think on it, girl! An alliance with Don Diego Vega is much to be
desired. Perhaps, when you know him better, you will like him more.
And the man may awaken. I thought I saw a flash of it this night,
deemed him jealous because of the presence of the captain here. If
you can arouse his jealousy—"
Señorita Lolita burst into tears, but soon the tempest of weeping
passed, and she dried her eyes.
"I—I shall do my best to like him," she said. "But I cannot bring
myself to say, yet, that I will be his wife."
She hurried into her room again, and called for the native woman
who attended her. Soon the house was in darkness, and the grounds
about it, save for the fires down by the adobe huts, where the
natives told one another grim tales of the night's events, each trying
to make his falsehood the greatest. A gentle snore came from the
apartment of Don Carlos Pulido and his wife.
But the Señorita Lolita did not slumber. She had her head propped
on one hand, and she was looking through a window at the fires in
the distance, and her mind was full of thoughts of Señor Zorro.
She remembered the grace of his bow, the music of his deep voice,
the touch of his lips upon her palm.
"I would he were not a rogue!" she sighed. "How a woman could
love such a man!"
CHAPTER XII
A VISIT

Shortly after daybreak the following morning there was considerable


tumult in the plaza at Reina de Los Angeles. Sergeant Pedro
Gonzales was there with a score of troopers, almost all that were
stationed at the local presidio, and they were preparing for the
chase of Señor Zorro.
The big sergeant's voice roared out above the din as men adjusted
saddles and looked to bridles and inspected their water-bottles and
small supplies of provisions. For Sergeant Gonzales had ordered that
his force travel light, and live off the country as much as possible.
He had taken the commands of his captain seriously—he was going
after Señor Zorro, and did not propose to return until he had him—
or had died in an effort to effect a capture.
"I shall nail the fellow's pelt to the presidio door, my friend," he told
the fat landlord. "Then I shall collect the governor's reward and pay
the score I owe you."
"I pray the saints it may be true!" the landlord said.
"What, fool? That I pay you? Do you fear to lose a few small coins?"
"I meant that I pray you may be successful in capturing the man,"
the landlord said, telling the falsehood glibly.
Captain Ramón was not up to see the start, having a small fever
because of his wound, but the people of the pueblo crowded around
Sergeant Gonzales and his men, asking a multitude of questions,
and the sergeant found himself the center of interest.
"This Curse of Capistrano soon shall cease to exist!" he boasted
loudly. "Pedro Gonzales is on his trail. Ha! When I stand face to face
with the fellow—"
The front door of Don Diego Vega's house opened at that juncture,
and Don Diego himself appeared, at which the townsmen wondered
a bit, since it was so early in the morning. Sergeant Gonzales
dropped a bundle he was handling, put his hands upon his hips, and
looked at his friend with sudden interest.
"You have not been to bed," he charged.
"But I have!" Don Diego declared.
"And are up again so soon? Here is some devilish mystery that
needs an explanation!"
"You made noise enough to awaken the dead," Don Diego said.
"It could not be helped, caballero, since we are acting under orders."
"Were it not possible to make your preparations at the presidio
instead of here in the plaza, or did you think not enough persons
would see your importance there?"
"Now, by the—"
"Do not say it!" Don Diego commanded. "As a matter of fact, I am
up early because I must make a confounded trip to my hacienda, a
journey of some ten miles, to inspect the flocks and herds. Never
become a wealthy man, Sergeant Gonzales, for wealth asks too
much of a man."
"Something tells me that never shall I suffer on that account," said
the sergeant, laughing. "You go with escort, my friend?"
"A couple of natives, that is all."
"If you should meet up with this Señor Zorro, he probably would
hold you for a pretty ransom."
"Is he supposed to be between this place and my hacienda?" Don
Diego asked.
"A native arrived a short time ago with word that he had been seen
on the road running to Pala and San Luis Rey. We ride in that
direction. And since your hacienda is the other way, no doubt you
will not meet the rascal now."
"I feel somewhat relieved to hear you say it. So you ride toward
Pala, my sergeant?"
"We do. We shall try to pick up his trail as soon as possible, and
once we have it we shall run this fox down. Meanwhile, we also shall
attempt to find his den. We start at once."
"I shall await news eagerly," Don Diego said. "Good fortune go with
you!"
Gonzales and his men mounted, and the sergeant shouted an order,
and they galloped across the plaza, raising great clouds of dust, and
took the highway toward Pala and San Luis Rey.
Don Diego looked after them until nothing could be seen but a tiny
dust-cloud in the distance, then called for his own horse. He, too,
mounted and rode away toward San Gabriel, and two native
servants rode mules and followed a short distance behind.
But before he departed, Don Diego wrote a message and sent it by
native courier to the Pulido hacienda. It was addressed to Don
Carlos, and read:

The soldiers are starting this morning to pursue this Señor


Zorro, and it has been reported that the highwayman has a
band of rogues under his command and may offer battle. There
is no telling, my friend, what may happen. I dislike having one
in whom I am interested subjected to danger, meaning your
daughter particularly, but also the Doña Catalina and yourself.
Moreover, this bandit saw your daughter last evening, and
certainly must have appreciated her beauty, and he may seek to
see her again.
I beg of you to come at once to my house in Reina de Los
Angeles, and make it as your home until matters are settled. I
am leaving this morning for my hacienda, but have left orders
with my servants that you are to give what commands you will.
I shall hope to see you when I return, which will be in two or
three days.
Diego.

Don Carlos read that epistle aloud to his wife and daughter, and then
looked up to see how they took it. He scoffed at the danger himself,
being an old war-horse, but did not wish to put his womenfolk in
jeopardy.
"What think you?" he asked.
"It has been some time since we have visited the pueblo," Doña
Catalina said. "I have some friends left among the ladies there. I
think it will be an excellent thing to do."
"It certainly will not injure our fortunes to have it become known we
are house guests of Don Diego Vega," Don Carlos said. "What does
our daughter think?"
It was a concession to ask her, and Lolita realized that she was
granted this unusual favor because of Don Diego's wooing. She
hesitated some time before answering.
"I believe it will be all right," she said. "I should like to visit the
pueblo, for we see scarcely anybody here at the hacienda. But
people may talk concerning Don Diego and myself."
"Nonsense!" Don Carlos exploded. "Could there be anything more
natural than that we should visit the Vegas, since our blood is almost
as good as theirs and better than that of others?"
"But it is Don Diego's house, and not that of his father. Still—he will
not be there for two or three days, he says, and we can return when
he comes."
"Then it is settled!" Don Carlos declared. "I shall see my
superintendent and give him instructions."
He hurried into the patio and rang the big bell for the
superintendent, being well pleased. For when the Señorita Lolita saw
the rich furnishings in the house of Don Diego Vega, she might the
more readily accept Don Diego as a husband, he thought. When she
saw the silks and satins, the elegant tapestries, the furniture inlaid
with gold and studded with precious stones, when she realized that
she could be mistress of this and much more besides—Don Carlos
flattered himself that he knew the feminine heart.
Soon after the siesta hour, a carreta was brought before the door,
drawn by mules and driven by a native. Doña Catalina and Lolita got
into it, and Don Carlos bestrode his best horse and rode at its side.
And so they went down the trail to the highway, and down the
highway toward Reina de Los Angeles.
They passed folk who marveled to see the Pulido family thus going
abroad, for it was well known that they had met with ill fortune and
scarcely went anywhere now. It was even whispered that the ladies
did not keep up with the fashions, and that the servants were poorly
fed, but remained at the hacienda because their master was so kind.
But Doña Catalina and her daughter held their heads proudly, as did
Don Carlos, and they greeted the people they knew, and so
continued along the highway.
Presently they made a turning and could see the pueblo in the
distance—the plaza, and the church with its high cross on one side
of it, and the inn and storehouses, and a few residences of the more
pretentious sort, like Don Diego's, and the scattered huts of natives
and poor folk.

You might also like