首页 > 解决方案 > 我在 python 中完成了词云,但无法获得输出

问题描述

def calculate_frequencies(file_contents):
    # Here is a list of punctuations and uninteresting words you can use to process your text
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", 
     "me", "my", \"we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", 
        "hers", "its", "they", "them","their", "what", "which", "who", "whom", "this", "that", "am", 
          "are", "was", "were", "be", "been", "being", \ have", "has", "had", "do", "does", "did", 
       "but", "at", "by", "with", "from", "here", "when", "where", "how", \"all", "any", "both", 
            "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"]

# LEARNER CODE START HERE
frequency={}
file_contents = file_contents.split()
str1 = ""

for word in file_contents:
    str1 = ''.join(ch for ch in word if ch.isalnum())
    if str1.lower() not in uninteresting_words:
       
        if str1.lower() not in frequency:
            frequency[str1.lower()]=1
        else:
            frequency[str1.lower()]+=1

    

cloud = wordcloud.WordCloud()
cloud.generate_from_frequencies(frequency)
return cloud.to_array() 

我的频率未定义我正在得到解决方案请帮助找到解决方案???无法找到错误。我尝试多次但无法解决

标签: python

解决方案


像这样在for循环之前添加频率字典

freq = {} 
for i in x:
   do smthg

cloud.generate_from_frequencies添加此行后

cloud.to_file("wordCloud.png")

否则通过本教程Python WordCloud帮助您自己

希望它有所帮助


推荐阅读