Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions demo/core/string/each_paragraph.md
Original file line number Diff line number Diff line change
@@ -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."]

8 changes: 8 additions & 0 deletions demo/core/string/each_sentence.md
Original file line number Diff line number Diff line change
@@ -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?"]

15 changes: 15 additions & 0 deletions demo/core/string/paragraphs.md
Original file line number Diff line number Diff line change
@@ -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"]

23 changes: 23 additions & 0 deletions demo/core/string/sentences.md
Original file line number Diff line number Diff line change
@@ -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."]

4 changes: 4 additions & 0 deletions lib/core/facets/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand Down
20 changes: 20 additions & 0 deletions lib/core/facets/string/each_paragraph.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions lib/core/facets/string/each_sentence.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lib/core/facets/string/paragraphs.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions lib/core/facets/string/sentences.rb
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions test/core/string/test_paragraphs.rb
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions test/core/string/test_sentences.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions work/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<`, `>`,
Expand Down