Code Monkey home page Code Monkey logo

talkify-classifier-keywords's Introduction

Keyword based classifier for Talkify

As simple as classification can get, this classifier will allow you to quickly create rules for topic classification based on keywords.

npm version Build Status Coverage Status

Usage

const KeywordsClassifier = require("talkify-classifier-keywords")
const myClassifier = new KeywordsClassifier({
    "food_topic": [
        "food", ["apple", "banana", "strawberry"]
    ]
});

const Bot = require("talkify").Bot;
const bot = new Bot({
    classifier: myClassifier
});

The above example creates a keywords classifier instance with one topic and adds it to the bot instance as its classifier.

This classifier does not need any training as the rules you create form as part of its training when its instantiated.

In the above example, the classifier will only classify a given statement as food when the input contains the word food and optionally one of apple, banana and strawberry.

The syntax requires that all the elements of the main array are evaluated with an and condition while all elements of an array within the main array are evaluated as an or alongside the items in the main array. To make this a bit clearer, consider the following example:

const myClassifier = new KeywordsClassifier({
    "food_topic": [
        "food", "eat", "edible"
    ]
});

In the above example, the classifier will match a given input to food with a lot higher confidence when all of food, eat, edible keywords are matched. It will evaluate them with an and condition.

Alternatively, consider the following example:

const myClassifier = new KeywordsClassifier({
    "food_topic": [
        ["food", "eat", "edible"],
        ["apple", "banana", "strawberry"]
    ]
});

Here, one of food, eat, edible and one of apple, banana, strawberry are evaluated. This means that the following sentence will receive classification with 100% confidence:

I want to eat a banana

while the following will only receive 50% (0.5) confidence in classification:

I want to eat something.

For multiple topics, the classifier will always return classifications in descending order i.e. the classification that the classifier has highest confidence in will be listed towards the top of the array. Consider this:

const myClassifier = new KeywordsClassifier({
    "eat_food": [
        "eat", ["apples", "banana", "mango"]
    ],
    "like_food": [
        "like", ["apples", "banana", "mango"]
    ]
});

For a given sentence:

I like apples

The classifier will return the following classifications:

[
    { label: "like_food", value: 1 },
    { label: "eat_food", value: 0.5 },
]

Classification Graphs

This classifier supports classification graphs. Consider the following graphical arrangement:

[questionClassifier] ======\\
                             ====== [lightOffClassifier]
                             ||
                             ====== [lightOnClassifier]
[commandClassifier] =======//

Here, the questionClassifier and commandClassifier are the top level classifiers while the lightOffClassifier and lightOnClassifier are downstream classifiers to those top level classifiers.

Here's how the implementation might look like:

const questionClassifier = new KeywordsClassifier({
    'question': [
        ["is", "check"], "turned"
    ]
});

const commandClassifier = new KeywordsClassifier({
    'command': [
        ["do", "please"], "turn"
    ]
});

const lightOffClassifier  = new KeywordsClassifier({
    'light_off': [
        ["off"], "light"
    ]
});

const lightOnClassifier= new KeywordsClassifier({
    'light_on': [
        ["on"], "light"
    ]
});

questionClassifier.addDownstreamClassifier("question", lightOffClassifier);
questionClassifier.addDownstreamClassifier("question", lightOnClassifier);

commandClassifier.addDownstreamClassifier("command", lightOffClassifier);
commandClassifier.addDownstreamClassifier("command", lightOnClassifier);

The above example is divided into two parts. In the first part, we're defining all our classifiers. In the second, we're associating the downstream classifiers like lightOffClassifier and lightOnClassifier to the top level classifiers like questionClassifier and commandClassifier using the addDownstreamClassifier method. This method takes two parameters, the name of the topic and the classifier object itself. This, in effect "subscribes" the downstream classifier to the topic that you specify. As you see, we're subscribing the downstream classifiers to the topics that the parent classifiers are producing. To keep things simple, in this case, each of the top level classifiers are only producing a single topic.

Once you've got this all setup, you can wire the classification graph to the bot like so:

const bot = new Bot({
    classifiers: [questionClassifier, commandClassifier]
});

Even though we've got a total of four classifiers in our classification graph, we're only adding two of them. This is because the other two are already linked to the two that are being added to the bot so in effect, by adding two classifiers, we are adding a total of four.

talkify-classifier-keywords's People

Contributors

manthanhd avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.