MCPcopy Create free account
hub / github.com/Dod-o/Statistical-Learning-Method_Code / frequency_counter

Function frequency_counter

PLSA/PLSA.py:58–86  ·  view source on GitHub ↗

INPUT: text - (list) 文本列表 words - (list) 单词列表 OUTPUT: words - (list) 出现频次为前1000的单词列表 X - (array) 单词-文本矩阵

(text, words)

Source from the content-addressed store, hash-verified

56
57#定义构建单词-文本矩阵的函数,这里矩阵的每一项表示单词在文本中的出现频次,也可以用TF-IDF来表示
58def frequency_counter(text, words):
59 '''
60 INPUT:
61 text - (list) 文本列表
62 words - (list) 单词列表
63
64 OUTPUT:
65 words - (list) 出现频次为前1000的单词列表
66 X - (array) 单词-文本矩阵
67
68 '''
69 words_cnt = np.zeros(len(words)) #用来保存单词的出现频次
70 X = np.zeros((1000, len(text))) #定义m*n的矩阵,其中m为单词列表中的单词个数,为避免运行时间过长,这里只取了出现频次为前1000的单词,因此m为1000,n为文本个数
71 #循环计算words列表中各单词出现的词频
72 for i in range(len(text)):
73 t = text[i] #取出第i条文本
74 for w in t:
75 ind = words.index(w) #取出第i条文本中的第t个单词在单词列表中的索引
76 words_cnt[ind] += 1 #对应位置的单词出现频次加一
77 sort_inds = np.argsort(words_cnt)[::-1] #对单词出现频次降序排列后取出其索引值
78 words = [words[ind] for ind in sort_inds[:1000]] #将出现频次前1000的单词保存到words列表
79 #构建单词-文本矩阵
80 for i in range(len(text)):
81 t = text[i] #取出第i条文本
82 for w in t:
83 if w in words: #如果文本t中的单词w在单词列表中,则将X矩阵中对应位置加一
84 ind = words.index(w)
85 X[ind, i] += 1
86 return words, X
87
88
89#定义概率潜在语义分析函数,采用EM算法进行PLSA模型的参数估计

Callers 1

PLSA.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected