For fun: An algorithm and Python Code for creating word chains between words of the same length. Words are linked on the chain if they differ by only one letter. All words must be English words (e.g. contained in Unix words file with possible additions of missing words). Example: wife -> life -> live -> love
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Word chains
1. Word Chains
An algorithm and Python Code for creating word chains between words of the same
length. Words are linked on the chain if they differ by only one letter. All words must be
English words (e.g. contained in Unix words file with possible additions of missing words)
Example: open -> shut ['open', 'omen', 'amen', 'agen', 'aged', 'ages', 'aces', 'acts', 'arts', 'arms', 'aims',
'dims', 'dams', 'dame', 'dare', 'mare', 'more', 'sore', 'sort', 'soot', 'shot', 'shut']
Example: old -> new [‘old’, 'odd', 'add', 'aid', 'aim', 'dim', 'din', 'den', 'dew', 'new']
2. Algorithm for Word Chains
There are over 235, 000 words in the Unix Word file. Even after partitioning words
by length. It would be computationally expensive to create a linked word graph and
find shortest paths.
The algorithm works by selecting pivot (“Kevin Bacon”) words for each word
length (aim, mare, parts). The user begins by selecting a starting word and
interactively building a chain to the pivot words. The program checks input to
ensure that the words are legitimate and also catches input errors.
When the user is successful, the program stores the word chain in a file. As the
used creates more chains to the pivot words, the program uses them to create
word chains automatically between any tow words that it has seen before. It
connects the two words through the pivot word so it is not optimal. However it
is easy for the user to optimize once a connecting chain is output.
As the user builds more word chains interactively, the program can produce
more chains automatically. Eventually for 3 and 4 letter words, the program will
be able to output chains in almost every case. There may not be chains for
words of length created than 4.
4. The Program is shown on next 4 slides
The program can be downloaded at https://drive.google.com/file/d/
1IrLNiNqN8oRk92VEmQqRw0-PgIE6kNtZ/view?usp=sharing.
Point the “words” file access to the Unix words file at /usr/share/dict/words.
(I made a copy so I could add some English words not in the Unix file).
Change the other file accesses in the program to a directory of your choice.
Creating word chains interactively is an entertaining puzzle especially for
obscure 4 letter words. I have included my interactive chains but it is
probably better to create your own. My stored chains will make most 3 and 4
letter chains between words automatically
5. Program for Word Chains(cont)
import os
import io
def check (w):
s = "grep {word} /Users/bobmarcus/Chains/words > /Users/bobmarcus/
Chains/checkword"
t = s.format(word = w)
os.system(t)
f = open("/Users/bobmarcus/Chains/checkword")
l = f.readlines()
c = l.count(w + "n")
if c == 0 :
c = checkplural(w)
if c == 0:
if len (w) >= 5:
last = len (w) - 1
if w[last] == "s" or w[last- 1: last +1] == "er" or w[last -1: last + 1] ==
"ed" or w[last - 2 : last + 1] == "ing":
s ="Is {word} a word? Answer y or n "
t = s.format(word = w)
ans = input(t)
if ans == "y":
c = 1
return(c)
def checkplural(w):
if w [len(w) - 1]!= "s":
return (0)
else:
w = w[0:len (w) - 1]
c = check(w)
return(c)
6. Program for Word Chains(cont)
def new (s, pos, letter):
l = list (s)
l[pos - 1] = letter
str = ""
for i in range (0,len(l)):
str = str + l[i]
ok = check(str)
if ok > 0:
print("OK")
return (str)
else:
print("Sorry")
return (s)
def findchains(word):
s = "grep {commaword} /Users/bobmarcus/Chains/chains.txt > /Users/
bobmarcus/Chains/ch.txt"
t = s.format(commaword ="," + word + ",")
os.system(t)
h = open("/Users/bobmarcus/Chains/ch.txt")
s1 = h.read()
s2 = s1.split("n")
s3 = s2[0]
s4 = s3.split(",")
s5 = s4 [1: len(s4) -1]
return(s5)
def trim (list, string):
if list.count (string)== 0:
return(list)
else:
i = list.index(string)
list = list[i: len(list)]
return list
7. Program for Word Chains *cont)
# findchainx returns [] if no match is found
print("Make finishing word 'aim', 'mare', or 'parts'to add chain to database")
done = False
c = 0
print("If you give up, enter'quit'")
while c == 0 :
start = input ("starting word? ")
c = check(start)
if c == 0:
print ("Not a word. Try again")
c = 0
while c == 0 :
finish = input ("finishing word? ")
c = check(finish)
if c == 0:
print ("Not a word. Try again")
elif len(start) != len (finish):
print("Length of finishing word must be the same as starting word. Try again")
c = 0
chain1 = findchains(start)
chain2 = findchains(finish)
if chain1 != [] and chain2 != []:
chain1 = trim(chain1, start)
chain2 = trim (chain2, finish)
chain2.reverse()
chain2 = chain2[1: len(chain2)]
chain = chain1 + chain2
print ("count = ", len(chain))
print (chain)
done = True
lp ="Success"
8. Program for Word Chains (cont)
if not (done):
chain = "," + start + ","
s = start
while (s != finish) and not(done):
while True:
try:
lp = input ("Enter new letter and position ")
if lp == 'quit':
done = True
break lps = lp.split(" ")
print(lps)
letter = lps[0]
pos = int(lps[1])
snew = new(s,pos,letter)
if snew != s:
chain = chain + snew + ","
s = snew
print(s)
break
except (ValueError, IndexError, RuntimeError, TypeError,
NameError):
print ("Bad Input, Try again")
if lp != 'quit':
print("Success")
else:
print("Better luck next time")
if not(done):
if finish == "mare" or finish == "aim" or finish == "parts":
g = open("/Users/bobmarcus/Chains/chains.txt", 'a')
g.write (chain + "n")
g. flush()
chainlist = chain.split(",")
chainlist = chainlist[1: len(chainlist) - 1]
count = len (chainlist)
print("count = ", count)
print (chainlist)
9. Word Chains created interactively are on next
6 slides for illustration. The program can
automatically create a link between any two
words (with same length) on any of the Chains.
16. The Unix words file is enormous building, storing,
and searching complete Word Graphs for each word
length would be very expensive computationally.
The next slide shows the first page of the words file
17. First page of Unix Words file (over 235,000 words)
A
a
aa
aal
aalii
aam
Aani
aardvark
aardwolf
Aaron
Aaronic
Aaronical
Aaronite
Aaronitic
Aaru
Ab
aba
Ababdeh
Ababua
abac
abaca
abacate
abacay
abacinate
abacination
abaciscus
abacist
aback
abactinal
abactinally
abaction
abactor
abaculus
abacus
19. Summary and Lessons Learned
The algorithm uses stored sample interactive solutions to enable automatic
solutions of a full range of problems.
This combination could be used for similar problems (e.g. building chains
between actors thru Kevin Bacon) or more diverse problems where sample
interactive solutions can provide guidance to general solutions.
There are several optimizations that I have not yet programmed including:
- automatically completing an interactive solution when reaching a stored word
- trimming an automatic solution to eliminate unnecessary words
e.g. ['bore', 'more', 'mare', 'more', ‘gore'] -> ['bore', 'more', ‘gore’] -> [‘bore’,‘gore’]
- using multiple linked pivot words to simplify interactive solutions
e.g using “arms” and “mare” as pivot words
Creating the interactive solutions with the program improved my skill at
building word chains. This increased human capability could be a useful side
effect when using the algorithm on other problems.
With sufficient computing power, the interactive sample solutions could be
generated automatically by a program using trial and error substitutions to
build word chains. This program would need some heuristics to guide it
towards the desired finish word or else it could make many unnecessary links