簡體   English   中英

帶有 generate_from_frequencies 的 Wordcloud python 中的錯誤

[英]Error in Wordcloud python with generate_from_frequencies

我正在嘗試使用 python 創建一個 wordcloud,但遇到了一個錯誤。

# This is the uploader widget

def _upload():

    _upload_widget = fileupload.FileUploadWidget()

    def _cb(change):
        global file_contents
        decoded = io.StringIO(change['owner'].data.decode('utf-8'))
        filename = change['owner'].filename
        print('Uploaded `{}` ({:.2f} kB)'.format(
            filename, len(decoded.read()) / 2 **10))
        file_contents = decoded.getvalue()

    _upload_widget.observe(_cb, names='data')
    display(_upload_widget)

_upload()

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
    import csv
    from wordcloud import WordCloud
    import matplotlib.pyplot as plt

    reader = csv.reader(open('namesDFtoCSV', 'r',newline='\n'))
    d = {}
    for k,v in reader:
        d[k] = int(v)

    #Generating wordcloud. Relative scaling value is to adjust the importance of a frequency word.
    #See documentation: https://github.com/amueller/word_cloud/blob/master/wordcloud/wordcloud.py
    wordcloud = WordCloud(width=900,height=500, max_words=1628,relative_scaling=1,normalize_plurals=False).generate_from_frequencies(d)

    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.show()
    #wordcloud
    cloud = wordcloud.WordCloud()
    cloud.generate_from_frequencies()
    return cloud.to_array()

# Display your wordcloud image

myimage = calculate_frequencies(file_contents)
plt.imshow(myimage, interpolation = 'nearest')
plt.axis('off')
plt.show()

你好,我研究了這么多后遇到錯誤,我無法找到解決方案,所以我決定在這里發布。 這就像我為編寫代碼做了很多努力,但你知道一個錯誤會保留一切。

NameError                                 Traceback (most recent call last)
<ipython-input-8-fd0f708f372c> in <module>
      1 # Display your wordcloud image
      2 
----> 3 myimage = calculate_frequencies(file_contents)
      4 plt.imshow(myimage, interpolation = 'nearest')
      5 plt.axis('off')

NameError: name 'file_contents' is not defined

請幫我解決這個問題。 我真的很需要!

該錯誤表明 python 運行時不知道 function。

您是否嘗試將整個源代碼保存為文件prog.py並使用python prog.py從命令行執行它?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM