首页 > 解决方案 > 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'

我需要帮助来理解为什么会发生这种情况以及如何解决它。谢谢你们!

标签: pythonword-cloud

解决方案


推荐阅读