r - 将书面数字转换为R中的数字

标签 r text qdap

是否有人知道将数字的文本表示形式转换为实际数字的功能,例如“2035”到20305年。我在数据帧行中写了数字,并希望将它们转换为数字。

在qdap软件包中,您可以用单词替换数字表示的数字(例如1001变为1000),但反之则不行:

library(qdap)
replace_number("I like 346457 ice cream cones.")
[1] "I like three hundred forty six thousand four hundred fifty seven ice cream cones."

最佳答案

这是一个应该使您成千上万的起点。

word2num <- function(word){
    wsplit <- strsplit(tolower(word)," ")[[1]]
    one_digits <- list(zero=0, one=1, two=2, three=3, four=4, five=5,
                       six=6, seven=7, eight=8, nine=9)
    teens <- list(eleven=11, twelve=12, thirteen=13, fourteen=14, fifteen=15,
                  sixteen=16, seventeen=17, eighteen=18, nineteen=19)
    ten_digits <- list(ten=10, twenty=20, thirty=30, forty=40, fifty=50,
                       sixty=60, seventy=70, eighty=80, ninety=90)
    doubles <- c(teens,ten_digits)
    out <- 0
    i <- 1
    while(i <= length(wsplit)){
        j <- 1
        if(i==1 && wsplit[i]=="hundred")
            temp <- 100
        else if(i==1 && wsplit[i]=="thousand")
            temp <- 1000
        else if(wsplit[i] %in% names(one_digits))
            temp <- as.numeric(one_digits[wsplit[i]])
        else if(wsplit[i] %in% names(teens))
            temp <- as.numeric(teens[wsplit[i]])
        else if(wsplit[i] %in% names(ten_digits))
            temp <- (as.numeric(ten_digits[wsplit[i]]))
        if(i < length(wsplit) && wsplit[i+1]=="hundred"){
            if(i>1 && wsplit[i-1] %in% c("hundred","thousand"))
                out <- out + 100*temp
            else
                out <- 100*(out + temp)
            j <- 2
        }
        else if(i < length(wsplit) && wsplit[i+1]=="thousand"){
            if(i>1 && wsplit[i-1] %in% c("hundred","thousand"))
                out <- out + 1000*temp
            else
                out <- 1000*(out + temp)
            j <- 2
        }
        else if(i < length(wsplit) && wsplit[i+1] %in% names(doubles)){
            temp <- temp*100
            out <- out + temp
        }
        else{
            out <- out + temp
        }
        i <- i + j
    }
    return(list(word,out))
}

结果:
> word2num("fifty seven")
[[1]]
[1] "fifty seven"

[[2]]
[1] 57

> word2num("four fifty seven")
[[1]]
[1] "four fifty seven"

[[2]]
[1] 457

> word2num("six thousand four fifty seven")
[[1]]
[1] "six thousand four fifty seven"

[[2]]
[1] 6457

> word2num("forty six thousand four fifty seven")
[[1]]
[1] "forty six thousand four fifty seven"

[[2]]
[1] 46457

> word2num("forty six thousand four hundred fifty seven")
[[1]]
[1] "forty six thousand four hundred fifty seven"

[[2]]
[1] 46457

> word2num("three forty six thousand four hundred fifty seven")
[[1]]
[1] "three forty six thousand four hundred fifty seven"

[[2]]
[1] 346457

我已经可以告诉您,这对于word2num("four hundred thousand fifty")不起作用,因为它不知道如何处理连续的“百”和“千”项,但是算法可能可以修改。如果有改进或以自己的答案为基础,则任何人都应随时对其进行编辑。我只是觉得这是一个有趣的问题(一段时间)。

编辑:显然,比尔·维纳 bool(boolean) 斯(Bill Venables)有一个名为english的软件包,与上面的代码相比,它甚至可以更好地实现这一目标。

关于r - 将书面数字转换为R中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332463/

相关文章:

r - 拆分为列时,为较短的字符串填充 NA 列

Excel 公式不起作用 - 无法识别数字

swift - 如何为字符串中的每个字母设置 UIColor

r - 连接键值不完全相等的R data.tables-合并时间最近的行

r - 如何在 Rstudio 演示文稿中创建表格

mysql - 远程存储文本文件的最佳方式是什么?

用R中的英文单词替换年份

r - 如何使用缺失值执行 RMSE?

r - 按矩阵列名称提取矩阵列值