如何将单词拆分为双字节,包括重复单词?

问题描述 投票:3回答:2

我想把一个单词分成双字组。我正在使用qlcMatrix包,但它只返回不同的二元组。例如,对于单词"detected",它只返回一次"te"。这是我使用的命令

test_domain <- c("detected")
library("qlcMatrix", lib.loc="~/R/win-library/3.2")
bigram1 <- splitStrings(test_domain, sep = "", bigrams = TRUE, left.boundary = "", right.boundary = "")$bigrams

这是我得到的结果:

bigram1
# [1] "ec" "ed" "de" "te" "ct" "et"
r text-processing
2个回答
7
投票

使用R基础的另一种方法是使用mapplysubstr

nc <- nchar("detected")
mapply(function(x, y){substr("detected", x, y)}, x=1:(nc-1), y=2:nc)
# [1] "de" "et" "te" "ec" "ct" "te" "ed"

5
投票

没有包你可以这样做:

test_domain <- c("detected")
temp <- strsplit(test_domain ,'')[[1]]
sapply(1:(length(temp)-1), function(x){paste(temp[x:(x+1)], collapse='')})
# [1] "de" "et" "te" "ec" "ct" "te" "ed"
© www.soinside.com 2019 - 2024. All rights reserved.