Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add floating number, negative integer support #4

Merged
merged 5 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 135 additions & 5 deletions helper/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package helper
import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -44,9 +46,38 @@ const (
SeptillionAsString string = "septilyon" // 10^24
OctillionAsString string = "oktilyon" // 10^27
NonillionAsString string = "nonilyon" // 10^30

//TODO Add remaining ones whenever have time
)

const (
NegativeAsWord string = "mənfi" // -
SeparatorAsWord string = "tam"
)

//Floating point number indicators
// count of zeros : suffix
var floatingPointDict = map[int]string{
10: "onda",
100: "yüzdə",

1000: "bir mində",
10_000: "on mində",
100_000: "yüz mində",

1_000_000: "bir milyonda",
10_000_000: "on milyonda",
100_000_000: "yüz milyonda",

1_000_000_000: "milyardda",
10_000_000_000: "on milyardda",
100_000_000_000: "yüz milyardda",

1_000_000_000_000: "bir trilyonda",
10_000_000_000_000: "on trilyonda",
100_000_000_000_000: "yüz trilyon",
}

var digits = []string{
ZeroAsString,
OneAsString,
Expand All @@ -73,14 +104,107 @@ var tens = []string{
NinetyAsString,
}

// Convert integer given in str to word
func ConvertIntPart(str string) string {
func SpellNumber(str string) (string, error) {

//check whether number is in valid format
if ok := validNumberRegex.MatchString(str); !ok {
return "", errors.New(fmt.Sprintf("Input: %s invalid", str))
}

//it may contain separator like `,` placed every 3 decimal places for numbers larger than 999
str = strings.ReplaceAll(str, ",", "")

isFloatingNumber := strings.Contains(str, ".")

if isFloatingNumber {
floatVal, err := strconv.ParseFloat(str, 64)

if err != nil {
return "", err
}

var sign string
if floatVal < 0 {
sign = NegativeAsWord
}

slices := strings.Split(str, ".")

intVal, err := strconv.Atoi(slices[0])
if err != nil {
return "", nil
}

intPartAsWord := convertIntPart(intVal)

floatingPart := slices[1]

//sanitize floatingPart by removing trailing zeros if exist
floatingPart = strings.TrimRight(floatingPart, "0")

//TODO 'str' may contain invalid symbols, need to sanitize it before processing
//it contains separator like `,`
floatingPartAsInteger, err := strconv.Atoi(floatingPart)
floatingPartAsIntegerWithWord := convertIntPart(floatingPartAsInteger)

//TODO 'str' may contain symbols like `,` to separate integers for readability
cnt := len(floatingPart)
seperatorKey := int(math.Pow10(cnt))

suffix, ok := floatingPointDict[seperatorKey]

if !ok {
return "", errors.New(fmt.Sprintf("No seperator found"))
}

wordBuilder := make([]string, 0)
if len(sign) != 0 {
wordBuilder = append(wordBuilder, sign)
}
wordBuilder = append(wordBuilder, intPartAsWord)
wordBuilder = append(wordBuilder, SeparatorAsWord)
wordBuilder = append(wordBuilder, suffix)
wordBuilder = append(wordBuilder, floatingPartAsIntegerWithWord)

return strings.Join(wordBuilder, " "), nil
} else {
wordBuilder := make([]string, 0)

var intAbsVal int
if sign, intPart, err := getSignAndInteger(str); err != nil {
return "", err
} else {
wordBuilder = append(wordBuilder, sign)
intAbsVal = int(math.Abs(float64(intPart)))
}

spelledInteger := convertIntPart(intAbsVal)

wordBuilder = append(wordBuilder, spelledInteger)

return strings.Join(wordBuilder, " "), nil
}
}

func getSignAndInteger(str string) (string, int, error) {
var sign string

if numberAsInt, err := strconv.Atoi(str); err == nil {

if numberAsInt < 0 {
sign = NegativeAsWord
}

return sign, numberAsInt, nil
} else {
return "", 0, err
}
}

// Convert integer given in str to word
func convertIntPart(intPart int) string {

//make it positive, if needed
intPart = int(math.Abs(float64(intPart)))

str := strconv.Itoa(intPart)
//starting from right hand side, pick up triples and start process it

//used to indicate level 10^3, 10^6
Expand Down Expand Up @@ -251,3 +375,9 @@ func tripleToWord(triple string) (string, error) {

return strings.Join(textBuilder, " "), nil
}

//TODO make regex be valid for numbers containing symbol: ','
var validNumberRegex = regexp.MustCompile("^-?\\d+(\\.\\d+)?$")

//var validNumberRegexForWholePositive = regexp.MustCompile("/^(0|[1-9]\\d*)$/\n")
//var validNumberRegexForWholePositive = regexp.MustCompile("/^(0|[1-9]\\d*)$/\n")
Loading