TypeError:/不支持的操作数类型:“函数”和“整数”

时间:2019-05-05 13:46:17

标签: python typeerror

我正在创建垃圾邮件分类器并获取此错误: TypeError: unsupported operand type(s) for /: 'function' and 'int'。 我想从getter方法get_spam_counterget_not_spam_counterToken类)获取值,但是我正在获取一个函数...

import glob, os, re
# from token_class import Token

training_not_spam_dir = r'C:\Users\GYTIS\PycharmProjects\spam_classifier\all_data\training\not_spam'
training_spam_dir = r'C:\Users\GYTIS\PycharmProjects\spam_classifier\all_data\training\spam'
testing_not_spam_dir = r'C:\Users\GYTIS\PycharmProjects\spam_classifier\all_data\testing\not_spam'
testing_spam_dir = r'C:\Users\GYTIS\PycharmProjects\spam_classifier\all_data\testing\spam'
all_tokens_counter_not_spam = 0
all_tokens_counter_spam = 0
tokens_dictionary = dict()


class Token:
    def _init_(self):
        self.__spam_counter = 0
        self.__not_spam_counter = 0
        self.__spam_probability = 0.0
        self.__not_spam_probability = 0.0
        self.__probability = 0.0

    def increment_spam_counter(self):
        self.__spam_counter += 1

    def increment_not_spam_counter(self):
        self.__not_spam_counter += 1

    def set_spam_probability(self, probability):
        self.__spam_probability = probability

    def set_not_spam_probability(self, probability):
        self.__not_spam_probability = probability

    def set_probability(self, probability):
        self.__probability = probability

    def get_spam_counter(self):
        return int(self.__spam_counter)

    def get_not_spam_counter(self):
        return int(self.__not_spam_counter)

    def get_spam_probability(self):
        return self.__spam_probability

    def get_not_spam_probability(self):
        return self.__not_spam_probability

    def get_probability(self):
        return self.__probability


def count_file_words(file):
    count = 0
    with open(file, 'r+', encoding='utf8', errors='ignore') as file_data:
        data = file_data.read()
        words = re.split('\W+', data.lower())
        for word in words:
            if word is not '':
                count += 1
    return count


def count_all_words(directory):
    count = 0
    os.chdir(directory)
    for f in glob.glob('*.txt'):
        count += count_file_words(f)
    return count


all_tokens_counter_not_spam \
    = count_all_words(directory=training_not_spam_dir)
all_tokens_counter_spam \
    = count_all_words(directory=training_spam_dir)


def generate_tokens_dict():
    os.chdir(training_not_spam_dir)
    print('*** generate_tokens_dict() not_spam_dir')
    for f in glob.glob('*.txt'):
        with open(f, 'r+', encoding='utf8', errors='ignore') as file_data:
            data = file_data.read()
            words = re.split('\W+', data.lower())
            for word in words:
                if word is not '':
                    if tokens_dictionary.__contains__(word):
                        t = tokens_dictionary.get(word)
                        t.increment_not_spam_counter
                        tokens_dictionary[word] = t
                    else:
                        t = Token
                        t.increment_not_spam_counter
                        tokens_dictionary[word] = t
    print('*** generate_tokens_dict() spam_dir')
    os.chdir(training_spam_dir)
    for f in glob.glob('*.txt'):
        with open(f, 'r+', encoding='utf8', errors='ignore') as file_data:
            data = file_data.read()
            words = re.split('\W+', data.lower())
            for word in words:
                if word is not '':
                    if tokens_dictionary.__contains__(word):
                        t = tokens_dictionary.get(word)
                        t.increment_spam_counter
                        tokens_dictionary[word] = t
                    else:
                        t = Token()
                        t.increment_spam_counter
                        tokens_dictionary[word] = t


def generate_tokens_probabilities():
    for k, v in tokens_dictionary.items():
        not_spam_counter = v.get_not_spam_counter
        spam_counter = v.get_spam_counter

        not_spam_probability = not_spam_counter / all_tokens_counter_not_spam
        spam_probability = spam_counter / all_tokens_counter_spam
        probability = spam_probability / (spam_probability + not_spam_probability)

        v.set_not_spam_probability(not_spam_probability)
        v.set_spam_probability(spam_probability)
        v.set_probability(probability)
        tokens_dictionary[k] = v


generate_tokens_dict()
"""cnt = 0
for token in tokens_dictionary.items():
    cnt += 1
    print(cnt, token)"""

generate_tokens_probabilities()

print(all_tokens_counter_not_spam)
print(all_tokens_counter_spam)
Traceback (most recent call last):
  File "C:/Users/GYTIS/PycharmProjects/spam_classifier/spam_classifier", line 94, in <module>
    generate_tokens_probabilities()
  File "C:/Users/GYTIS/PycharmProjects/spam_classifier/spam_classifier", line 78, in generate_tokens_probabilities
    not_spam_probability = not_spam_counter / all_tokens_counter_not_spam
TypeError: unsupported operand type(s) for /: 'function' and 'int'

Process finished with exit code 1

0 个答案:

没有答案