# This library is a simple implementation of a function to convert textual
# numbers written in English into their integer representations.
#
# This code is open source according to the MIT License as follows.
#
# Copyright (c) 2008 Greg Hewgill
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# https://github.com/ghewgill/text2num/

_UNITNUMBERS = Dict([
    "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,
])

_ORDEROFMAGNITUDE = Dict([
    "thousand" => 1000,
    "million" => 1000000,
    "billion" => 1000000000,
    "trillion" => 1000000000000,
    "quadrillion" => 1000000000000000,
    "quintillion" => 1000000000000000000,
    "sextillion" => 1000000000000000000000,
    "septillion" => 1000000000000000000000000,
    "octillion" => 1000000000000000000000000000,
    "nonillion" => 1000000000000000000000000000000,
    "decillion" => 1000000000000000000000000000000000,
])

struct NumberException <: Exception
    numbertext::AbstractString
end

Base.show(io::IO, n::NumberException) = print(io, "Unknown number: $(n.numbertext)")

function text2num(s::AbstractString)::Real
    s = replace(lowercase(s), " and " => " ")
    re::Regex = r"[\s-]+"
    a::Vector{AbstractString} = split(s, re)
    n = 0
    g = 0
    for w in a
        x = get(_UNITNUMBERS, w, nothing)
        if !(x isa Nothing)
            g += x
        elseif w == "hundred" && g != 0
            g *= 100
        else
            x = get(_ORDEROFMAGNITUDE, w, nothing)
            if !(x isa Nothing)
                n += g * x
                g = 0
            else
                throw(NumberException(w))
            end
        end
    end
    return n + g
end

text2num(x::Number)::Number = x

function replace_text_numbers(text::AbstractString)::AbstractString
    text = replace(strip(lowercase(text)), '\ua0' => " ")
    seg::Vector{AbstractString} = split(text, " ")
    for (i, x) in enumerate(seg)
        try
            seg[i] = "$(text2num(x))"
        catch
            continue
        end
    end
    return join(seg, " ")
end
