-
Notifications
You must be signed in to change notification settings - Fork 2
/
word_2_num.go
146 lines (124 loc) · 2.85 KB
/
word_2_num.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package khmer_number
import (
"fmt"
"strings"
)
func text2Token(text string) ([]string, error) {
cleanText := strings.ReplaceAll(text, " ", "")
tokens := []string{}
for {
token := ""
for _, word := range NUM_WORDS_LIST {
wordLength := len(word)
if wordLength <= len(cleanText) {
if word == cleanText[0:wordLength] {
token = word
cleanText = cleanText[wordLength:]
break
}
}
}
if token == "" && cleanText != "" {
return nil, fmt.Errorf("cannot segment word")
}
tokens = append(tokens, token)
if len(cleanText) <= 0 {
break
}
}
return tokens, nil
}
func collect(s []int, t int) ([]int, int) {
tmp := s
k := 0
for {
if len(tmp) <= 0 {
break
}
if tmp[len(tmp) - 1] >= t {
return tmp, k
}
k += tmp[len(tmp) - 1]
tmp = tmp[:len(tmp) - 1]
}
return tmp, k
}
func text2Int(tokens []string) string {
// numArr := []int{}
numArr := make([]int, 0)
// num := 0
// num_word := 0
for _, token := range tokens {
if n, isFound := KHMER_DIGIT_WORD_MAP[token]; isFound {
numArr = append(numArr, n)
} else if n, isFound := KHMER_TENTH_MAP[token]; isFound {
numArr = append(numArr, n)
} else if token == "លាន" {
n := 0
numArr, n = collect(numArr, 1000000)
numArr = append(numArr, n * 1000000)
} else if token == "សែន" {
n := 0
numArr, n = collect(numArr, 100000)
numArr = append(numArr, n * 100000)
} else if token == "ម៉ឺន" {
n := 0
numArr, n = collect(numArr, 10000)
numArr = append(numArr, n * 10000)
} else if token == "ពាន់" {
n := 0
numArr, n = collect(numArr, 1000)
numArr = append(numArr, n * 1000)
} else if token == "រយ" {
n := 0
numArr, n = collect(numArr, 100)
numArr = append(numArr, n * 100)
}
}
num_word := 0
for _, num := range numArr {
num_word += num
}
return fmt.Sprintf("%d", num_word)
}
func text2number(text string) (string, error) {
tokens, err := text2Token(text)
if err != nil {
return "", err
}
posDot := IndexOf("ចុច", tokens)
if posDot == -1 {
return text2Int(tokens), nil
}
digit := tokens[0:posDot]
precision := tokens[posDot + 1:]
leadZero := ""
count := 0
for _, value := range precision {
if value == "សូន្យ" {
leadZero += "0"
count += 1
} else {
break
}
}
if count == len(precision) {
return fmt.Sprintf("%s.%s", text2Int(digit), leadZero), nil
} else {
return fmt.Sprintf("%s.%s%s", text2Int(digit), leadZero, text2Int(precision)), nil
}
}
func Word2NumEN(text string) (string, error) {
num, err := text2number(text)
if err != nil {
return "", err
}
return Khmer2RomanNum(num), nil
}
func Word2NumKH(text string) (string, error) {
num, err := text2number(text)
if err != nil {
return "", err
}
return Roman2KhmerNum(num), nil
}