nltk/nltk — pull request #192 by heatherleaf (2012-01-31 09:53:47+00:00)
Title: Making ConditionalFreqDist a subclass of defaultdict

Here are some simplifications to `probability.py`. Most important is that I made `ConditionalFreqdist` a subclass of `defaultdict`. Apart from making it possible to remove some code, it fixes a bug:

Currently, the following gives an infinite loop:

for key in cfd:
print key, cfd[key]

The reason is that since the cfd has `__len__` and `__getitem__` you can loop over it. But Python tries to loop over indexes calls cfd[0], cfd[1], etc. Then `__getitem__` creates a new `FreqDist` for each of these values, and then `__len__` increases, and so on.

The hard solution is to add a new method, `__iter__`. The simple solution is to make the class a subclass of `defaultdict`. Then we can also remove the definitions of `__getitem__` and `__len__`.

More changes:

- I removed `count`, `sorted` and `sorted_samples` from `FreqDist`, since they have anyway been obsolete for a long time.
- I removed `__eq__` and `__ne__` from `FreqDist` and `ConditionalFreqDist`, since they don't do anything else than what `dict` does. But I kept `__le__`, `__gt__` etc. since they have another semantics than for `dict`.
- I change `__str__` of `FreqDist` so that it only display the 10 most frequent items - otherwise the string can be veeery long.