我有一个包含n-gram的词汇表,如下所示.
myvocabulary = ['tim tam', 'jam', 'fresh milk', 'chocolates', 'biscuit pudding']
Run Code Online (Sandbox Code Playgroud)
我想用这些词来计算TF-IDF值.
我还有一个语料库字典如下(键=食谱号,值=食谱).
corpus = {1: "making chocolates biscuit pudding easy first get your favourite biscuit chocolates", 2: "tim tam drink new recipe that yummy and tasty more thicker than typical milkshake that uses normal chocolates", 3: "making chocolates drink different way using fresh milk egg"}
Run Code Online (Sandbox Code Playgroud)
我目前正在使用以下代码.
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(vocabulary = myvocabulary, stop_words = 'english')
tfs = tfidf.fit_transform(corpus.values())
Run Code Online (Sandbox Code Playgroud)
现在我正在打印令牌或n-gram的配方1 corpus以及tF-IDF值,如下所示.
feature_names = tfidf.get_feature_names()
doc = …Run Code Online (Sandbox Code Playgroud)