Created
September 10, 2009 18:58
-
-
Save maddox/184738 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string = %{-cat -mouse man OR woman until:2009-09-09 @maddox #avatar #xbox since:2009-09-16 near:"richmond, va" @maddox apple to:dog within:15mi to:maddox from:mschrag "football game" "oh yes"} | |
# words | |
exact_phrase = /".*?"/ | |
any_words = /\w+ OR \w+/ | |
minus_word = /-\w+/ | |
hashtag = /#\w+/ | |
# people | |
to_person = /to:\w+/ | |
from_person = /from:\w+/ | |
referenced_person = /@\w+/ | |
#places | |
near = /near:".*?"/ | |
within = /within:\w+/ | |
#dates | |
since = /since:\d\d\d\d-\d\d-\d\d/ | |
until_ = /until:\d\d\d\d-\d\d-\d\d/ | |
# any_words = /(\w+)( OR )?/ | |
# puts "man OR country OR house and then some".scan(any_words).inspect | |
matchers = [['until', until_], ['since', since], ['near', near], ['within', within], ['to_person', to_person], | |
['from_person', from_person], ['referenced_person', referenced_person], | |
['hashtag', hashtag], ['minus_word', minus_word], ['exact_phrase', exact_phrase], ['any_words', any_words]] | |
query_parts = {} | |
matchers.each do |matcher| | |
puts string | |
results = string.scan(matcher[1]).map{|r|r.to_s} | |
query_parts[matcher[0]] = results | |
results.each {|r| string.gsub! r, ''} | |
puts | |
puts query_parts.inspect | |
puts string | |
end | |
{"hashtag"=>["#avatar", "#xbox"], "from_person"=>["from:mschrag"], "any_words"=>["man OR woman"], "within"=>["within:15mi"], "minus_word"=>["-cat", "-mouse"], "exact_phrase"=>["\"football game\"", "\"oh yes\""], "to_person"=>["to:dog", "to:maddox"], "referenced_person"=>["@maddox", "@maddox"], "near"=>["near:\"richmond, va\""], "since"=>["since:2009-09-16"], "until"=>["until:2009-09-09"]} | |
apple |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment