r - 如何使用 hunspell 包在 R 中的列中建议正确的单词?

标签 r spell-checking hunspell

我目前正在处理每行包含大量文本的大型数据框,并希望使用 hunspell 有效识别和替换每个句子中拼写错误的单词。包裹。我能够识别拼写错误的单词,但不知道该怎么做 hunspell_suggest在名单上。

这是数据框的示例:

df1 <- data.frame("Index" = 1:7, "Text" = c("A complec sentence joins an independet",
                                            "Mary and Samantha arived at the bus staton before noon",
                                            "I did not see thm at the station in the mrning",
                                            "The participnts read 60 sentences in radom order",
                                            "how to fix mispelled words in R languge",
                                            "today is Tuesday",
                                            "bing sports quiz"))

我将文本列转换为字符并使用 hunspell识别每行中拼写错误的单词。
library(hunspell)
df1$Text <- as.character(df1$Text)
df1$word_check <- hunspell(df1$Text)

我试过
df1$suggest <- hunspell_suggest(df1$word_check)

但它不断给出这个错误:
Error in hunspell_suggest(df1$word_check) : 
  is.character(words) is not TRUE

我是新手,所以我不太确定建议列如何使用 hunspell_suggest功能就会出来。任何帮助将不胜感激。

最佳答案

检查您的中间步骤。 df1$word_check的输出如下:

List of 5
 $ : chr [1:2] "complec" "independet"
 $ : chr [1:2] "arived" "staton"
 $ : chr [1:2] "thm" "mrning"
 $ : chr [1:2] "participnts" "radom"
 $ : chr [1:2] "mispelled" "languge"

类型为 list .如果你这样做了 lapply(df1$word_check, hunspell_suggest)你可以得到建议。

编辑

我决定更详细地讨论这个问题,因为我没有看到任何简单的选择。这是我想出的:
cleantext = function(x){

  sapply(1:length(x),function(y){
    bad = hunspell(x[y])[[1]]
    good = unlist(lapply(hunspell_suggest(bad),`[[`,1))

    if (length(bad)){
      for (i in 1:length(bad)){
        x[y] <<- gsub(bad[i],good[i],x[y])
      }}})
  x
}

尽管可能有一种更优雅的方法,但此函数返回一个字符串向量更正如下:
> df1$Text
[1] "A complec sentence joins an independet"                
[2] "Mary and Samantha arived at the bus staton before noon"
[3] "I did not see thm at the station in the mrning"        
[4] "The participnts read 60 sentences in radom order"      
[5] "how to fix mispelled words in R languge"               
[6] "today is Tuesday"                                      
[7] "bing sports quiz" 

> cleantext(df1$Text)
[1] "A complex sentence joins an independent"               
[2] "Mary and Samantha rived at the bus station before noon"
[3] "I did not see them at the station in the morning"      
[4] "The participants read 60 sentences in radon order"     
[5] "how to fix misspelled words in R language"             
[6] "today is Tuesday"                                      
[7] "bung sports quiz" 

注意,因为这会返回 hunspell 给出的第一个建议- 这可能正确也可能不正确。

关于r - 如何使用 hunspell 包在 R 中的列中建议正确的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56026550/

相关文章:

r - 手动检查 R 中的面板单位根测试

开放式拼写检查器的字典文件结构

c# - 基于词频的最大编辑距离和建议

C++ - 将 HunSpell 1.3.2 与 Visual Studio 2010 结合使用

linux - 在所有子目录中运行 Hunspell

在ggplot2图表中代表负钱

java - 使用 JRI 从 Java 调用 R,如何转换返回值

r - 如何根据另一个栅格网格单元值对栅格进行子集(分类)?

c++ - SymSpellPlusPlus 中的分词

objective-c - 自定义 NSTextView insertText :replacementRange breaks Spell Checking