NameError: name 'file_content' is not defined

Viewed 6446

I did an assignment on Coursera and successfully passed that.I compiled and got accurate result on Jupyter notebook. https://poskzjbvcahynihgdhljpw.coursera-apps.org/notebooks/C1M6L2_Final_Project_V3.ipynb
I typed on my pycharm the similar code:

But I got an error:

import wordcloud
import numpy as np
from matplotlib import pyplot as plt
from IPython.display import display
import fileupload
import io
import sys

# 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,use to process the 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"]
    count_frequency = {}

    for contents in file_contents:
        if (contents not in punctuations) and (contents not in uninteresting_words):
            if contents not in count_frequency:
                count_frequency[contents] = 1
            else:
                count_frequency[contents] += 1

    # wordcloud
    cloud = wordcloud.WordCloud()
    cloud.generate_from_frequencies(count_frequency)
    return cloud.to_array()


# Displaying my wordcloud image

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

Traceback (most recent call last):
      File "/home/ashik/PycharmProjects/RawCodes/final_project_google_crash_course_with_python.py", line 54, in <module>
        myimage = calculate_frequencies(file_content)
    NameError: name 'file_content' is not defined
    FileUploadWidget(label='Browse', _dom_classes=('widget_item', 'btn-group'))
    
    Process finished with exit code 1
2 Answers

Do the following changes :

1.

def _cb(change):
   global file_contents
myimage = calculate_frequencies(file_contents)

Should fix the issue

You need to do the following steps:

IMPORTANT! If this was your first time running the above cell containing the installs and imports, you will need save this notebook now. Then under the File menu above, select Close and Halt. When the notebook has completely shut down, reopen it. This is the only way the necessary changes will take affect.

To upload your text file, run the following cell that contains all the code for a custom uploader widget. Once you run this cell, a "Browse" button should appear below it. Click this button and navigate the window to locate your saved text file.

Related