【问题标题】:How to split a text into two meaningful words in R如何在R中将文本拆分为两个有意义的单词
【发布时间】:2023-03-25 21:52:01
【问题描述】:

这是我的数据框 df 中的文本,其中有一个名为“problem_note_text”的文本列

SSCI 问题:票据分配器故障执行检查/分配器故障/要求商店将票据分配器取出并放回原处/仍然错误消息显示前门已打开/因此 CE attn req联系方式详细信息 - Olivia taber 01159063390 / 上午 7 点至晚上 11 点

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)

现在有一个数据框,它有一个单词列表,但有像

这样的单词

“失败”

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

“失败”“执行”。

我该怎么做,单词数据框也包含像

这样的单词

“我”,“h”

这没有意义,必须删除,我不知道如何实现。

【问题讨论】:

  • 如果没有模式,则不可行
  • 你会如何对待 nowhere 之类的东西 - 如 nowherenowhere
  • 我在想也许有一些可用的字典可以用来解析句子。我使用qdap包all_words函数从我有的句子中取出单词,但是似乎有几个单词没有被很好地解析,我得到了没有意义的联合词。
  • 你能分享一段数据吗?如果传感器建议在您的文档中作为两个单独的单词出现,您可以更改预处理以避免丢失空间。
  • 我猜这可能是因为您在原始数据中有连字符的分隔字符(即sensor-advised)。如果您可以分享一些导致问题的数据(简单的搜索应该会显示导致问题的初始单词),我们可以更好地指导您。以下 qdap 小插图可以帮助调试和清理文本以隔离问题:cran.r-project.org/web/packages/qdap/vignettes/…

标签: r string-split stemming text-analysis


【解决方案1】:

给定一个英语单词列表,您可以非常简单地通过查找列表中单词的每个可能拆分来做到这一点。我将使用在我的单词列表中找到的第一个 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

【讨论】:

    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2020-03-25
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多