Git Product home page Git Product logo

expert's Introduction

Expert.js

Build Status Expert.js is miniature semantic network framework written in JavaScript.

Motivation

Representing knowledge for artificial intelligence applications can be simplified by the flexibility of scripting languages. Expert.js provides a DSL-like constructs for building semantic networks. The result is readable code, that's easy to work with.

Quick Example

var expert = require('expert'),
    _ = require('underscore');

var domain   = expert.Domain(),
    Concept  = domain.Concept,
    Relation = domain.Relation,

    mammal = Concept.create({id:"mammal"}),
    fish = Concept.create({id:"fish"}),
    dog = Concept.create({id:"dog"}),
    cat = Concept.create({id:"cat"}),
    mouse = Concept.create({id:"mouse"}),
    whale = Concept.create({id:"whale"}),
    salmon = Concept.create({id:"salmon"}),

    fur = Concept.create({id:"fur"}),
    bark = Concept.create({id:"bark"}),
    swim = Concept.create({id:"swim"}),

    isa = domain.isa,
    example = domain.example,

    has = Relation.create({id:"has"}),
    whatHas = Relation.create({id:"what has",inverseFor:has}),

    can = Relation.create({id:"can"}),
    whatCan = Relation.create({id:"what can",inverseFor:can}),

    biggerThan = Relation.create({id:"biggerThan",isTransitive: true});
    smallerThan = Relation.create({id:"smallerThan",isTransitive: true, 
                                   inverseFor:biggerThan});

salmon
   .isa(fish)
   .biggerThan(mouse)
   .can(swim);

whale
   .isa(mammal)
   .biggerThan(dog)
   .biggerThan(cat)
   .can(swim);

dog
   .isa(mammal)
   .has(fur)
   .can(bark)
   .biggerThan(mouse);

cat
   .isa(mammal)
   .has(fur)
   .biggerThan(mouse);

mouse
   .isa(mammal)
   .has(fur);

console.log("what has fur?");
var answer1 = whatHas(fur);
console.log(_.map( answer1, function(c){ return c.id; }));

console.log("what mammal that a mouse is smaller than, can swim?");
var answer2 = _.intersection( example(mammal),
                              whatCan(swim),
                              smallerThan(mouse) );
console.log(_.map( answer2, function(c){ return c.id; }));

Step by Step

We begin by including Expert.js and Underscore.js.

var expert = require('expert'),
    _ = require('underscore');

To begin working with Expert.js you must first create a Domain object. Concepts and Relations are part of a Domain. You can separate different semantic networks using different Domains.

The Concept and Relation objects are mere references and are declared for brevity.

var domain   = expert.Domain(),
    Concept  = domain.Concept,
    Relation = domain.Relation,

Concepts are the building blocks of your semantic network. You can also provide a unique identifier that can help you identify the concepts later on. Concepts can be tangible or abstract. We will soon see how you can describe the abstraction hierarchy of related Concepts using an "isa" relation.

    mammal = Concept.create({id:"mammal"}),
    fish = Concept.create({id:"fish"}),
    dog = Concept.create({id:"dog"}),
    cat = Concept.create({id:"cat"}),
    mouse = Concept.create({id:"mouse"}),
    whale = Concept.create({id:"whale"}),
    salmon = Concept.create({id:"salmon"}),

    fur = Concept.create({id:"fur"}),
    bark = Concept.create({id:"bark"}),
    swim = Concept.create({id:"swim"}),

Expert.js comes with 2 predefined relations: "isa" and "example". You can use an "isa" relation to express abstraction hierarchies of Concepts. In this example we will use it to indicate that a dog, a cat, a whale and a mouse are kinds of mammal.

    isa = domain.isa,
    example = domain.example,

The "example" relation is the inverse relation of "isa". Inverse relations maintain a back link between related concepts. This will allow you to query relations from an inverse direction in the semantic network.

You can define as many relation types as you wish. Relations allow you to introduce facts to your network of Concepts. Relations can also have unique identifiers. If you use an identifier that is also a valid JavaScript function name, Expert.js will create a method for your Domain's Concept prototype - for syntactic sugar.

    has = Relation.create({id:"has"}),
    whatHas = Relation.create({id:"what has",inverseFor:has}),

    can = Relation.create({id:"can"}),
    whatCan = Relation.create({id:"what can",inverseFor:can}),

    biggerThan = Relation.create({id:"biggerThan",isTransitive: true});
    smallerThan = Relation.create({id:"smallerThan",isTransitive: true, 
                                   inverseFor:biggerThan});

We can now use our defined relations to establish facts. In this example we use the shorthand method. This method was made possible because we used valid function names as our relations identifiers.

salmon
   .isa(fish)
   .biggerThan(mouse)
   .can(swim);

whale
   .isa(mammal)
   .biggerThan(dog)
   .biggerThan(cat)
   .can(swim);

dog
   .isa(mammal)
   .has(fur)
   .can(bark)
   .biggerThan(mouse);

cat
   .isa(mammal)
   .has(fur)
   .biggerThan(mouse);

mouse
   .isa(mammal)
   .has(fur);

Finally, we can inspect our semantic network. Note how we use Underscore.js to do a primitive "join" over inverse relations:

console.log("what has fur?");
var answer1 = whatHas(fur);
console.log(_.map( answer1, function(c){ return c.id; }));

console.log("what mammal that a mouse is small than, can swim?");
var answer2 = _.intersection( example(mammal),
                              whatCan(swim),
                              smallerThan(mouse) );
console.log(_.map( answer2, function(c){ return c.id; }));

expert's People

Contributors

ithailevi avatar rlofc avatar

Watchers

James Cloos avatar http://albertahn.com 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.