enavu webdevmania tutsvalley psdestination
visit the enavu network
7 Comments Article Title Overlay

PHP Smart Date Parsing: Natural Language Input (task.fm explained)

Have you ever tried task.fm ? task.fm uses something called “Natural Language Input,” and it’s very interesting. To put it simply, you enter a natural language input such as, “Pick up my kids from school tomorrow 3pm” or “Pool party this Saturday at 4pm.”
“Pick up my kids from school tomorrow 3pm” will be parsed as, [...]

DZoneDiggDesign FloatDeliciousBlinklistRedditFacebookGoogle BuzzTwitterStumbleUponTumblrShare

Have you ever tried task.fm ? task.fm uses something called “Natural Language Input,” and it’s very interesting. To put it simply, you enter a natural language input such as, “Pick up my kids from school tomorrow 3pm” or “Pool party this Saturday at 4pm.”

“Pick up my kids from school tomorrow 3pm” will be parsed as, “Pick up my kids from school” with date pre-selected. If you’ve never seen this before, this is pretty interesting stuff. Question is, how do they do it?

There are few ways to do this, just by looking at task.fm application, they allow maximum of 140 characters. Which means, it possibly uses recursive function to go through every possible pair. If we let every word be paired with another word, then three words, then four words, and so on, then we can possibly run every single combination, given that it’s only 140 characters long.

I would say the most easiest way to do it is to use strtotime() function. The function evaluates the word, if it passes as a string with valid date/time variables, it will return a number. If not, it will return -1.

Let’s brainstorm: “I am going to go to the beach at 3pm tomorrow”

If we run strtotime() function on every single word, then it will return -1 on every word except “3pm” and “tomorrow”

Time to do some coding:

function parse_to_date_and_string($parse) {
$exclude = array('i', 'at');
$parse_array = explode(" ", $parse);
$date = array();
$data['text'] = array();
$word_count = count($parse_array);
for($i = 0; $i < $word_count; $i++) {
if (strtotime($parse_array[$i]) > 0 && !in_array($parse_array[$i], $exclude)) {
$date[] = $parse_array[$i];
} else {
$data['text'][] = $parse_array[$i];
}
}
$data['date'] = strtotime(implode(" ", $date));
return $data;
}

Looking at the code above, let me explain the general concept. strtotime() function returns -1 if the word does not parse into a real date. We check every word whether they are parseable into a date, if they are, we combine them together, then parse it at the end.

What will it look like?

Example) Tomorrow I have to go see my chiropractor at 12:00pm

Found parseable words) Tomorrow 12:00pm

Non-parseable words) have to go see my chiropractor

Outcome) if we run strtotime(“Tomorrow 12:00pm”), it should return the correct date.

By the way, certain words will be parseable, such as “i”, so had to take that out using $exclude array. I also took out the word, “at” because if someone enters, “pick up my kids at 12pm” then it will be parsed like “pick up my kids at” “12pm”

There’s many things we could experiment with, just by using strtotime() function. This way, we don’t necessarily have to have recursive function that checks for all possible combination. The code I’ve written is still very primitive, but you can experiment further to make something actually usable.

DZoneDiggDesign FloatDeliciousBlinklistRedditFacebookGoogle BuzzTwitterStumbleUponTumblrShare

Related posts:

  1. Working with cookies using jQuery and JavaScript
  2. Easy Unobstructive jQuery Print Page Tutorial
  3. Description text inside an input field
  4. Automatic input field focus on page load

Terence Pae

Did you absolutely LOVE this article... share it!

Comments

  • Great to see an interest in our natural language capabilities! When I founded Task.fm there was only 2-3 other companies working on similar natural language parsing. There’s still lots of experimentation to be done and Task.fm still has a long way to go before it is as “smart” as I would like to see it.

    Great post!

  • You might want to lowercase the string so that i actually matches I as well…

  • because && is shot-circuit operator, I would change the if statement from:
    if (strtotime($parse_array[$i]) > 0 && !in_array($parse_array[$i], $exclude))

    to:
    if (!in_array($parse_array[$i], $exclude) && strtotime($parse_array[$i]) > 0)

    this way we don’t have to parse excluded words

  • i m just intested to know more about nlp with php

Leave your own!

Sponsors

freedcamp free project management

Popular Articles

fzilla file hosting