Skip to content

Commit 94c02a2

Browse files
authored
Update klingon.sh
1 parent 889cf39 commit 94c02a2

File tree

1 file changed

+3
-52
lines changed

1 file changed

+3
-52
lines changed

kli/klingon.sh

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#!/bin/bash
22

3-
# ansi color codes
43
RED='\033[0;31m'
54
GREEN='\033[0;32m'
65
YELLOW='\033[0;33m'
76
BLUE='\033[0;34m'
87
MAGENTA='\033[0;35m'
98
CYAN='\033[0;36m'
10-
NC='\033[0m' # no color
9+
NC='\033[0m'
1110

12-
# mapping from latin letters to klingon pIqaD code points
1311
declare -A klingon_map=(
1412
["a"]=$'\uF8D0'
1513
["b"]=$'\uF8D1'
@@ -42,32 +40,24 @@ declare -A klingon_map=(
4240
["'"]=$'\uF8E9'
4341
)
4442

45-
# english to klingon word dictionary
4643
declare -A klingon_dictionary=(
47-
# greetings
4844
["hello"]="nuqneH"
4945
["goodbye"]="Qapla'"
5046
["hi"]="nuqneH"
5147
["greetings"]="nuqneH"
52-
53-
# pronouns
5448
["i"]="jIH"
5549
["you"]="SoH"
5650
["we"]="maH"
5751
["they"]="naDev"
5852
["he"]="ghaH"
5953
["she"]="ghaH"
6054
["it"]="ghaH"
61-
62-
# common phrases
6355
["yes"]="HIja'"
6456
["no"]="ghobe'"
6557
["please"]="qa'plu'"
6658
["thank"]="qatlho'"
6759
["thanks"]="qatlho'"
6860
["sorry"]="jIQoch"
69-
70-
# nouns
7161
["friend"]="jup"
7262
["enemy"]="jagh"
7363
["battle"]="veS"
@@ -84,37 +74,28 @@ declare -A klingon_dictionary=(
8474
["weaponry"]="puqpu'"
8575
["sword"]="QIch"
8676
["blood"]="beq"
87-
88-
# verbs
8977
["attack"]="batlh"
9078
["fight"]="batlh"
9179
["love"]="bang"
9280
["hate"]="QInvam"
9381
["run"]="vItlhutlh"
9482
["speak"]="tlhIngan Hol"
95-
96-
# adjectives
9783
["strong"]="vItlhutlh"
9884
["brave"]="batlh"
9985
["quick"]="pagh"
10086
["swift"]="pagh"
101-
102-
# others
10387
["money"]="tlhegh"
10488
["food"]="choq"
10589
["leader"]="Qun"
10690
["king"]="raS"
10791
["queen"]="raS Qun"
108-
# add more words as desired
10992
)
11093

111-
# function to translate english to klingon word-by-word
11294
translate_to_klingon() {
11395
local sentence=("$@")
11496
local klingon_sentence=()
11597
local word translated_word
11698

117-
# simple pluralization: append 'pu'' for people or 'mey' for objects
11899
pluralize() {
119100
case "$1" in
120101
jup|jagh|veS|batlh|tlhIngan|wo\'|Duj|DujDaq|Hov|Hegh|ngaD|puq|puqpu\'|QIch|beq|bang|QInvam|vItlhutlh|tlhIngan\ Hol|choq|Qun|raS|raS\ Qun)
@@ -126,9 +107,7 @@ translate_to_klingon() {
126107
esac
127108
}
128109

129-
# iterate over each word
130110
for word in "${sentence[@]}"; do
131-
# handle plural words
132111
if [[ "$word" == *"s" ]]; then
133112
base_word="${word%s}"
134113
if [ "${klingon_dictionary[$base_word]+_}" ]; then
@@ -138,36 +117,25 @@ translate_to_klingon() {
138117
fi
139118
fi
140119

141-
# translate word if exists in dictionary
142120
if [ "${klingon_dictionary[$word]+_}" ]; then
143121
klingon_sentence+=("${klingon_dictionary[$word]}")
144122
else
145123
klingon_sentence+=("$word")
146124
fi
147125
done
148126

149-
# implement basic Object-Verb-Subject (OVS) word order
150-
# this is a naive implementation and works for simple sentences
151-
# example: "I attack enemy" -> "enemy attack I"
152-
153-
# identify positions of subject, verb, object
154-
# for simplicity, assume sentences are in "Subject Verb Object" format
155-
# and reorder them to "Object Verb Subject"
156-
157127
local subject=""
158128
local verb=""
159129
local object=""
160130
local reordered_sentence=()
161131

162-
# find subject (pronouns or known subjects)
163132
for idx in "${!klingon_sentence[@]}"; do
164133
local w="${klingon_sentence[$idx]}"
165134
if [[ "$w" == "jIH" || "$w" == "SoH" || "$w" == "maH" || "$w" == "naDev" || "$w" == "ghaH" ]]; then
166135
subject="$w"
167136
fi
168137
done
169138

170-
# find verb (from a set of known verbs)
171139
declare -A verbs
172140
verbs=( ["batlh"]=1 ["bang"]=1 ["QInvam"]=1 ["vItlhutlh"]=1 ["tlhIngan Hol"]=1 )
173141

@@ -178,34 +146,28 @@ translate_to_klingon() {
178146
fi
179147
done
180148

181-
# find object (remaining words)
182149
for w in "${klingon_sentence[@]}"; do
183150
if [[ "$w" != "$subject" && "$w" != "$verb" ]]; then
184151
object="$w"
185152
fi
186153
done
187154

188-
# reorder to Object Verb Subject
189155
if [[ -n "$object" && -n "$verb" && -n "$subject" ]]; then
190156
reordered_sentence=("$object" "$verb" "$subject")
191157
else
192-
# if cannot determine, keep original order
193158
reordered_sentence=("${klingon_sentence[@]}")
194159
fi
195160

196-
# join the reordered sentence into a single string
197161
echo "${reordered_sentence[@]}"
198162
}
199163

200-
# function to convert translated Klingon words to pIqaD glyphs
201164
convert_to_piqaD() {
202165
local translated_text="$1"
203166
local output_text=""
204167
local i=0
205168
local length=${#translated_text}
206169

207170
while [ $i -lt $length ]; do
208-
# handle multicharacter symbols
209171
if [ "${translated_text:$i:3}" == "tlh" ]; then
210172
output_text+="${klingon_map["tlh"]}"
211173
i=$((i+3))
@@ -219,7 +181,7 @@ convert_to_piqaD() {
219181
output_text+="${klingon_map["ng"]}"
220182
i=$((i+2))
221183
elif [ "${translated_text:$i:3}" == "pu'" ]; then
222-
output_text+="${klingon_map["'"]}" # represent plural marker as apostrophe
184+
output_text+="${klingon_map["'"]}"
223185
i=$((i+3))
224186
elif [ "${translated_text:$i:1}" == "'" ]; then
225187
output_text+="${klingon_map["'"]}"
@@ -229,7 +191,6 @@ convert_to_piqaD() {
229191
if [ "${klingon_map[$char]+_}" ]; then
230192
output_text+="${klingon_map[$char]}"
231193
else
232-
# for characters not in the mapping, add them as is
233194
output_text+="$char"
234195
fi
235196
i=$((i+1))
@@ -239,34 +200,24 @@ convert_to_piqaD() {
239200
echo -e "$output_text"
240201
}
241202

242-
# main Loop
243203
while true; do
244-
# Read user input
245204
echo -e "${YELLOW}Enter text (or type 'exit' to quit):${NC} "
246205
read -p "" input_text
247206

248-
# exit if user types 'exit' or an empty line
249207
if [ -z "$input_text" ] || [ "$input_text" == "exit" ]; then
250208
echo -e "${GREEN}Qapla'! (Success!)${NC}"
251209
break
252210
fi
253211

254-
# convert input text to lower case
255212
input_text=$(echo "$input_text" | tr '[:upper:]' '[:lower:]')
256213

257-
# split input into words
258214
IFS=' ' read -r -a words <<< "$input_text"
259215

260-
# translate words to Klingon
261216
translated_sentence=$(translate_to_klingon "${words[@]}")
262217

263-
# convert translated sentence to pIqaD glyphs
264218
piqad_text=$(convert_to_piqaD "$translated_sentence")
265219

266-
# output the converted text
267-
#echo -e "${BLUE}English:${NC} $input_text"
268-
#echo -e "${MAGENTA}Klingon:${NC} $translated_sentence"
269220
echo -e "${CYAN}pIqaD:${NC}"
270221
echo -e " $piqad_text"
271-
echo "" # add an empty line for better readability
222+
echo ""
272223
done

0 commit comments

Comments
 (0)