2017-09-14 20:30:06 +00:00
|
|
|
#!/usr/bin/env bash
|
2017-10-06 17:33:08 +00:00
|
|
|
source bashlib
|
|
|
|
cdsource
|
2017-09-14 20:30:06 +00:00
|
|
|
|
|
|
|
getword() {
|
|
|
|
local cat=$1 pop_limit=$2 words=()
|
|
|
|
|
|
|
|
while read pop word; do
|
|
|
|
(( pop_limit && pop > pop_limit )) && break
|
|
|
|
words+=( "$word" )
|
|
|
|
done < "words.txt.$cat"
|
|
|
|
|
|
|
|
(( ${#words[@]} )) || { echo '1 -'; return 1; }
|
|
|
|
echo "${#words[@]} ${words[RANDOM % ${#words[@]}]}"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare -A categoryByCharacter=(
|
|
|
|
['a']=adj
|
|
|
|
['r']=adv
|
|
|
|
['n']=noun
|
|
|
|
['v']=verb
|
|
|
|
)
|
|
|
|
templates=(
|
2017-10-06 17:33:08 +00:00
|
|
|
ran # ~32.0 bit
|
2017-09-14 20:30:06 +00:00
|
|
|
)
|
|
|
|
permutations=1
|
|
|
|
|
|
|
|
while getopts :t:p: arg; do
|
|
|
|
case $arg in
|
|
|
|
# The sentence template to use.
|
|
|
|
t) template=$OPTARG ;;
|
|
|
|
# Use only the top N most popular words.
|
|
|
|
p) pop_limit=$OPTARG ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
template=${template:-${templates[$((RANDOM % ${#templates[@]}))]}}
|
|
|
|
|
|
|
|
|
2020-04-02 20:13:12 +00:00
|
|
|
while true; do (
|
|
|
|
printf 'sentence: '
|
|
|
|
while read -n1 character && [[ $character ]]; do
|
|
|
|
category=${categoryByCharacter["$character"]}
|
|
|
|
read p word < <(getword "$category" "$pop_limit")
|
|
|
|
(( permutations *= p ))
|
|
|
|
printf '%s ' "$word"
|
|
|
|
done <<< "$template"
|
|
|
|
echo
|
|
|
|
printf 'permutations: %s, entropy: ~%.1f bit\n' "$permutations" "$(bc -l <<< "l($permutations) / l(2)")"
|
|
|
|
|
|
|
|
read -s -n 1 -p 'Generate another? [Yn]' && echo || break
|
|
|
|
[[ $REPLY = n ]] && break
|
|
|
|
); done
|
2017-09-14 20:30:06 +00:00
|
|
|
|