r - qdap 包 : bug in converting zero digits to "zero" words

标签 r numbers text-processing qdap

之前(作为菜鸟)我将它作为 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"

我认为以下所有内容都很糟糕,因为输出中缺少“零”:
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

将零值转换为空白。如果我自己遇到这个问题,我会 fork qdap repo ,转至 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 上。 .

关于r - qdap 包 : bug in converting zero digits to "zero" words,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38709392/

相关文章:

r - 从列表(带通配符)跨多列搜索后将指示符变量添加到数据帧

python - 如何遍历字母和数字

c# - 在数字中插入 1 位 C#

regex - 如何使用 SED 或 AWK 在行中添加双引号?

r - 使用循环和命名约定在数据框中创建新的命名变量

R-Hadoop 集成 - 如何将 R 连接到远程 hdfs

R:测试函数从封闭环境中获取哪些对象

mysql - 我的十进制数在创建时会缩短,但在编辑时不会

cmake - 如何去除 CMake 变量中的尾随空格?

php - 使用 PHP 提取 HTML 文档的正文