Skip to content

Building a Rule Based Weather Bot

Amitha R edited this page Oct 13, 2018 · 11 revisions

Rule Based Weather Bot

How do Rule Based Bots work?

  • Human Input -> Match input to a pattern/rule -> Extract intent and data -> Process and create a response -> Human;
    • Intent - Subject of the conversation.
    • Entities - Actionable data components that the bot has to extract.
    • E.g - Will it rain in New York tomorrow? , Will it rain tomorrow in New York?
      • INTENT - Weather Forecast.
      • ENTITIES - rain, New York, tomorrow.
      • rain - weather | New York - location | tomorrow - time.
      • City :: New York | Weather :: Rain | Time :: Tomorrow.

Input (Intent / Entities) -> Intent Processor ---> Weather ---> [Weather API] ---> Weather --> Response Parse --> Response (Yes, there will be rain in New York tomorrow.)

Command Line Bot / Terminal Bot

  • Expected output - node app.js (vanillaChatbot code);

Hi

  • Hi to you too!

what is the weather like in bangalore

  • Let me check...
  • Right now, it is partly cloudy in Bengaluru, India. It is quite warm at 29 degrees Celsius.

what is it like in new york

  • Let me check...
  • Right now, it is cloudy in New York, United States. It is pretty cold at 10 degrees Celsius.

will it rain in tokyo tomorrow

  • Let me check...
  • Yes, there will be Rain tomorrow in Tokyo, Japan

will it rain in moscow day after tomorrow

  • Let me check...
  • No, it will be Sunny day after tomorrow in Moscow, Russia

bye

  • Have a great day!

Application Structure

Interactive Terminal interface (cmd) ---> Patter Matcher --[--> Regex Pattern Dictionarry -->]--Entity Extractor ---> Yahoo weather API ---> Response Parser --[--> Sentence Formation Helper Dictionary -->] --> CMD.

Pre-Requisite - Nodejs - Runtime environment that uses Javascript to build server-side applications.
  • Create a folder -> npm init - initialize a project - package.json file.

RegEX - Regular Expression

  • Requires 2 forward slashes - / /;
  • pattern - /colou?rs?/ (/ - optional)
  • optionalflages - /colou?rs?/ig
    • i - ignore case, g - global;
  • RegExr - Test Regex expressions.
    • ?- optional - colors, colou?rs, colou?rs?,
    • \b- word boundaries the, \bthe\b (\b - word boundaries)
    • \w- word selection - ad\w - select all words that are ad.
    • \w+ - word selection from the pattern.
    • () -match group - \b(cold|hot|winter)\b
    • pincode - \b\d{3}\s?\d{3}\b - \d - digits. {} - n quantifier. \s - whitespace - 160 061
    • emailid - \b[a-z0-9\_\-\.]+\@[a-z0-9]+\.[a-z\.]{2,24}\b

XRegExp - node Module

Weather API

  • Yahoo Weather API
  • Requires - YQL - Yahoo! Query Language - An SQL like syntax that is used to interact with Yahoo! Weather API.
    1. Get WOEID for the location. (WOEID - Where on Earth ID),
      • select woeid from geo.places(1) where text = "New York"
    2. Query the weather.forecast service
      • select * from weather.forecast where woeid in (select woeid from geo.places(1) where text = "New York")
    3. Parse the JSON response

Pattern Matcher

  • [weather] (hot|cold|rain|summer) in [location] ([a-z]+[a-z]+?) [time] (day after tomorrow | tomorrow | today).

Response Parser

  • Yes/No , getPrefix(ConditionCode, "futture" /"Present"), conditionText(Yahoo), extracted from user input , in , location.city
  • No, it will be Mostly Sunny tomorrow in NewYork.
    • No --- Yes or Now
    • it will be ---- getPrefix(ConditionCode, "futture" /"Present")
    • Mostly Sunny --- conditionText(Yahoo)
    • tomorrow --- extracted from user input
    • New York --- location.city

Summary

  • A simple rules based chatbot.
  • Terminal Based.
  • Used regular expression.
  • Partially scalable.
  • Allowed for creation of intents and extraction of entities.