首页 > 解决方案 > 拆分由点连接的两个单词

问题描述

我有一个包含新闻文章的大数据框。我注意到有些文章有两个由点连接的单词,如下例所示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在列表中使用吗?

标签: rregexstringstrsplit

解决方案


您可以使用

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/ftpor https/ftps,然后匹配://任何 0 个或多个非空白字符,并跳过匹配并继续从遇到 SKIP PCRE 动词时的位置搜索匹配。

推荐阅读