java - Scala/Java - 解析一些文本并删除标点符号的库?

标签 java scala text punctuation

我正在 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/

相关文章:

java - Spring boot - 由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext

scala - 当最后一列名称包含空格时,Spark 无法读取 CSV

java - 在这个Java套接字代码中,这个无限循环是否会因套接字关闭而终止?

scala - 如何在多模块构建中禁用根聚合模块的打包和发布任务?

java - Scala + Eclipse + Casbah = 对象 mongodb 不是包 com 的成员

node.js - 同步文本编辑

text - 连接文本文件的两列

ios - 单击按钮时如何更改按钮的文本? - swift

java - 这种多态性有特殊的名称吗? (一个类实现两个或多个接口(interface))

java - Intellij Idea CE 2018.2.1 中资源目录不包含在输出目录中