I have got this line of code from https://stevenloria.com/tf-idf/
scores = {word: tfidf(word, blob, bloblist) for word in blob.words}
Where the tfidf function is:
import math
from textblob import TextBlob as tb
def tf(word, blob):
return blob.words.count(word) / len(blob.words)
def n_containing(word, bloblist):
return sum(1 for blob in bloblist if word in blob.words)
def idf(word, bloblist):
return math.log(len(bloblist) / (1 + n_containing(word, bloblist)))
def tfidf(word, blob, bloblist):
return tf(word, blob) * idf(word, bloblist)
In order to better understand the process, I'd like to turn the shorthand loop into a regular-looking "for" loop.
Would this be correct?
scores = []
for word in blob.words:
scores.append(tfidf(word, blob, bloblist))
Also, what's the advantage or writing short-form for loops?