给定一个英语单词列表,您可以通过查找列表中单词的每个可能的拆分来非常简单地完成此操作。我将使用我在单词列表中找到的第一个 Google 搜索结果,其中包含大约 70k 个小写单词:
wl <- read.table("http://www-personal.umich.edu/~jlawler/wordlist")$V1
check.word <- function(x, wl) {
x <- tolower(x)
nc <- nchar(x)
parts <- sapply(1:(nc-1), function(y) c(substr(x, 1, y), substr(x, y+1, nc)))
parts[,parts[1,] %in% wl & parts[2,] %in% wl]
}
这有时有效:
check.word("screenunable", wl)
# [1] "screen" "unable"
check.word("nowhere", wl)
# [,1] [,2]
# [1,] "no" "now"
# [2,] "where" "here"
但有时当相关单词不在单词列表中时也会失败(在这种情况下“传感器”丢失):
check.word("sensoradvise", wl)
#
# [1,]
# [2,]
"sensor" %in% wl
# [1] FALSE
"advise" %in% wl
# [1] TRUE