简体   繁体   English

Gensim:类型错误:doc2bow 需要输入的 unicode 标记数组,而不是单个字符串

[英]Gensim: TypeError: doc2bow expects an array of unicode tokens on input, not a single string

I am starting with some python task, I am facing a problem while using gensim.我从一些 python 任务开始,我在使用 gensim 时遇到了一个问题。 I am trying to load files from my disk and process them (split them and lowercase() them)我正在尝试从我的磁盘加载文件并处理它们(将它们拆分并小写()它们)

The code I have is below:我的代码如下:

dictionary_arr=[]
for file_path in glob.glob(os.path.join(path, '*.txt')):
    with open (file_path, "r") as myfile:
        text=myfile.read()
        for words in text.lower().split():
            dictionary_arr.append(words)
dictionary = corpora.Dictionary(dictionary_arr)

The list (dictionary_arr) contains the list of all words across all the file, I then use gensim corpora.Dictionary to process the list.列表 (dictionary_arr) 包含所有文件中所有单词的列表,然后我使用 gensim corpora.Dictionary 来处理列表。 However I face a error.但是我面临一个错误。

TypeError: doc2bow expects an array of unicode tokens on input, not a single string

I cant understand whats a problem, A little guidance would be appreciated.我不明白有什么问题,请提供一点指导,我们将不胜感激。

In dictionary.py, the initialize function is:在dictionary.py中,初始化函数是:

def __init__(self, documents=None):
    self.token2id = {} # token -> tokenId
    self.id2token = {} # reverse mapping for token2id; only formed on request, to save memory
    self.dfs = {} # document frequencies: tokenId -> in how many documents this token appeared

    self.num_docs = 0 # number of documents processed
    self.num_pos = 0 # total number of corpus positions
    self.num_nnz = 0 # total number of non-zeroes in the BOW matrix

    if documents is not None:
        self.add_documents(documents)

Function add_documents Build dictionary from a collection of documents.函数 add_documents 从文档集合构建字典。 Each document is a list of tokens:每个文档都是一个令牌列表:

def add_documents(self, documents):

    for docno, document in enumerate(documents):
        if docno % 10000 == 0:
            logger.info("adding document #%i to %s" % (docno, self))
        _ = self.doc2bow(document, allow_update=True) # ignore the result, here we only care about updating token ids
    logger.info("built %s from %i documents (total %i corpus positions)" %
                 (self, self.num_docs, self.num_pos))

So ,if you initialize Dictionary in this way, you must pass documents but not a single document.所以,如果你以这种方式初始化 Dictionary ,你必须传递文档而不是单个文档。 For example,例如,

dic = corpora.Dictionary([a.split()])

is OK.没问题。

Dictionary needs a tokenized strings for its input:字典的输入需要一个标记化的字符串:

dataset = ['driving car ',
           'drive car carefully',
           'student and university']

# be sure to split sentence before feed into Dictionary
dataset = [d.split() for d in dataset]

vocab = Dictionary(dataset)

Hello everyone i ran into the same problem.大家好,我遇到了同样的问题。 This is what worked for me这对我有用

    #Tokenize the sentence into words
    tokens = [word for word in sentence.split()]

    #Create dictionary
    dictionary = corpora.Dictionary([tokens])
    print(dictionary)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:doc2bow 需要输入的 unicode 标记数组,而不是使用 gensim.corpora.Dictionary() 时的单个字符串 - TypeError: doc2bow expects an array of unicode tokens on input, not a single string when using gensim.corpora.Dictionary() 获取“doc2bow 需要输入 unicode 令牌数组,而不是单个字符串”作为尝试使用 gensim 执行 nlp 有解决方案吗? - Getting “doc2bow expects an array of unicode tokens on input, not a single string” as a try to do nlp using gensim" Is there a solution? 主题建模错误(doc2bow 需要输入 unicode 令牌数组,而不是单个字符串) - topic modeling error (doc2bow expects an array of unicode tokens on input, not a single string) python gensim TypeError:强制转换为Unicode:需要字符串或缓冲区,找到列表 - python gensim TypeError: coercing to Unicode: need string or buffer, list found 如何在 Gensim 词典中输入由不同标记组成的系列/列表? - How to input a series/list consisting of different tokens in a Gensim Dictionary? TypeError:必须是字符串,而不是unicode - TypeError: must be string, not unicode 令牌化-字符串到令牌数组 - Tokenize - String to Array of Tokens Python中的Gensim和unicode - Gensim and unicode in Python TypeError:强制转换为Unicode:需要字符串或缓冲区,在python中找到文件(将现有文件中的数据写入单个文件) - TypeError: coercing to Unicode: need string or buffer, file found in python(writing data from existing files into single file) 如何将标记添加到 gensim 字典 - how to add tokens to gensim dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM