我使用readLines导入了一个文本文件,并折叠了所有行。现在,我想要编写一个函数,该函数将循环遍历整个折叠文本,并检测每个句子的末尾,为每个句子开始一个新行。它将检测(句点,问号,句点后加引号,或问号后加引号)
举个例子:
"I need help. How do I write this code?"
会变成:
I need help.
How do I write this code?
有没有人知道我该怎么做呢?
发布于 2020-04-06 07:20:37
gsub也许能行得通。
gsub('. ', '.\n', your_text)
将'. '
模式替换为'\n '
,它是换行符的符号。
your_text = 'lets. try'
aa = gsub('. ', '.\n', your_text)
print(aa)
cat(aa)
发布于 2020-04-06 07:40:05
我们可以使用正则表达式后面的正则表达式来匹配"."
或问号"?"
,并将其替换为新行(\n
)。
str = "I need help. How do I write this code? "
cat(gsub('(?<=[.?])\\s', '\n', str, perl = TRUE))
#I need help.
#How do I write this code?
https://stackoverflow.com/questions/61053770
复制相似问题