|
#!/bin/bash |
|
function ipsum |
|
{ |
|
# Random Word Generator |
|
# Inspired by: |
|
# - https://linuxconfig.org/random-word-generator |
|
# - http://stackoverflow.com/a/14203146/4769802 |
|
# - http://www.unix.com/shell-programming-and-scripting/156551-check-whether-string-begin-uppercase-lowercase-digit.html |
|
# |
|
# Depends on: |
|
# - translate-shell: https://github.com/soimort/translate-shell |
|
# - This ^ thing is awesome. Seriously, try it out! |
|
|
|
|
|
#-----------------------------------------------------------------Constants |
|
X=0 |
|
COUNT=1 |
|
MAX_INT=16777216 |
|
LANGUAGE=false |
|
LANGUAGES="en" |
|
NUM_LANGUAGES=1 |
|
ENTROPY=false |
|
ALL_NON_RANDOM_WORDS=/home/bbolen/scripts/words.txt # dictionary file |
|
non_random_words=`cat $ALL_NON_RANDOM_WORDS | wc -l` # total # of words |
|
proper_nouns=`cat $ALL_NON_RANDOM_WORDS | grep [A-Z].* | wc -l` # apprx # of proper nouns |
|
total_options=$((non_random_words - proper_nouns)) # apprx # of options |
|
|
|
#-----------------------------------------------------------------Handle Flags w. Params |
|
while [[ $# -gt 1 ]] |
|
do |
|
key="$1" |
|
case $key in |
|
-e|--entropy) |
|
# print the entropy |
|
ENTROPY=true |
|
;; |
|
-l|--lang) |
|
# Get the specifed languages! |
|
LANGUAGE=true |
|
LANGUAGES=(${2//+/ }) |
|
NUM_LANGUAGES=${#LANGUAGES[@]} |
|
shift # past argument |
|
;; |
|
--lang-all) |
|
# Use top 10 languages at once! |
|
LANGUAGE=true |
|
LANGUAGES=(zh pt es bn en ru hi ja ar pa) |
|
NUM_LANGUAGES=${#LANGUAGES[@]} |
|
;; |
|
*) |
|
echo "Unrecognized argument: try 'ipsum --help'" |
|
exit |
|
;; |
|
esac |
|
shift # past argument or value |
|
done |
|
|
|
#-----------------------------------------------------------------Handle Main Param |
|
if [[ -n $1 ]]; then |
|
case $1 in |
|
-a|--about) |
|
# Give a description of self |
|
echo "Ipsum is a minimalist random word generator" |
|
echo "with several notable features:" |
|
echo " - Off-line: uses local dictionary so" |
|
echo " no internet is needed" |
|
echo " - Languages: can generate non-English words" |
|
echo " - Note: this requires the internet" |
|
echo " - Random: uses '/dev/urandom' which is a CSPRNG" |
|
echo " - Entropy: calculates entropy of requested word combo" |
|
echo "" |
|
exit |
|
;; |
|
-h|--help) |
|
# Show some examples |
|
echo "" |
|
echo "Example Usage: ipsum" |
|
echo " ipsum 12" |
|
echo " ipsum -lang fr 12" |
|
echo " ipsum -l fr 12" |
|
echo " ipsum -l fr+ru 12" |
|
echo " ipsum --entropy 12" |
|
echo " ipsum -e 12" |
|
echo " ipsum -l fr+ru -e 12" |
|
echo "" |
|
echo "Language Options:" |
|
echo " - Found here :: https://github.com/soimort/translate-shell" |
|
echo "" |
|
exit |
|
;; |
|
-i|--info) |
|
# Show information about numbers and such |
|
echo "" |
|
echo "Dictionary Location: $ALL_NON_RANDOM_WORDS" |
|
echo "Dictionary Size: $non_random_words" |
|
echo "Proper Nouns: $proper_nouns" |
|
echo "Usable Words: $total_options" |
|
echo "Default Language: $LANGUAGES" |
|
echo "Possible Languages: 124" |
|
echo " _________________________________ " |
|
echo " | Top Ten Most Common Languages |" |
|
echo " |----------------|----------------|" |
|
echo " | Chinese - zh | Portuguese - pt|" |
|
echo " | Spanish - es | Bengali - bn |" |
|
echo " | English - en | Russian - ru |" |
|
echo " | Hindi - hi | Japanese - ja |" |
|
echo " | Arabic - ar | Punjabi - pa |" |
|
echo " |________________|________________|" |
|
echo "" |
|
exit |
|
;; |
|
[0-9]*) |
|
# It is probably a number, so work normally |
|
COUNT="$1" |
|
;; |
|
*) |
|
echo "Unrecognized argument: try 'ipsum --help'" |
|
exit |
|
;; |
|
esac |
|
|
|
fi |
|
|
|
#-----------------------------------------------------------------Calculate Entrophy |
|
if [ "$ENTROPY" = true ]; then |
|
# Equation = log_2( #_of_options * #_of_languages ) * #_of_words |
|
log_base_2=`echo "l($total_options*$NUM_LANGUAGES)/l(2)" | bc -l` # log base 2 of options |
|
entropy_bits=`printf "%.*f\n" 2 $log_base_2` # bits of entropy per word |
|
total_entropy=$(echo "$entropy_bits * $COUNT" | bc) |
|
echo "$COUNT word(s) from $NUM_LANGUAGES language(s) produces $total_entropy bits of entropy." |
|
echo "" |
|
fi |
|
|
|
#-----------------------------------------------------------------Make $COUNT Random Words |
|
while [[ "$X" -lt "$COUNT" ]]; do |
|
random_number=`echo "$(od -N3 -An -i /dev/urandom)*$non_random_words/$MAX_INT" | bc` |
|
WORD=$(sed `echo $random_number`"q;d" $ALL_NON_RANDOM_WORDS) |
|
# Clean and Format |
|
DEL="'s" |
|
WORD=${WORD%$DEL} |
|
DEL="'" |
|
WORD=${WORD%$DEL} |
|
# Clean out 'funny' words (ie proper nouns) |
|
case ${WORD:0:1} in |
|
[[:lower:]]) |
|
# Lowercase indicates a common noun |
|
if [ "$LANGUAGE" = true ]; then # If lang was specified use trans |
|
random_index=`echo "$(od -N3 -An -i /dev/urandom)*$NUM_LANGUAGES/$MAX_INT" | bc` |
|
LANG=${LANGUAGES[random_index]} |
|
trans -b :$LANG $WORD 2> /dev/null |
|
echo " ^ $LANG for $WORD" |
|
else # else just given normal word |
|
echo "$WORD" |
|
fi |
|
let "X = X + 1" |
|
;; |
|
*) |
|
# Don't use whatever $WORD was |
|
;; |
|
esac |
|
done |
|
} |
Maintained (and simplified) version of this script is here: https://github.com/NonlinearFruit/dotfiles/blob/master/scripts/ipsum