Nand Kishor Contributor

Nand Kishor is the Product Manager of House of Bots. After finishing his studies in computer science, he ideated & re-launched Real Estate Business Intelligence Tool, where he created one of the leading Business Intelligence Tool for property price analysis in 2012. He also writes, research and sharing knowledge about Artificial Intelligence (AI), Machine Learning (ML), Data Science, Big Data, Python Language etc... ...

Follow on

Nand Kishor is the Product Manager of House of Bots. After finishing his studies in computer science, he ideated & re-launched Real Estate Business Intelligence Tool, where he created one of the leading Business Intelligence Tool for property price analysis in 2012. He also writes, research and sharing knowledge about Artificial Intelligence (AI), Machine Learning (ML), Data Science, Big Data, Python Language etc...

3 Best Programming Languages For Internet of Things Development In 2018
9 days ago

Data science is the big draw in business schools
182 days ago

7 Effective Methods for Fitting a Liner
192 days ago

3 Thoughts on Why Deep Learning Works So Well
192 days ago

3 million at risk from the rise of robots
192 days ago

Top 10 Hot Artificial Intelligence (AI) Technologies
211299 views

Here's why so many data scientists are leaving their jobs
75273 views

Want to be a millionaire before you turn 25? Study artificial intelligence or machine learning
68244 views

2018 Data Science Interview Questions for Top Tech Companies
58677 views

Google announces scholarship program to train 1.3 lakh Indian developers in emerging technologies
56748 views

How to make a chat bot that talks to your Rails database

Mar 30, 2017 | 5076 Views

So its Easter Friday and I come across this cool startup on ProductHunt that turns plain English into SQL queries. So something like "Show users where city is Melbourne" and it'll process that and output something like

SELECT * FROM users WHERE city = 'Melbourne';

I thought this was just the greatest and had to find how all this worked. Non-technical people at my organization could really use something like this. Imagine going into Slack and going

/db_bot how many users signed up this week?

or

/db_bot find the 5 latest users

So my goal by the end of the day turned into How to make a bot that talks to my Rails database.

1. The first thing I would need to do was research Natural Language Processing (NPL). You can basically apply Machine Learning to a phrase and extract meaning. It's all the craze at the moment so I thought surely there has to be a nice free library out there. After weighing up a few options Wit.ai seemed perfect for what I needed.

2. Wit.ai is an interface where you can train your bot with the statements and phrases you'd like your bot to understand. It was actually incredibly easy to get started. Have a look below at what I did but essentially I assigned relevant words in my phrase meaning, meaning that will later be returned as attributes. Have a look at their docs https://wit.ai/docs to learn more.

3. Now that I my initial phrases trained let's jump into some Ruby. Wit.ai has a gem great! Install wit-ruby and jump right in. It's really easy to use. Send a message and get back a hash of all the entities you setup in the previous step. Using all this data is enough for us to construct our database queries.

> Wit.message('Find users where name is Allison')

=> {

"verb" => [

{

"confidence" => 0.9961181593747102,

"type" => "value",

"value" => "return"

}

],

"table" => [

{

"confidence" => 0.9972328497408993,

"type" => "value",

"value" => "users"

}

],

"target_attribute" => [

{

"confidence" => 0.9919267848022522,

"type" => "value",

"value" => "name"

}

],

"search_query" => [

{

"confidence" => 0.9543589657183698,

"type" => "value",

"value" => "allison",

"suggested" => true

}

]

}

4. The rest is fairly straight forward. You assign the entities to variables, apply a little magic and viola you have your first query.

@class = @table.singularize.camelize.constantize

@class.where(@target_attribute.to_sym => @search_query)

Here's example of one I prepared earlier. You can find the source code for it here on Github

> bot = DbBot.message('How many users signed up this week?')

> bot.response

=> "5 users"

> bot = DbBot.message('Find the last 10 users')

> bot.response

=> "There you go"

> bot.collection

=> #<ActiveRecord::Relation [#<User id: 1...

There's still a lot that needs to go into db_bot but it's the foundation for what I'd eventually like to power a Slack bot that our team can use to extract data out of our databases.



Source: Hackernoon