python - AttributeError:“dict”对象没有属性“translate”
问题描述
我对这段代码有疑问,这是一个练习,我需要通过捕捉文本中单词的频率来设置词云。
我能够使用 map 和 translate 去除标点符号,然后将它们全部转换为较低的字符串,并使用 for 循环检查它们的频率。
当我尝试将它们传递给 wordcloud 生成器时,出现错误:AttributeError: 'dict' object has no attribute 'translate'
import string
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
word_listing=[]
container = {}
table = str.maketrans(dict.fromkeys(string.punctuation)) #Removing punctuation first
file_contents = file_contents.translate(table)
#-------------------------------------------------------------------------------
file_contents=file_contents.split()
file_contents=map(str.lower,file_contents) #making lower string
file_contents=list(file_contents)
#-------------------------------------------------------------------------------
for item in file_contents:
if item not in (uninteresting_words): #removing uninteresting words
container[item]=item
word_listing.append(item)
#setting up frequency count
for i in word_listing:
container[i] = 0
for i in word_listing:
container[i] += 1
#wordcloud
cloud = wordcloud.WordCloud()
cloud.generate_from_frequencies(container)
return cloud.to_array()
没问题,当我设置这个问题时出现了:
myimage = calculate_frequencies(container)
plt.imshow(myimage, interpolation = 'nearest')
plt.axis('off')
plt.show()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-cacedc8fdb8f> in <module>
1 # Display your wordcloud image
2
----> 3 myimage = calculate_frequencies(container)
4 plt.imshow(myimage, interpolation = 'nearest')
5 plt.axis('off')
<ipython-input-13-0620c3cd20f5> in calculate_frequencies(file_contents)
15
16 table = str.maketrans(dict.fromkeys(string.punctuation)) #Removing punctuation first
---> 17 file_contents = file_contents.translate(table)
18 #-------------------------------------------------------------------------------
19 file_contents=file_contents.split()
AttributeError: 'dict' object has no attribute 'translate'
我需要帮助来理解为什么会发生这种情况以及如何解决它。谢谢你们!
解决方案
推荐阅读
- javascript - 我可以使用 javascript 在跨度文本上附加锚标记吗
- javascript - 如何使用事件 DRY 创建函数?
- ios - 使用自动填充支持更改密码
- python - 当页面偏移值永远不会结束时如何进行网页抓取
- flutter - Flutter 代码是否可以在不混淆的情况下轻松进行逆向工程?
- javascript - 如何将集合复制到 mongoDB 中的另一个新集合
- r - 如何将公式传递给具有权重的 lm 对象?
- java - How to inject beans without interface in Mongock changelogs
- java - 无法通过 Gradle 使用 log4j 将日志写入日志文件
- javascript - TypeError [INVALID_TYPE]:提供的覆盖不是权限覆盖的数组或集合。不和谐 v12