Saturday, July 7, 2018

Really Really Random Thoughts

ML seems more like curve fitting.
Why can't we share. Why are we afraid ?
Is it possible to stay completely present ?

A great book about friendship
I will be like you and you'll be like me... - Maurice Sendak

Keep showing up and doing your work
Don't take anything personally.
Live guilt free by front loading commitments

Great things in life after love and friendship
Music
Great thoughts
Pictures

From breath becomes air...
Sooner or later all the ambitions will either be done or abandoned. Either way they will be in the past. What will remember is just the present moment. Try where does this lead to ? Live the moment




Monday, June 25, 2018

Storm in a teacup - Physics of everyday life - Helen Czerski

The world we live in is governed by various physical laws. If we pay attention to them, we can observe the workings of those physical laws.

Humans/ various life forms  have taken advantage of those physical properties that govern this world. This book is a look about the various physical properties and how we are taking advantage of these things. Helen Czerski the author of the book is a researcher based in UK. She writes enthusiastically about the various experiments she did both as a child and as a researcher. The book talks about the various physical aspects of the world without using any mathematical symbols, but just talks about them and tries to give an intuitive understanding about the principles. The book is very entertaining and illuminating at the same time. It also makes us appreciate the physical laws and wonder at the ingenuity of the people who discovered and harnessed those laws for our own advantage. This book is about linking little things we see everyday to the big world we live in. This books also changes your perspective. In author's words - The world is a mosaic of physical patterns, once you're familiar with the basics, you start to see how these patterns fit together. 



Gas Laws - Pop Corns & Rockets  : 

How does a popcorn pop ? As the corn is heated the starch inside the corn is heated and the pressure increases. After some time, the kernel of the corn breaks and suddenly the hot goo inside the kernel expands enough so that pressure inside the kernel is equal to the pressure outside. That is what makes popcorn. The thing to observe is how changing the temperature increased the pressure in the kernel. 
The pressure of a gas is inversely proportional to volume and directly proportional to temperature. 

The same principle of temperature changing the volume is used in baking. Also when denser air pushes the colder air travelling from high land, the pressure increases which increases the temperature, this is called adibatic heating. This gas law is how we breather and the same gas law is used for sending rockets. Otto Van guericke created a vaccum pump and in a demonstration he created two spherical pieces and removed all the air between them. Then the air outside pushed the spheres so hard that horses couldn't pull them at all. Once he pumped air between the spherical pieces they just fell off. This shows how much the air pushes us. 

This is how we breather and drink using straws. This is the principle on which a steam engine works which ushered in the industrial revolution. Here coal was used to heat water which generated steam which pushed a piston in a cylinder which was then converted to rotational motion. There are two cylinders where steam is alternatively pushed and when the steam is released to atmosphere, it makes the chuff sound. This same principle is used in a rocket where an explosive is used to heat the gas which pushes in front of the rocket but other side is empty so there is no pushback from that side so there is a push forward till the explosive heat is all gone. 

Gravity - what goes up must come down: 

Next is all about gravity - How everything is pulled down by the earth. how this is used by the London tower bridge to open and close the bridge using an ingenious mechanism. There are two very heavy balls which are in the middle of the road when the bridge is closed and used as a road, but when it is time to open the bridge to let the ships in, an engine is used to pull the heavy ball a little inwards towards the ends of the road. But once it is pulled a little gravity pull the ball down and since it is heavier at the ends now the road rises and opens up the bridge so ships may pass. Hotter air is lighter than the cooler air and this will cause the hotter particles to rise upward and cooler ones to move downward.This is what causes ocean currents and the weather. 

When an object is in water, how do we know whether it will sink or float ? When an object is in water it is displacing some amount of water. If the water an object displaced is heavier than the object itself, the object will float otherwise it will sink. This is the archimedes principle and it is totally obvious when thought with the gravity principle in mind. Basically whichever is heavier will sink which is lighter will float. 

Surface tension & viscosity - Small is beautiful:

Next is about the the small. How water is attracted to other water molecules. each water molecule is holding on to two other water molecules. The water on the top layer is only being held by the bottom ones but on the top it is not holding on to anything. So it is like an elastic sheet that is being pulled from the bottom. This is called surface tension. When you add a detergent to this, it has one end that loves water and one end that loves air. So it sits in the boundary of water. When a bubble forms,and it floats up it can stay there without bursting because the surface is is more stable  and water is not pulling down with that much force as before as the detergent has decreased the surface tension. 

