Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 分句_R - Fatal编程技术网

R 分句

R 分句,r,R,我使用readLines导入文本文件,并折叠了所有行。现在我想写一个函数,在整个折叠文本中循环,并检测每个句子的结尾,为每个句子开始一行新行。它将检测(句号、问号、句号后跟引号,或问号后跟引号) 例如: "I need help. How do I write this code?" 将成为: I need help. How do I write this code? 有人知道我该怎么做吗?gsub可能会奏效 gsub('. ', '.\n', your_text) 替换'图案由'\n'

我使用readLines导入文本文件,并折叠了所有行。现在我想写一个函数,在整个折叠文本中循环,并检测每个句子的结尾,为每个句子开始一行新行。它将检测(句号、问号、句号后跟引号,或问号后跟引号)

例如:

"I need help. How do I write this code?"
将成为:

I need help.
How do I write this code?
有人知道我该怎么做吗?

gsub可能会奏效

gsub('. ', '.\n', your_text)
替换<代码>'图案由
'\n'
组成,它是换行符的符号

your_text = 'lets. try'
aa = gsub('. ', '.\n', your_text)
print(aa)
cat(aa)

我们可以使用正向查找正则表达式来匹配
或问号
”?“
,并将其替换为新行(
\n

str=“我需要帮助。如何编写此代码?”

cat(gsub)(“?我试过了,但是没有创建一个新行,而是用一个实际的“\n”替换了每个句点。我认为这是因为您需要使用cat函数输出ti
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?