diff --git a/README.md b/README.md index 4ee9bb9..c2c07ff 100644 --- a/README.md +++ b/README.md @@ -62,14 +62,15 @@ To see all available options type typist --help or just ### Tips Doing the same test over and over again isn't good practice. I have provided -a text file and a small python script to randomize the most used words in the -English language. You can use this script on any text file, though it will -remove punctuation marks and reformats the text. +a text file and a small python script and a shell script to randomize the most +used words in the English language. You can use this script on any text file, +though it will remove punctuation marks and reformats the text. ``` -python3 utils/randomize.py most-used-words.txt +python3 utils/randomize.py most_used_words.txt +# or +utils/randomize.sh most_used_words.txt ``` - --- ### [Issues / Bugs](https://github.com/ny64/typist/issues) diff --git a/utils/randomize.sh b/utils/randomize.sh new file mode 100755 index 0000000..8373237 --- /dev/null +++ b/utils/randomize.sh @@ -0,0 +1,50 @@ +#!/bin/sh + +# use a POSIX shell shuf alternative if none on the system +command -v shuf > /dev/null 2> /dev/null || shuf() { + awk ' + BEGIN { + srand(); + OFMT = "%.17f" + } + { + print rand(), $0 + } + ' "$@" | + sort -k1,1n | + cut -d ' ' -f2- ;} + +# first argument is the filename +filename="$1" +[ ! -f "$filename" ] && printf "%s\n" "You must provide a text file as argument." >&2 && exit 1 + +words="" +# read file line by line +while IFS= read -r line; do + # split the line into words and set them as positional parameters + # shellcheck disable=SC2086 + set -- $line + for val; do + # clean up each word: remove leading and trailing punctuation and quotes + val=$(printf "%s" "$val" | sed -e 's/^[({'"'"'"]*//' -e 's/[.,!?;:)]}'"'"'"]*$//') + words="$words +$val" # one word per line in $words var + done +done < "$filename" + +line_length=0 +{ + # loop over each shuffled words + printf "%s\n" "$words" | shuf | while IFS= read -r word; do + # update line length + line_length=$((line_length + ${#word} + 1)) + # if line length exceeds 80 characters, make a new line + if [ "$line_length" -gt 80 ]; then + echo + # update line length + line_length=$(( ${#word} + 1 )) + fi + printf "%s " "$word" + done +echo +} > "$filename"