Towels work because of this attraction between water molecules. When the first water molecule is attracted by the towel, it pulls the next water molecule and the cotton towel has lots of air pockets and channels and water will occupy all of this. Cotton (wool, kitchen towel)  is made of cellulose which has chains of sugars to which water molecule attaches very easily.

Capillary action which pulls the water in the towel can push water a little high from the a tree's roots but how does it reach the sky? On top of the tree, all the narrow cellulose pipes open up in the leaves. The sunlight heats the water in the leaves and one of the molecule gets enough energy to become gas and leave. Now the surface of the leaf is out of equilibrium, so the water molecules from below is pulled through the cellulose pipe and water molecules will be in equilibrium again.

This same principle of surface tension and viscosity will be used in microfluid based diagnostic kits using blood and urin which can take just one drop of blood or urine and using water loving paper channels with waxed water hating walls. They route the blood to the correct part of the paper, capillary action splits it up and reroutes to different test zones. Each one contains different test and hence change color based on the test results.

At the human level, gravity and intertia are king because of our big size. But in the world of small, the surface tension and viscosity are the king. Viscosity is the measure of how hard it is for one layer of fluid to slide over another layer. Raw milk contains fat globules, some small and some large. The fat globule is buoyant (its weight is less than the milk space it takes) so it slowly floats upward but in floating upward it has to slide over the liquid on top, so this is the viscosity. The smaller the fat globule is the more viscosity it will experience. when these globules are moving to the top, they meet multiple globules and they will join together. As they get bigger, it is easier for them to rise.


Equilibrium - A moment in time 

Time is important in the physical world because the speed at which things happen matters If you do something at twice the speed, you sometimes get the same result in half the time, but sometimes you get a completely different result. This is useful and we can use to control our world. A dam placed to control the water flow will provide a barrier to water moving from top of a hill to the bottom and it will do useful work for us.
When it comes to harvesting energy, all we are doing is to put a few obstacles to energy that was already on its way from somewhere to somewhere else. The physical world will always move towards equilibrium, but we can sometimes control where the nearest equilibrium is and how quickly something in the world can get there. By controlling the flow, we can control the timing of energy release. Then we make sure it flows through the artificial obstacles on its way towards equilibrium, it does something useful for us. We don't create energy or destroy it. We just move the goalposts and divert it for our own use.

By choosing the flow towards equilibrium happens and the speed of that flow, we give ourselves enormous control over this world. But it is not always the case that things reach equilibrium and stay there. When they are going very fast at the balance point, they will just keep going and fly right through. This opens the door towards new set of phenomena.

When you're walking with tea in a mug, the tea sloshes around on the walls for the mug.  Since we are moving forward, the tea is also pushed forward and it accumulates on one side of the mug but gravity pulls it down, so the same process is repeated on the other side. There is a fight between the gravity and the momentum of tea. This causes the sloshing around. The speed at which this happens is called the frequency - the natural frequency. The speed at which we are walking equals the natural frequency the sloshing increases a lot due to resonance. Since our walking is exactly travelling in the way the sloshing is happening, it adds to it and the solution is to slow down. That way the sloshing frequency is faster than our walking and it doesn't increase the sloshing. Everything that swings has a natural frequency. It is fixed by the relationship between how hard the pull to equilibrium is and how fast things are going when they get there.

Life itself started at point where alkaline water meets acidic water from underwater sea vents to the sea. Some organism stayed at the gate and controlled the energy release from the vent and used for its own use. Every living system is the same. It takes energy from some place, does some transformation and it uses that to grow, create copies of itself. As long as energy is flowing the body injects the system which moves it from equilibrium doing useful work. When everything is in equilibrium, life ceases to exist.

Waves - Making waves

A wave is a regular moving shape that carries energy. Waves transport energy but they don't move anything (?) .
Waves have a frequency and a wavelength. There are waves of different types, water waves, light waves, sound waves etc. Humans have also created waves for our own communication.
Waves are interesting because the environment through which they are passing changes them. They can be reflected, refracted or absorbed.
We can see things because light is reflected from it. Some items bend the light and that is called refraction. Diamonds slow down and bend the light and reflect it and that is why they are so shiny. We can see transparent substances as we feel something is messing up with the light coming from there. And ocean water is blue because even though the light coming in has all the colors, the water absorbs all the colors just leaving blue which can travel over long distances. That is why tap water doesn't have color but enough water collected in lake starts to have color.

