我正在 Java 中使用 BreakIterator
实现来删除字符串中的标点符号。我需要在 Scala 中重写它,所以我认为这可能是一个用更好的库替换它的好机会(我的实现非常幼稚,而且我确信它在边缘情况下会失败)。
是否存在可以使用的此类库?
编辑:这是我在 Scala 中的快速解决方案:
private val getWordsFromLine = (line: String) => {
line.split(" ")
.map(_.toLowerCase())
.map(word => word.filter(Character.isLetter(_)))
.filter(_.length() > 1)
.toList
}
给出这个List[String]
(每行一个......是的......这就是圣经 - 它是很好的测试用例):
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.
你会得到一个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)
最佳答案
对于这种特殊情况,我会使用正则表达式。
def toWords(lines: List[String]) = lines flatMap { line =>
"[a-zA-Z]+".r findAllIn line map (_.toLowerCase)
}
关于java - Scala/Java - 解析一些文本并删除标点符号的库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11861069/