csr_matrix计算tf

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)