You can use TfidfVectorizer from sklean
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
from scipy.sparse.csr import csr_matrix #need this if you want to save tfidf_matrix
tf = TfidfVectorizer(input='filename', analyzer='word', ngram_range=(1,6),
min_df = 0, stop_words = 'english', sublinear_tf=True)
tfidf_matrix = tf.fit_transform(corpus)
The above tfidf_matix has the TF-IDF values of all the documents in the corpus. This is a big sparse matrix. Now,
feature_names = tf.get_feature_names()
this gives you the list of all the tokens or n-grams or words.
For the first document in your corpus,
doc = 0
feature_index = tfidf_matrix[doc,:].nonzero()[1]
tfidf_scores = zip(feature_index, [tfidf_matrix[doc, x] for x in feature_index])
Lets print them,
for w, s in [(feature_names[i], s) for (i, s) in tfidf_scores]:
print w, s
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…