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

Sample Code: Counting From X to Y
 
 

I am trying to create a topic that handles requests/commands to count.  Here is my sample code so far for others to use in their ChatScript chatbots.  Please let me know if you have any suggestions for improvements.  Is there a function to output numbers as text, such as displaying 1 as one, 2 as two, etc.?  Currently the output is in numbers.


concept: ~CountCommands ( count countdown enumerate list tell say )

concept: ~CountMultiples ( zeroes ones twos threes fours fives sixes sevens eights nines tens )

topic: ~CountFromTo keep repeat ( ~CountCommands ~numbers)

u: ( * ) $$countdirection = forwards $$countstep = 1 _0 = null _1 = null

u: ( by _~CountMultiples ) $$countstep = ^find(~CountMultiples ‘_0)
if ( $$countstep = 0 ) { [Really? ][Not happening. ][Tricky. ] You cannot count by zeroes. }

u: ( [ in by ] multiples of _~number ) $$countstep = _0
if ( $$countstep = 0 ) { [Zero is not a multiple.][Right, zero is a multiple, good one.][You first] }

u: ( [backwards descending down countdown] ) $$countdirection = backwards

u: ( << ~CountCommands >> from {the}{number} _~number * to {the}{number} _~number )
^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> between * _~number * _~number )
^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> _~number * [get reach until to ] * _~number ) ^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> to {the}{number} _~number ) 
^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> [start begin] * [at with] _~number and [end stop] [at with] _~number ) 
^reuse(COUNT_FROM_TO)


s: COUNT_FROM_TO (?)
$$x = _0
$$y = _1

#——- Figure range if only one number supplied——-
if ( _1 == null AND $$x > 0 ) { $$y = $$x $$x = $$countstep }
else if ( _1 == null AND $$x < 0 ) { $$y = $$x $$x = $$countstep * -1 }

#——-swap x an y if count direction is backwards——-  if ( $$countdirecti and $$y > $$x and $$y > 0 ) {
$$temp = $$x
$$x = $$y
$$y = $$temp

#——- set count direction to backwards if implied by first number being larger than second——-
if ( $$x > $$y ) {
$$countdirection = backwards
$$countstep = $$countstep * -1}

#——- calculate the number of numbers to be output——-
$$CountRange = $$y - $$x / $$countstep

#——- handle output where countrange exceeds maxcount, set maxcount to a reasonable number——-
#——- how many numbers would a human humor a questioner with?——-
$$maxcount = 12
if ( $$CountRange > $$maxcount ) {
$$limit = $$maxcount * $$countstep - $$countstep
if ( $$countstep < 0 ) { $$y = $$x - $$limit } else { $$y = $$x + $$limit }
  [I’m not going to count that many numbers!][Give me a break.][Sorry, I don’t have all day. ]  The first $$maxcount are:
  }
Else {
# ----- generate random response -----
$$EndPunct = .
[][][][][]
[Like $$EndPunct = ? ]
[Like this $$EndPunct = ? ]
[Do you mean like $$EndPunct = ? ]
[Such as $$EndPunct = ? ]
[As in $$EndPunct = ? ]
[Ta da! ]
[You only ask me that because you think I am not human. ]
[Easy. ][Presto ][You want me to count? ]
[Boring. If you want to know if I am human then ask me about my feelings. \n]
}

#——- loop through numbers——-
$countnum = $$x
$countnum
^loop () {
$countnum = $countnum + $$countstep
if ( $$countstep > 0 and $countnum > $$y ) {end(LOOP)}
  if ( $$countstep < 0 and $countnum < $$y ) {end(LOOP)}
  $countnum

}

