如何在R中将文本拆分为两个有意义的单词

Shw*_*ble 3 r text-analysis stemming string-split

这是我的dataframe df中的文本,其中有一个名为'problem_note_text'的文本列

SSCIssue:注意分配器故障执行检查/分配器故障/要求商店取出纸币分配器并将其设置回/仍然错误消息说前门打开/因此CE attn reqContact详细信息 - Olivia taber 01159063390/7 am-11pm

df$problem_note_text <- tolower(df$problem_note_text)
df$problem_note_text <- tm::removeNumbers(df$problem_note_text)
df$problem_note_text<- str_replace_all(df$problem_note_text, "  ", "") # replace double spaces with single space
df$problem_note_text = str_replace_all(df$problem_note_text, pattern = "[[:punct:]]", " ")
df$problem_note_text<- tm::removeWords(x = df$problem_note_text, stopwords(kind = 'english'))
Words = all_words(df$problem_note_text, begins.with=NULL)
Run Code Online (Sandbox Code Playgroud)

现在有一个数据框,其中包含一个单词列表,但有一些单词

"Failureperformed"

需要分成两个有意义的词,比如

"失败""表演".

我该怎么做呢,dataframe这个词也包含像

"im","h"

哪些没有意义,必须删除,我不知道如何实现这一点.

jos*_*ber 7

给出一个英文单词列表,您可以通过查找列表中单词的每个可能分割来完成此操作.我将使用我在单词列表中找到的第一个Google点击,其中包含大约70,000个小写单词:

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]
}
Run Code Online (Sandbox Code Playgroud)

这有时有效:

check.word("screenunable", wl)
# [1] "screen" "unable"
check.word("nowhere", wl)
#      [,1]    [,2]  
# [1,] "no"    "now" 
# [2,] "where" "here"
Run Code Online (Sandbox Code Playgroud)

但是,当相关单词不在单词列表中时(有时"传感器"缺失),有时也会失败:

check.word("sensoradvise", wl)
#     
# [1,]
# [2,]
"sensor" %in% wl
# [1] FALSE
"advise" %in% wl
# [1] TRUE
Run Code Online (Sandbox Code Playgroud)