r - 按句子拆分文本,但不按特殊模式拆分

标签 r regex split

这是我的示例文本:

text = "First sentence. This is a second sentence. I like pets e.g. cats or birds."

我有一个按句子拆分文本的功能
library(stringi)
split_by_sentence <- function (text) {

  # split based on periods, exclams or question marks
  result <- unlist(strsplit(text, "\\.\\s|\\?|!") )

  result <- stri_trim_both(result)
  result <- result [nchar (result) > 0]

  if (length (result) == 0)
    result <- ""

  return (result)
}

它实际上是由标点符号分割的。这是输出:
> split_by_sentence(text)
[1] "First sentence"            "This is a second sentence" "I like pets e.g"           "cats or birds." 

是否有可能排除诸如“例如”之类的特殊模式?

最佳答案

在您的模式中,您可以指定要在任何后跟空格的标点符号处进行拆分,前提是它之前至少有 2 个字母数字字符(使用环视)。这将导致:

unlist(strsplit(text, "(?<=[[:alnum:]]{3})[?!.]\\s", perl=TRUE))
#[1] "First sentence"                  "This is a second sentence"       "I like pets e.g. cats or birds."

如果你想保留标点符号,那么你可以在后视中添加模式并且只在空间上分割:
unlist(strsplit(text, "(?<=[[:alnum:]]{3}[[?!.]])\\s", perl=TRUE))
# [1] "First sentence."                 "This is a second sentence."      "I like pets e.g. cats or birds."

text2 <- "I like pets (cats and birds) and horses. I have 1.8 bn. horses."

unlist(strsplit(text2, "(?<=[[:alnum:]]{3}[?!.])\\s", perl=TRUE))
#[1] "I like pets (cats and birds) and horses." "I have 1.8 bn. horses."

注意:如果标点符号后可能有多个空格,可以输入 \\s+而不是 \\s在模式中

关于r - 按句子拆分文本,但不按特殊模式拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47828661/

相关文章:

r - 如何使用 R 中的机器学习和 Caret 包在新数据集上测试调整后的 SVM 模型?

R data.table 按两列分组和迭代

正则表达式前瞻、后瞻和原子组

php - 使用正则表达式检索字符串中特殊字符之间的链接

regex - Linux 将多个文本文件解析成单独的文件

c# - 字符串拆分逻辑 C#

r - 如何控制在ggplot2中首先绘制哪个因素?

r - 如何正确使用 `[` 和 (l|s)apply 从矩阵列表中选择特定列?

regex - 初学者 perl 问题

python - 如何使用 Python 分隔文件名的数字部分?