Created
June 3, 2010 20:40
Revisions
-
coryschires revised this gist
Jun 3, 2010 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains 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 charactersOriginal file line number Diff line number Diff line change @@ -9,11 +9,12 @@ def sentence_truncate(string, num_sentences, enforce_max_length=true) sentence << matches[sentences.index(sentence)] end.join("").chop # optionally enforce max length in case of really long sentences if enforce_max_length max_length = num_sentences*100 truncated_sentence = truncate(truncated_sentence, :length => max_length) if truncated_sentence.length > max_length end truncated_sentence end -
coryschires revised this gist
Jun 3, 2010 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ # Based on http://stackoverflow.com/questions/1293573/rails-smart-text-truncation def sentence_truncate(string, num_sentences, enforce_max_length=true) pattern = /[\.\?\!]{1,}\s/ matches = string.scan(pattern) sentences = string.split(pattern) @@ -10,7 +10,10 @@ def sentence_truncate(string, num_sentences) end.join("").chop # force max length in case of really long sentences if enforce_max_length max_length = num_sentences*100 truncated_sentence = truncate(truncated_sentence, :length => max_length) if truncated_sentence.length > max_length end truncated_sentence end -
coryschires revised this gist
Jun 3, 2010 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,5 @@ # Based on http://stackoverflow.com/questions/1293573/rails-smart-text-truncation def sentence_truncate(string, num_sentences) pattern = /[\.\?\!]{1,}\s/ matches = string.scan(pattern) sentences = string.split(pattern) -
coryschires created this gist
Jun 3, 2010 .There are no files selected for viewing
This file contains 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ # Based on http://stackoverflow.com/questions/1293573/rails-smart-text-truncation def sentence_truncate(string, num_sentences) string = scrub_html(string) pattern = /[\.\?\!]{1,}\s/ matches = string.scan(pattern) sentences = string.split(pattern) num_sentences = matches.length if matches.length < num_sentences truncated_sentence = sentences[0...num_sentences].map do |sentence| sentence << matches[sentences.index(sentence)] end.join("").chop # force max length in case of really long sentences truncated_sentence = truncate(truncated_sentence, :length => 350) if truncated_sentence.length > 350 truncated_sentence end # rspec test it "should split a text block on sentences" do text = "I am a sentence about radio station 95.5 WHOT. What a hot station! I love that one. Don't you? Well, onto other things, my friend. Like the fourth sentence with a period. And the fifth." sentence_truncate(text, 4).should == "I am a sentence about radio station 95.5 WHOT. What a hot station! I love that one. Don't you?" end