Well, it's getting there... very slowly.  Still nothing from 37
signals, does anyone have a good address to send to?

Anyway, my function is coming along.  Not the prettiest, but already
farely powerful.  If anyone is interested check it out and make it
better. (it uses a few things in the rails time_extensions.rb code so
run it in script/console, or include the file)   I'll keep you
posted...

#TODO:
 # Turn words into numbers
 # Do days in the month eg. "the 22nd", "february 3rd"
 # Maybe incorporate ParseDate

def time_warp period
   period = period.strip.downcase
   now = Time.now

   #lasts & nexts eg. "last monday", "last mar", "last week" TODO
next is wonky....
   if period =~ /(last|next|this)\s*(\w+)/
     type = $2
     rel = $1 =~ /last/ ? -1 : 1
     now =
     if i = Date::DAYNAMES.index(type.capitalize) then now + rel *
(now.wday - i <= 0 ? (now.wday - i) + 7 : now.wday - i).days
     elsif i = Date::ABBR_DAYNAMES.index(type.capitalize) then now +
rel * (now.wday - i <= 0 ? (now.wday - i) + 7 : now.wday - i).days
     elsif i = Date::MONTHNAMES.index(type.capitalize) then now + rel
* (now.month - i <= 0 ? (now.month - i) + 12 : now.month - i).months
     elsif i = Date::ABBR_MONTHNAMES.index(type.capitalize) then now
+ rel * (now.month - i <= 0 ? (now.month - i) + 12 : now.month -
i).months
     elsif type == "week" then now + rel * 1.week
     elsif type == "month" then now + rel * 1.month
     elsif type == "year" then now + rel * 1.year
     else
       now + rel * 1.day
     end
   end

   #Awesome relative stuff... eg. "the week before last tuesday", "2
weeks from now"
   if period =~ /(\d+|the*) (day|month|week|year)(?:s)?
((ago|before)|(from now|after))/
     rel = $3; type = $2
     num = $1 =~ /(\d+)/ ? $1.to_i : 1
     num *= rel =~ /(ago|before)/ ? -1 : 1
     now =
     case type
     when "day"; now + num.days
     when "month"; now + num.months
     when "week"; now + num.weeks
     when "year"; now + num.years
     else now
     end
   end

   #relitavize
   now =
   case period
   when /yesterday/; now.yesterday
   when /tomorrow/; now.tomorrow
   else
     now
   end

   # arbitrary times
   if period =~ /(morning|afternoon|evening|night|lunchtime)/
     now =
     case $1
     when "morning"; now.change(:hour => 8)
     when "afternoon"; now.change(:hour => 15)
     when "evening", "night"; now.change(:hour => 20)
     when "lunchtime"; now.change(:hour => 12)
     else now
     end
   end

   #at's, does PM and AM as well
   if period =~ /at (\d+):*(\d*)\s*(pm)*/
     hour = $1.to_i > 23 ? 23 : $1.to_i
     min = $2.to_i > 59 ? 59 : $2
     if $3 == "pm" then hour += 12 end
     now = now.change(:hour => hour, :min => min)
   end

   return now
 end

On 8/8/06, Chris Van Pelt <[EMAIL PROTECTED]> wrote:
The ParseDate.parsdate method in the standard library does the first
three on your list, http://www.rubycentral.com/book/lib_standard.html

I want that and some more fun stuff like your other examples or "last
tuesday at 5", "four days ago in the afternoon" (maybe a little
rediculous).  It's the relative stuff that gets interesting...

Does anyone know of something out there that will turn number strings
(eg. "four") into a number?  It gets fun when you do stuff like
"thirteenth"...

Chris



On 8/8/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> This is natural language processing; i started working on my own this
> week, i have the following patterns implemented:
>
> ####-##-##
> dec[]##[]####
> december[]##[]####
> tomorrow
> next [day name]
> etc.
>
> Is this what you guys are looking for?
>
> -Jordan
>
> On 8/8/2006, "Patrick Crowley" <[EMAIL PROTECTED]> wrote:
>
> >I could see it being rolled into Rails, but not anytime soon.
> >
> >Best,
> >Patrick
> >
> >
> >On Aug 8, 2006, at 3:39 pm, Chris Van Pelt wrote:
> >
> >> I sent an email to 37 signals asking for theres... I doubt I'll see
> >> it.  I'm working on my own, with little success.  Hopefully we can get
> >> something usefull soon.
> >>
> >> Chris
> >_______________________________________________
> >Sdruby mailing list
> >[email protected]
> >http://lists.sdruby.com/mailman/listinfo/sdruby
> _______________________________________________
> Sdruby mailing list
> [email protected]
> http://lists.sdruby.com/mailman/listinfo/sdruby
>

_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby

Reply via email to