Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created July 20, 2012 20:34
Show Gist options
  • Save ashaw/3153072 to your computer and use it in GitHub Desktop.
Save ashaw/3153072 to your computer and use it in GitHub Desktop.
STOPWORDS = ["the","and","to","of","a","i","in","was","he","that","it","his","her","you","as","had","with","for","she","not","at","but","be","my","on","have","him","is","said","me","which","by","so","this","all","from","they","no","were","if","would","or","when","what","there","been","one","could","very","an"]
def snippet(terms)
text = ""
tokens = terms.split(/ /)
tokens.reject! {|q| STOPWORDS.include? q }
tokens.each do |tok|
text << self.first_message.clean_text.gsub(/\n/," ").
scan(/(?:[a-zA-Z0-9\.'-_\$,\:]+ ?){0,5}?#{tok}[ ,\:](?:[a-zA-Z0-9\.'-_\$,\:]+ ?){0,5}?/i).join(" ... ")
end
text.gsub!(/(#{tokens.join("|")})/i, '<strong>\1</strong>')
text
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment