-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcutter-mk4.sh
executable file
·100 lines (84 loc) · 2.3 KB
/
wordcutter-mk4.sh
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
#!/bin/bash
cons=(b d g k l p r t w '"' '!')
fn=(c f h j m n s v x y z '*' '#' ':')
aff=(t~s t~c d~z d~j ^)
vowel=(a e i o u q - / \' '$' '?')
declare -A ermapping
ermapping['"']=k
ermapping['!']=p
ermapping['*']=c
ermapping['#']=m
ermapping[':']=x
function isinarray()
{
for eachElement in "${@:2}"; do
if [ "$eachElement" = "$1" ]; then
return 0
fi
done
return 1;
}
function getNextChar()
{
word="$1"
index="$2"
if [ "$index" -ge "${#word}" ]; then
echo ""
return
fi
if [ $(( $index + 3 )) -le "${#word}" ] && isinarray "${input:$index:3}" "${aff[@]}"; then
echo "${input:$index:3}"
return
fi
echo "${input:$index:1}"
}
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <word>" >&2
echo " Where <word> is the word to split." >&2
echo " Outputs as CSV (unquoted)." >&2
fi
while [ "$#" -gt 0 ]; do
input="$1"
if isinarray "$(getNextChar "$input" 0)" "${fn[@]}" || isinarray "$(getNextChar "$input" 0)" "${aff[@]}"; then
input='?'"$input"
fi
i="$(( ${#input} - 1 ))"
if isinarray "${input:$i:1}" "${cons[@]}"; then
input="$input"'?'
fi
index=0
curChar="$(getNextChar "$input" $index)"
while [ "$curChar" != "" ]; do
if [ "${ermapping["$curChar"]}" != "" ]; then
curChar="${ermapping["$curChar"]}"
fi
if [ "$(getNextChar "$input" "$(( $index + ${#curChar} ))")" = "" ]; then
echo "$curChar"
let index+="${#curChar}"
curChar="$(getNextChar "$input" $index)"
continue
fi
if isinarray "$curChar" "${vowel[@]}"; then
if [ "$(getNextChar "$input" "$(( $index + ${#curChar} ))")" = "$curChar" ]; then
echo -n "$curChar"'^,'
let index+="${#curChar}"
curChar="$(getNextChar "$input" $index)"
continue
fi
if isinarray "$(getNextChar "$input" "$(( $index + ${#curChar} ))")" "${fn[@]}" || isinarray "$(getNextChar "$input" "$(( $index + ${#curChar} ))")" "${aff[@]}"; then
echo -n "$curChar"
let index+="${#curChar}"
curChar="$(getNextChar "$input" $index)"
continue
fi
echo -n "$curChar,"
elif isinarray "$curChar" "${fn[@]}" || isinarray "$curChar" "${aff[@]}"; then
echo -n "$curChar,"
else
echo -n "$curChar"
fi
let index+="${#curChar}"
curChar="$(getNextChar "$input" $index)"
done
shift
done