PDA

View Full Version : Javascript age script

chriscc17
02-23-2011, 02:01 PM
I have a javascript chatbot that people use an input box to type to it not a form. The questions can be presented in a number of ways. I am trying to answer these type questions.

If I was born in 1980 how old am I?
If I was born in May of 1980 how old am I?
If I was born on May 23, 1980 how old am I?

I have a script that tell the current day, month and year but how do I pull out the person's birthday from the examples above to make the calculations.

Here is what I have so far if only the year is given. Note - I know with only year given I could only give approximate age.

if ((input.search("i was born")!= -1) && (input.search(/\b(19[0-9]{2}|200[0-9]|201[01])\b/)!=-1) && (input.search("how old am i")!= -1))
var age = tellTheYear() - ??????
document.result.result.value = "Your approximate age is "+age+".";
return true;}

tellTheYear() is the current date.

djr33
02-23-2011, 04:25 PM
This is very difficult. This is why most websites use standardized forms (such as dropdown select menus) to select numbers. Extracting it from text is not easy.
The best you can do is guess what it might look like. So either you could search the text for anything that looks like a month ("May" or "April") and for 1-2 digit numbers (3, 23) and for 4 digit numbers (1980) and just hope that they are correct, or you can actually guess what exact formats may appear and make a list.
Check for yyyy, or check for Month of yyyy, or check for Month dd, yyyy. And any other possibilities.

But beyond that you will get into natural language processing (something I'm interested in), and it will become incredibly difficult.

It's best to suggest that they limit their questions to a certain format.

chriscc17
02-23-2011, 08:44 PM
I understand but that is the challenge of making a chatterbot is processing natural language. I'm just not a programmer but enjoy creating the responses and the like. But bare with me if you will. :)

I have a script now that gives:

current day = tellTheDay()
curent month = tellTheMonth()
current year = tellTheYear()

I came up with this if only a year is given. I can't give exact date without month and day but close.

if ((input.search("i was born")!= -1) && (input.search(/\b(19[0-9]{2}|200[0-9]|201[01])\b/)!= -1) || (input.search("how old am i")!= -1)) {
var myMatch = input.match(/\b(19[0-9]{2}|200[0-9]|201[01])\b/);
var age = (tellTheYear() - myMatch[0]);
document.result.result.value = "You're approximate age is "+age+".";
return true;}

I can pull out the month if it's spelled out like Jan, Feb, March etc. and I can pull the day by setting a range of 1 to 31. I know there are a lot of other combination that will pop up but that's something I can work on down the road.

What I can't do is figure how to use the day, month and year and come up with the exact age.

This script does the calculations but I'm not sure how to use it in the bot.

function getAge(year, month, day) {
var now = new Date();

var today =
{
"year" : now.getFullYear(),
"month": now.getMonth()+1,
"day" : now.getDate()
}

var age = today["year"] - year;

if(month > today["month"])
age--;
else if(month == today["month"])
if(day > today["day"]) age--
return age;
}

vwphillips
02-23-2011, 09:20 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<title></title>

<body>
<input id="tst" name="" value="23-3-1945"/> <input type="button" name="" value="Age" onmouseup="Age('tst');"/>
<script type="text/javascript">
/*<![CDATA[*/

function Age(){
var today=new Date().getTime();
var birth=document.getElementById('tst').value.split(/\D/);
birth=new Date(birth[2],birth[1]-1,birth[0]).getTime();
birth=new Date(today-birth);
alert((birth.getFullYear()-1970)+' years, '+birth.getMonth()+' months, '+(birth.getDate()-1)+' days');
}
/*]]>*/
</script>
</body>

</html>

chriscc17
02-23-2011, 10:49 PM
Thanks but I'm not looking for another script. I have one already. My chatterbot uses an input box not a form.

This input.search statement will trigger on a questions like this:

If I was born on March 23, 1945 how old am I?

if ((input.search("born")!= -1) && (input.search(/\b([1-9]|[12][0-9]|3[01])\b/)!= -1) && (input.search(/(january|february|march|april|may|june|july|august|september|october|november|december)/)!=-1) && (input.search(/\b(19[0-9]{2}|200[0-9]|201[01])\b/)!= -1)) {
?????
document.result.result.value = "You're approximate age is +age+".";
return true;}

I made the calculations work in the previous post with the year only to give approximate age. Don't know how to use the month and day to determine exact age. That's what I'm looking to do.