Hot objects send out light at some wavelengths more than the other. So based on the wavelength of light sent out, we can say the temp of the object.
It is 1800 F - bright red, 4900 F - yellow, 7200 F - white hot. We can only see visible red but there is also other waves called infra red which we cannot see but these are the waves that are heating up the bread in a toaster. Anything above absolute zero, converts some of the heat energy into light. And that is why we are visible to an infrared camera because we are giving out light too!

Also each wave is different and they don't interfere with each other light waves is not affected by piano sound nor is sound wave affected by disco lights. Each organism is adapted to see and hear some frequencies and makes use of them. Water is a good carrier of sound, but most of the light is absorbed by the water. Longer wavelengths travel faster. Radio waves as very long wavelength light(electromagnetic) waves.

To send information in the waves, we have to modify the waves somehow to create a pattern that the receiver can decode and understand. Radio waves can travel much farther than flares or sounds because the upper ionosphere reflects the radio waves so we can communicate with radio waves (light) even though there is no line of sight between the sender and receiver.  While sending messages using waves there should be a way to separate the waves so that messages are intelligible. The solution is that each wavelength is separate. Red light has a higher wavelength than blue light. If you write something with red light, it is not visible under a red light. But if you use a blue light you can see it. So if you look for specific wavelength, nothing else interferes with that message.

The atmosphere absorbs the long wavelength and ultraviolet light waves but lets the visible light across. When the earth is heated but they light waves carrying the energy from the sun, the earth will radiate the heat out as infra red rays. The co2, methane and other gases in the atmosphere will absorb these infra red rays and once they become heated, they will radiate the infrared back to earth. Because of this greenhouse effect earth's temperature is more than it should be and allows liquid water to exist on earth.
In the mobile phone, each call has different wavelength within a base station cell. If we move from one station to other, our wavelenghts change but are unique within that cell so they don't interfere with each other's conversations. With waves we have shrunk the distance.

Transfer of energy  - Why Don't ducks get cold feet?

Everything is made of atoms. Each atom is always wriggling.we cannot be sure about one atom but statistically we can predict what is happening with atoms.
Water moves from solid to to liquid to gas at fairly common temperatures. When something is liquid, the atoms can jiggle around and move around easily, but when it is solid they have to be fixed in place. With glass, we cool it fast so that they can't get arranged into crystalline structures. But water when it becomes ice has specific crystalline structure. Water density decreases as it solidifies so it floats.

Heat flows from hot to cold items. And don't avoid getting cold feet because by the time the blood flows to the feet, the heat is transferred to the veins and the blood is very cold by the time it comes into the legs. So they don't feel cold because there is very little loss of heat through blood.

We can hold hot stuff in ceramic mugs, but not metal mugs because metals have free electrons which transfer the heat from the hot substance inside to the metal, so the mug will become hot. But for ceramic, there are no free electrons so heat has to be transferred by the whole atoms and most of it is lost to air before it is transferred. That is why metals are good for cooking utensils. Also refrigeration helps to preserve the vegetables because it cools down so the energy needed for microbes to decay them is not available.


Angular Momentum - Spoons, Spirals & Sputnik.
Our civilization is full of things that spin - clothes dryer, discuss throwers, gyroscopes. Earth itself spins around the sun. Spinning is important because it lets you do lot of things, sometimes involving forces and oodles of energy without going anywhere.

If you're moving in a circle something must be pushing or pulling you inward, forcing you to change direction constantly. If that force is taken away, you travel in a straight line. The faster you're going, the stronger the push has to be as more force is required to change direction. It is easier to balance the bicycle or bike when we are driving is because of the rotation, it needs more force to change direction and inertia of the bike is greater than the force of gravity. The same principle applies to the gyroscope so it can maintain its position as long as it is spinning.

The solid particles will try to maintain shape while spinning but liquid and gases are free to separate out. So the clothes in a dryer while spinning have to maintain their shape, the water separates out and moves into the holes in the dryer. Similarly when milk is spun, the cream in the milk separates out very faster compared to leaving it aside for gravity to do the work. Similar principle is used in centrifuging the various components of blood while testing blood.
The fact that when the force stops that is pulling or pushing things in a circle goes away whatever is moving will try to move in a straight line. This principle is used in trebuchet, to throw heavy weights at enemy castles. A satellite also spins around the earth by using gravity to keep spinning. It will be moving so fast sideways that by the time it falls, the earth would have curved under it keeping the distance constant and as long as it keeps spinning it will not fall. The spin is faster away from axis rather than closer to axis. So if we release a satellite from near the equator it is 5% faster than when it is released from poles.
We feel dizzy when we spin around is because there is a liquid canal behind our ears and our brain uses it to orient itself in space. Since it is a liquid and keeps spinning even after we stop spinning, our brain takes time to reorient and it feels we are still spinning. When a boiled egg and raw egg are spun the raw egg keeps moving for more time because of the same reason, the liquid inside it doesn't need to maintain its shape and it will keep spinning for more time than the solid egg. This rotational motion is used in a flywheel to store energy and use to convert it to electricity when required.

Electromagentic effect - When opposites attract.

Magnets have a force field where they can push or pull on metallic objects. They have two poles north and south and one magnet's north pole can attract another magnets south pole. When you bring a metallic substance near a magnet, the magnet will align the internal magnetic fields which were randomly aligned to a uniform alignment. The north pole will repel the field such that north of the metals is on the other side of the magnet and south pole is closer to the magnet. So these metals are attracted to the magnet. The magnets are very important in generation of electricity as we will see.

Everything is made of atoms having a nucleus made of protons and neutrons at its center. These have a positive charge. There is a swarm of electrons around this nucleus attracted because of its negative charge. But the electrons are very nimble. When two things touch each other, the electrons hop from one to the other. But they can't easily hop on to some substances called insulators. So when we are wearing rubber shoes which are not good conductors, the electrons are not flowing outside of our body, but from other things like coat, bags etc are on our body. So when our hand touches some metallic item, they all jump from our body to the metallic item via our hand and it doesn't seem pleasant. This is an electrostatic discharge or a shock. There are some items which are good conductors (they have feel electrons or lack electrons and are ready to give/take more), insulators(which have enough and are not ready to give/take more) and hence block the flow of electricity. With these things we can control the flow of electrons and use it for our use.

A battery is an ingenious device. A lead acid battery has two lead plates in an acid. One plate doesn't have electrons and it is ready to take electrons and another plate has lot of electrons to give. these plates are pushed as far as they can go and are placed in acid, where the electrons can flow only in one direction. When the other ends of the circuit are connected with a wire, the circuit is complete and causes the electrons to flow from one end of lead to the other end. The voltage differential caused is the energy the electrons have to get rid of before reaching the other side and can be used to do useful work. If this battery is connected to a camera circuit, it can take photos and some of it is lost as heat.

So the voltage is the amount of energy available and if you have lot of electrons to provide the energy to something very quickly, it can takethe energy fast. This amount of electrons flowing is called current. This current from a battery is called DC and the electrictity from plugs is called AC (the direction of the flow changes multiple times). This is more efficient to lessen the heat loss.

A magnetic field can be used to control the direction of the current and to control magnetism we can use electricity. These two are parts of the same thing. This is how the CRT TV works. There is an electron gun which sprays electrons from one end to the monitor. Then there are guidance magnets with signals from the antenna which bend these electrons creating the patterns we see on TV. And the electron guns scans the lines multiple times a minute (refresh).

Put an electron in electric field - it will speed up or slow down. Put an electron in a magnetic field, its path will curve. This is how TVS worked.

When electricity is passed through a coil, it will be magnetized. This creates an electromagnet which then turns off as soon as the magnet is de magnetized. Similarly electricity is produced when a magnet is moved around a coil through induction. When a moving magnet is placed in a magnetic field, it will align itself in the direction of the magnetic field. This is the principle of a compass. Earth has a magnetic fields. The poles of this magnet change based on the currents flowing in the earths core and the aviation industry has to correct for this changes.

This was a great book that made us say aha.. so many times and enlightened me on various physical phenomenon in a very entertaining and exciting way. Great book. highly recommended. 

Monday, June 18, 2018

A home robot - what does it need ?

In our personal life, we spend a lot of time organizing, cooking and cleaning. If it is possible to automate these on any level even if it is not intelligent, it can save at least one hour a day easily.

So I want to work on making robots do these things in narrow domains. The state of the art ai is just now learning to pick all sorts of things.

For organizing, an ai has to identify the homes for each thing, grab the thing and put it there. If the space is full, it has to think of an alternate space and put it there. It has to understand when something can be discarded and then discard it. So it should be able to move and access all the storage spaces and also identify what thing is important/ unimportant and make decisions accordingly.

