Incomprehensible Name Error using Class Variables within a Class in Python

78 Views Asked by At

I am new to object-oriented programming and I have the following problem concerning class variables: I want to use a profanity filter for which I have the class Profanity, and I want to pass a transcript file and a wordlist file as arguments in the __init__ method. (Please see below for the whole code).

The resulting object should be defined like this: p = Profanity("sample_transcript.txt", eng), where eng stands for English, and this language argument should be replaceable with other language arguments like deu for German, etc.). To achieve this, I have defined the class variables eng = "/home/user/sw_filter/wordlists/google_profanity_wordlist_eng.txt" and deu = "/home/user/sw_filter/wordlists/profanity_wordlist_deu.txt" directly below the class command.

I thought that these class variables are shared across all objects of the class, but when I run the code I get the error NameError: name 'eng' is not defined. When I set the language file paths as global variables though, it works, but I want them to be stored inside the class. Is there any way to do this? What am I doing wrong? I really appreciate any help I can get.

Thank you very much!

import re
from better_profanity import profanity 


class Profanity(object):

    # Wordlist file paths for languages
    eng = "/home/mareike/sw_filter/wordlists/google_profanity_wordlist_eng.txt"
    deu = "/home/mareike/sw_filter/wordlists/profanity_wordlist_deu.txt"
    # Further language variables...

    

    def __init__(self, transcript_filename, wordlist_language):
        self.transcript_filename = transcript_filename
        self.wordlist_language = wordlist_language
        
        
    def load_transcript(self):
        f = open(self.transcript_filename, "r")
        file = f.read()
        f.close()

        return file
    

    def load_wordlist(self):
        f = open(self.wordlist_language, "r")
        file = f.read()
        f.close()

        return file


    def preprocess(self):
        # Remove noisy punctuation
        prep_transcript = transcript.replace('--', '').replace('>', '')
        prep_transcript = prep_transcript.replace('[', '').replace(']', '')
        prep_transcript = prep_transcript.replace('(', '').replace(')', '')
        prep_transcript = prep_transcript.replace("'", '')
        prep_transcript = prep_transcript.replace('.', '')
        prep_transcript = prep_transcript.replace(';', '')
        prep_transcript = prep_transcript.replace('!', '')
        prep_transcript = prep_transcript.replace('?', '')
        prep_transcript = prep_transcript.replace('-', ' ')
        prep_transcript = re.sub(r":\B", "", prep_transcript, flags = re.MULTILINE)
        prep_transcript = re.sub(r",\D\b", " ", prep_transcript, flags = re.MULTILINE)
        prep_transcript = re.sub(r",\n", "\n", prep_transcript, flags = re.MULTILINE)

        # Lowercase text
        prep_transcript = prep_transcript.lower()

        return prep_transcript


    def apply_filter(self, prep_transcript):
        if __name__ == "__main__":
            profanity.load_censor_words_from_file(self.wordlist_language)                                                                                                             
            censored_transcript = profanity.censor(prep_transcript)

        return censored_transcript


p = Profanity("sample_transcript.txt", eng)

transcript = p.load_transcript()
wordlist = p.load_wordlist()
prep_transcript = p.preprocess()

censored_transcript = p.apply_filter(prep_transcript)
print(censored_transcript)
1

There are 1 best solutions below

0
On

In Your code, the problem is with this line:

p = Profanity("sample_transcript.txt", eng)

In creating object, the second arguments you have passed is not defined in that domain but it is defined in class as a class attributes. And as we know, class attributes is usually common for all instances of class and it can be accessed either from instance or class name.

So what you are supposed to do is to just change the above mentioned line like this:

p = Profanity("sample_transcript.txt", Profanity.eng)