superscriptjs / superscript

A dialogue engine for creating chat bots
http://superscriptjs.com
MIT License
1.65k stars 209 forks source link

Array implementation from rivescript #110

Closed volumetric closed 9 years ago

volumetric commented 9 years ago

Rivescript has the functionality of array, with array variable being set using ! at the beginning. like:

! array colors = red blue green yellow and then you can use it in matching input as

+ My favorite color is (@colors), whats yours? - What a coincidence, my favorite color is also <star>.

This is useful for making an custom equivalence set of a keyword for matching. This is different from wordnet word expansion for concepts. Here you can have words with spelling mistake of some word and basically a custom equivalence set.

Does Superscript has this functionality?

Also, rivescript doesn't have ability to add @arr1 inside another array like:

! array red_shades = red pink ! array colors = @red_shades blue green yellow

would be great if this can be done in superscript?

silentrob commented 9 years ago

I'm aware of rivescript array implentation, and it is not included in superscript. I found it was hard to scale it nicly and didn't provide that much power/flexibility. For managing data and sets, we use a truple store, this allows us to have a third dimension for creating a larger ontology on facts and data.

The library also supports chatscript style tables. See - https://github.com/silentrob/superscript-websocket-demo/blob/master/data/color.tbl to get a sense of how the format looks.

Then you can read from the set by using a plugin that queries the set and returns a value. See http://superscriptjs.com/documentation/scripting#custom for more information on creating a custom plugin.

volumetric commented 9 years ago

Thanks @silentrob. This is very useful.

volumetric commented 9 years ago

@silentrob i looked at the code of the demo example, and am not able to understand, how color.tbl file is getting used in the process flow. Is it being converted to colorData.json file, which is been referenced in color.js in the line var dataset = require("../data/colorData");

Can you refer me something, where i can understand about fact, truple and .tbl like i dont know what the first few lines of the color.tbl file are doing:

table: ~colorof (^color ^object)
^createfact(^object color ^color)
^addproperty(^color NOUN NOUN_SINGULAR)

 DATA:
 green [grass leaf plant emerald ~plants turtle parsley topaz]
 green [lettuce lime spinach celery pear broccoli apple shamrock ]

Looks like createfact and addproperty are built-in functions like addMessageProp, and updates some facts db, would highly appreciate if you can point me to some docs for facts and other built-in functions. If their is no such docs, you can just tell me here and i'll update it on the project wiki. Thanks a lot.

silentrob commented 9 years ago

I may have sent you down the wrong path. var dataset = require("../data/colorData"); has nothing to do with fact data and the tripple store.

So let me walk you though this.

Step 1. We include sfacts, this is the library for managing the Tripple DB. (LevelDB under the hood) https://github.com/silentrob/superscript-websocket-demo/blob/master/server.js#L10

Step 2. We define all the files we want to import. https://github.com/silentrob/superscript-websocket-demo/blob/master/server.js#L21

Step 3. We import the table data to the DB. https://github.com/silentrob/superscript-websocket-demo/blob/master/server.js#L49

Step 4. We tell Superscript to use THIS factsystem rather than creating a default one internally. https://github.com/silentrob/superscript-websocket-demo/blob/master/server.js#L50

Step 5. Start Superscript like you would normally. https://github.com/silentrob/superscript-websocket-demo/blob/master/server.js#L52

Now, for the fun part, quering the DB to get results. I'm assuming you aleady know how to create a simple plugin and call is from ss. From the plugin, the fact system is available automatically.

See https://github.com/silentrob/superscript-websocket-demo/blob/master/plugins/color.js#L76 this.facts.db Is the actual raw LevelGraph DB. Here is the API https://github.com/mcollina/levelgraph

The last thing to note is we actually use sublevel which means the db is partitioned, and the BOT has one, and each USER can have one too.

You can see the built in user plugin referenes memory https://github.com/silentrob/superscript/blob/master/plugins/user.js#L55

Lastly, poke around in sfacts to see what other methods are available. I will formalize this more and document it as it gets flushed out. https://github.com/silentrob/sfacts

volumetric commented 9 years ago

Many Thanks @silentrob