For the cleaning ai, it should also have capabilities to handle solids and liquids. It has to understand how tightly or softly it has to grip stuff - what is clean and what is unclean and how to get from unclean to clean. It should be able to access cleaning utilities and also know how to use them. It should also know where such stuff is available in a home and should be able to access them.

Cooking is even more complex and would need to understand the basics of cooking. It should also handle different kinds of stuff (hard, soft, wet, dry, powdery, thin, thick etc). It should also be able to identify textures to know if something is done or not even to follow existing recipes. It should be able to cut, mix, saute and other cooking related actions.

All of these should understand the humans in the house, interact with them, follow their directions but not do them any harm.

I think each of these increase in complexity and it will be interested to see how each will be approached and solved. I am interested in these use cases and will be keeping an out on the advances here. 

Monday, June 11, 2018

How to do good scientific research work - Advice from Richard Hamming

Richard Hamming is an american mathematician who worked in Bell labs and discovered error correcting codes and digital filters among other things. He gave a talk called 'You and Your Research' about how to do great work. It is worth reading the original fully so go ahead and read it here.

Here are some interesting points from that lecture.

He talks about luck here.. 

Once in a while a person does only one thing in his whole life, and we'll talk about that later, but a lot of times there is repetition. I claim that luck will not cover everything. And I will cite Pasteur who said, ``Luck favors the prepared mind.'' And I think that says it the way I believe it. There is indeed an element of luck, and no, there isn't. The prepared mind sooner or later finds something important and does it. So yes, it is luck. The particular thing you do is luck, but that you do something is not.


The importance of courage and confidence. 


One of the characteristics of successful scientists is having courage. Once you get your courage up and believe that you can do important problems, then you can. If you think you can't, almost surely you are not going to. 

On turning defects to assets


I think that if you look carefully you will see that often the great scientists, by turning the problem around a bit, changed a defect to an asset. For example, many scientists when they found they couldn't do a problem finally began to study why not. They then turned it around the other way and said, ``But of course, this is what it is'' and got an important result. 
Talking about compounding of learning and how having the drive does it


``Knowledge and productivity are like compound interest.'' Given two people of approximately the same ability and one person who works ten percent more than the other, the latter will more than twice outproduce the former. The more you know, the more you learn; the more you learn, the more you can do; the more you can do, the more the opportunity - it is very much like compound interest. I don't want to give you a rate, but it is a very high rate. Given two people with exactly the same ability, the one person who manages day in and day out to get in one more hour of thinking will be tremendously more productive over a lifetime. 

On being committed and thinking about a problem till you find a solution


when you have a real important problem you don't let anything else get the center of your attention - you keep your thoughts on the problem. Keep your subconscious starved so it has to work on your problem, so you can sleep peacefully and get the answer in the morning, free.


On thinking and attacking the important problems in the field. 


If you do not work on an important problem, it's unlikely you'll do important work. It's perfectly obvious.You can't always know exactly where to be, but you can keep active in places where something might happen. And even if you believe that great science is a matter of luck, you can stand on a mountain top where lightning strikes; you don't have to hide in the valley where you're safe. 
Most great scientists know many important problems. They have something between 10 and 20 important problems for which they are looking for an attack. And when they see a new idea come up, one hears them say ``Well that bears on this problem.'' They drop all the other things and get after it.The great scientists, when an opportunity opens up, get after it and they pursue it. They drop all other things. They get rid of other things and they get after an idea because they had already thought the thing through. Their minds are prepared; they see the opportunity and they go after it. Now of course lots of times it doesn't work out, but you don't have to hit many of them to do some great science. It's kind of easy. One of the chief tricks is to live a long time!

On generalizing the results of your work


You should do your job in such a fashion that others can build on top of it, so they will indeed say, ``Yes, I've stood on so and so's shoulders and I saw further.'' The essence of science is cumulative. By changing a problem slightly you can often do great work rather than merely good work.

You have to sell your work


You have to learn to write clearly and well so that people will read it, you must learn to give reasonably formal talks, and you also must learn to give informal talks.

on self-delusion


 self-delusion in humans is very, very common. There are enumerable ways of you changing a thing and kidding yourself and making it look some other way. When you ask, ``Why didn't you do such and such,'' the person has a thousand alibis. If you look at the history of science, usually these days there are 10 people right there ready, and we pay off for the person who is there first. The other nine fellows say, ``Well, I had the idea but I didn't do it and so on and so on.'' There are so many alibis. Why weren't you first? Why didn't you do it right? Don't try an alibi. Don't try and kid yourself. You can tell other people all the alibis you want. I don't mind. But to yourself try to be honest.

