将拼写出的数字(二十二)转换为数字(22)

时间:2014-09-20 23:23:58

标签: javascript math numbers numeric

我试图将拼写出的数字字符串转换为数字/数字字符串。

我设法在下面做了相反的事情,但当我试图扭转它时,我没有那么好地工作(它返回未定义的未定义00未定义为'两个'。

var th = [ "", "thousand", "million", "billion", "trillion" ];

var dg = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ];

var tn = [ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ];

var tw = [ "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ];

function toWords(s) {
    s = s.toString();
    s = s.replace(/[\, ]/g, "");
    if (s != parseFloat(s)) return "not a number";
    var x = s.indexOf(".");
    if (x == -1) x = s.length;
    if (x > 15) return "too big";
    var n = s.split("");
    var str = "";
    var sk = 0;
    for (var i = 0; i < x; i++) {
        if ((x - i) % 3 == 2) {
            if (n[i] == "1") {
                str += tn[Number(n[i + 1])] + " ";
                i++;
                sk = 1;
             } else if (n[i] != 0) {
                str += tw[n[i] - 2] + " ";
                sk = 1;
             }
        } else if (n[i] != 0) {
            str += dg[n[i]] + " ";
            if ((x - i) % 3 == 0) str += "hundred ";
            sk = 1;
        }
        if ((x - i) % 3 == 1) {
            if (sk) str += th[(x - i - 1) / 3] + " ";
            sk = 0;
        }
    }
    if (x != s.length) {
        var y = s.length;
        str += "point ";
        for (var i = x + 1; i < y; i++) str += dg[n[i]] + " ";
    }
    return str.replace(/\s+/g, " ");
}

有人可以帮助我在javascript(原来的javascript中)反转这个吗?或者就如何完成我想要做的事情给我一些建议。

我尝试过使用解析器,但我觉得我正在定义一个= 1,两个= 2,...十六= 16,十七= 17,......和七十三个= 73。所以基本上字面上定义了许多数字字符串与它们的数字对应物。必须有更好的方法来做到这一点。

ML已经超越了我的想法,但为什么要学习一些东西?一个是1.我没有获得各种输入并尝试将其与单个输出相关联/匹配。例如,你好,嘿,嗨,以及类似的东西都是问候语。

3 个答案:

答案 0 :(得分:1)

幸运的是,除了数十之外,英语编号是合理的。您需要将“一”到“十九”的字符串定义为“二十”到“十九”,“百”,“千”,“百万”等所有十位。

然后,对于解析器,将字符串解析为标记(基本上将其拆分)。如果您愿意,可以将令牌转换为数字,或者如果您愿意,可以将它们保留为字符串。我在这里使用数字,因为它有点短。 :)

所以,如果你有一个“四千八百五十”的字符串,你可以将其解析为令牌:

  

4 1000 100 80 5

(你可以丢弃'和'这个词,以及逗号和其他噪音。)

然后,您可以使用以下非常简化的伪代码从后面处理此字符串。

总计= 0; Multiplicator = 1;

repeat
  Number = ReadNumber || 1;
  Total += Multiplicator * Number;
  Multiplicator = ReadMultiplicator;

乘数是10,20,30 ...... 100,1000,1000000 数字是1 .. 19.这些是可选的。如果找不到号码,请将其视为已阅读。

所以最后,你应该将字符串视为

  

4 1000 1 100 1 80 5

哪个应该被处理为

  

5 + 1 * 80 + 1 * 100 + 4 * 1000

我说伪代码非常简单。首先,你解析后需要能够“偷看”前方。如果前面的标记不是数字,它必须是乘数,所以不应该跳过它。 此外,您需要一些额外的检查。毕竟,100之前的数字是特殊的。您不希望将字符串“三八十四”解析为244。

然后你需要一些额外的层。 “八万五千”怎么样?看起来你不应该在乘数之前只读取一个数字,而是实际上所有比乘数更小的标记,并将它们解析在一起,所以

  十六万

应解析为

  

(1 100 1 60)1000

您可以稍后在该片段中添加额外的支票,尽管您最初可以不用。毕竟,“千万”实际上是“十亿”,但它会正确解析甚至给出正确的结果。

所以,我认为解析正确的字符串是非常可行的。检查异常会让它变得更难。 “二十三百”将开箱即用,但你可以写“一万二千三百”而实际得到12.300的结果也很奇怪。但话说回来,我认为这样的检查一开始并不严格。

答案 1 :(得分:1)

如果你只想要整数,你可以使用键值对象来构建单个词的数字 -

