是否有一个库负责将数字转换为单词?
例如:
转换One hundred and three
- > 5765
要么
转换Five thousand seven hundred and sixty-five
- > from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import tempfile
from six.moves import urllib
import pandas as pd
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import math
FLAGS = None
myImportedDatax1_np = np.empty((100, 1))
myImportedDatax2_np = np.empty((100, 1))
myImportedDatay_np = np.empty((100, 1))
def trueOutput(x1, x2):
return [3 * math.pow(x1, 2) + 4 * math.pow(x2, 2)]
count = 0
# Create data, using true equation, in range x1= 0 to 9, and x2=0 to 9
for a in range(0, 10):
for b in range(0, 10):
myImportedDatax1_np[count] = a
myImportedDatax2_np[count] = b
myImportedDatay_np[count] = trueOutput(myImportedDatax1_np[count], myImportedDatax2_np[count])
print(myImportedDatay_np[count])
count = count + 1
combined_Imported_Data_x = np.append(myImportedDatax1_np, myImportedDatax2_np, axis=1)
def build_estimator(model_dir, model_type):
x1 = tf.contrib.layers.real_valued_column("x1")
x2 = tf.contrib.layers.real_valued_column("x2")
wide_columns = [x1, x2]
m = tf.contrib.learn.LinearRegressor(model_dir=model_dir, feature_columns=wide_columns)
return m
def input_fn(input_batch, output_batch):
inputs = {"x1": tf.constant(input_batch[:,0]), "x2": tf.constant(input_batch[:,1])}
output = tf.constant(output_batch)
return inputs, output
def input_fn_predict(x1, x2):
inputs = {"x1": tf.constant([[x1]]), "x2": tf.constant([[x2]])}
return inputs
def train_and_eval(model_dir, model_type, train_steps, train_data, test_data):
model_dir = tempfile.mkdtemp() if not model_dir else model_dir
print("model directory = %s" % model_dir)
m = build_estimator(model_dir, model_type)
m.fit(input_fn=lambda: input_fn(combined_Imported_Data_x, myImportedDatay_np), steps=train_steps)
my_x1 = 2
my_x2 = 6
prediction = list(m.predict(input_fn=lambda: input_fn_predict(my_x1, my_x2)))
print("Prediction value is: ")
print(prediction)
print("Actual value is: ")
true_y = trueOutput(my_x1, my_x2)
print(true_y)
def main(_):
train_and_eval(FLAGS.model_dir, FLAGS.model_type, FLAGS.train_steps,
FLAGS.train_data, FLAGS.test_data)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.register("type", "bool", lambda v: v.lower() == "true")
parser.add_argument(
"--model_dir",
type=str,
default="",
help="Base directory for output models."
)
parser.add_argument(
"--model_type",
type=str,
default="wide_n_deep",
help="Valid model types: {'wide', 'deep', 'wide_n_deep'}."
)
parser.add_argument(
"--train_steps",
type=int,
default=10000,
help="Number of training steps."
)
parser.add_argument(
"--train_data",
type=str,
default="",
help="Path to the training data."
)
parser.add_argument(
"--test_data",
type=str,
default="",
help="Path to the test data."
)
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
答案 0 :(得分:3)
未发布到Hex.pm,而是发布实现数字到单词算法的模块的here's a gist:
iex(2)> NumberToWord.say(123312133123)
"one hundred and twenty three billion, three hundred and twelve million, one hundred and thirty three thousand, one hundred and twenty three"
来源:
defmodule NumberToWord do
@spec say(integer) :: String.t
def say(n), do: n |> say_io() |> IO.iodata_to_binary()
@spec say_io(integer) :: iodata
def say_io(1), do: "one"
def say_io(2), do: "two"
def say_io(3), do: "three"
def say_io(4), do: "four"
def say_io(5), do: "five"
def say_io(6), do: "six"
def say_io(7), do: "seven"
def say_io(8), do: "eight"
def say_io(9), do: "nine"
def say_io(10), do: "ten"
def say_io(11), do: "eleven"
def say_io(12), do: "twelve"
def say_io(13), do: "thirteen"
def say_io(14), do: "fourteen"
def say_io(15), do: "fifteen"
def say_io(16), do: "sixteen"
def say_io(17), do: "seventeen"
def say_io(18), do: "eighteen"
def say_io(19), do: "nineteen"
def say_io(20), do: "twenty"
def say_io(30), do: "thirty"
def say_io(40), do: "forty"
def say_io(50), do: "fifty"
def say_io(60), do: "sixty"
def say_io(70), do: "seventy"
def say_io(80), do: "eighty"
def say_io(90), do: "ninety"
def say_io(n) when n < 100 do
tens = div(n, 10) * 10
remainder = rem(n, 10)
format(tens, "", " ", remainder)
end
def say_io(n) when n < 1000 do
hundreds = div(n, 100)
remainder = rem(n, 100)
format(hundreds, " hundred", separator(remainder), remainder)
end
~w[thousand million billion trillion quadrillion quintillion sextillion septillion octillion nonillion decillion]
|> Enum.zip(Stream.unfold(1000, fn acc -> {acc, acc * 1000} end))
|> Enum.each(fn {suffix, m} ->
def say_io(n) when n < (unquote(m) * 1000) do
number = div(n, unquote(m))
remainder = rem(n, unquote(m))
format(number, " " <> unquote(suffix), separator(remainder), remainder)
end
end)
@spec separator(integer) :: String.t
def separator(remainder) when remainder < 100, do: " and "
def separator(_remainder), do: ", "
@spec format(integer, String.t, String.t, integer) :: iodata
def format(number, illion, _separator, 0), do: [say_io(number) | illion]
def format(number, illion, separator, remainder), do: [say_io(number), illion, separator | say_io(remainder)]
end
答案 1 :(得分:0)
The Cldr
library(除其他事项外)执行此操作。
定义一个承载该库配置的模块:
defmodule MyApp.Cldr do
use Cldr,
locales: Application.get_env(:partially, :languages, ["en"]),
default_locale: "en",
gettext: MyApp.Gettext,
providers: [Cldr.Number, Cldr.List]
end
然后,您可以将数字转换为这样的单词:
iex> MyApp.Cldr.Number.to_string(103, format: :spellout)
{:ok, "one hundred three"}
iex> MyApp.Cldr.Number.to_string(5765, format: :spellout)
{:ok, "five thousand seven hundred sixty-five"}