AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Any map mechanism (key/value pairs) in Chatscript? 
 
 

Looking for such structure like <map> in AIML. Must have been a common requirement but I could not find any clue searching the forum.

Thanks for any assist.

 

 
  [ # 1 ]

In general, concepts can do most anything in this area, along with ^query as needed.  What is an example of what you want to do?

 

 
  [ # 2 ]

Say I have concepts of key/value pairs like this:

:concept ~keys (key1 key2 key3)
:concept ~values (value1 value2 value3)

How can I get the corresponding value given a key?

Facts and ^query seem to provide functions a lot more wider and complicated.

Thanks.

 

 
  [ # 3 ]

for a one-to-one map, you can use $$tmp = ^find(~keys key1) to get the integer offset into the set
and then
^nth(~values $$tmp) 
to get the mate.

 

 
  [ # 4 ]

Thank you for the instruction, Bruce. I will certainly try it.

 

 
  [ # 5 ]

Need further help.

The ^find call works well. However I could not make ^nth work. Test screen attached.

Did I make any mistake? Or is ^nth a 5.3 function (mine is 5.1)? Thanks.

Image Attachments
cs_nth.png
 

 
  [ # 6 ]

This works for me:

concept: ~val( val1 val2 val3 val4)

topic: ~introductions   keep ()
t: hi ^keep() ^repeat()
u: (test) nth(~val 2)

Nth is not new code.

 

 
  [ # 7 ]

however, allowing it for sets was done in the version after 5.1

 

 
  [ # 8 ]

Yes, I noticed it was done in 5.2. I have migrated my script to the 5.3 environment. So nth() works perfectly now.

Thank you so much for all your patience and advice, Bruce.

 

 
  [ # 9 ]

Another approach would be to use ^query as Bruce suggested and to use facts and create actual key/value pairs.

In an effort to make facts more accessible I am posting a couple of output macros that wrap some of the fact logic in more familiar commands .

The following implement “mapadd” to create a key value pair and “map” to retrieve a value based on the key:

# Add Key Value Pair to a Map
outputmacro: ^mapadd( ^mapname ^key ^value )
$$mapname = ^join( mapname: ^mapname )
^createfact( $$mapname ^key ^value )

# Retrieve Key Value Pair from a Map
outputmacro: ^map( ^mapname ^key )
$$mapname = ^join( mapname: ^mapname )
@0 = ^query( direct_sv $$mapname ^key ? 1 )
@0object


You can place the following in a topic to add and retrieve key/value pairs interactively:

# Add a Key Value Pair to a Map
#! mapadd( daysofweek , 1 , Monday )
u: ( mapadd \(  _* , _* , _* \) ) ^keep ^repeat
^mapadd( ‘_0 ‘_1 ‘_2 )
      Key\/Value pair \( ‘_1 , ‘_2 \) was added to map \” ‘_0 \”.

# Retrieve a Value for a Key in a Map
#! map ( daysofweek , 1 )
u: ( map \( _* , _* \) ) ^keep ^repeat
$$value = ^map( ‘_0 ‘_1 )
Key \” ‘_1 \” has a value of \” $$value \” in map \” ‘_0 \”.


Sample Output:
  >mapadd( daysofweek , 7 , Sunday )
Key/Value pair (7, Sunday ) was added to map “daysofweek”.
  >map( daysofweek , 7 )
Key “7” has a value of “Sunday” in map “daysofweek”.
  >

The command names could just as easily been named “factadd” and “getfactobject”.

Note: The fact subject is added with “mapname:” prefixed to the actual subject so that other facts do not mix together with these “maps” or named sets of key/value pairs.  If you wanted to query for the mapname in the facts you will need to prefix “mapname:” to the subject being queried.

 

 
  [ # 10 ]

Thank you very much, Alaric, for such nice design and sharing.

I’ll certainly try the design when my project requirement gets more complicated. Guess this is also a good example for newbie like me to learn about fact. Appreciate it.

 

 
  login or register to react