所用代码(在网上搜的 做了修改)
-*- coding: cp936 -*-
textlist=open('out.txt','rb')
points=[]
for line in textlist:
a=line.split()#通过指定分隔符对字符串进行切片,默认为空格
points.append(a)#把a添加到points[]列表的最后
textlist.close()
#调用调试文本并储存到points这个list里面,list的每个元素是一个子list,即一个地点,每个子list里的元素是一个地点分出来的词
wordlist=open('自己的词库.txt','rb')
words=[]
for ele in wordlist:
b=ele.split()#通过指定分隔符对字符串进行切片,默认为空格
words.append(b[0])
wordlist.close()
#调用调试词库并储存到wordlist这个list里面,list的每个元素是一个整体文本分词降噪统计词频后的词条
import numpy as np
matrix=np.zeros((len(points),len(words)))
#创建n行(n个)m列(m个词条)的初始矩阵matrix0
for n in range(len(points)):#len()返回字符串的长度
for m in range(len(words)):
for l in range(len(points[n])):
if points[n][l] == words[m]:
matrix[n,m]+=1
#通过循环生成matrix
print (matrix)