Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

pulling data from a string

7 views
Skip to first unread message

Chase Southard

unread,
Apr 12, 2012, 10:45:16 PM4/12/12
to Kentucky Ruby user group
Fellow Rubyists, 

In what very little spare time I have, I've been circling the quantified self idea. 

There are many tools that will allow you to record various notes through a web app. That's fine and all, but I'm wondering if I can build one myself. 

I'm thinking that it's a simple form that could take any string, where the string can be parsed to pull out data within and stored properly. 

Something along the lines of:

sleep  #=> SleepWakes.create(:sleeptime => Time.now)

or

drank 2 rootbeers #=> Beverages.create(:quantity => 2, :desc => "rootbeers")

or 

weight 123 #=> Tonnage.create(:weight => 123)

Right now, I'm mulling over what could turn into a nasty set of tangled if and case statements. 

Is there some sort of pattern, tool, technology, thing that would be more appropriate or is spaghetti-if-statements the best method?

Thanks,

Chase


--
R. Chase Southard
http://about.me/southard

jhollinger

unread,
Apr 13, 2012, 12:41:54 AM4/13/12
to ky...@googlegroups.com
That's an interesting problem. While I've never worked with it, I think there's defiantly a better solution than just ifs and cases. I put up a quick idea as a gist. It's half crap, but I think I like the other half and may have a use for it. https://gist.github.com/2373661

The idea is to isolate the object (or implied object of an action), and then apply the modifiers/attributes. Each object "knows" only about its own domain, so your code stays clean. This is just a rough sketch. It would benefit from some DSL-ification.

In summary it turns the string "drank 2 rootbears" into the Ruby code "Sentence.new.drank.2.rootbears" and executes it.

Todd Willey

unread,
Apr 13, 2012, 8:35:02 AM4/13/12
to ky...@googlegroups.com
I don't think parsing is the hard part. Cucumber does a good job of
building a DSL for testing that mimics English, so I believe you can
look to that for inspiration on how to build a micro-language for
describing your domain.

It seems you can almost model everything as
VERB [COUNT] [ADJECTIVE] OBJECT [with HELPER-OBJECT+] [on CATEGORY]

weighed 190 pounds
drove 23 miles with Camaro
walked 10 minutes
visited eye doctor with nearsightedness
waited 23 minutes with waiting-room
slept 3:15 hours with restlessness
spent 23.95 dollars with Kroger on food
watched 44 minutes with Nova: How Our Brain Works on Television

I think these things are all just essentially tags to chart different
data inside of collectors. For example, weight, money, and sleep-time
would be collectors. The "with" and "on" clauses would be tags for
breaking things up into pie-chart slices or the like when graphing or
otherwise reviewing data.

From a data modeling perspective, the language maps to underlying
concepts like this:

ACCUMULATOR AMOUNT UNIT with TAG(specific) on TAG(general)

Then you just cram the annotated/tagged values into their
set/accumulator/collector.

I think the most interesting thing here is building a complete
taxonomy. I "watched" Nova, and I "read" Zero History, but in an
ideal world, both of those would show up in Media Consumption, with a
TAG(interaction-type) that would let me break apart that data or see
it all together. Ate/Drank have the same problem, as do Drove /
Walked / Biked.

I also think making the unit converters so that the accumulator has a
single type is another interesting taxonomic undertaking. For
example, knowing a single rootbeer is equivalent to 130 Calories is
interesting (assuming the 'food' collector is tracking a single
dimension of calories, and not multidimensional across all health
info).

At any rate, I'd recommend just using the language starting now, and
see what it looks like. Then you can iterate on the backend using
your previously captured data as much as you want without having to
start over.

Putting a timer in the app seem like a good helper function to
maintain good data.

-todd[1]

> --
> You received this message because you are subscribed to the Google Groups
> "Kentucky Ruby user group" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/kyrug/-/Op6JpC9H8m4J.
>
> To post to this group, send email to ky...@googlegroups.com.
> To unsubscribe from this group, send email to
> kyrug+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/kyrug?hl=en.

--
CONFIDENTIALITY NOTICE:  This email is for sole use of intended
recipient(s).  Unauthorized use is prohibited.

Chase Southard

unread,
Apr 13, 2012, 9:57:32 AM4/13/12
to ky...@googlegroups.com
Ah. Very nice, guys. Thanks.

I'll investigate these today.

Chase A James

unread,
Apr 13, 2012, 11:16:47 AM4/13/12
to ky...@googlegroups.com
This reminds me of a Story.awk: http://awk.info/?doc/story.html

I could see the "list of all the things done" being tracked in a text
file and just being piped through a parsing program to generate
reports.

Todd Willey

unread,
Apr 13, 2012, 11:50:02 AM4/13/12
to ky...@googlegroups.com
Reading this made my day:

Earth scientists invent enormous bugs who are Friendly and and
They Get Married And Live Happily Forever After

Reply all
Reply to author
Forward
0 new messages