Pseudo Random Nonsense Generator

Python <-> Javascript

ยท note

just how readable is javascript nowadays? I find them to be very comparable with python, especially since es6 (ES2017)

# https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers


def text2int(textnum, numwords={}):
    if not numwords:
        units = [
            "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
            "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
            "sixteen", "seventeen", "eighteen", "nineteen",
        ]

        tens = ["", "", "twenty", "thirty", "forty",
                "fifty", "sixty", "seventy", "eighty", "ninety"]

        scales = ["hundred", "thousand", "million", "billion", "trillion"]

        numwords["and"] = (1, 0)
        for idx, word in enumerate(units):
            numwords[word] = (1, idx)
        for idx, word in enumerate(tens):
            numwords[word] = (1, idx * 10)
        for idx, word in enumerate(
                scales):
            numwords[word] = (10 ** (idx * 3 or 2), 0)

    current = 0
    result = 0
    output = []
    for word in textnum.split():
        if word not in numwords:
            if result + current is not 0:
                output.append(result + current)
            else:
                output.append(False)
            current = 0
            result = 0
            continue
            #raise Exception("Illegal word: " + word)

        scale, increment = numwords[word]
        current = current * scale + increment
        if scale > 100:
            result += current
            current = 0
    if result + current is not 0:
        output.append(result + current)
    else:
        output.append(False)

    return output


tests = [
    "seven billion one hundred million thirty one thousand three hundred thirty seven",
    "twenty five weeks ago i went and ate eight muffins and burped five times",
    "extract numbers out of multiple texts",
    "my word this is cool eight"
]

results = [text2int(t) for t in tests]

print(results)

and this is the analogous code, converted by the author into javascript

const text2Int = textNum => {
  let numwords = {}
  const units = [
    'zero',
    'one',
    'two',
    'three',
    'four',
    'five',
    'six',
    'seven',
    'eight',
    'nine',
    'ten',
    'eleven',
    'twelve',
    'thirteen',
    'fourteen',
    'fifteen',
    'sixteen',
    'seventeen',
    'eighteen',
    'nineteen',
  ]

  const tens = [
    '',
    '',
    'twenty',
    'thirty',
    'forty',
    'fifty',
    'sixty',
    'seventy',
    'eighty',
    'ninety',
  ]

  const scales = ['hundred', 'thousand', 'million', 'billion', 'trillion']

  numwords['and'] = [1, 0]
  units.forEach((word, i) => (numwords[word] = [1, i]))
  tens.forEach((word, i) => (numwords[word] = [1, i * 10]))
  scales.forEach((word, i) => (numwords[word] = [10 ** (i * 3 || 2), 0]))

  let current = 0
  let result = 0
  let output = []
  textNum.split(' ').forEach(word => {
    if (Object.keys(numwords).indexOf(word) === -1) {
      if (result + current !== 0) {
        output.push(result + current)
      } else {
        output.push(null)
      }
      current = 0
      result = 0
    } else {
      const [scale, increment] = numwords[word]
      current = current * scale + increment
      if (scale > 100) {
        result += current
        current = 0
      }
    }
  })

  if (result + current !== 0) {
    output.push(result + current)
  } else {
    output.push(null)
  }

  return output
}

const tests = [
  'seven billion one hundred million thirty one thousand three hundred thirty seven',
  'twenty five weeks ago i went and ate eight muffins and burped five times',
  'extract numbers out of multiple texts',
  'my word this is cool eight',
]

const results = tests.map(t => {
  return text2Int(t)
})

console.log(results)

by the way it returns this:

[
  [7100031337],
  [25, null, null, null, null, 8, null, 5, null],
  [null, null, null, null, null, null, null],
  [null, null, null, null, null, 8]
]