from scipy.sparse import csr_matrix
def tf(docs):
"""
As an example of how to construct a CSR matrix incrementally, the
following snippet builds a term-document matrix from texts:
:type docs:List[List[str]]
:param docs:
:return:
"""
data = []
indices = []
indptr = [0]
vocabulary = {}
for doc in docs:
for term in doc:
data.append(1)
indices.append(vocabulary.setdefault(term, len(vocabulary)))
indptr.append(len(indices))
return csr_matrix((data, indices, indptr)).toarray()
corpus = open('/home/fhqplzj/IdeaProjects/DocumentClustering/target/data/ap').readlines()
corpus = map(lambda line: line.strip().split(), corpus)
print tf(corpus)
csr_matrix计算tf
最新推荐文章于 2022-05-25 17:08:12 发布