function wordsToNumber(s){
    var w= s.toLowerCase().replace(/(thousand|[mbr]illion),?/g, '$1,');
    var n= 0, nArray= w.split(/,/), 
    sub= 0, tem, segment, 
    nwords={
        billion: 1e9, eight: 8, eighteen: 18, eighty: 80, 
        eleven: 11, fifteen: 15, fifty: 50,  five: 5, forty: 40, 
        four: 4, fourteen: 14,  hundred: 100, million: 1e6,  
        nine: 9, nineteen: 19, ninety: 90, one: 1, quadrillion: 1e15, 
        seven: 7, seventeen: 17, seventy: 70, six: 6, sixteen: 16, 
        sixty: 60, ten: 10,  thirteen: 13, thirty: 30, thousand: 1e3, 
        three: 3, trillion: 1e12,twelve: 12, twenty: 20, two: 2, zero: 0
    };
    while(nArray.length){
        sub= 0;
        segment= nArray.shift().match(/[a-z]+/g) || [];
        segment.forEach(function(w2){
            tem= nwords[w2.trim()];
            if(isFinite(tem)){
                if(tem<100) sub+= tem;
                else sub*= tem;
            }
        });
        n+= sub;
    }
    return n;
}
var str= 'twenty two thousand one hundred and fifty one';

wordsToNumber(str);

/*  returned value: (Number)
22151
*/

要获得更多功能,您可以编写更多代码 -

