Ошибка типа: получил несколько значений для аргумента 'словарь' - PullRequest
0 голосов
/ 15 сентября 2018

Я читал другие вопросы, которые задавались об этой ошибке ранее. Но все же я не понимаю, где я делаю ошибку. Когда я вызываю функцию, я получил эту ошибку. Я очень новичок в этом форуме, любая помощь в решении моей проблемы будет оценена. Вот мой код

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'

1 Ответ

0 голосов
/ 15 сентября 2018

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'
...