Import Soul Wouldn’t it be nice if things just worked

8Feb/120

The programmers itch

As one could easily tell by reading the articles on this site, almost all of which revolve around computing or programming I take great enjoyment in having just enough skills with a computer to be able to whip out a laptop and peck out a quick program to solve some mundane problem.

I have been seen to pull out my laptop, on the bus on the way to a ski-trip to create a program to gather statistics on my skiing day, during family holidays to make up a spreadsheet to easily score and graph the progress of games of cards, to come up with scripts to get out of having to go though pages of paper to count up different things and many other occasions.

Just recently I have discovered how strong this "itch" to write programs to solve  silly programs to solve every day problems, when lying in bed, flicking though comics and I came across this.

While the regular person may be amused by the joke, or think it isn't funny at all the programmer in me instantly thought, It couldn't be that hard to figure out what he said. So of I went, ending up with this little program.

from collections import defaultdict

def strip(word):
    out = ""
    for letter in word:
        if letter in "aeiou":
            out += letter
    return out

def paths(tree,base=''):
    for leaf in tree[0]:
        if len(tree)==1:
            yield base + " " + leaf
        else:
            for branch in paths(tree[1:],base + " " + leaf):
                yield branch

words = defaultdict(list)
f = open("words.txt")
for line in f:
    line = line.strip().lower()
    stripped = strip(line)
    if stripped:
        words[stripped].append(line)
f.close()

col_width = 10

raw_sentence = "A UI AOE UIE OU EAI"
sentence = raw_sentence.lower().split(" ")

sentence_tree = []
for word in sentence:
    sentence_tree.append(words[word])

for result in paths(sentence_tree):
    print result

I am quite proud of some parts of my code, like the nice little recursive function/generator for traversing the tree, but I was a bit lacking foresight as to what kind of, or should i say how many results my program would produce. To try and figure out exactly how many results I began with adding a rather naive counter to the final loop and commenting out the print statement. 20 minutes latter and still with no results, I realized how this approach was not going to be a success. That gave rise to this line, which is able to calculate instantly how many possible things could have been said in the comic.

print reduce(lambda a,b:a*b,map(len,sentence_tree)), "possible arrangments"

And this makes it fairly obvious why it was taking so long to list the results with 24299547659100 possible arrangements. Naturally this led on to looking up different topics such as language parsing so that I would be able to determine how much grammatical sense a sentence makes. Only at this point did I realize that I had got rather sidetracked on trying to get to sleep, and gave up on finding out what he could have said as a problem for another day. Maybe in the next couple of days I will get really interested in the problem again and try and write a program where I can outsource to the internet and have it present people with a sentence and have them say yes or no to if it makes sense.

And that's what has led me to write up this article, this "itch" to think you are able to come up with a clever way to try and figure out what the majority of people would not even consider.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.