A2Z: Binary tree and concordance in Python
For this week's homework, I implemented a simple binary tree and text concordance in Python. The binary tree can store any Python object and use any Python object as a key (so long as it implements a __cmp__ method). The text concordance keeps track of which words occur in a given text, along with a list of locations in the file where those words occur. By default, the concordance script prints out a list of words sorted in descending order by the number of times they occur in the text.
Source code here, and excruciatingly boring sample output after the jump. (The source text was a set of stories by H. P. Lovecraft. Top that for geekiness.)
Not very creative this week, but hopefully this script will serve as a stepping stone for more creative endeavors in future weeks. WOO.
the: 462
and: 269
of: 227
to: 100
that: 75
he: 73
in: 71
a: 65
i: 51
had: 50
was: 41
as: 39
it: 38
with: 36
from: 35
for: 34
but: 31
where: 31
his: 29
on: 29
him: 26
kuranes: 26
we: 26
which: 26
at: 24
not: 24
over: 24
is: 23
me: 23
are: 22
city: 22
there: 22
when: 22
were: 21
white: 21
sea: 20
so: 20
they: 20
came: 19
its: 19
man: 19
many: 19
all: 18
have: 18
land: 17
or: 17
upon: 17
beyond: 16
my: 16
sky: 16
far: 15
more: 15
then: 15
by: 14
men: 14
night: 14
no: 14
out: 14
would: 14
before: 13
down: 13
moon: 13
who: 13
bearded: 12
into: 12
saw: 12
strange: 12
cathuria: 11
celephais: 11
dreams: 11
things: 11
through: 11
time: 11
told: 11
away: 10
beheld: 10
cities: 10
full: 10
ship: 10
this: 10
again: 9
been: 9
distant: 9
one: 9
shore: 9
than: 9
very: 9
village: 9
what: 9
could: 8
golden: 8
horizon: 8
nyl: 8
only: 8
pillars: 8
sailed: 8
seemed: 8
seen: 8
sona: 8
stone: 8
them: 8
toward: 8
bird: 7
come: 7
day: 7
did: 7
ever: 7
found: 7
gardens: 7
green: 7
long: 7
nargai: 7
ooth: 7
sail: 7
some: 7
their: 7
those: 7
west: 7
back: 6
basalt: 6
be: 6
blue: 6
dream: 6
galleys: 6
great: 6
harbour: 6
might: 6
now: 6
old: 6
past: 6
up: 6
walked: 6
waters: 6
ways: 6
world: 6
about: 5
abyss: 5
after: 5
beauty: 5
beneath: 5
bridge: 5
carven: 5
days: 5
first: 5
gay: 5
gods: 5
gold: 5
grew: 5
high: 5
houses: 5
know: 5
known: 5
like: 5
looked: 5
marble: 5
meets: 5
never: 5
ocean: 5
once: 5
our: 5
regions: 5
say: 5
seas: 5
soft: 5
sought: 5
space: 5
spoke: 5
sun: 5
temples: 5
though: 5
unknown: 5
walls: 5
watched: 5
wind: 5
ago: 4
alone: 4
among: 4
an: 4
another: 4
any: 4
clouds: 4
coast: 4
dawn: 4
death: 4
dreamed: 4
dwelt: 4
even: 4
father: 4
floated: 4
fragrant: 4
galley: 4
glittering: 4
grandfather: 4
gray: 4
greater: 4
groves: 4
heard: 4
here: 4
hills: 4
last: 4
name: 4
near: 4
nor: 4
river: 4
rocks: 4
see: 4
silently: 4
since: 4
sometimes: 4
songs: 4
south: 4
streets: 4
such: 4
terraces: 4
these: 4
trees: 4
valley: 4
wall: 4
wherein: 4
whom: 4
whose: 4
wonders: 4
aeons: 3
ahead: 3
along: 3
also: 3
appeared: 3
athib: 3
beautiful: 3
below: 3
born: 3
bright: 3
bronze: 3
childhood: 3
cliffs: 3
crash: 3
dark: 3
darkness: 3
dead: 3
drugs: 3
end: 3
eyes: 3
fancy: 3
few: 3
filled: 3
finally: 3
flowers: 3
flying: 3
followed: 3
fountains: 3
glimpses: 3
goal: 3
god: 3
happy: 3
harbor: 3
heavens: 3
if: 3
infinity: 3
knew: 3
knights: 3
lands: 3
left: 3
life: 3
lore: 3
may: 3
mist: 3
moonlight: 3
mountains: 3
myself: 3
narg: 3
neither: 3
new: 3
none: 3
oarsmen: 3
often: 3
overlooking: 3
painted: 3
palace: 3
part: 3
passed: 3
people: 3
places: 3
precipice: 3
red: 3
remote: 3
roofed: 3
roofs: 3
rose: 3
said: 3
same: 3
serannian: 3
shine: 3
shores: 3
sleep: 3
small: 3
soon: 3
splendid: 3
spread: 3
still: 3
street: 3
summer: 3
sweet: 3
tales: 3
thalarion: 3
tower: 3
turn: 3
under: 3
warm: 3
went: 3
whether: 3
wonder: 3
years: 3
yet: 3
young: 3
zar: 3
afterward: 2
air: 2
alive: 2
alluring: 2
amid: 2
ancestors: 2
anchored: 2
ancient: 2
apart: 2
approached: 2
aran: 2
asleep: 2
awake: 2
azure: 2
beckon: 2
behold: 2
birds: 2
blossoms: 2
books: 2
bound: 2
breeze: 2
brightness: 2
brink: 2
built: 2
called: 2
calm: 2
can: 2
care: 2
carried: 2
ceased: 2
celestial: 2
channel: 2
clear: 2
commenced: 2
companies: 2
cool: 2
covered: 2
crystal: 2
damp: 2
descended: 2
dorieb: 2
each: 2
east: 2
edge: 2
effulgent: 2
endlessly: 2
enter: 2
entered: 2
exist: 2
failed: 2
faint: 2
fashioned: 2
feet: 2
find: 2
flowery: 2
forests: 2
form: 2
friendly: 2
gaily: 2
garret: 2
gas: 2
gate: 2
gates: 2
gave: 2
gently: 2
gigantic: 2
glide: 2
glided: 2
glowing: 2
gorgeous: 2
grant: 2
half: 2
has: 2
hath: 2
having: 2
headlands: 2
heaven: 2
heroes: 2
hid: 2
home: 2
horsemen: 2
horses: 2
hour: 2
house: 2
hue: 2
illusion: 2
indeed: 2
ivy: 2
just: 2
kept: 2
lane: 2
led: 2
length: 2
lies: 2
light: 2
lighthouse: 2
listen: 2
lit: 2
london: 2
lovely: 2
marvellous: 2
meadows: 2
memories: 2
met: 2
mighty: 2
mists: 2
mockingly: 2
moment: 2
money: 2
moonbeams: 2
mount: 2
mountain: 2
music: 2
native: 2
notes: 2
off: 2
onward: 2
opened: 2
other: 2
others: 2
pagodas: 2
palaces: 2
pavements: 2
peak: 2
peaks: 2
phosphorescent: 2
pink: 2
place: 2
plain: 2
played: 2
praises: 2
quaint: 2
reach: 2
reigns: 2
remembered: 2
return: 2
reveal: 2
rich: 2
rift: 2
risen: 2
rising: 2
rode: 2
sang: 2
saying: 2
scented: 2
secrets: 2
sent: 2
set: 2
sight: 2
silent: 2
smoothly: 2
snowy: 2
somewhere: 2
song: 2
sound: 2
speak: 2
spires: 2
splendor: 2
studded: 2
suddenly: 2
summits: 2
swaying: 2
tanarian: 2
tell: 2
thereafter: 2
think: 2
thinner: 2
thoughts: 2
thousand: 2
three: 2
tide: 2
till: 2
times: 2
too: 2
towers: 2
towns: 2
try: 2
turned: 2
twilight: 2
urged: 2
us: 2
vain: 2
valleys: 2
verdant: 2
view: 2