Summarizing 

In summary, I claim that some of the reasons why so many people who have greatness within their grasp don't succeed are: they don't work on important problems, they don't become emotionally involved, they don't try and change what is difficult to some other situation which is easily done but is still important, and they keep giving themselves alibis why they don't. They keep saying that it is a matter of luck. 

Go read the whole thing. It is very interesting and packed with gems!

Pair it up with the process for inventing anything by Claude Shannon, the inventor of information theory here

Monday, June 4, 2018

Improving the design of google home app

In the current design of the app, the number of quotes is hardcoded in the app. But because of that each time, I add new quotes I will have to redeploy the app. This will cause reluctance in me to redeploy and change the quotes - which means it has difficult maintenance.

To fix this, I want to modify it such that either I will set the total count as a runtime configuration which the app can pick up automatically. I tried using the firebase runtime configuration. But it seems for this config change to take effect I will need to redeploy the function. This seems to not fix any of the issue.

Researching further I found a solution to this problem by adding a random float to each quote and when querying just querying for quote greater than a specific random number. That way I will get a different random quote each time and I don't need to redploy the app each time. I haven't made the changes yet and redeployed the app but this should fix the maintenance nightmare and will make it easy for me to add new quotes to the app. 

Thursday, May 31, 2018

Developing a Google Assistant app with NodeJs, Dialogflow & Firebase Functions - Part 4

This is part of a multipart series. 

Part 1

Part 2

Part 3

This is part 4. 


Test the app

Once you have entered the fulfillment url, you can test either in the dialogflow console on the right side of the console or in google assistant.
To test in google assistant, click on ‘Integrations’ in the dialogflow console and select ‘Google Assistant’. On the next screen select ‘test’.
It will open up a test window like this.


We can test each of your intent here. We can also see the request and response and any errors we might encounter.
For e.g. in this app, when I ask to ‘Talk to LitInspire’ , it will retrieve a quote and display on the simulator. It will also read the quote with appropriate pauses as specified in the SSML. It will also show the suggested replies. We can click on the suggested replies to test the next part of the conversation.
We can also view logs by following the link on the simulator. We can see the request and response and exception stack traces also. In the google cloud platform logs console, you can select the right application (actions on google app or the cloud function) to see the logs at the appropriate level.
Once we have verified the happy paths, the fallback paths and conversations and we are satisfied with the result, we can submit the app for approval.
Submit the app for approval
To prepare the app for approval, we have to provide more details so that the app can be visible in the directory. We can provide a short description, more details and invocation phrases to talk with the app.
We also have to provide images in two different sizes. I used the canva app to create these images. We have to provide a banner image and also a logo


Next we have to create a privacy policy.
Google provides a sample doc which we can copy and edit to create our own privacy policy. You can check my privacy policy here.

Lit Inpsire Privacy Policy.

Next we can select the category the app belongs to and other details helpful for review. We can also select the countries where the app is available and also the devices which can use the app. Then we can submit the app for review.
The app will be reviewed and if there are no issues, the app will be deployed to production. We will get an email informing us of the same. That completes the process of building and deployment.
Summary
In summary we have seen how to design, build and deploy an actions on google app. To summarize the steps, they are
Design a conversation
Create a project and define actions
Build a fulfillment service
Test the actions
Prepare for deployment and submit.
Thanks for staying with me. Hope this was helpful. Feel free to reach out if you have questions. You can comment here or reach out to me via twitter @thisisananth

Wednesday, May 30, 2018

Developing a Google Assistant app with NodeJs, Dialogflow & Firebase Functions - Part 3

This is part of a multipart series. 
This is part 3. 

Setting up the project.
  1. Install firebase-cli. In many cases, new features and bug fixes are available only with the latest version of the Firebase CLI and thefirebase-functions SDK. So first install the latest versions of firebase-functions and firebase-admin
npm install firebase-functions@latest firebase-admin@latest --save
npm install -g firebase-tools
2. Once the dependencies are installed, do the following to initialize the project
a. Run firebase login to log in via the browser and authenticate the firebase tool.
b. Go to your Firebase project directory.
c. Run firebase init. The tool gives you an option to install dependencies with npm. We will select functions for the function and hosting for any public files. The init tool will guide through various options to choose the language, and the initialization options for functions and hosting

