当前位置: 代码迷 >> python >> TypeError:参数“ dictionary”有多个值
  详细解决方案

TypeError:参数“ dictionary”有多个值

热度:90   发布时间:2023-06-19 09:25:12.0

我更早地阅读了其他询问有关此错误的问题。但仍然我没有弄错我在哪里。当我调用该函数时,我遇到了这个错误。 我在这个论坛上是新手,任何解决我的问题的帮助将不胜感激。这是我的代码

def lda_train(self, documents):
        # create dictionary
        dictionary= corpora.Dictionary(documents)
        dictionary.compactify()
        dictionary.save(self.DICTIONARY_FILE)  # store the dictionary, for future reference
        print ("============ Dictionary Generated and Saved ============")

        ############# Create Corpus##########################

        corpus = [dictionary.doc2bow(text) for text in documents]
        print('Number of unique tokens: %d' % len(dictionary))
        print('Number of documents: %d' % len(corpus))
        return dictionary,corpus

def compute_coherence_values(dictionary,corpus,documents,  limit, start=2, step=3):
        num_topics = 10
        coherence_values = []
        model_list = []
        for num_topics in range(start, limit, step):
            lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,id2word=dictionary, num_topics=num_topics,  random_state=100, alpha='auto')
            model_list.append(model)
            coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v')
            coherence_values.append(coherencemodel.get_coherence())
        return model_list, coherence_values

当我通过使用以下代码在主要调用此函数时:

if __name__ == '__main__':
    limit=40
    start=2
    step=6
    obj = LDAModel()
    lda_input = get_lda_input_from_corpus_folder('./dataset/TRAIN')
    dictionary,corpus =obj.lda_train(lda_input)
    model_list, coherence_values = obj.compute_coherence_values(dictionary=dictionary,corpus=corpus, texts=lda_input,  start=2, limit=40, step=6)

我收到错误消息:

 model_list, coherence_values=obj.compute_coherence_values(dictionary=dictionary,corpus=corpus, texts=lda_input,  start=2, limit=40, step=6) 
TypeError: compute_coherence_values() got multiple values for argument 'dictionary'

TL; DR

更改

def compute_coherence_values(dictionary, corpus, documents, limit, start=2, step=3)

def compute_coherence_values(self, dictionary, corpus, documents, limit, start=2, step=3)


您忘记将self作为第一个参数传递,因此将实例作为dictionary参数传递,但是您也将dictionary作为显式关键字参数传递。

此行为可以很容易地重现:

class Foo:
   def bar(a):
       pass

Foo().bar(a='a')
TypeError: bar() got multiple values for argument 'a'
  相关解决方案