要创建文档术语矩阵,我将文本文件result.txt
作为输入。我试图以这种方式计算出现的单词:
Counter({'STTP': 6, 'AVENUES': 4, 'ENGINEERING': 4, 'MINING': 4, 'THE': 4, 'SCOE': 4, 'HERE': 4, 'DATA': 4, 'TOOLS': 4, 'PROGRAMMING': 3, 'TEMPERATURE': 3})
但以这种方式得到了结果:
"degree,the,mituski,programming,national,it,high,sakal,engineering,paper,college,signed
1,4,2,3,1,2,1,1,4,1,1,1"
这是我使用的代码:
tdm = textmining.TermDocumentMatrix()
files = glob.glob("result.txt")
for f in files:
content = open(f).read()
content = content.replace('\n', ' \n')
tdm.add_doc(content)
tdm.write_csv('matrix1.csv', cutoff=1)
结果是格式正确的csv
文件。第一行是标题(单词),第二行是单词数。
您正在展示的东西看起来像dict
传递给class
构造函数。
来自Python 文本挖掘包:
您也可以直接访问其行,而不是写出矩阵。
# Let's print them to the screen. for row in tdm.rows(cutoff=1): print row
因此,要获得dict
as 在您的问题中,您可以通过:
result_rows = list(tdm.rows(cutoff=1))
result_dict = {}
for i in range(len(result_rows[0])):
result_dict[result_rows[0][i]] = result_rows[1][i]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句