如何将一个单词拆分为双元组,包括重复的词组? [英] How can I split a word into bi-grams, including repeated ones?

查看:41
本文介绍了如何将一个单词拆分为双元组,包括重复的词组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将单词拆分为双元词组.我正在使用 qlcMatrix 包,但它只返回不同的二元组.例如,对于单词 "detected",它只返回 "te" 一次.这是我使用的命令

I am trying to split a word into bi-grams. I am using the qlcMatrix package, but it only returns distinct bi-grams. For example, for the word "detected", it only returns "te" once. This is the command I use

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 基础的方法是使用 mapplysubstr:

Another way to do it with base R is to use mapply and substr:

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"

这篇关于如何将一个单词拆分为双元组,包括重复的词组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