Monthly Archives: September 2017

Chatbots and more

What are chatbots? This is not the regular programming post, but more of a discussion post and where we are heading with our technology. Alexa, Google Home, Cortona, and the number of personal assistants are available at our perusal. With these kinds of products, we are slowly evolving into artificial intelligence-driven technology. A lot of manual labor jobs might be in danger in the near future. Politics aside, I am more interested to understand this topic from technology and humane perspective. While we still struggle with a lot of other ethical issues with existing technology, AI will only create social conundrums.

What I want to discuss in this post, is more about chatbots. You can think of this as a scribbling post. I am just bringing some ideas forefront to build a chatbot using java.

What are chatbots?

Chatbots are a crude version of personal assistants. Personal assistants help you in many ways, in-process saving you time, providing you leisure. The simplest version of these chatbots is those that answer your questions like “What’s the weather like today?”, a chatbot will connect to a weather website to find out today’s weather and then answer you accordingly. Similarly, on an eCommerce site, I can ask a question by typing “Where can I find this book?”, the chatbot will answer “In literature and short stories section”. A chatbot can also help build customer support, taking away the traditional customer support people. In a more advanced version, the same chatbots can build a library based on your likings, dislikings, answers, and provide more options for lifestyle.

Wikipedia definition says

A chatbot is a computer program that conducts a conversation via auditory or textual methods.

Chatbots are part of Artificial intelligence that has been popularized these days.

Design of chatbots

In this article, we will not be showing any code for chatbots, but we will be building chatbots in the next post. This is a post where we bring the idea of a chatbot into the design. As we discussed the definition of a chatbot, we will be building an agent that will chat with us in a natural language that we use for daily communication.

Me: “Hi Mr. Chatbot, how are you today?”

Mr. Chatbot: “I am fine, Mr. Mali. Thank You”

Me: “What’s the day today?”

Mr. Chatbot: “It’s Wednesday today.”

This is an example of a conversation about how a chatbot would answer. We will build a database that will have natural language processing ability to find out what question has been asked and based on that answer the question as accurately as possible. This chatbot is an experimental build-up.

Does it mean it can falter? Glad you ask that it definitely means it can answer erratic. But it’s ok in our experimentation world, even Google home had his off days.

We will need a chat engine and we will be using plain English to type our messages. We will use AIML (Artificial Intelligence Markup Language) to build this chatbot.

In conclusion, I would provide the implementation of this chatbot in the next few posts. We will have more discussion about chatbots in future articles. If you enjoyed this post, subscribe to my blog here.

 

References

  1. AIML
  2. Chatbot

 

Design Pattern – Adapter Pattern – Part VII

Till now, we have discussed all creational types of design patterns. In this post, we will be creating a demo about structural design patterns. In this series, our first design pattern is Adapter design pattern. As said, this design pattern is structural design pattern. This design pattern combines the capabilities of two independent interfaces. It basically acts like a bridge between two incompatible interfaces.

Easiest example to understand adapter pattern in real life is the electric socket in different continents provide different voltages. A traveler from Asia can use an adapter in Europe to get 240 V of electricity for electronic devices.

When to use Adapter Design Pattern?

Firstly, when a client expects different interface than available, at that time adapter pattern can help to convert a interface of a class into another interface that the client can use. However, Adapter pattern allows reuse of lot of code and it is one of the major reasons why it is most favorable among software engineering. Similarly, a real example you would find in JDK libraries of InputStreamReader and OutputStreamWriter.

How to use Adapter Design Pattern?

So in this implementation, we will show how to use Adapter design pattern. We have a traveler from Asia traveling in Europe, he wants to use his electronic device which needs 240 V of electricity from socket, but Europe only provides 120 V of electricity. We will design an adapter class that will convert 120 V of electricity to 240 V of electricity.

Our target class or client class is AsiaSocket, as shown below:


package com.betterjavacode.designpatterns.adapterexample;

public class AsiaSocket {

    public void provideElectricity() {
        System.out.println("Provide electricity of 240 V");
    }
}

It’s a simple class with a method provideElectricity.

Our adaptee class is EuropeSocket which implements an interface IEuropeSocket as shown below:


package com.betterjavacode.designpatterns.adapterexample;

public class EuropeSocket implements IEuropeSocket {

    public void getElectricity() {
        System.out.println("Get electricity of 120 V");
    }

}

Secondly, we will implement an Adapter class that will provide adapter between Europe and Asia Socket classes. This will look like below:


package com.betterjavacode.designpatterns.adapterexample;

public class EuropeAsiaAdapter implements IEuropeSocket {

    AsiaSocket asiaSocket;

    public EuropeAsiaAdapter(AsiaSocket asiaSocket) {
        this.asiaSocket = asiaSocket;
    }

    public void getElectricity() {
        asiaSocket.provideElectricity();
    }

}

This class has a constructor to instantiate AsiaSocket and implements IEuropeSocket interface.

Now in our demo class, we will show how to use this adapter pattern.


package com.betterjavacode.designpatterns;

import com.betterjavacode.designpatterns.adapterexample.AsiaSocket;
import com.betterjavacode.designpatterns.adapterexample.EuropeAsiaAdapter;
import com.betterjavacode.designpatterns.adapterexample.EuropeSocket;
import com.betterjavacode.designpatterns.adapterexample.IEuropeSocket;

public class AdapterDemo {

    public static void main(String[] args) {
        EuropeSocket es = new EuropeSocket();

        AsiaSocket as = new AsiaSocket();

        IEuropeSocket europeAsiaAdapter = new EuropeAsiaAdapter(as);

        System.out.println(" Electricity in Asia ");
        as.provideElectricity();

        System.out.println(" Electricity in Europe ");
        es.getElectricity();

        System.out.println(" Electricity for Asia in Europe");
        europeAsiaAdapter.getElectricity();

    }

}

Therefore, if you run this demo class, the output will show that we will be getting electricity of 240 V for Asian electronic devices in Europe.

Download

In conclusion, we showed how to use the Adapter pattern. The demo code will be available on GitHub repository here.

References

  1. Adapter Design Pattern
  2. Adapter Design Pattern in Java

Spring Boot and Microservices

Over the last few months, I have worked on Spring Boot and tried to collect my knowledge around Microservices. I was discussing lot of this with my friends and one friend did suggest me to write a book. Initially I was little hesitant to write a book about something I was learning. But also the whole point of learning is to teach someone at some point of time.

Spring Boot and Microservices

So I took this as a challenge to write a book about Spring Boot and Microservices. Initially I created series of posts and posted on this blog to see how much it could benefit people. And now to make it easy for every one, I have collected all this information and wrote an ebook. This ebook Spring Boot and Microservices is free to download. I hope this will help people to understand the concepts of Microservices and my example can help them to head start the building their projects.

Spring Boot and Microservices

What’s next?

Where do we go from here? There are a lot of questions about the next strategy for Spring Boot and Microservices. What I have covered in this book, is a tiny portion of big picture. There is lot of options like scaling the service, adding a health check for the service, and deploying the service on the cloud with automation. But for right now, I just want to take a break from thinking about this and I will come up with a next plan soon.

Till that time, you can download, read, and send me your feedback about the book. It would be great if you could leave a review for the book here.

If you have any questions, please leave your comments on this blog and I will try my best to answer them.