<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>Number Words</title>
<style>
</style>
<script>
Math.fraction= function(n, prec, up){
    var s= String(n), 
    p= s.indexOf('.');
    if(p== -1) return s;
    var i= Math.floor(n) || '', 
    dec= s.substring(p), 
    m= prec || Math.pow(10, dec.length-1), 
    num= up=== 1? Math.ceil(dec*m): Math.round(dec*m), 
    den= m, 
    g= Math.gcd(num, den);
    if(den/g== 1) return String(i+num);
    if(i) i= i+' and  ';
    return i+ String(num/g)+'/'+String(den/g);
}
Number.fromFraction= function(str, prec){
    var rx=/(\d+)\/(\d+)/, dec= 0, I= 0, 
    M= rx.exec(str);
    if(M){
        if(M.index>0) I= parseFloat(str);
        if(M[1]) dec= M[1]/M[2];
        if(!prec) prec= str.length+1;
        if(typeof prec== 'number') dec= +(dec.toFixed(prec));
        return I+dec;
    }
    return parseFloat(str);
}
Number.fromRoman= function(s){
    s= String(s).toUpperCase();
    if(s.length>15 ||  /[^MDCLXVI]/.test(s)) return NaN;
    var L= s.length, sum= 0, i= 0, next, val, 
    R={
        M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1
    };
    while(i<L){
        val= s.charAt(i++);
        if(!R[val]) return NaN;
        val= R[val];
        next= R[(s.charAt(i) || 'N')] || 0;
        if(next>val) val*= -1;
        sum+= val;
    }
    if(sum.toRoman()== s) return sum;
    return NaN;
}
Number.prototype.toRoman= function(){
    var n= Math.floor(this), val, s= '', limit= 3999, i= 0, 
    v= [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1], 
    r= ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
    if(n<1 || n>limit) return '';
    while(i<13){
        val= v[i];
        while(n>= val){
            n-= val;
            s+= r[i];
        }
        if(n== 0) return s;
        ++i;
    }
    return '';
}
var NW={
    /* numbers to words */
    nameNumbers: function(str){
        var nw= NW.numberWords, 
        s= (str+'').replace(/([^\d])(?=\.\d+)/g, '$1 0').trim();
        var f, w= '', x, xs, n, ns, M, 
        rx=/(-?\d+)(\.(\d+)([eE]([+-]?\d+))?)?/g, 
        rx2=/(\d+)(\.(\d+)([eE]([+-]?\d+))?)?/, 
        rq=/((\d+)? +(and +)?)?(\d+)\/(\d+)/g;
        s= s.replace(rq, function(a){
            return NW.printFraction(a);
        });
        s= s.replace(rx, function(q){
            n= +q;
            ns= n+'';
            M= rx2.exec(ns) || [];
            w= (n<0)? 'minus ': '';
            if(M[1]) w+= nw(M[1]);
            if(M[3]){
                f= M[3].split('').map(function(itm){
                    return nw(itm);
                });
                w+= ' point '+ f.join(' ');
                if(M[5]){
                    x= +M[5];
                    xs= x<0? 'minus ': '';
                    xs+= NW.ordinal(nw(x));
                    w+= ' times ten to the '+xs;
                }
            }
            return w || q;
        });
        return s.replace(/ {2,}/g, ' ');
    },
    numberWords: function numberWords(x){
        var n= +x;
        if(isNaN(n) || n%1) return ''+x;
        n= Math.abs(n);
        var i= 0, p, prefix= [], num, rem, 
        NK= NW.numberwords_array, 
        w= NK[0], w1= NK[1], w2= NK[2], mag= NK[3];
        while(n>99){
            x= mag[i];
            if(n>= x){
                p= Math.floor(n/x);
                n%= x;
                prefix.push(numberWords(p)+w2[i]);
            }
            ++i;
        }
        if(prefix.length){
            prefix= prefix.join(', ');
            if(n) prefix+= ' and ';
            else return prefix;
        }
        else prefix= '';
        if(n<20) num= w[n];
        else{
            num= w1[Math.floor(n/10)];
            rem= n%10;
            if(rem) num+= ' '+w[rem];
        }
        return prefix+num;
    },
    numberwords_array: [
        ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 
        'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 
        'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'], 
        ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 
        'seventy', 'eighty', 'ninety'], 
        [' quadrillion', ' trillion', ' billion', ' million', 
        ' thousand', ' hundred'], 
        [1e15, 1e12, 1e9, 1e6, 1e3, 100]
    ], 
    ordinal: function(s){
        var ax, rx, suffix, str= ' '+s.trim();
        ax= str.lastIndexOf(' ');
        suffix= str.substring(ax).toLowerCase();
        if(str.charAt(str.length-1)== 'y'){
            return str.slice(0, -2)+'tieth';
        }
        if(ax>1) str= str.substring(0, ax);
        else str= ' ';
        switch(suffix.trim()){
            case 'one': return str+' first';
            case 'two': return str+' second';
            case 'three': return str+' third';
            case 'five': return str+' fifth';
            case 'eight': return str+' eighth';
            case 'nine': return str+' ninth';
            case 'twelve': return str+' twelfth';
            default: return str+suffix+'th';
        }
    },
    printFraction: function(str){
        var num, RN= NW.numberWords, 
        M=/((\d+) +)?(\d+) *\/ *(\d+)/g.exec(str.trim());
        if(!M) return str;
        num= M[2]? RN(M[2])+' and ': '';
        num+= RN(+M[3])+'-';
        if(M[4]=== '2') num+= 'half';
        else if(M[4]=== '4') num+= 'quarter';
        else num+= NW.ordinal(RN(+M[4]));
        if(M[3]!== '1') num+= 's';
        return num;
    },
    /* words to numbers */
    reDigit: function(n){
        var sub= [], tem, d= n.match(/[a-z]+/g), L= d.length-1, 
        nwords= NW.wordnumber_keys;
        d.map(function(w, i){
            tem= nwords[w];
            if(isFinite(tem)){
                if(tem<20 || i== L) sub.push(tem+'');
                else sub.push((tem+'').charAt(0));
            }
        });
        return sub.join('');
    },
    reFraction: function(str){
        var M, w= 0, n, d, f2= 0, prec, ax;
        ax= str.lastIndexOf(' and ');
        if(ax== -1){
            ax= 0;
            w= 'zero';
        }
        else{
            w= str.substring(0, ax);
            ax+= 4;
        }
        M= str.substring(ax).split('-');
        n= M[0];
        d= M[1];
        if(n && d){
            d= NW.wordsToNumber(d);
            n= NW.wordsToNumber(n);
            return [w, n/d];
        }
        return [w];
    },
    wordNumber: function(str){
        var n= 0, sign= 1, whole= 0, dec= 0, frac, 
        suffix= 0, exp= 1, exsign= 1, prec, pt, 
        rx1=  /(illion|thousand|hundred)/i, 
        rx2=/( +times ten to the( +minus)? +)/g, 
        rx3=/(point|times|[^a-z, ])/, 
        s= str.toLowerCase().trim().replace(/ieth?$/, 'y').replace(/(ths?|s)$/, '');
        if(s.indexOf('minus ')== 0){
            sign= -1;
            s= s.substring(5);
        }
        if(!rx3.test(s)){
            n= (!rx1.test(s))? NW.reDigit(s): NW.wordsToNumber(s);
            return n*sign;
        }
        whole= s;
        var pt= s.split('point ');
        dec= pt[1];
        if(dec){
            whole= pt[0];
            temp= rx2.exec(dec);
            if(temp!= null){
                if(temp[2]) exsign= -1;
                suffix= dec.substring(rx2.lastIndex);
                dec= dec.substring(0, temp.index);
                suffix= NW.wordsToNumber(suffix);
                exp= Math.pow(10, suffix*exsign);
            }
            dec= NW.reDigit(dec);
            prec= dec.length+1;
            dec= +('0.'+dec);
        }
        else if(whole.indexOf('-')!= -1){
            pt= NW.reFraction(whole);
            whole= pt[0];
            dec= pt[1];
        }
        n= NW.wordsToNumber(whole.replace(/ +and +/g, ' '));
        if(dec){
            n+= dec;
            if(!prec) frac= Math.min(String(n).length, 15);
            n= n.toPrecision(frac);
        }
        if(exp!== 1) return sign*((n*exp).toExponential(prec));
        return sign*n;
    },
    wordnumber_keys:{
        billion: 1e9, eigh: 8, eight: 8, eighteen: 18, eightt: 8, eighty: 80, 
        eleven: 11, fif: 5, fifteen: 15, fifty: 50, first: 1, five: 5, forty: 40, 
        four: 4, fourteen: 14, half: 2, hundred: 100, million: 1e6, nin: 9, 
        nine: 9, nineteen: 19, ninety: 90, one: 1, quadrillion: 1e15, quarter: 4, 
        second: 2, seven: 7, seventeen: 17, seventy: 70, six: 6, sixteen: 16, 
        sixty: 60, ten: 10, third: 3, thirteen: 13, thirty: 30, thousand: 1e3, 
        three: 3, trillion: 1e12, twelf: 12, twelve: 12, twenty: 20, two: 2, zero: 0
    },
    wordsToNumber: function(s){
        var w= s.toLowerCase().replace(/(thousand|[mbr]illion),?/g, '$1,');
        var n= 0, nArray= w.split(/,/), 
        sub= 0, tem, segment, 
        nwords= NW.wordnumber_keys;
        while(nArray.length){
            sub= 0;
            segment= nArray.shift().match(/[a-z]+/g) || [];
            segment.forEach(function(w2){
                tem= nwords[w2.trim()];
                if(isFinite(tem)){
                    if(tem<100) sub+= tem;
                    else sub*= tem;
                }
            });
            n+= sub;
        }
        return n;
    }
}

