Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有lambda的Python 3语法错误_Python_Python 3.x - Fatal编程技术网

带有lambda的Python 3语法错误

带有lambda的Python 3语法错误,python,python-3.x,Python,Python 3.x,我在教程后面遇到了一个语法错误。它感觉像一个Python 3 getcha。提前谢谢你 def lda_description(review_text, min_topic_freq=0.05): """ accept the original text of a review and (1) parse it with spaCy, (2) apply text pre-processing steps, (3) create a bag-of-words representation, (4

我在教程后面遇到了一个语法错误。它感觉像一个Python 3 getcha。提前谢谢你

def lda_description(review_text, min_topic_freq=0.05):
"""
accept the original text of a review and (1) parse it with spaCy,
(2) apply text pre-processing steps, (3) create a bag-of-words
representation, (4) create an LDA representation, and
(5) print a sorted list of the top topics in the LDA representation
"""

# parse the review text with spaCy
parsed_review = nlp(review_text)

# lemmatize the text and remove punctuation and whitespace
unigram_review = [token.lemma_ for token in parsed_review
                  if not punct_space(token)]

# apply the first-order and secord-order phrase models
bigram_review = bigram_model[unigram_review]
trigram_review = trigram_model[bigram_review]

# remove any remaining stopwords
trigram_review = [term for term in trigram_review
                  if not term in spacy.en.STOPWORDS]

# create a bag-of-words representation
review_bow = trigram_dictionary.doc2bow(trigram_review)

# create an LDA representation
review_lda = lda[review_bow]

# sort with the most highly related topics first
review_lda = sorted(review_lda, key=lambda (topic_number, freq): -freq)

for topic_number, freq in review_lda:
    if freq < min_topic_freq:
        break

    # print the most highly related topic names and frequencies
    print ('{:25} {}'.format(topic_names[topic_number],)
                            round(freq, 3))
def lda\u说明(查看文本,最小主题频率=0.05):
"""
接受评论的原始文本并(1)使用spaCy解析它,
(2) 应用文本预处理步骤,(3)创建一个单词包
表示,(4)创建LDA表示,以及
(5) 打印LDA表示中最热门主题的排序列表
"""
#使用spaCy解析审阅文本
已解析的检查=nlp(检查检查文本)
#对文本进行逻辑化,删除标点和空格
unigram_review=[token.lemma_uu]表示已解析_review中的令牌
如果不是点_空间(令牌)]
#应用一阶和二阶短语模型
bigram_review=bigram_模型[unigram_review]
trigram_review=trigram_模型[bigram_review]
#删除所有剩余的停止字
trigram_review=[trigram_review中的术语对术语
如果不是空间中的术语,请参见[单词]
#创建一个单词包
复习bow=trigram\u字典.doc2bow(trigram\u复习)
#创建LDA表示
回顾=回顾[回顾]
#首先对最相关的主题进行排序
查看\u lda=排序(查看\u lda,键=lambda(主题编号,频率):-freq)
对于主题编号,查看中的频率\u lda:
如果频率<最小主题频率:
打破
#打印最相关的主题名称和频率
打印({:25}{})。格式(主题名称[主题编号],)
圆形(频率,3))
弹出的错误是:

  File "<ipython-input-62-745b97e51bcb>", line 31
review_lda = sorted(review_lda, key=lambda (topic_number, freq): -freq)
                                           ^
文件“”,第31行
查看\u lda=排序(查看\u lda,键=lambda(主题编号,频率):-freq)
^
SyntaxError:Python3中的无效语法不能再使用了。您可以通过以下方式重写lambda:

review_lda = sorted(review_lda, key=lambda topic_and_freq: -topic_and_freq[1])
在Python3中,您不能再使用了。您可以通过以下方式重写lambda:

review_lda = sorted(review_lda, key=lambda topic_and_freq: -topic_and_freq[1])

可能是重复的不是重复的链接问题。这一个与删除有关。可能重复的,可能重复的,可能重复的,可能重复的,可能重复的,可能重复的,可能重复的。这一个与删除.BTW的可能重复有关,最好使用
sorted
reverse=True
参数,而不是负号。我不知道Python 3不支持元组解包。这解决了我的问题。非常感谢你!顺便说一句,最好使用排序的
sorted
reverse=True
参数,而不是负号。我不知道Python 3不支持元组解包。这解决了我的问题。非常感谢你!