Tuesday, May 21, 2013

A partial crossword solver in Python

A Cryptic Crossword Clue Solver ←

Saw this via Twitter.

It is a partial crossword solver, because it only helps solve a particular category of crossword clues - those in which the clue (which is usually a sentence or phrase) contains both a "definition" of the answer as well a hint of some kind that leads to the same answer. This solver tries to compute the answer using both the definition and the hint, and checks whether the results match. Ingenious.

I found it interesting because this is a somewhat difficult problem, and yet the author managed to create a solution (involving NLTK and parsing) that works in many, if not all cases.

Also, long ago, in college days, I had written another kind of partial crossword solver (in BASIC); it was much simpler, using a brute force method - what it did was help solve the kind of crossword clues in which the answer is a permutation of a substring of the characters comprising the clue sentence or phrase. The program would generate and display on the screen, all possible permutations of all possible substrings of the sentence, that were of the same length as the answer. Then you had to view those permutations and guess whether any of them was the right answer, based on the clue.

I wrote the permutation-generation code by hand, but saw recently that the Python itertools module has methods to generate permutations (as well as combinations) from sequences:

http://docs.python.org/2/library/itertools.html

http://en.m.wikipedia.org/wiki/Permutation

http://en.wikipedia.org/wiki/Crossword

- Vasudev Ram
dancingbison.com

Charge your phone in 20 seconds

Teen's prize-winning invention may charge your phone in 20 seconds - CNN.com

Interesting  ... She got a $50,000 award for it at an Intel Science competition for high school students.

Monday, May 20, 2013

HuffShell suggests aliases for your Unix commands

paulmars/huffshell · GitHub

huffshell is a gem to suggest optimized aliases for your frequently used Unix commands. It looks at your command history to do that.

Seen via this Hacker News post which is interesting too:

What are your top 100 unix commands? (for science) :

https://news.ycombinator.com/item?id=5733426

- Vasudev Ram
dancingbison.com

Saturday, May 18, 2013

Python's inspect module is powerful

27.13. inspect — Inspect live objects — Python v2.7.5 documentation

The inspect module comes as part of the standard library of Python. It allows you to inspect live objects at run time by using its methods.

Here is an example:

import inspect
class Bar( object):
    def foo( self ):
        print "in Bar.foo 4"
        self .a = 1
        self .di = { 'b' : 2, 'c' : 3 }
        self .li = [  4, 5, 6 ]

bar = Bar()
bar.foo()
for member in inspect.getmembers(bar):
    print member

Running the above code gives this as (partial) output:

in Bar.foo 4

('__dict__', {'a': 1, 'li': [4, 5, 6], 'di': {'c': 3, 'b': 2}})
('__doc__', None)
('a', 1)
('di', {'c': 3, 'b': 2})
('foo', bound method Bar.foo of __main__.Bar object at 0x4035bfac)
('li', [4, 5, 6])

This shows both the bound methods and the member variables of the instance bar.

The inspect module can do a lot of other things too. Check the docs for it, linked at the top of this post.

You can also modify and run the above code snippet at this codepad.org URL:

http://codepad.org/dMLmkhQ6

It may not last there for too long, though, since they probably delete pastes to make room for new ones.

- Vasudev Ram
dancingbison.com

Thursday, May 16, 2013

Reading WAV file info with Python


The Python standard library comes with a module called wave.py. It allows you to read and write WAV format audio files.

Here is an example of reading WAV file info with Python:

import wave

wavf = wave.open('horse.wav', 'r')
print wavf.getnchannels()
print wavf.getsampwidth()
print wavf.getframerate()
print wavf.getnframes()
print wavf.getcompname()

The above code prints the number of channels (mono or stereo), the sampling width, the frame rate, the number of frames, and the name of the compression type (if any) of the WAV file horse.wav.

You can also write WAV files, but you need to know what data to write.

Wikipedia entry for WAV:

http://en.m.wikipedia.org/wiki/WAV

- Vasudev Ram
www.dancingbison.com