这是我的示例文本:
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/