下面提到的代码工作正常,没有名为" BigramsClassifier()"但是因为需要上课,所以我添加了它包括参考" self"按要求。然后我得到了这个错误,如下所示。但是,这个解决方案here已经解决了我的类似问题但不是这个问题。
这是包含类的代码,
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)