【发布时间】: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 <- SimpleCorpus(VectorSource(new_text)) for (i in 1:n){ docs <- tm_map(docs, content_transformer(gsub), pattern = match[i], replacement = dict[i]) } -
除了@CallumF 所说的,您需要提供一个示例微型数据集。我相信我们需要通过将数据集运行 1000 倍并测试各种解决方案来对您的性能进行基准测试。
-
@Kamil 感谢您的建议,我已经在测试集中进行了编辑。
标签: r string performance loops text