为什么我遇到 AttributeError:

下面是我尝试编码的一种方法。但是,在代码的第 3 行中,它表示存在属性错误,并且“WordListCorpusReader”对象在 python 中没有属性“word”。请帮我看看下面的代码:((


'''step 3. conduct preprocessing steps'''


# setting up the resources for the preprocessing steps

stop = set(stopwords.word('english'))

exclude = set(string.punctuation)

lemma = WordNetLemmatizer()


def clean(doc):

    stop_free = ''.join([i for i in doc.lower().split() if i not in stop])

    punc_free = ''.join([ch for ch in stop_free if ch not in exclude])

    normalized = ''.join(wn.lemma.lemmatize(word) for word in punc_free.split())

    return normalized

    doc_clean = [clean(doc).split() for doc in corpus]

    '''step 4. prepare word representation'''

    dictionary = corpora.Dictionary(doc_clean)

    doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean]

    '''step 5. create lda model'''

    topic_num = 5

    word_num = 5

    Lda = gensim.models.ldamodel.LdaModel

    ldamodel = Lda(doc_term_matrix, num_topics=topic_num, id2word=dictionary, passes=20)

    pprint(ldamodel.print_topics(num_topics=topic_num, num_words=word_num))

这是运行代码后的回溯:


Traceback (most recent call last):

  File "C:/Users/user/PycharmProjects/topicmodel/topicmodel.py", line 41, in <module>

    stop = set(stopwords.word('english'))

  File "C:\Users\user\AppData\Roaming\Python\Python37\site-packages\nltk\corpus\util.py", line 119, in __getattr__

    return getattr(self, attr)

AttributeError: 'WordListCorpusReader' object has no attribute 'word'


开满天机
浏览 450回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python