0

The code mentioned below was working fine without the a class called "BigramsClassifier()" but since a class was needed i added it included the reference "self" as required. Then i got this error as seen below. However, this solution posted here worked for a similar issue of mine but not this. enter image description here

This is the code with the class included,

class BigramsClassifier(object): #line 40
def __init__(self):
    pass

def remove_duplicates(self,numbers):
    numbers.sort()
    i = len(numbers) - 1
    while i > 0:
        if numbers[i] == numbers[i - 1]:
            numbers.pop(i)
        i -= 1
    return numbers


def balance_pos_phrases(self,pos_list,neg_list):
    new_mix = list(set(pos_list).intersection(set(neg_list)))  # gets only the common words between two classes

    for item in new_mix:
        for pos_i in pos_list:
            if item in pos_list:
                pos_list.remove(item)
                pos_list = self.remove_duplicates(pos_list)
    return pos_list


def balance_neg_phrases(self,pos_list, neg_list):
    new_mix = list(set(neg_list).intersection(set(pos_list)))  # gets only the common words between two classes

    for item in new_mix:
        for neg_i in neg_list:
            if item in neg_list:
                neg_list.remove(item)
                neg_list = self.remove_duplicates(neg_list)
    return neg_list



short_pos = open("\positive4.txt", "r").read()
short_neg = open("\\negative4.txt", "r").read()

pos_documents = []
neg_documents = []
all_words = []
allowed_word_types = ["J", "N", "V"]  # J-Adjectives, N-nouns, V-Verb, R-Adverb

for p in short_pos.split('\n'):
    pos_documents.append((p, "pos"))
    words = word_tokenize(p)
    pos = nltk.pos_tag(words)
    for w in pos:
        if w[1][0] in allowed_word_types:
            all_words.append(w[0].lower())

for p in short_neg.split('\n'):
    neg_documents.append((p, "neg"))
    words = word_tokenize(p)
    pos = nltk.pos_tag(words)
    for w in pos:
        if w[1][0] in allowed_word_types:
            all_words.append(w[0].lower())

short_pos_words = word_tokenize(short_pos)
short_neg_words = word_tokenize(short_neg)

balanced_pos_words = balance_pos_phrases(short_pos_words, short_neg_words) ####line 104####
balanced_neg_words = balance_neg_phrases(short_pos_words, short_neg_words)
rando
  • 67
  • 6
  • `balance_pos_phrases` is method of a class, you need to call it like that `bigr.balance_pos_phrases(short_pos_words, short_neg_words)` where `bigr` is a object of a class `BigramsClassifier` – Alex Pshenko Oct 11 '17 at 13:51
  • @AlexPshenko I am new to python should the object creation be like bigr=BigramsClassifier() ? – rando Oct 11 '17 at 13:56
  • Yes, you also need to include those methods in the body of a `BigramsClassifier` class for that to work – Alex Pshenko Oct 11 '17 at 13:58
  • can you show me an example how to do it in the body of the BigramsClassifier class ? – rando Oct 11 '17 at 14:03

0 Answers0