将数字转换为nodejs中所有语言的数字

时间:2017-10-06 16:59:32

标签: node.js

我有一个将单词转换为数字英文的函数,我尝试创建一个“全局”函数来解释所有具有输入数组的语言。不幸的是,我没有运气。

考虑到我有两个不同语言的数组(我从一个不同的SO问题得到这个)我将如何将它用于所有具有数组的语言

var BR = {"BRAZILIAN"}

var EN = {
    'zero': 0,
    'one': 1,
    'two': 2,
    'three': 3,
    'four': 4,
    'five': 5,
    'six': 6,
    'seven': 7,
    'eight': 8,
    'nine': 9,
    'ten': 10,
    'eleven': 11,
    'twelve': 12,
    'thirteen': 13,
    'fourteen': 14,
    'fifteen': 15,
    'sixteen': 16,
    'seventeen': 17,
    'eighteen': 18,
    'nineteen': 19,
    'twenty': 20,
    'thirty': 30,
    'forty': 40,
    'fifty': 50,
    'sixty': 60,
    'seventy': 70,
    'eighty': 80,
    'ninety': 90
};

var EN_Magnitude = {
    'thousand':     1000,
    'million':      1000000,
    'billion':      1000000000,
    'trillion':     1000000000000,
    'quadrillion':  1000000000000000,
    'quintillion':  1000000000000000000,
    'sextillion':   1000000000000000000000,
    'septillion':   1000000000000000000000000,
    'octillion':    1000000000000000000000000000,
    'nonillion':    1000000000000000000000000000000,
    'decillion':    1000000000000000000000000000000000,
};

function feach(w,n,g) {

    w = w.toLowerCase();

    var x = EN[w];

    if (x != null) {
       return g = g + x;
    }
    else if (w == "hundred") {
       return g = g * 100;
    }
    else {
        x = EN_Magnitude[w];
        if (x != null) {
            n = n + g * x
            g = 0;
        }
        else {
            return w;
        }
    }
    return n+g;
}

module.exports.text2num = function (word) {
    word = word.toString().toLowerCase();
    n = 0;
    g = 0;
    return feach(word,n,g);
  };

nodejs中有没有这个库?任何人都可以帮我解决这个问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用多种语言执行此操作的简单方法是将语言添加为键,将数字对象添加为值,而不是为每种语言创建对象。

**此解决方案仅适用于遵循与英语相同的单词编号规则的语言....您需要实现其他语言的规则(例如德语)

基本功能:

t2n.js档案:

var units = {
  'en': {
    'zero': 0,
    'one': 1,
    'two': 2,
    'three': 3,
    'four': 4,
    'five': 5,
    'six': 6,
    'seven': 7,
    'eight': 8,
    'nine': 9,
    'ten': 10,
    'eleven': 11,
    'twelve': 12,
    'thirteen': 13,
    'fourteen': 14,
    'fifteen': 15,
    'sixteen': 16,
    'seventeen': 17,
    'eighteen': 18,
    'nineteen': 19
  }
}

var tens = {
  'en': {
    'twenty': 20,
    'thirty': 30,
    'forty': 40,
    'fifty': 50,
    'sixty': 60,
    'seventy': 70,
    'eighty': 80,
    'ninety': 90
  }
}

var magnitude = {
  'en': {
    'hundred': 100,
    'thousand': 1000,
    'million': 1000000,
    'billion': 1000000000,
    'trillion': 1000000000000,
    'quadrillion': 1000000000000000,
    'quintillion': 1000000000000000000,
    'sextillion': 1000000000000000000000,
    'septillion': 1000000000000000000000000,
    'octillion': 1000000000000000000000000000,
    'nonillion': 1000000000000000000000000000000,
    'decillion': 1000000000000000000000000000000000
  }
}

function getNumber (text, language) {
  let textArray = text.toLowerCase().replace(/ and /g, ' ').split(' ')
  let temp = null
  let result = 0
  for (let word of textArray) {
    if (units[language].hasOwnProperty(word)) {
      if (textArray.indexOf(word) === textArray.length - 1) {
        result += (temp === null) ? units[language][word] : temp + units[language][word]
      } else {
        temp = (temp === null) ? units[language][word] : temp + units[language][word]
      }
    } else if (tens[language].hasOwnProperty(word)) {
      if (textArray.indexOf(word) === textArray.length - 1) {
        result += units[language][word]
      } else {
        temp = tens[language][word]
      }
    } else if (magnitude[language].hasOwnProperty(word)) {
      result += magnitude[language][word] * temp
      temp = null
    } else {
      return 'wrong text'
    }
  }
  return result
}
module.exports.text2num = getNumber

app.js文件:

var { text2num } = require('./t2n')
console.log(text2num('one million five hundred and twenty nine', 'en'))
console.log(text2num('twenty thousand five hundred and sixty nine', 'en'))
console.log(text2num('one Quintillion one quadrillion one trillion and one hundred', 'en'))