【问题标题】:Speed of Cleaning Text in R using a Dictionary使用字典在 R 中清理文本的速度
【发布时间】:2018-05-11 06:00:43
【问题描述】:

我目前有一个拼写错误列表和一个更正列表,以 1 对 1 的关系编入索引。

这些更正特定于我正在做的工作,因此我无法使用现有的拼写更正包。

给定我想要应用这些更正的字符串列表,我有以下代码:

for (i in 1:n){
  new_text <- gsub(match[i], dict[i], new_text)
  new_text <- gsub('[[:punct:]]', '', new_text)
}

虽然这给出了我想要的结果,但它需要一天的大部分时间才能运行。 我不知道如何使用 apply 函数,因为操作在同一个对象上以特定顺序发生。

还有什么我可以尝试加快速度的吗?

编辑:这是我为基准性能而放在一起的非常小的测试集。

match <- c("\\b(abouta|aobut|bout|abot|abotu)\\b","\\b(avdised|advisd|advized|advsied)\\b","\\b(posible|possibl)\\b","\\b(replacment|repalcement|replacemnt|replcement|rplacement)\\b","\\b(tommorrow|tomorow|tommorow|tomorro|tommoro)\\b")
dict <- c('about','advised','possible','replacement','tomorrow')
new_text <- c('be advisd replacment coming tomorow','did you get the email aobut the repalcement tomorro','the customer has been avdised of a posible replacement','there is a replacement coming tomorrow','what time tommorow is the replacment coming')
n <- 5

在此数据上运行我当前的代码 1000 次,经过 0.424 次。

【问题讨论】:

  • 你应该阅读强大的文本挖掘包tm。尤其是使用SimpleCorpus 以获得高性能。
  • @KevinArseneau 你会这样做吗? docs &lt;- SimpleCorpus(VectorSource(new_text)) for (i in 1:n){ docs &lt;- tm_map(docs, content_transformer(gsub), pattern = match[i], replacement = dict[i]) }
  • 除了@CallumF 所说的,您需要提供一个示例微型数据集。我相信我们需要通过将数据集运行 1000 倍并测试各种解决方案来对您的性能进行基准测试。
  • @Kamil 感谢您的建议,我已经在测试集中进行了编辑。

标签: r string performance loops text


【解决方案1】:

尝试使用自定义词干分析器的 corpus 库。该库允许您提供任意 stemmer 函数。在您的情况下,您可以为词干分析器使用类似以下内容:

library(corpus)
dict <- strsplit(split = "\\|",
    c("about" = "abouta|aobut|bout|abot|abotu",
      "advised" = "avdised|advisd|advized|advsied",
      "possible" = "posible|possibl",
      "replacement" = "replacment|repalcement|replacemnt|replcement|rplacement",
      "tomorrow" = "tommorrow|tomorow|tommorow|tomorro|tommoro"))
my_stemmer <- new_stemmer(unlist(dict), rep(names(dict), lengths(dict)))

然后,您可以将此函数作为stemmer 参数传递给任何需要文本的函数,或者您可以创建一个带有stemmer 属性的corpus_text 对象(作为定义文本获取方式的token_filter 的一部分)转换为令牌):

new_text <- c('be advisd replacment coming tomorow',
              'did you get the email aobut the repalcement tomorro',
              'the customer has been avdised of a posible replacement',
              'there is a replacement coming tomorrow','what time tommorow is the replacment coming')

使用term_stats 计算(词干)令牌出现次数:

text <- as_corpus_text(new_text, stemmer = my_stemmer, drop_punct = TRUE)
term_stats(text)
#>    term        count support
#> 1  replacement     5       5
#> 2  tomorrow        4       4
#> 3  the             4       3
#> 4  coming          3       3
#> 5  a               2       2
#> 6  advised         2       2
#> 7  is              2       2
#> 8  about           1       1
#> 9  be              1       1
#> 10 been            1       1
#> 11 customer        1       1
#> 12 did             1       1
#> 13 email           1       1
#> 14 get             1       1
#> 15 has             1       1
#> 16 of              1       1
#> 17 possible        1       1
#> 18 there           1       1
#> 19 time            1       1
#> 20 what            1       1
#> ⋮  (21 rows total)

使用text_locate 在原文中查找(词干)标记的实例:

text_locate(text, "replacement")
#>   text           before             instance              after            
#> 1 1                     be advisd  replacment   coming tomorow             
#> 2 2    …u get the email aobut the  repalcement  tomorro                    
#> 3 3    …been avdised of a posible  replacement                             
#> 4 4                    there is a  replacement  coming tomorrow            
#> 5 5     what time tommorow is the  replacment   coming

词干提取函数的结果被缓存,所以这一切都非常快。 更多示例请参见http://corpustext.com/articles/stemmer.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2018-11-11
    • 1970-01-01
    • 2019-05-28
    相关资源
    最近更新 更多