python - 我在 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()
我的频率未定义我正在得到解决方案请帮助找到解决方案???无法找到错误。我尝试多次但无法解决
解决方案
像这样在for循环之前添加频率字典
freq = {}
for i in x:
do smthg
cloud.generate_from_frequencies
添加此行后
cloud.to_file("wordCloud.png")
希望它有所帮助
推荐阅读
- c++ - 在 C++ 中,为什么为函数定义和声明声明不同的返回类型会改变函数的输出?
- postgresql - 退出在 PostgreSQL 中返回 JSON 的函数时删除临时表
- ruby-on-rails - 全新的 Rails 6.1.4.1 因 webpack 错误而失败 - TypeError: Class constructor ServeCommand cannot be invoked without 'new'
- java - TOMEEE - 无法查找 ejb
- python - 请帮助解释在范围函数中使用 len 函数的循环
- cypress - 赛普拉斯拦截 - 从未发生过请求
- azure - 使用 Java 从 Azure Function 访问 Key Vault 证书
- typescript - 即使使用默认参数值,对象也可能为 null
- .net - .Net 5 Azure 功能未显示在门户中
- jpa - JSF 和来自 EntityManager 的同步延迟加载