Results 1 to 5 of 5

Thread: Javascript age script

  1. #1
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Javascript age script

    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.

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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    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.

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

    Code:
    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; 
    }

  4. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!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">
    
    <head>
      <title></title>
    </head>
    
    <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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  5. #5
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    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?


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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •