Created
August 5, 2009 13:30
-
-
Save Narnach/162676 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
| # Simple message filtering logic for a Ruby-based Twitter client. | |
| # | |
| # This demonstrates two simple string-based filtering approaches: | |
| # * blacklist words | |
| # * blacklist phrases | |
| # | |
| # It uses a slightly optimized matching and shows a sub-optimal one as comment. | |
| # | |
| # This can ofcourse also be used for highlights, dynamic grouping and whatnot. | |
| messages = [ | |
| 'First tweet', | |
| 'Foursquare annoys me', | |
| 'I am awesome #foursquare', | |
| 'I twote about tweeter' | |
| ] | |
| filter_words = ['#foursquare', 'tweet'] | |
| filter_phrases = [ | |
| 'twote about' | |
| ] | |
| # Runs in O(msg_words * filter_words): | |
| # leftover_words = messages.reject {|msg| msg.split(" ") & filter_words).size > 0} | |
| # Terminates early as soon as a match is found. | |
| # Runs in O(msg_words * filter_words) only in worst case (no match or last words match) | |
| # Average case is O(0.5 * msg_words * filter_words) | |
| leftover_words = messages.reject do |msg| | |
| words = msg.split(" ") | |
| words.find {|msg_word| filter_words.find {|filter_word| msg_word == filter_word}} | |
| end | |
| leftover_phrases = messages.reject do |msg| | |
| # Uses Array#find to return a true value as soon as a match is found | |
| filter_phrases.find {|phrase| msg.include?(phrase)} | |
| end | |
| leftover_both = leftover_words & leftover_phrases | |
| p leftover_words # => ["Foursquare annoys me", "I twote about tweeter"] | |
| p leftover_phrases # => ["First tweet", "Foursquare annoys me", "I am awesome #foursquare"] | |
| p leftover_both # => ["Foursquare annoys me"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment