qdap 包:将零数字转换为 "zero" 字时的错误

qdap package: bug in converting zero digits to "zero" words

在(作为菜鸟)我将其作为 R 程序包错误提交之前,让我 运行 由你们来完成。我认为以下所有内容都很好:

replace_number("123 0 boogie")
[1] "one hundred twenty three boogie"
replace_number("1;1 foo")
[1] "one;one foo"
replace_number("47 bar")
[1] "forty seven bar"
replace_number("0")
"zero"

我认为以下所有内容都不好,因为输出中缺少 "zero":

replace_number("1;0 foo")
[1] "one; foo"
replace_number("00 bar")
[1] "bar"
replace_number("0x")
[1] "x"

基本上,我会说 replace_number() 无法处理包含数字 0(“0”除外)的字符串。这是一个真正的错误吗?

如果你深入研究 replace_number 的内脏:

 unlist(lapply(lapply(gsub(",([0-9])", "\1", text.var), function(x) {
        if (!is.na(x) & length(unlist(strsplit(x, "([0-9])", 
            perl = TRUE))) > 1) {
            num_sub(x, num.paste = num.paste)
        }
        else {
            x
        }
    }), function(x) mgsub(0:9, ones, x)))

可以看到问题出现在qdap:::num_sub

qdap:::num_sub("101", num.paste = "combine") ## "onehundredone"
qdap:::num_sub("0", num.paste = "combine")   ## ""

在该函数中挖掘,问题出现在 numb2word,它有内部代码

ones <- c("", "one", "two", "three", "four", "five", "six", 
    "seven", "eight", "nine")
names(ones) <- 0:9

将零值转换为空白。如果我自己遇到这个问题,我会分叉 qdap repo, go to replace_number.R,并尝试以 向后兼容 的方式更改它,以便 replace_number 可以采用逻辑参数 blank_zeros=TRUE,它被传递给 numb2word 并做了正确的事情,例如

ones <- c(if (blank_zeros) "" else "zero",
          "one", "two", "three", "four", "five", "six", 
    "seven", "eight", "nine")

与此同时,我已将此发布到 qdap issues list