Runtime fixes and optimizations

Fixed a few runtime errors, notably log calcuations throwing ValueError
(although I don't think is should happen at all, so current handling may
not be sound)

Weight calculations were taking as much as an hour! on the training
file, and would have taken a few minutes on the test file.

By moving the for loop to flag unigrams as nonsingleton histories, i.e.
to be discounted by given factor, and using dictionaries a little bit
more intelligently to avoid extra string comparisons, the small test
file takes only a few seconds. Yet to test on larger file.

TODO: Try sorting dictionaries to gain more speed.