myproject
 +- .firebaserc    # Hidden file that helps you quickly switch between
 |                 # projects with `firebase use`
 |
 +- firebase.json  # Describes properties for your project
 |
 +- functions/     # Directory containing all your functions code
      |
      +- .eslintrc.json  # Optional file containing rules for JavaScript linting.
      |
      +- package.json  # npm package file describing your Cloud Functions code
      |
      +- index.js      # main source file for your Cloud Functions code
      |
      +- node_modules/ # directory where your dependencies (declared in
                       # package.json) are installed


Once the project is initialized, we can develop our functions.
This is the package.json for the dependencies we have. We are using actions-on-google, firebase-admin, firebase-functions and google cloud datastore. We are using es-lint for linting. Code linting is a type of static analysis that is frequently used to find problematic patterns or code that doesn’t adhere to certain style guidelines.
{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint --fix \"**/*.js\"",
    "start": "firebase serve",
    "deploy": "firebase deploy",
    "test": "npm run lint"
  },
  "dependencies": {
    "@google-cloud/datastore": "1.1.0",
    "actions-on-google": "^2.0.0",
    "ajv": "^5.0.0",
    "firebase-admin": "^5.11.0",
    "firebase-functions": "^1.0.0"
  },
  "devDependencies": {
    "eslint": "^4.19.1",
    "eslint-config-google": "^0.9.1"
  },
  "private": true,
  "version": "0.0.1"
}
Next let us look at the functions. When user invokes the app by name or google matches the request to any of our intents, then it sends a request to our app with the intent and other parameters. In our function we will implement the callback function for each intent. 
'use strict';
//Initialize libraries
const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const Datastore = require('@google-cloud/datastore');
const {
  SimpleResponse,
  BasicCard,
  Image,
  Suggestions,
  Button
} = require('actions-on-google');
// Instantiate a datastore client
const datastore = Datastore();
  
  const app = dialogflow({debug: true});
app.middleware((conv) => {
    
  });
//Setup contexts
const Contexts = {
    ONE_MORE: 'one_more'
  };
app.intent('quit_app', (conv) => {
    conv.close("Have a good day! come back again. Bye!");
  });
app.intent('start_app', (conv) => {
    conv.contexts.set(Contexts.ONE_MORE,5);
    const initMessage = ` Welcome to LitInspire. With great quotes and inspiring passages, I will inspire you.`;
return  getQuote().then((entity)=>{
         return getMessageFromQuote(entity,initMessage,conv);
    });
      
  });
app.intent('one_more_yes', (conv) => {
    conv.contexts.set(Contexts.ONE_MORE,3);
      const initMessage = `Great! Here is another one.`;
         
    return  getQuote().then((entity)=>{
      return getMessageFromQuote(entity,initMessage,conv);
  });
      
  });
app.intent('one_more_no', (conv) => {
    conv.close("Hope you're inspired and ready to take on your challenges. Have a good day and come back for more.");
});
app.intent('Default Fallback Intent', (conv) => {
    console.log(conv.data.fallbackCount);
    if (typeof conv.data.fallbackCount !== 'number') {
      conv.data.fallbackCount = 0;
    }
    conv.data.fallbackCount++;
    // Provide two prompts before ending game
    if (conv.data.fallbackCount === 1) {
      conv.contexts.set(Contexts.ONE_MORE,2);
      return conv.ask(new Suggestions('Yes Please', 'No thanks'), new SimpleResponse("Would you like to hear a quote?"));
    }else if(conv.data.fallbackCount === 2){
      return conv.ask(new Suggestions('Yes Please', 'No thanks'), new SimpleResponse("Welcome to LitInspire. With great quotes and inspiring passages, I will inspire you.Would you like to hear a quote?"));
    }
   return conv.close("This isn't working.Have a good day. Bye! ");
});
function getRandomNumber(){
return  Math.floor((Math.random()*num_quotes)+1);
}

function buildReadableQuoteFromEntity(entity){
  let readableQuote =  entity.quote + 
     ` This was said by ` + entity.author + ` `  ;
     if(entity.comments){
       readableQuote +=  entity.comments + ` `;
     }
     return readableQuote;
}
function getViewableQuote(entity){
  let viewableQuote =  entity.quote + 
     `.This was said by ` + entity.author + ` `  ;
     if(entity.comments){
      viewableQuote +=  entity.comments + ` `;
     }
     return viewableQuote;
}
function getEndingMessage(){
return `  
" clipBegin="10s" clipEnd="13s">Consider the quote!
Do you want to listen to another quote?`;
}
function getEndingMessageText(){
  return `.Do you want to listen to another quote?`;
  }
