Python 如何在主题建模的引导式LDA中生成术语矩阵?

Python 如何在主题建模的引导式LDA中生成术语矩阵?,python,lda,topic-modeling,text-analysis,Python,Lda,Topic Modeling,Text Analysis,我目前正在分析在线评论。我想尝试一下guidelda(),因为有些主题是重叠的。我已成功安装该软件包。 但是,我不确定如何使用excel文档作为输入来生成文档术语矩阵(在网站的代码中称为X)和vocab。有人能帮忙吗?我试图在各种论坛上进行在线搜索,但没有找到任何有用的东西。摘自TDM类的textmining软件包 进口稀土 导入csv 导入操作系统 ''' 导入词干分析器 ''' 您可以将下面的代码另存为单独的python文件,并将其作为常规模块导入代码中,例如create_tdm.py 导入

我目前正在分析在线评论。我想尝试一下guidelda(),因为有些主题是重叠的。我已成功安装该软件包。
但是,我不确定如何使用excel文档作为输入来生成文档术语矩阵(在网站的代码中称为X)和vocab。有人能帮忙吗?我试图在各种论坛上进行在线搜索,但没有找到任何有用的东西。

摘自TDM类的textmining软件包

进口稀土

导入csv

导入操作系统

'''

导入词干分析器

'''

您可以将下面的代码另存为单独的python文件,并将其作为常规模块导入代码中,例如create_tdm.py

导入创建tdm

X=创建\u tdm.TermDocumentMatrix(“您的文本”)

''' 对于Vocab '''

word2id=dict((v,idx)表示枚举中的idx,v(“您的文本”))

'''

确保你的文本中应该有一个引导词列表,否则你会得到关键错误,只是为了检查一下 作为pd进口熊猫

c=pd.DataFrame(列表(word2id))

'''

类别TermDocumentMatrix(对象):


对于一个包含文本的熊猫专栏,我们如何做到这一点?您找到其他解决方案了吗?我也在找同样的
"""
Class to efficiently create a term-document matrix.

The only initialization parameter is a tokenizer function, which should
take in a single string representing a document and return a list of
strings representing the tokens in the document. If the tokenizer
parameter is omitted it defaults to using textmining.simple_tokenize

Use the add_doc method to add a document (document is a string). Use the
write_csv method to output the current term-document matrix to a csv
file. You can use the rows method to return the rows of the matrix if
you wish to access the individual elements without writing directly to a
file.

"""

def __init__(self, tokenizer=simple_tokenize):
    """Initialize with tokenizer to split documents into words."""
    # Set tokenizer to use for tokenizing new documents
    self.tokenize = tokenizer
    # The term document matrix is a sparse matrix represented as a
    # list of dictionaries. Each dictionary contains the word
    # counts for a document.
    self.sparse = []
    # Keep track of the number of documents containing the word.
    self.doc_count = {}

def add_doc(self, document):
    """Add document to the term-document matrix."""
    # Split document up into list of strings
    words = self.tokenize(document)
    # Count word frequencies in this document
    word_counts = {}
    for word in words:
        word_counts[word] = word_counts.get(word, 0) + 1
    # Add word counts as new row to sparse matrix
    self.sparse.append(word_counts)
    # Add to total document count for each word
    for word in word_counts:
        self.doc_count[word] = self.doc_count.get(word, 0) + 1

def rows(self, cutoff=2):
    """Helper function that returns rows of term-document matrix."""
    # Get master list of words that meet or exceed the cutoff frequency
    words = [word for word in self.doc_count \
      if self.doc_count[word] >= cutoff]
    # Return header
    yield words
    # Loop over rows
    for row in self.sparse:
        # Get word counts for all words in master list. If a word does
        # not appear in this document it gets a count of 0.
        data = [row.get(word, 0) for word in words]
        yield data

def write_csv(self, filename, cutoff=2):
    """
    Write term-document matrix to a CSV file.

    filename is the name of the output file (e.g. 'mymatrix.csv').
    cutoff is an integer that specifies only words which appear in
    'cutoff' or more documents should be written out as columns in
    the matrix.

    """
    f = csv.writer(open(filename, 'wb'))
    for row in self.rows(cutoff=cutoff):
        f.writerow(row)