$$EndPunct

 

 
  [ # 1 ]

Here is sample output:

  >count to 10
1 2 3 4 5 6 7 8 9 10 .
  >count to 10 by twos
2 4 6 8 10 .
  >can you say the numbers between 15 and 20?
15 16 17 18 19 20 .
  >could you tell me the numbers from 3 to 9?
Do you mean like 3 4 5 6 7 8 9 ?
  >can you countdown from 10 to 1?
10 9 8 7 6 5 4 3 2 1 .
  >can you count from -10 to -5?
You want me to count? -10 -9 -8 -7 -6 -5 .
  >can you count to one hundred by tens?
Presto 10 20 30 40 50 60 70 80 90 100 .
  >can you count to 20 in multiples of 5?
Such as 5 10 15 20 ?
  >from 5 to 15 how about you count for me?
You only ask me that because you think I am not human. 5 6 7 8 9 10 11 12 13 14
15 .
  >Can you enumerate the numbers from 1 to 100?
Sorry, I don’t have all day. The first 12 are: 1 2 3 4 5 6 7 8 9 10 11 12
  >Can you count from 20 to 25?
Boring. If you want to know if I am human then ask me about my feelings.
20 21 22 23 24 25 .

 

 
  [ # 2 ]

u: ( * ) $$countdirection = forwards $$countstep = 1 _0 = null _1 = null

You don’t need * in the pattern, though you are welcome to it.
While you can clear _0 and _1, clearing _0 never has value in your script because it is always set before you can get to COUNT_FROM_TO. Similarly except for 1 place, _1 will always have been set, so you might instead choose to clear it from that one place before the ^reuse.

Technically there was a flaw in CS canonization, such that zeros and zeroes would not use the numeric form for the canon. That has been fixed. This means you would not need $$countstep = ^find(~CountMultiples ‘_0) and could instead merely do $$countstep = _0. Additionally, you would not use the pattern u: ( by _~CountMultiples )  and instead just say
(by _~number)  and thus would accept “count from 1 to 10 by 2” as a fine input.
You could also consolidate that one and another by:
u: ( [in   by ] {"multiples of"}  _~number ) $$countstep = _0
assuming you’d accept “count in threes”  which would be fine and “count in three” which would be OK.

use of { } inside of << >> is pointless UNLESS you try to memorize it.  The pattern matches with or without it and there is no POSITIONAL processing going on, so it does not act as a word alignment device.

 

 
  [ # 3 ]

Thanks Bruce for the suggestions.

Here is the updated code:


concept: ~CountCommands ( count countdown enumerate list tell say )

topic: ~CountFromTo keep repeat ( ~CountCommands ~numbers)

u: ( * ) $$countdirection = forwards $$countstep = 1

u: ( [ in by use ] {multiples of}{steps of} _~number ) $$countstep = _0
if ( $$countstep = 0 ) { [You cannot count by zero.][Right, counting by zeros, good one.][You first] }

u: ( [backwards descending down countdown] ) $$countdirection = backwards

u: ( << ~CountCommands >> from {the}{number} _~number * to {the}{number} _~number )
^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> between * _~number * _~number )
^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> _~number * [get reach until to ] * _~number ) ^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> to {the}{number} _~number ) 
_1 = null
^reuse(COUNT_FROM_TO)

u: ( << ~CountCommands >> [start begin] * [at with] _~number and [end stop] [at with] _~number ) 
^reuse(COUNT_FROM_TO)


s: COUNT_FROM_TO (?)
$$x = _0
$$y = _1

#——- Figure range if only one number supplied——-
if ( _1 == null AND $$x > 0 ) { $$y = $$x $$x = $$countstep }
else if ( _1 == null AND $$x < 0 ) { $$y = $$x $$x = $$countstep * -1 }

#——-swap x an y if count direction is backwards——-  if ( $$countdirecti and $$y > $$x and $$y > 0 ) {
$$temp = $$x
$$x = $$y
$$y = $$temp

#——- set count direction to backwards if implied by first number being larger than second——-
if ( $$x > $$y ) {
$$countdirection = backwards
$$countstep = $$countstep * -1}

#——- calculate the number of numbers to be output——-
$$CountRange = $$y - $$x / $$countstep

#——- handle output where countrange exceeds maxcount, set maxcount to a reasonable number——-
#——- how many numbers would a human humor a questioner with?——-
$$maxcount = 12
if ( $$CountRange > $$maxcount ) {
$$limit = $$maxcount * $$countstep - $$countstep
if ( $$countstep < 0 ) { $$y = $$x - $$limit } else { $$y = $$x + $$limit }
  [I’m not going to count that many numbers!][Give me a break.][Sorry, I don’t have all day. ]  The first $$maxcount are:
  }
Else {
# ----- generate random response ----- $$EndPunctuati
[][][][][] [Like $$EndPunctuati ] [Like this $$EndPunctuati ] [Do you mean like $$EndPunctuati ] [Such as $$EndPunctuati ] [As in $$EndPunctuati ]
[Ta da! ]
[You only ask me that because you think I am not human. ]
[Easy. ][Presto ][You want me to count? ]
[Boring. If you want to know if I am human then ask me about my feelings. \n]
}

#——- loop through numbers——-
$countnum = $$x
$countnum
^loop () {
$countnum = $countnum + $$countstep
if ( $$countstep > 0 and $countnum > $$y ) {end(LOOP)}
  if ( $$countstep < 0 and $countnum < $$y ) {end(LOOP)}
  $countnum

}

$$EndPunctuation

 

 
  [ # 4 ]

If I wanted to have the output of the counting in words such as one, two, three, etc.  or four hundred one, four hundred two, etc.,  would I need to write a special output macro for that or is there already a special function that converts numbers to text?

 

 
  [ # 5 ]

There is no function for the moment that converts numbers to words. One could write a macro for that.

 

 
  login or register to react
‹‹ No Match Message.      Multiple inputs ››