code for calculating tf-idf for each (term, doc) pair