Improved tf calculation speed

Used defaultdict to count word frequency. And removed some unnecessary
loops.