Spelling Bee (text processing)
by danielbmartin from LinuxQuestions.org on (#529KG)
This is a learning exercise done just for fun.
It is inspired by a NYTimes word puzzle called Spelling Bee
written by Patrick Berry.
Have: a file of English words called WordList.
Have: a string of 7 characters called Hive.
Want:
Step 1...
Find words of length >4 letters which use ONLY the letters in
the string "hive" and MUST use the first letter in "hive".
Step 2...
Find words which meet the criteria in Step 1,
and use ALL of the letters in "hive".
This is my "brute force" solution.
Code:#!/bin/bash Daniel B. Martin Apr20
# Step 1...
# Find words of length >4 letters which use ONLY the letters in
# the string "hive" and MUST use the first letter in "hive".
# Step 2...
# Find words which meet the criteria in Step 1,
# and use ALL of the letters in "hive".
# File identification
Path=${0%%.*}
Only=$Path"only.txt"
All=$Path"all.txt"
WordList='/usr/share/dict/words'
hive='luenopt'
echo 'Words which use only the letters in "'$hive'"'
echo ' and contain the letter "'${hive:0:1}'".'
sed -n '/^.\{5\}/p' $WordList \
|tr -c $hive"\n" "~" \
|grep -v "~" \
|grep ${hive:0:1} \
>$Only
cat $Only
echo; echo 'Words which use all of the letters in "'$hive'".'
grep "${hive:0:1}" <$Only \
|grep "${hive:1:1}" \
|grep "${hive:2:1}" \
|grep "${hive:3:1}" \
|grep "${hive:4:1}" \
|grep "${hive:5:1}" \
|grep "${hive:6:1}" \
>$All
cat $All
echo; echo "Normal end of job."; echo; exitIt produces this result:
Code:Words which use only the letters in "luenopt"
and contain the letter "l".
elope
letup
lotto
nettle
opulent
outlet
pellet
people
pollen
pollute
pullet
pullout
topple
tulle
tunnel
Words which use all of the letters in "luenopt".
opulent
Normal end of job.I suspect there is a cleaner better faster way.
Ideas? Suggestions?
Daniel B. Martin
.


It is inspired by a NYTimes word puzzle called Spelling Bee
written by Patrick Berry.
Have: a file of English words called WordList.
Have: a string of 7 characters called Hive.
Want:
Step 1...
Find words of length >4 letters which use ONLY the letters in
the string "hive" and MUST use the first letter in "hive".
Step 2...
Find words which meet the criteria in Step 1,
and use ALL of the letters in "hive".
This is my "brute force" solution.
Code:#!/bin/bash Daniel B. Martin Apr20
# Step 1...
# Find words of length >4 letters which use ONLY the letters in
# the string "hive" and MUST use the first letter in "hive".
# Step 2...
# Find words which meet the criteria in Step 1,
# and use ALL of the letters in "hive".
# File identification
Path=${0%%.*}
Only=$Path"only.txt"
All=$Path"all.txt"
WordList='/usr/share/dict/words'
hive='luenopt'
echo 'Words which use only the letters in "'$hive'"'
echo ' and contain the letter "'${hive:0:1}'".'
sed -n '/^.\{5\}/p' $WordList \
|tr -c $hive"\n" "~" \
|grep -v "~" \
|grep ${hive:0:1} \
>$Only
cat $Only
echo; echo 'Words which use all of the letters in "'$hive'".'
grep "${hive:0:1}" <$Only \
|grep "${hive:1:1}" \
|grep "${hive:2:1}" \
|grep "${hive:3:1}" \
|grep "${hive:4:1}" \
|grep "${hive:5:1}" \
|grep "${hive:6:1}" \
>$All
cat $All
echo; echo "Normal end of job."; echo; exitIt produces this result:
Code:Words which use only the letters in "luenopt"
and contain the letter "l".
elope
letup
lotto
nettle
opulent
outlet
pellet
people
pollen
pollute
pullet
pullout
topple
tulle
tunnel
Words which use all of the letters in "luenopt".
opulent
Normal end of job.I suspect there is a cleaner better faster way.
Ideas? Suggestions?
Daniel B. Martin
.