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..

Easy post-processing for output to web page
 
 

I really needed to get CS to output more than just the response text to my web page, and the extracting from json thing is still beyond me, so I came up with a simple (minded) fix that works for me at least.

Using the PHP/javascript interface developed by Dave Morton, I first I needed to add easily parsed stuff to the end of CS output that could be sent to separate containers on the page (like an image container).  I used the default simpleControl.top, with $cs_control_post added.

$cs_control_main = ~control 
$cs_control_post 
= ~addposthoc # test post-processing 

This $cs_control_post is called after the response has been generated normally. The topic ~addposthoc is:

topic: ~addposthoc system () # should be called for post processing addition of image url and or other data.

t: ( ) # postprocessing, here I am separating eveything I want to send within separate [ ], which will be parsed by php web page
 
\[ begin postprocessing \]
 \[ intent 
$$INTENT \]
 \[ topic 
%topic \] 
 
 
# get an image based on current emotion (form $INTENT)
 
nofail(topic ^respond(~getimage))
 
# oddly, this does the exact same thing as: 
 # nofail(topic respond(~getimage)) 
 
 
\[ emoimg $EMOIMAGE \]
 \[ end postprocessing \]

 
# we are done here
 
^end(RULE

To ensure the $intent variable is set, I added ^respond(~intent) in the main per-sentence processing part:

u: () # main per-sentence processing
 
 
$_responseCount = %response
    
# %response == $_responseCount means we have not generated an output for this input yet,
    # so allow continued analysis until we do.

 # if ($singleResponse) {$_responseCount = 0} # disable this to respond to each sentence in a volley

 
$$currenttopic = %topic  # get the current topic at start of volley
 
^respond(~intent# get the intent
.... 

The image retrieval is done by the topic ~getimage as follows:

topic: ~getimage nostay()

u: () ^keep()
  
# get an img name based on the current emotion 
  
nofail(topic ^respond(~getimageemo))

  
# painfully construct url, but allows you to debug  where each nasty special char is
  
$EMOIMAGE = ^join(../../../img$EMOIMAGENAME)
  
$EMOIMAGE = ^join(\"img src="\\$EMOIMAGE)
  
$EMOIMAGE = ^join($EMOIMAGE \")
  
$EMOIMAGE = ^join($EMOIMAGE \>)
  
$EMOIMAGE = ^join(\$EMOIMAGE)
  
# $EMOIMAGE # set as image url

topic: ~getimageemo nostay()

u: ( _* ) ^refine()
 
a: (~feeling_sad$EMOIMAGENAME sad.png 
 a
: (~feeling_happy$EMOIMAGENAME happy.png 
 a
: (~emocurse$EMOIMAGENAME curse.png
 a
: (~feeling_angry$EMOIMAGENAME angry.png 
 a
: (~no)  $EMOIMAGENAME no.png
 a
: (~yes)  $EMOIMAGENAME yes.png
 a
: (~emogoodbye)  $EMOIMAGENAME bye.png 
 a
: (~xsex$EMOIMAGENAME sex.png
 a
: ( _* ) $EMOIMAGENAME = default.png 

From the input: “stop moving”, it outputs a string like:

You want me to stop moving? stop-verb? Really? [begin postprocessing ] [intent ackgrunt ] [currenttopic stoptest ]  [emoimg ../../../img/default.png ] [end postprocessing ]

This can then be parsed into an array using js on the web page and the array elements placed/used however you want.

btw, the stoptest topic is:

topic: ~stoptest stop halt )

u: ( stop~) ^keep()
 
You want me to stopverbReally?

u: ( stop~) ^keep()
 
StopnounReally?


u: ( ~stop_verbs ) ^keep()
 
You want me to stop movingstop-verbReally

Hope this is useful to someone other than myself.

 

 
  [ # 1 ]

The typical way to manage oob stuff is to have a function you call in script:
outputmacro: ^oob($_what)
$$oob = $_what
Then in postprocessing have a rule like this:
t: ($$oob) ^postprintbefore( \[ $$oob \]) $$oob = null

The ^oob function can also be written to concatenate output from multiple calls, if you have some convention for disassembling them on the other side.  I typically concatenate with |  as the separator

 

 
  [ # 2 ]

Thanks for the bread crumbs.  I think I have a more typical structure as follows:

topic: ~addposthoc system () # should be called for post processing addition of image url and or other data.

t: () ^oob()
   ^
postprintbefore$$oob ]) $$oob null 

In a separate file (for testing) called outputmacros.top I have just the following:

outputmacro: ^oob()
 
$_sendINTENT = $$INTENT
 $_sendTOPIC 
= %topic
 
 nofail
(topic ^respond(~getimage)) # get the current img
 
nofail(topic ^respond(~saveit)) # get the saved it

 
$_item1name postprocessing
 $_item1value 
begin 
 $_item2name 
xintent
 $_item2value 
$_sendINTENT
 $_item3name 
xtopic
 $_item3value 
$_sendTOPIC
 $_item4name 
xemoimg
 $_item4value 
$EMOIMAGE
 $_item5name 
xsavedit
 $_item5value 
$thing
 $_item6name 
postprocessing         
 $_item6value 
end
 
# join it all together for a string output using pipe '|' between items and using ':' between item name and value for downstream web client parsing
 
$$oob = ^join(\$_item1name $_item1value \$_item2name $_item2value \$_item3name $_item3value \$_item4name $_item4value \$_item5name $_item5value \$_item6name $_item6value)
 
# now print it out to be appended to normal output
 
$$oob 

This results in the following in CS resulting from the input of “cow” using test bot named ‘A’:

Inputadmin:(Acow 
Respond
: (~introductions1 cow  ==> OK. |postprocessing:begin|xintent:ackgrunt|xtopic:~introductions|xemoimg:<img src="../../../img/default.png">|xsavedit:cow|postprocessing:endWhy:~honest_response.45.0=GRUNT.~quibble_all.12.0 ~addposthoc.0.0 
 

 
  login or register to react