Play scrabble with grep!

Doing a crossword and no one around to ask about a word, then this instructable is for you. Have not played scrabble since I was a kid. Also like to do crossword puzzles. If everyone uses the same tools, then it really is playing fair.There is an easy way to make the game more interesting and you increase your vocabulary as well as your typing skills at the same time.

Recommended that you have at least some time at the linux command line. These tricks can also be done on the Mac and BSD with minor modifications.

Note: a quick grep trick for finding a file wth certain text is:

 locate filename | grep search text

$ locate.pdf | grep arduino

Step 1: Whats required?

Hardware:
Linux based computer where you can access the command line.

Software:
A word file which should be already available in linux.
Access to the "grep" command.

Knowledge:
Assuming you know how to do a crossword puzzle and or know how to play scrabble.
You know how to use the linux command line.

Step 2: Getting the words.

What would one need? Maybe a list of words and a way to search the list of words. Let’s see there is a list of words on a linux system. That file could be easily copied to the home directory. You definitely want to copy it so if you edit it and there is an issue, there will be no problems with your system. $ cp /usr/share/dict/words .

Step 3: Using grep.

To get to know a command called grep a little better, tried to explore how it could be used to an advantage.  For now, how can I search through the file. The grep command will work perfectly. Well how do you use it. Lets say we want to add some letters where there is ed at the end. We use the “$” sign to show that we want something at the end of the word. $ grep “ed$” words

Alfred
Americanized
Anglicized
Appleseed
Brailled
Englished
Ethelred
Fed
Fred
Frenched

This generates a rather long list. so we might want to paginate the output.
$ grep “ed$” words | less
or we might want to save the list to a file we can edit and peruse for later use.
$ grep “ed$” words > wordfile
That is nice, now lets do letters at the beggining of the file. We use the “^” sign to show that we want something at the beggining of the word.
$ grep “^th” words

thank
thanked
thankful
thankfuller
thankfullest
thankfully
thankfulness
thankfulness’s

You can combine the two commands so that you can find a word that has the beginning and the ending we want. We want to use an “*” to say any letters can go in between. In this case thought we want one of the letters to be “m”. We use “[]” to show which possible letter or letters we want in between.
$ grep “^zo*[m]*ed$” words
zed
zoomed
There you can use the “[]” to say I want to have any words that start with these two letters.
$ grep “^[xz]“

xylem’s
xylophone
xylophone’s
xylophones
xylophonist
xylophonists
z
zanied
zanier
zanies
zaniest


grep "^m..t..s$" words
mantels
mantles
martyrs
masters
matters
mentors
misters
mittens
mortals
mortars
mottoes
mouthes
mutters
mystics

Blow by blow:

* ^ - The carat (shift-6) says "this is the beginning of the line". Without it, it would find all words like "fundamentals".
* $ - The dollar sign is the same thing, only for the end of the line. Without it, you'd also get words like "mattresses".
* . - The period means "any character here". One, and one only, character will match here.

But suppose you're dealing with a game besides a crossword puzzle, like Scrabble for instance, and you're limited by more constraints than in a crossword. You might want to 'hook' (Scrabble lingo for 'add letters to the beginning or end of a word to form more words'). So, let's see how many words end in "are".

ß grep "are$" words | wc -l
43

Well, those are good odds. But we hit the edge of the board with some of them (I peeked). So, we need words that are seven letters or less which end in "are". "^....are$" would get all of the seven letter words, but not the shorter ones. The solution is rather cryptic this time:

ß grep "^.\{1,4\}are$" words
airfare
aware
bare
beware
blare
care
compare
dare
declare
ensnare
fanfare
fare
flare
glare
hare
mare
pare
prepare
rare
scare
share
snare
spare
square
stare
unaware
ware
warfare
welfare

...but we've met the caret, dollar sign, and period before, so really the new part is the \{1,4\}. This says "match as few as one, and as many as four, repetitions of the previous character". The activator for the number range is the curly braces, which then have to be escaped with slashes (does anybody know why, class?). And since the previous character is a period, which matches any letter, we've found all the words shorter than eight letters which end in "are".

This is all well and good, but we only have so many letters to work with in Scrabble at one time. Say that our current rack has the letters "C F T W A B M". Can we limit it to only words which use those letters?

ß grep "^[cftwabm]\{1,4\}are$" words
aware
bare
care
fare
mare
ware

Ah, now we're getting somewhere! The [] square brackets give the set of acceptable characters. Another way to use them is to express a range (e. g. [0-9]), but that's hardly the usual case in word games.

The only limitation is the imagination. If you are playing scrabble, let your partners know what you doing to prevent any hurt feelings. I like to use this same idea for crossword puzzles also.

 Picture of Last thoughts. 

 

 

 

 

 

 

 

 

 

 

 

Step 4: Last thoughts.



If you want to combine two or more word lists to have a greater vocabulary to work from is fine also: (words.new must be an ascii or text file.)

$ mv words words.old
$ cat words.old words.new > words.temp
$ sort words.temp | uniq -u > words

List amount of words

$ wc -l words

You should now have a larger word file!

One other hint:
One zip file I downloaded of words had the words in files separated by the first letter. I needed them in one list. No problem.

$ cat {A..Z}.DIC > words.new

There are plenty of on-line web pages to do all this stuff, but for me it is nice not to have to depend on the internet all the time for one's needs. Have fun!

Step 5: Bonus: What words can you make.


Like anagrams or wonder what words you can make then try the "an" command.
$ sudo apt-get update
$ sudo apt-get install an
$ an "instructables"
inscrutable st
inscrutable t's
inscrutable ts
inscrutable t s
inscrutable t s
....
....
....
...
Want a limited list of only 10 options try:
$ an -n 10 "instructables"
inscrutable st
inscrutable t's
inscrutable ts
inscrutable t s
inscrutable t s
inscrutable t s
inscrutable t s
incurables st t
incurables t's t
incurables ts t

Step 6: Use grep to extract from a stream


Simple batch file to get weather information: (head command limits results).
#!/bin/bash
zip=$1
tmon=$(date +"%b")
tday=$(date +"%d")
echo "The weather for $zip on $tmon $tday:"
lynx -width 1000 -dump "http://m.wund.com/cgi-bin/findweather/getForecast?brand=mobile&query=$zip" > weather
cat weather | grep "Updated"
cat weather | grep "Observed"
cat weather | grep "Temperature"
cat weather | grep "Humidity"
cat weather | grep " Dew Point"
cat weather | grep "Wind" | head -1
cat weather | grep "Wind Gust" | head -1
cat weather | grep "Pressure"
cat weather | grep "Conditions" | head -1
cat weather | grep "Visibility"
cat weather | grep "UV"
cat weather | grep "Clouds"
cat weather | grep "Yesterday's Maximum"
cat weather | grep " Yesterday's Minimum"
cat weather | grep "Sunrise"
cat weather | grep "Sunset"
cat weather | grep "Moon Rise"
cat weather | grep "Moon Set"
cat weather | grep -A1 "Moon Phase"
cat weather | grep "Raw METAR"

Comments

Popular posts from this blog

Guiless?

MSOffice vs Libreoffice

Raspberry pi zero.