onload=function(){
    var nw= NW, v2= [], v;
    var A= ['150', '0.12', 'fourteen hundred and ninety two', '65 1/16', '65.0045', 
    'thirty three and one- third', '2012', '98.6', 'MCMLIX', '897456971.25', 
    '1 1/2', '12 3/4', '-9', '33 1/3', Math.PI, 'nine and three- quarters', 
    'seventy eight point six four', 'XIX', '1.1550046210e+17', '1.222e3', 'minus two hundred', 
    '4.5e-10', '3.125e-3', '1.56760e+25'].map(function(sv){
        if(/^[IVXLCDM]+$/.test(sv)) sv= Number.fromRoman(sv);
        if(/^[a-z ,-]+$/i.test(sv)){
            v2= nw.wordNumber(sv);
        }
        else v2= nw.nameNumbers(sv);
        return '<p\>'+sv+'= '+v2+'</p\>';
    });
    document.getElementById('resultset').innerHTML=A.join('\n');
}

</script>

</head>
<body>
<div id="resultset">
</div>
</body>
</html>

答案 2 :(得分:0)

以下是其他答案之一的实现:

我喜欢monkeypatching(是的,是的,我知道......),但它可以很容易地从这个实现中删除。

var NUMBER_NAMES = {
  a: 1, billion: 1e9, eight: 8, eighteen: 18, eighty: 80,
  eleven: 11, fifteen: 15, fifty: 50,  five: 5, forty: 40,
  four: 4, fourteen: 14,  hundred: 100, million: 1e6,
  nine: 9, nineteen: 19, ninety: 90, one: 1, quadrillion: 1e15,
  seven: 7, seventeen: 17, seventy: 70, six: 6, sixteen: 16,
  sixty: 60, ten: 10,  thirteen: 13, thirty: 30, thousand: 1e3,
  three: 3, trillion: 1e12, twelve: 12, twenty: 20, two: 2, zero: 0
}

Array.prototype.last = function() {
  return this[this.length - 1];
}

String.prototype.toNumber = function() {
  var stripped = this.toLowerCase().replace(/\-| and /g, ' ').split(' ');
  var numbers = stripped.map(function(word) { return NUMBER_NAMES[word] });
  if (numbers.some(function(number) { return number == null })) return NaN;
  var parseNumbers = function(numbers) {
    var total = 0;
    var readNumber = function(def) {
      return (numbers.last() < 20) ? numbers.pop() : def;
    }
    var readMagnitude = function() {
      return (numbers.last() > 19) ? numbers.pop() : 0;
    }
    total += readNumber(0);
    while (numbers.length) {
      var magnitude = readMagnitude();
      var sub_numbers = [];
      while (numbers.length && numbers.last() < magnitude) {
        sub_numbers.unshift(numbers.pop());
      }
      var number = sub_numbers.length ? parseNumbers(sub_numbers) : 1;
      total += magnitude * number;
    }
    return total;
  }
  return parseNumbers(numbers);
}

"twenty-two hundred and one".toNumber() // 2201