Skip to content

Instantly share code, notes, and snippets.

@coryschires
Created June 3, 2010 20:40

Revisions

  1. coryschires revised this gist Jun 3, 2010. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion sentence_truncate_helper.rb
    Original 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

    # force max length in case of really long sentences
    # 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

  2. coryschires revised this gist Jun 3, 2010. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions sentence_truncate_helper.rb
    Original 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)
    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
    truncated_sentence = truncate(truncated_sentence, :length => 350) if truncated_sentence.length > 350
    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

  3. coryschires revised this gist Jun 3, 2010. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions sentence_truncate_helper.rb
    Original 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)
    string = scrub_html(string)

    pattern = /[\.\?\!]{1,}\s/
    matches = string.scan(pattern)
    sentences = string.split(pattern)
  4. coryschires created this gist Jun 3, 2010.
    25 changes: 25 additions & 0 deletions sentence_truncate_helper.rb
    Original 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