Groovyで文字列を段落に分ける

Rubyレシピブックと同様に「段落」を以下の定義とする
1)日本語ルール:改行(\n)が段落の区切りを示す
2)英語ルール:空行が段落の区切りを示す


日本語でも、ブログだと読みやすさから英語ルールの記載の方が多いと思うけど、
一応上記の定義とする。
例文は、amazonでのGroovy イン・アクションの紹介文。

日本語版

Groovyとは「Javaのためのスクリプト言語」ですが、その一言だけで語れるものではありません。公式サイトでは、「GroovyはJavaプラットフォームのためのアジャイルで動的な言語である。PythonRubySmalltalkなどの言語から多くの機能を取り入れ、Java開発者が Java風の構文を使ってこれらを利用できるようにしている」と述べられています。これは、どういうことでしょうか?
GroovyはJava(とGroovy自体)で書かれており、そのコードはJavaバイトコードにプリコンパイルすることも可能です。つまり、Groovyでプログラムを書くということは、特別な種類のJavaプログラムを書いているということにほかなりません。
Groovy はJava仮想マシンの上で実行され、Javaのライブラリを利用します。また、その構文はJavaと整合性を備えています。このような「Javaとの親和性」を持ちつつも、RubyPythonのような動的言語の特長も有しているのがGroovyなのです。
そして、Java言語自体に次いで、GroovyはJavaプラットフォーム上で2番目の標準言語にもなりました。さらに、「Groovy版Ruby on Rails」とも呼べる「Grails」というWebフレームワークの開発も進んでいます(本書でも、1章を割いて紹介しています)。
Groovyの魅力はまだまだありますが、ここに挙げただけでも、Java開発者であればGroovyを試さない理由はないでしょう。

英語版

Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language.

Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively.

Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:

* Simple and collective Groovy data types
* Working with Closures and Groovy Control Structures
* Dynamic Object Orientation, Groovy style

Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including

* How to Work with Builders and the GDK
* Database programming with Groovy

Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,

* Tips and Tricks
* Unit Testing and Build Support
* Groovy on Windows

An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework.

ソース

Groovy_in_Action_japanese =
"""Groovyとは「Javaのためのスクリプト言語」ですが、その一言だけで語れるものではありません。公式サイトでは、「GroovyはJavaプラットフォームのためのアジャイルで動的な言語である。PythonやRuby、Smalltalkなどの言語から多くの機能を取り入れ、Java開発者が Java風の構文を使ってこれらを利用できるようにしている」と述べられています。これは、どういうことでしょうか?
GroovyはJava(とGroovy自体)で書かれており、そのコードはJavaバイトコードにプリコンパイルすることも可能です。つまり、Groovyでプログラムを書くということは、特別な種類のJavaプログラムを書いているということにほかなりません。
Groovy はJava仮想マシンの上で実行され、Javaのライブラリを利用します。また、その構文はJavaと整合性を備えています。このような「Javaとの親和性」を持ちつつも、RubyやPythonのような動的言語の特長も有しているのがGroovyなのです。
そして、Java言語自体に次いで、GroovyはJavaプラットフォーム上で2番目の標準言語にもなりました。さらに、「Groovy版Ruby on Rails」とも呼べる「Grails」というWebフレームワークの開発も進んでいます(本書でも、1章を割いて紹介しています)。
Groovyの魅力はまだまだありますが、ここに挙げただけでも、Java開発者であればGroovyを試さない理由はないでしょう。"""


Groovy_in_Action_english=
"""Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language.

Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively.

Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:

 * Simple and collective Groovy data types
 * Working with Closures and Groovy Control Structures
 * Dynamic Object Orientation, Groovy style

Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including

 * How to Work with Builders and the GDK
 * Database programming with Groovy

Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,

 * Tips and Tricks
 * Unit Testing and Build Support
 * Groovy on Windows
 
An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework. """


j = Groovy_in_Action_japanese.split(/\n/)
println j
println j.length
println ""

e = Groovy_in_Action_english.split(/\n\n/)
println e
println e.length
println ""

e_strict  = Groovy_in_Action_english.split(/\n[ \t\r\f]*\n/)
println e_strict
println e_strict.length
println ""

結果

[D:\workspace\groovy_SandBox]groovy Q045_文字列を段落に分ける.groovy
[Groovyとは「Javaのためのスクリプト言語」ですが、その一言だけで語れるものではありません。公式サイトでは、「GroovyはJavaプラットフォームのためのアジャイルで動的
な言語である。PythonやRuby、Smalltalkなどの言語から多くの機能を取り入れ、Java開発者が Java風の構文を使ってこれらを利用できるようにしている」と述べられています。これは、どういうことでしょうか?, GroovyはJava(とGroovy自体)で書かれており、そのコードはJavaバイトコードにプリコンパイルすることも可能です。つまり、Groovyでプロ
グラムを書くということは、特別な種類のJavaプログラムを書いているということにほかなりません。, Groovy はJava仮想マシンの上で実行され、Javaのライブラリを利用しま
す。また、その構文はJavaと整合性を備えています。このような「Javaとの親和性」を持ちつつも、RubyやPythonのような動的言語の特長も有しているのがGroovyなのです。, そして、Java言語自体に次いで、GroovyはJavaプラットフォーム上で2番目の標準言語にもなりました。さらに、「Groovy版Ruby on Rails」とも呼べる「Grails」というWebフレー
ムワークの開発も進んでいます(本書でも、1章を割いて紹介しています)。, Groovyの魅力はまだまだありますが、ここに挙げただけでも、Java開発者であればGroovyを試さない
理由はないでしょう。]
5

[Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the
source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language., Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively., Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:,  * Simple and collective Groovy data types
 * Working with Closures and Groovy Control Structures
 * Dynamic Object Orientation, Groovy style, Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including,  * How to Work with Builders and the GDK
 * Database programming with Groovy, Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,,  * Tips and Tricks
 * Unit Testing and Build Support
 * Groovy on Windows

An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework. ]
8

[Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the
source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language., Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively., Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:,  * Simple and collective Groovy data types
 * Working with Closures and Groovy Control Structures
 * Dynamic Object Orientation, Groovy style, Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including,  * How to Work with Builders and the GDK
 * Database programming with Groovy, Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,,  * Tips and Tricks
 * Unit Testing and Build Support
 * Groovy on Windows, An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework. ]
9

説明

日本語版は\nで分割
(Groovyにもrubyのto_aに相当するメソッドがあるのかも知れないが分からなかったから。)

英語は\n\nで分割
英語厳密版は空行だけでなく、空白だけの行、タブだけの行なども区切りとして扱っている。
英語の紹介文には下から2行目が空白1文字を混ぜているので以下の差異がある。

実行メソッド 段落数
println e.length 8
println e_strict.length 9

実行環境














Groovyの詳細についてはJavadocと以下の書籍を参考にしている。

Groovyイン・アクション
Dierk Konig Andrew Glover Paul King Guillaume Laforge Jon Skeet
毎日コミュニケーションズ
売り上げランキング: 294340


問題自体は以下の書籍のもの。rubyと似てる部分も多いので、ヒントにもなる。
写経でもいいが自分で考えるために他言語の例をGroovyで置き換えてる。

Rubyレシピブック 第2版 268の技
青木 峰郎 後藤 裕蔵 高橋 征義
ソフトバンク クリエイティブ
売り上げランキング: 80467


Groovyイン・アクションを読むならあった方が便利かな。

ブックストッパー

トモエ算盤
売り上げランキング: 614


Rubyレシピブックは「ほんたった」で立ててる