0.0.8 • Published 8 years ago

craft-ai-interpreter v0.0.8

Weekly downloads
2
License
BSD-3-Clause
Repository
github
Last release
8 years ago

craft ai interpreter js

craft ai isomorphic javascript decision trees interpreter

Version Build License Dependencies Dev Dependencies

API

decide

A simple function able to evaluate a decision tree computed by craft ai.

import decide, { Time } from 'craft-ai-interpreter';

// `tree` is the decision tree as retrieved through the craft ai REST API
let tree = { ... };

// Compute the decision on a context created from the given one and filling the
// `day_of_week`, `time_of_day` and `timezone` properties from the given `Time`
let decision = decide(
  tree,
  {
    presence: 'gisele'
  },
  new Time('2010-01-01T05:06:30'));

Any number of partial contextes and/or Time instances can be provided to decide, it follows the same semantics than Object.assign(...): the later arguments overriding the properties value from the previous ones)

The computed decision looks like:

{
  "context": { // In which context the decision was taken
    "presence": "gisele",
    "day": 4,
    "time": 5.108333333333333,
    "tz": "+01:00"
  },
  "decision": { // The decision itself
    "blind": "OPEN"
  },
  "confidence": 0.9937745256361138, //The confidence in the decision
  "predicates": [ // The ordered list of predicates that were validated to reach this decision
    {
      "property": "timeOfDay",
      "op": "continuous.greaterthanorequal",
      "value": "6"
    },
    {
      "property": "timeOfDay",
      "op": "continuous.lessthan",
      "value": "19"
    },
    {
      "property": "dayOfWeek",
      "op": "continuous.greaterthanorequal",
      "value": "5"
    },
    {
      "property": "timeOfDay",
      "op": "continuous.greaterthanorequal",
      "value": "10"
    }
  ]
}

Time

The Time class facilitates the handling of time types in craft ai. It is able to extract the different craft ai formats from various datetime representations, thanks to Moment.js.

import { Time } from 'craft-ai-interpreter'

// From a unix timestamp and an explicit UTC offset
const t1 = new Time(1465496929, '+10:00');

// t1 === {
//   utc: '2016-06-09T18:28:49.000Z',
//   timestamp: 1465496929,
//   day_of_week: 4,
//   time_of_day: 4.480277777777778,
//   timezone: '+10:00'
// }

// From a unix timestamp and using the local UTC offset.
const t2 = new Time(1465496929);

// Value are valid if in Paris !
// t2 === {
//   utc: '2016-06-09T18:28:49.000Z',
//   timestamp: 1465496929,
//   day_of_week: 3,
//   time_of_day: 20.480277777777776,
//   timezone: '+02:00'
// }

// From a ISO 8601 string
const t3 = new Time('1977-04-22T01:00:00-05:00');

// t3 === {
//   utc: '1977-04-22T06:00:00.000Z',
//   timestamp: 230536800,
//   day_of_week: 4,
//   time_of_day: 1,
//   timezone: '-05:00'
// }

// Retrieve the current time with the local UTC offset
const now = new Time();

// Retrieve the current time with the given UTC offset
const nowP5 = new Time(undefined, '+05:00');

Developers

Setup

npm install

Tests

npm run test

Releasing a new version (needs administrator rights)

  1. Make sure the build of the master branch is passing
  2. Checkout the master branch locally
git fetch
git checkout master
git reset --hard origin/master
  1. Bump the version and push
npm version patch
git push origin master
git push --tags
0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago