r - 拆分由点连接的两个单词
问题描述
我有一个包含新闻文章的大数据框。我注意到有些文章有两个由点连接的单词,如下例所示The government.said it was important to quit.
。我将进行一些主题建模,因此我需要将每个单词分开。
这是我用来分隔这些词的代码
#String example
test <- c("i need.to separate the words connected by dots. however, I need.to keep having the dots separating sentences")
#Code to separate the words
test <- do.call(paste, as.list(strsplit(test, "\\.")[[1]]))
#This is what I get
> test
[1] "i need to separate the words connected by dots however, I need to keep having the dots separating sentences"
如您所见,我删除了文本上的所有点(句点)。我怎么能得到以下结果:
"i need to separate the words connected by dots. however, I need to keep having the dots separating sentences"
最后说明
我的数据框由 17.000 篇文章组成;所有的文字都是小写的。我只是提供了一个小例子来说明我在尝试分隔由点连接的两个单词时遇到的问题。另外,有什么方法可以strsplit
在列表中使用吗?
解决方案
您可以使用
test <- c("i need.to separate the words connected by dots. however, I need.to keep having the dots separating sentences. Look at http://google.com for s.0.m.e more details.")
# Replace each dot that is in between word characters
gsub("\\b\\.\\b", " ", test, perl=TRUE)
# Replace each dot that is in between letters
gsub("(?<=\\p{L})\\.(?=\\p{L})", " ", test, perl=TRUE)
# Replace each dot that is in between word characters, but no in URLs
gsub("(?:ht|f)tps?://\\S*(*SKIP)(*F)|\\b\\.\\b", " ", test, perl=TRUE)
在线查看R 演示。
输出:
[1] "i need to separate the words connected by dots. however, I need to keep having the dots separating sentences. Look at http://google com for s 0 m e more details."
[1] "i need to separate the words connected by dots. however, I need to keep having the dots separating sentences. Look at http://google com for s.0.m e more details."
[1] "i need to separate the words connected by dots. however, I need to keep having the dots separating sentences. Look at http://google.com for s 0 m e more details."
细节
\b\.\b
- 一个被单词边界包围的点(即前后.
不能是任何非单词字符,除了字母、数字或下划线之外不能有任何字符(?<=\p{L})\.(?=\p{L})
匹配一个不紧跟在字母之前或之后的点((?<=\p{L})
是一个否定的lookbehind并且(?=\p{L})
是一个否定的lookahead)(?:ht|f)tps?://\\S*(*SKIP)(*F)|\b\.\b
匹配http/ftp
orhttps/ftps
,然后匹配://
任何 0 个或多个非空白字符,并跳过匹配并继续从遇到 SKIP PCRE 动词时的位置搜索匹配。
推荐阅读
- reactjs - React Navigation ReferenceError:找不到变量:isSelectionModeEnabled
- python - Pandas remove reversed duplicates across two columns
- node.js - 如何在 Node 上运行 Typescript 脚本而不从中导入任何内容?
- python - 将唯一值从一个熊猫数据帧映射到另一个数据帧的最快方法是什么?
- javascript - 创建多维对象的函数
- node.js - dynamodb deleteItem 有效,但它的回调返回未定义
- javascript - 检测 VSCode 中未使用的 JavaScript 方法
- javascript - NPM 包描述有问题
- audio - ffmpeg - 在输出到 rtmp 之前正确处理未对齐的音频/视频输入流
- r - 计算滚动窗口上的自回归 AR1 模型系数