function getMessageFromQuote(entity,initMessage,conv){
  return conv.ask(new Suggestions('Yes Please', 'No thanks'), new SimpleResponse(initMessage),
  new SimpleResponse( {text: getViewableQuote(entity) + getEndingMessageText(),
speech: ` ` +  buildReadableQuoteFromEntity(entity)   + getEndingMessage() + `   ` }));
 }
function getQuote(){
  return new Promise(((resolve,reject) => {
    let randomQuoteNum = getRandomNumber();
  console.log("the id of the quote is: quote_"+randomQuoteNum);
  const key = datastore.key(['quote', 'quote_'+randomQuoteNum]);
  console.log("Querying datastore for the quote..."+key);
  let readableQuote = '';
  datastore.get(key,(err,entity) => {
    if(!err){
      console.log('entity:'+entity.quote);
    resolve(entity);
    }else{
     reject(console.log('Error occured'));
    }
  });
  }));
}
// HTTP Cloud Function for Firebase handler
exports.InspireMe = functions.https.onRequest(app);
Let us look at the intent handler for the start_app. First since we are in the app, we want the user to be able to ask for multiple quotes. Since it is in the one_more context, we will set the context to one_more. Next we need to get a quote and return it. We have the quotes stored in google cloud datastore. So we make a call to the datastore to return a random quote. 
app.intent('start_app', (conv) => {
    conv.contexts.set(Contexts.ONE_MORE,5);
    const initMessage = ` Welcome to LitInspire. With great quotes and inspiring passages, I will inspire you.`;
return  getQuote().then((entity)=>{
         return getMessageFromQuote(entity,initMessage,conv);
    });
      
  });
The getQuote() function returns a promise with the quote. And in the intent handler, we use the .then() function of the promise to build the message from quote and return it. In the getMessageFromQuote() method, we can see join together multiple responses and return it. Here we use the ask method, which tells the user our quote and waits for the user response. And in this method we can pass suggestions, and atmost two simple responses. The simple response converts the text to speech. We are using the SSML (speech synthesis markup lanuguage) to specify how to generate the speech. With SSML, we can specify where to pause, and add music to the text. There are other types of responses also like Basic Card, Image, Button and List (Carousel) responses. 
function getMessageFromQuote(entity,initMessage,conv){
  return conv.ask(new Suggestions('Yes Please', 'No thanks'), new SimpleResponse(initMessage),
  new SimpleResponse( {text: getViewableQuote(entity) + getEndingMessageText(),
speech: ` ` +  buildReadableQuoteFromEntity(entity)   + getEndingMessage() + `   ` }));
 }
The two other things to see are how to end the conversation and how to handle unknown input. In the below function you can see that when the user says no, we use the conv.close() function to end the conversation with a message. 
app.intent('one_more_no', (conv) => {
    conv.close("Hope you're inspired and ready to take on your challenges. Have a good day and come back for more.");
});
When the user provides unknown input, google invokes the default fallback function. Lets look at that. We are giving options to the user two times whether to listen to a quote or not. And if the user doesn’t give a valid response even after two times, we are ending the conversation. 
app.intent('Default Fallback Intent', (conv) => {
    console.log(conv.data.fallbackCount);
    if (typeof conv.data.fallbackCount !== 'number') {
      conv.data.fallbackCount = 0;
    }
    conv.data.fallbackCount++;
    // Provide two prompts before ending game
    if (conv.data.fallbackCount === 1) {
      conv.contexts.set(Contexts.ONE_MORE,2);
      return conv.ask(new Suggestions('Yes Please', 'No thanks'), new SimpleResponse("Would you like to hear a quote?"));
    }else if(conv.data.fallbackCount === 2){
      return conv.ask(new Suggestions('Yes Please', 'No thanks'), new SimpleResponse("Welcome to LitInspire. With great quotes and inspiring passages, I will inspire you.Would you like to hear a quote?"));
    }
   return conv.close("This isn't working.Have a good day. Bye! ");
});
Now that’s it. We have written intent handlers for all possible intents and fallback handler also. Now this is ready for testing. 
Use the firebase deploy command to deploy the function to firebase functions. 
firebase deploy
Once deployed you will get a functions url. Go to the dialogflow console and go to the fulfilment menu item and provide the function url as the fulfilment url. 

Enter fulfilment