AI Zone Admin Forum Add your forum
Using Chatscript Facts and inference to create a chatbot that learns
 
 

I am getting back into Chatscript facts and the query language and playing with using Chatscript facts and queries to create a chatbot that learns.  The problem that I had a couple of years ago and that I still have is that the matching of the object does not appear to work for me. 

I have created a query pattern called “infer”.  I would like it to scan up the hierarchy using the verb “is_a” and match the verb and the object passed in anywhere up the hierarchy.  This basic example ignores concepts and topics. 

I believe the commented line below should work without further coding.  I have had to add extra code to loop through the results and force a match on object that was passed in since the match o3 is not limiting the results to the object that was passed in but is tagging everything up the hierarchy.  But the extra code seems to be working fine.

Here is some simple code to add, delete and query facts:

queryinfer "4'is_a.t 1s<4q 2vt 3ot: queue s: match v2 o3"

outputmacro: ^IsInferred( ^^^)
## if ( ^query( infer ^s ^v ^o ) ) {TRUE} else {FALSE}

 
@= ^queryinfer ^^^)
 $
$RecordCount = ^length(@2)
 $
$InferredMatch FALSE
 loop
($$RecordCount
  
{
   
if ( ^first(@2Object) = ^$$InferredMatch TRUE end(loop}
  }
 
$$InferredMatch 

outputmacro
: ^deletefact( ^^^)
 @
= ^querydirect_svo ^^^)
 ^
delete(@2)

topic: ~reason keep repeat is a are infer can able to does have )

u: ( delete fact a _is a _* )
 ^
deletefact_0 is_a _1 )
 
OkFact_0 is_a _1 has been deleted.

u: ( a _is a _* ) 
 if ( ^
findfact_0 is_a _1 ) ) { I knew that already}
 
else ^createfact_0 is_a _1 OkI have learned that a _0 is a _1}

u
: ( is a _a _* )
 if ( ^
findfact_0 is_a _1 ) ) { YesA _0 is a _1
 
else if ( ^IsInferred_0 is_a _1 ) = TRUE ^IsInferreddebug_0 is_a _1 YesA _0 is a _1
 
else {NoA _0 is not a _1.

u
: ( a _can _* ) 
 if ( ^
findfact_0 can _1 ) ) { I knew that already}
 
else ^createfact_0 can _1 OkI have learned that a _0 can _1}

u
: ( is a _able to _* )
 if ( ^
findfact_0 can _1 ) ) { YesA _0 can _1
 
else if ( ^IsInferred_0 can _1 ) ) { YesA _0 can _1
 
else {NoA _0 can not _1.

u
: ( can a _~mainsubject _~mainverb )
 if ( ^
findfact_0 can _1 ) ) { YesA _0 can _1
 
else if ( ^IsInferred_0 can _1 ) ) { YesA _0 can _1
 
else {NoA _0 can not _1.}

u
: ( a _has {a} _* ) 
 if ( ^
findfact_0 has_part _1 ) ) { I knew that already}
 
else ^createfact_0 has_part _1 OkI have learned that a _0 has '_1. } 

u: ( does a _* have {a} _* )
 if ( ^findfact( _0 has_part _1 ) ) { Yes. A _0 has '
_1
 
else if ( ^IsInferred_0 has_part _1 ) ) { YesA _0 has '_1. } 
 else {No. A _0 does not have '
_1.

u
: ( a _is _* ) 
 if ( ^
findfact_0 has_attribute _1 ) ) { I knew that already}
 
else {  ^createfact_0 has_attribute _1 OkI have learned that a _0 is _1}

u
: ( is a _~mainsubject _* )
 if ( ^
findfact_0 has_attribute _1 ) ) { YesA _0 is _1
 
else if ( ^IsInferred_0 has_attribute _1 ) ) { YesA _0 is _1
 
else {NoA _0 is not _1.
 

 
  [ # 1 ]

Sample Output:

HARRY:  Welcome to ChatScript.
trainer: > a bird is a creature
HARRY:  Ok. I have learned that a bird is a creature.
trainer: > a mammal is a creature
HARRY:  Ok. I have learned that a mammal is a creature.
trainer: > an insect is a creature
HARRY:  Ok. I have learned that a insect is a creature.
trainer: > a fish is a creature
HARRY:  Ok. I have learned that a fish is a creature.
trainer: > a robin is a bird
HARRY:  Ok. I have learned that a robin is a bird.
trainer: > a crow is a bird
HARRY:  Ok. I have learned that a crow is a bird.
trainer: > a hawk is a bird
HARRY:  Ok. I have learned that a hawk is a bird.
trainer: > a bird has feathers
HARRY:  Ok. I have learned that a bird has feathers.
trainer: > a creature is alive
HARRY:  Ok. I have learned that a creature is alive.
trainer: > does a crow have feathers?
HARRY:  Yes. A crow has feathers.
trainer: > is an insect alive?
HARRY:  Yes. A insect is alive.
trainer: > a bird can fly
HARRY:  Ok. I have learned that a bird can fly.
trainer: > can a robin fly?
HARRY:  Yes. A robin can fly.
trainer: > a hammer is a tool
HARRY:  Ok. I have learned that a hammer is a tool.
trainer: > can a hammer fly
HARRY:  No. A hammer can not fly.
trainer: > does a hammer have feathers?
HARRY:  No. A hammer does not have feathers.
trainer: > is a hammer alive?
HARRY:  No. A hammer is not alive.
trainer: > a mammal can bear live young
HARRY:  Ok. I have learned that a mammal can bear live young.
trainer: > a dog is a mammal
HARRY:  Ok. I have learned that a dog is a mammal.
trainer: > can a dog bear live young?
HARRY:  No. A bear can not live.
trainer: >

Ok. So it is not perfect. smile The example needs to be expanded to handle different phrasing, word order, etc.  I need to add some functionality to handle multiple word subjects, verbs and object phrases.  This does show that this simple code can handle inheriting attributes, parts and abilities using inference and a hierarchy built using a custom “is_a” verb/predicate.  (isa is already in use by Chatscript).  The attributes and relations at higher levels in the hierarchy do not need to be physically expanded into new facts at all lower levels.  They can just be “reasoned” with in memory.

 

 
  login or register to react