Advertisement

手工编写TF-IDF Python版本

阅读量:
复制代码
    import math
    class Solution():
    def word_count(self, word_list):
        countlist = []
        for list1 in word_list:
            dict1 = {}
            for j in list1:
                if j not in dict1:
                    dict1[j] = 0
                dict1[j] += 1
            countlist.append(dict1)
        return countlist
    
    
    def tf(self, word, count):
        """
        :param word: word可以通过count得到
        :param count: count可以通过countlist得到
        :return: count[word]可以得到每个单词的词频, sum(count.values())得到整个句子的单词总数
        """
        print(word)
        print(count

全部评论 (0)

还没有任何评论哟~