From f48a79473c5c9d59901c9dc66e2993e9afcf425f Mon Sep 17 00:00:00 2001 From: Thomas Sawyer Date: Tue, 24 Nov 2026 23:23:26 -0500 Subject: [PATCH 1/2] Add String#paragraphs, #sentences, #each_paragraph and #each_sentence From the Language gem, rewritten. Co-Authored-By: Claude Fable 5.2 --- HISTORY.md | 17 ++++++ demo/core/string/each_paragraph.md | 8 +++ demo/core/string/each_sentence.md | 8 +++ demo/core/string/paragraphs.md | 15 +++++ demo/core/string/sentences.md | 23 ++++++++ lib/core/facets/string.rb | 4 ++ lib/core/facets/string/each_paragraph.rb | 20 +++++++ lib/core/facets/string/each_sentence.rb | 20 +++++++ lib/core/facets/string/paragraphs.rb | 19 +++++++ lib/core/facets/string/sentences.rb | 38 +++++++++++++ test/core/string/test_paragraphs.rb | 65 +++++++++++++++++++++ test/core/string/test_sentences.rb | 72 ++++++++++++++++++++++++ 12 files changed, 309 insertions(+) create mode 100644 demo/core/string/each_paragraph.md create mode 100644 demo/core/string/each_sentence.md create mode 100644 demo/core/string/paragraphs.md create mode 100644 demo/core/string/sentences.md create mode 100644 lib/core/facets/string/each_paragraph.rb create mode 100644 lib/core/facets/string/each_sentence.rb create mode 100644 lib/core/facets/string/paragraphs.rb create mode 100644 lib/core/facets/string/sentences.rb create mode 100644 test/core/string/test_paragraphs.rb create mode 100644 test/core/string/test_sentences.rb diff --git a/HISTORY.md b/HISTORY.md index 3faf1006..bf76f1e7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,22 @@ # Facets Release History +## Unreleased + +Changes: + +* Additions + + * `String#paragraphs` splits a string at its blank lines: + `"One.\n\nTwo.".paragraphs` is `["One.", "Two."]`. + * `String#sentences` splits a string into sentences by their closing + punctuation: `"It works. Does it? Yes!".sentences` is + `["It works.", "Does it?", "Yes!"]`. It goes by punctuation alone, so + an abbreviation such as "Dr." is taken for the end of a sentence. + * `String#each_paragraph` and `String#each_sentence` iterate over them. + All four load with `require 'facets'`. They come from the Language + gem, rewritten. + + ## 4.2.0 / 2026-11-17 This release brings back four libraries that began in Facets and were diff --git a/demo/core/string/each_paragraph.md b/demo/core/string/each_paragraph.md new file mode 100644 index 00000000..8ff81880 --- /dev/null +++ b/demo/core/string/each_paragraph.md @@ -0,0 +1,8 @@ +## String#each_paragraph + + require 'facets/string/each_paragraph' + + a = [] + "One.\n\nTwo.".each_paragraph{ |para| a << para } + a.assert == ["One.", "Two."] + diff --git a/demo/core/string/each_sentence.md b/demo/core/string/each_sentence.md new file mode 100644 index 00000000..e767e3fa --- /dev/null +++ b/demo/core/string/each_sentence.md @@ -0,0 +1,8 @@ +## String#each_sentence + + require 'facets/string/each_sentence' + + a = [] + "It works. Does it?".each_sentence{ |sentence| a << sentence } + a.assert == ["It works.", "Does it?"] + diff --git a/demo/core/string/paragraphs.md b/demo/core/string/paragraphs.md new file mode 100644 index 00000000..998b820f --- /dev/null +++ b/demo/core/string/paragraphs.md @@ -0,0 +1,15 @@ +## String#paragraphs + + require 'facets/string/paragraphs' + +Paragraphs are separated by blank lines. + + x = "One.\nStill one.\n\nTwo." + x.paragraphs.assert == ["One.\nStill one.", "Two."] + +Any number of blank lines makes one separator, and a line holding only +whitespace is blank. + + x = "a\n\n \n\nb" + x.paragraphs.assert == ["a", "b"] + diff --git a/demo/core/string/sentences.md b/demo/core/string/sentences.md new file mode 100644 index 00000000..d5e97705 --- /dev/null +++ b/demo/core/string/sentences.md @@ -0,0 +1,23 @@ +## String#sentences + + require 'facets/string/sentences' + + x = "It works. Does it? Yes!" + x.sentences.assert == ["It works.", "Does it?", "Yes!"] + +A period that isn't followed by whitespace doesn't end a sentence. + + x = "Pi is 3.14. See pi.rb." + x.sentences.assert == ["Pi is 3.14.", "See pi.rb."] + +A blank line ends a sentence, so a heading stands on its own. + + x = "A Heading\n\nFirst. Second." + x.sentences.assert == ["A Heading", "First.", "Second."] + +The method goes by punctuation alone, so it takes an abbreviation for the +end of a sentence. + + x = "Dr. Who is in." + x.sentences.assert == ["Dr.", "Who is in."] + diff --git a/lib/core/facets/string.rb b/lib/core/facets/string.rb index 001e1ab4..f25dfcaf 100644 --- a/lib/core/facets/string.rb +++ b/lib/core/facets/string.rb @@ -9,6 +9,8 @@ require_relative 'string/compress_lines.rb' #require_relative 'string/crypt.rb' # uncommon require_relative 'string/divide.rb' +require_relative 'string/each_paragraph.rb' +require_relative 'string/each_sentence.rb' require_relative 'string/each_word.rb' require_relative 'string/edit_distance.rb' require_relative 'string/ends_with.rb' @@ -36,6 +38,7 @@ require_relative 'string/op_div.rb' require_relative 'string/op_sub.rb' # included in `remove`. require_relative 'string/outdent.rb' +require_relative 'string/paragraphs.rb' require_relative 'string/pathize.rb' require_relative 'string/quote.rb' require_relative 'string/random.rb' @@ -46,6 +49,7 @@ require_relative 'string/rewrite.rb' #require_relative 'integer/roman.rb' # uncommon require_relative 'string/rotate.rb' +require_relative 'string/sentences.rb' require_relative 'string/shatter.rb' require_relative 'string/similarity.rb' require_relative 'string/snakecase.rb' diff --git a/lib/core/facets/string/each_paragraph.rb b/lib/core/facets/string/each_paragraph.rb new file mode 100644 index 00000000..059579c4 --- /dev/null +++ b/lib/core/facets/string/each_paragraph.rb @@ -0,0 +1,20 @@ +require 'facets/string/paragraphs' + +class String + + # Iterate through each paragraph of a string. + # + # a = [] + # + # "One.\n\nTwo.".each_paragraph { |para| a << para } + # + # a #=> ["One.", "Two."] + # + # Without a block, returns an enumerator. + # + def each_paragraph(&block) + return paragraphs.each unless block + paragraphs.each(&block) + end + +end diff --git a/lib/core/facets/string/each_sentence.rb b/lib/core/facets/string/each_sentence.rb new file mode 100644 index 00000000..a68af121 --- /dev/null +++ b/lib/core/facets/string/each_sentence.rb @@ -0,0 +1,20 @@ +require 'facets/string/sentences' + +class String + + # Iterate through each sentence of a string. + # + # a = [] + # + # "It works. Does it?".each_sentence { |sentence| a << sentence } + # + # a #=> ["It works.", "Does it?"] + # + # Without a block, returns an enumerator. + # + def each_sentence(&block) + return sentences.each unless block + sentences.each(&block) + end + +end diff --git a/lib/core/facets/string/paragraphs.rb b/lib/core/facets/string/paragraphs.rb new file mode 100644 index 00000000..2bedf2ef --- /dev/null +++ b/lib/core/facets/string/paragraphs.rb @@ -0,0 +1,19 @@ +class String + + # Returns an array of the paragraphs in a string. Paragraphs are + # separated by one or more blank lines, and a line holding only + # whitespace counts as blank. + # + # "One.\nStill one.\n\nTwo.".paragraphs #=> ["One.\nStill one.", "Two."] + # + # The lines of a paragraph are left as they are. Blank lines at the + # start and end of the string are ignored, as is whitespace at the end + # of each paragraph. + # + # CREDIT: Trans + # + def paragraphs + strip.split(/(?:\r?\n[ \t]*){2,}/).map{ |para| para.rstrip }.reject{ |para| para.empty? } + end + +end diff --git a/lib/core/facets/string/sentences.rb b/lib/core/facets/string/sentences.rb new file mode 100644 index 00000000..e4bda5ab --- /dev/null +++ b/lib/core/facets/string/sentences.rb @@ -0,0 +1,38 @@ +require 'facets/string/paragraphs' + +class String + + # Returns an array of the sentences in a string. + # + # "It works. Does it? Yes!".sentences #=> ["It works.", "Does it?", "Yes!"] + # + # A sentence ends with a period, question mark, exclamation mark or + # ellipsis, along with any quotation marks or brackets that close it, + # followed by whitespace or the end of the text. So the period in a + # number or a file name doesn't end one. + # + # "Pi is 3.14. See pi.rb.".sentences #=> ["Pi is 3.14.", "See pi.rb."] + # + # 'He said "Stop." Then he left.'.sentences + # #=> ["He said \"Stop.\"", "Then he left."] + # + # A blank line ends a sentence too, so a heading with no period is a + # sentence of its own. The full-width marks of Chinese and Japanese end + # a sentence without needing a space after them. + # + # This goes by punctuation alone and knows nothing of any language, so + # an abbreviation is taken for the end of a sentence: + # + # "Dr. Who is in.".sentences #=> ["Dr.", "Who is in."] + # + # CREDIT: Trans + # + def sentences + close = %q{["'”’»)\]]*} + re = /.+?(?:[.!?…]+#{close}(?=\s|\z)|[。!?]+[」』”’]*|\z)/m + paragraphs.flat_map do |para| + para.scan(re).map{ |sentence| sentence.strip }.reject{ |sentence| sentence.empty? } + end + end + +end diff --git a/test/core/string/test_paragraphs.rb b/test/core/string/test_paragraphs.rb new file mode 100644 index 00000000..3868420c --- /dev/null +++ b/test/core/string/test_paragraphs.rb @@ -0,0 +1,65 @@ +covers 'facets/string/paragraphs' +covers 'facets/string/each_paragraph' + +test_case String do + + method :paragraphs do + + test "blank lines separate paragraphs" do + "One.\n\nTwo.".paragraphs.assert == ["One.", "Two."] + end + + test "the lines of a paragraph are kept" do + "One.\nStill one.\n\nTwo.".paragraphs.assert == ["One.\nStill one.", "Two."] + end + + test "several blank lines are one separator" do + "a\n\n\n\nb".paragraphs.assert == ["a", "b"] + end + + test "a line of only whitespace is blank" do + "a\n \t\nb".paragraphs.assert == ["a", "b"] + end + + test "blank lines at the start and end are ignored" do + "\n\n a\n\nb \n\n".paragraphs.assert == ["a", "b"] + end + + test "Windows line endings" do + "a\r\n\r\nb\r\n".paragraphs.assert == ["a", "b"] + end + + test "one paragraph" do + "just this".paragraphs.assert == ["just this"] + end + + test "an empty or blank string has none" do + "".paragraphs.assert == [] + " \n\n ".paragraphs.assert == [] + end + + test "the string is not changed" do + s = "a\n\nb" + s.paragraphs + s.assert == "a\n\nb" + end + + end + + method :each_paragraph do + + test do + a = [] + "One.\n\nTwo.".each_paragraph{ |para| a << para } + a.assert == ["One.", "Two."] + end + + test "without a block gives an enumerator" do + e = "One.\n\nTwo.".each_paragraph + e.assert.is_a?(Enumerator) + e.to_a.assert == ["One.", "Two."] + end + + end + +end diff --git a/test/core/string/test_sentences.rb b/test/core/string/test_sentences.rb new file mode 100644 index 00000000..e7bf9508 --- /dev/null +++ b/test/core/string/test_sentences.rb @@ -0,0 +1,72 @@ +covers 'facets/string/sentences' +covers 'facets/string/each_sentence' + +test_case String do + + method :sentences do + + test "periods, question marks and exclamation marks" do + "It works. Does it? Yes!".sentences.assert == ["It works.", "Does it?", "Yes!"] + end + + test "a period inside a number or a name doesn't end a sentence" do + "Pi is 3.14. See pi.rb.".sentences.assert == ["Pi is 3.14.", "See pi.rb."] + end + + test "closing quotation marks and brackets stay with their sentence" do + 'He said "Stop." Then he left.'.sentences.assert == ['He said "Stop."', "Then he left."] + "(See below.) Next.".sentences.assert == ["(See below.)", "Next."] + "\u201cStop.\u201d He left.".sentences.assert == ["\u201cStop.\u201d", "He left."] + end + + test "marks that come together" do + "Really?! Yes.".sentences.assert == ["Really?!", "Yes."] + "Wait... what?".sentences.assert == ["Wait...", "what?"] + "Wait\u2026 what?".sentences.assert == ["Wait\u2026", "what?"] + end + + test "text without a final mark is a sentence" do + "One. and the rest".sentences.assert == ["One.", "and the rest"] + "no stop at all".sentences.assert == ["no stop at all"] + end + + test "a sentence can run over lines" do + "One\ntwo. Three.".sentences.assert == ["One\ntwo.", "Three."] + end + + test "a blank line ends a sentence" do + "A Heading\n\nFirst. Second.".sentences.assert == ["A Heading", "First.", "Second."] + end + + test "full-width marks need no space after them" do + "\u3053\u308c\u3002\u3042\u308c\u3002".sentences.assert == ["\u3053\u308c\u3002", "\u3042\u308c\u3002"] + end + + test "abbreviations are taken for sentence ends" do + "Dr. Who is in.".sentences.assert == ["Dr.", "Who is in."] + end + + test "an empty or blank string has none" do + "".sentences.assert == [] + " \n ".sentences.assert == [] + end + + end + + method :each_sentence do + + test do + a = [] + "It works. Does it?".each_sentence{ |sentence| a << sentence } + a.assert == ["It works.", "Does it?"] + end + + test "without a block gives an enumerator" do + e = "It works. Does it?".each_sentence + e.assert.is_a?(Enumerator) + e.to_a.assert == ["It works.", "Does it?"] + end + + end + +end From 9ca041aa2b984077ac2258c72a6b3dda3217758f Mon Sep 17 00:00:00 2001 From: Thomas Sawyer Date: Tue, 24 Nov 2026 23:23:26 -0500 Subject: [PATCH 2/2] TODO: pillage ProUtils next Co-Authored-By: Claude Fable 5.2 --- work/TODO.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/work/TODO.md b/work/TODO.md index 877203c7..c7636e93 100644 --- a/work/TODO.md +++ b/work/TODO.md @@ -2,6 +2,12 @@ Small follow-ups that aren't GitHub issues. Newest first. +## Pillage ProUtils next (2026-11-24) + +Go through the ProUtils libraries for methods and small classes that +belong in Facets, as was done for Radix, Instance, Paramix and Strmask +in 4.2.0, and for the Language gem's `sentences` and `paragraphs`. + ## Date and Time comparisons (2026-11-08) Plain Ruby can't compare a Date with a Time: `<=>` returns nil, so `<`, `>`,