1

BreakIterator文字列から句読点を削除するためにJavaの実装を使用しています。これをScalaで書き直す必要があるので、これをより良いライブラリに置き換える良い機会かもしれないと考えました(私の実装は非常に素朴で、エッジケースでは失敗すると確信しています)。

使用される可能性のあるそのようなライブラリはありますか?

編集:これがScalaでの私の簡単な解決策です:

  private val getWordsFromLine = (line: String) => {
    line.split(" ")
        .map(_.toLowerCase())
        .map(word => word.filter(Character.isLetter(_)))
        .filter(_.length() > 1)
        .toList
  }

そしてこれを考えるとList[String](各行に1つ...そしてそうです...それは聖書です-それは良いテストケースになります):

モーセの第二の本、呼ばれる脱出

第1章1さて、これらはエジプトにやって来たイスラエルの人々の名である。すべての人と彼の家族はジェイコブと一緒に来ました。2ルベン、シメオン、レビ、ユダ、3イッサカル、ゼブルン、ベンジャミン、4ダン、ナフタリ、ガド、アシェル。

あなたはそのList[String]ようになります:

List(the, second, book, of, moses, called, exodus, chapter, now, these, are, the, names, of, the, children, of, israel, which, came, into, egypt, every, man, and, his, household, came, with, jacob, reuben, simeon, levi, and, judah, issachar, zebulun, and, benjamin, dan, and, naphtali, gad, and, asher)
4

2 に答える 2

2

この特定のケースでは、正規表現を使用します。

def toWords(lines: List[String]) = lines flatMap { line =>
  "[a-zA-Z]+".r findAllIn line map (_.toLowerCase)
}
于 2012-08-08T18:35:43.087 に答える
0

これは正規表現を使用したアプローチです。ただし、1 文字の単語はまだフィルタリングされません。

val s = """
THE SECOND BOOK OF MOSES, CALLED EXODUS

CHAPTER 1 1 Now these [are] the names of the children of Israel,
which came into Egypt; every man and his household came with
Jacob. 2 Reuben, Simeon, Levi, and Judah, 3 Issachar, Zebulun,
and Benjamin, 4 Dan, and Naphtali, Gad, and Asher.
"""

/* \p{L} denotes Unicode letters */
var items = """\b\p{L}+\b""".r findAllIn s

println(items.toList)
  /* List(THE, SECOND, BOOK, OF, MOSES, CALLED, EXODUS,
          CHAPTER, Now, these, are, the, names, of, the,
          children, of, Israel, which, came, into, Egypt,
          every, man, and, his, household, came, with,
          Jacob, Reuben, Simeon, Levi, and, Judah,
          Issachar, Zebulun, and, Benjamin, Dan, and,
          Naphtali, Gad, and, Asher)
  */

/* \w denotes word characters */
items = """\b\w+\b""".r findAllIn s
println(items.toList)
  /* List(THE, SECOND, BOOK, OF, MOSES, CALLED, EXODUS,
          CHAPTER, 1, 1, Now, these, are, the, names, of,
          the, children, of, Israel, which, came, into,
          Egypt, every, man, and, his, household, came,
          with, Jacob, 2, Reuben, Simeon, Levi, and, Judah,
          3, Issachar, Zebulun, and, Benjamin, 4, Dan, and,
          Naphtali, Gad, and, Asher)
  */

単語の境界についてはここ\bで説明されています。正規表現の Javadoc はここにあります。

于 2012-08-08T13:10:47.377 に答える