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

Determining the ordinal suffix for a date, or other number
 
 

From following this thread about ChatScript and time, I got to working on a similar rule for the current date, and almost immediately ran into a snag, in that I need to figure out how to determine the ordinal suffix for the day of the month (e.g. the 12th, 3rd, 22nd, etc.). I thought I had seen something of it in one of the files in an earlier version than 1.13, but I can’t seem to locate it now. If this were PHP, I would (and did, in the past) handle it like this:

$n date('d'); // The current day of the month
    
if ( $n and $n 21 )
        
$suffix =  'th';
 
    
// Determine the suffix for numbers ending in 1, 2 or 3, otherwise add a 'th'
    
switch ( $n 10 ){
        
case '1'$suffix =  'st';
        case 
'2'$suffix =  'nd';
        case 
'3'$suffix =  'rd';
        default:  
$suffix =  'th';
    

I expect, however, that there is no “switch” block statement in ChatScript, so I’m thinking of creating a function rule, or maybe an output macro for this, if there isn’t already one in place. The snippet below is just off the top of my head, so please don’t shoot me. smile

outputmacro: ^ORDINALSUFFIX(^number)
  $
$num = ^number 100 # We only need to worry about numbers from 0 to 99, really...
  
if ( $$num and $$num 21 # I'm not certain about this portion. It may have to be altered.
    
$$suffix =  th
  }
  
else {
    
$$LSD = $$num 10
    
if ($$LSD == 1$$suffix st }
    
else if ($$LSD == 2$$suffix nd }
    
else if ($$LSD == 3$$suffix rd }
    
else $$suffix th }

    
^number $$suffix # final output - I think(?) 

To my way of thinking, this should work, but I haven’t tried it yet. I’ll let you all know. smile

 

 
  [ # 1 ]

How about something like this pseduo code:

date_suffix=“th”
if date=1 or date=21 or date=31 then date_suffix=“st”
if date=2 or date=22 then date_suffix=“nd”
if date=3 or date=23 then date_suffix=“rd”

 

 
  [ # 2 ]

whichever is your pleasure… but for the former, the output cannot be ^number $$suffix but must be join(^number $$suffix)

 

 
  [ # 3 ]
Steve Worswick - May 1, 2011:

How about something like this pseduo code:

date_suffix=“th”
if date=1 or date=21 or date=31 then date_suffix=“st”
if date=2 or date=22 then date_suffix=“nd”
if date=3 or date=23 then date_suffix=“rd”

If all we’re worried about is the date, which is restricted to numbers from 1 to 31, then that would be ok, but I plan on this being more versatile than just for the date. If I want to mention, for example, some number that was computed, and lies in the range of, say 600 to 700, then a more robust macro would be needed. It’s best, I think, to plan for more contingencies than just the original need. smile

@Bruce: Thanks. I’ll be sure to add it. It’s join(), and not ^join()? Just making sure.

 

 
  [ # 4 ]

INTERNALLY, it is always ^xxx for a function or macro.  But you are only REQUIRED to use that notation when declaring a macro. For use, you can use either the full form ^join()  or the common parlance form join().  It’s stylistically up to you.

 

 
  login or register to react