#!/usr/bin/env bash
source bashlib
cdsource

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=(
    ran # ~32.0 bit
)
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[@]}))]}}


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