Exemplo n.º 1
0
def best_n_attack(classifier):
    WORD_LIMIT = 200
    goodWords = run_best_n_words(classifier)
    spam = load_spam()

    avg = 0
    flag = False
    import random

    for comment in spam:
        for i in range(WORD_LIMIT):
            comment += " {}".format(random.choice(goodWords))
            if type(classifier) == NaiveClassifier:
                if classifier.predict(comment) == False:
                    avg += i
                    break
                if i == WORD_LIMIT - 1:
                    avg += WORD_LIMIT
            else:
                if classifier.classify(list_to_dict(
                        comment.split(" "))) == str(0):
                    avg += i
                    break
                if i == WORD_LIMIT - 1:
                    avg += WORD_LIMIT
    print(avg / len(spam))
Exemplo n.º 2
0
def find_witness(spam, legit, classifier):
    length = min([len(spam), len(legit)])

    curr = spam
    i = 0
    flag = False
    prev = curr
    if type(classifier) == NaiveClassifier:
        while classifier.predict("".join(curr)) != True:
            prev = curr
            for j in range(len(curr)):
                if curr[j] not in spam:
                    del curr[j]
                    flag = True
                    break
            if not flag:
                for j in range(len(spam)):
                    if spam[j] not in curr:
                        curr.append(j)
                        break
            flag = False
            i += 1
            if i == length:
                return curr, prev
    elif type(classifier) == nltk.classify.maxent.MaxentClassifier:
        while classifier.classify(list_to_dict(curr)) != str(1):
            prev = curr
            for j in range(len(curr)):
                if curr[j] not in spam:
                    del curr[j]
                    flag = True
                    break
            if not flag:
                for j in range(len(spam)):
                    if spam[j] not in curr:
                        curr.append(j)
                        break
            flag = False
            i += 1
            if i == length:
                return curr, prev
    else:
        while classifier.classify("".join(curr)) != 0:
            prev = curr
            for j in range(len(curr)):
                if curr[j] not in spam:
                    del curr[j]
                    flag = True
                    break
            if not flag:
                for j in range(len(spam)):
                    if spam[j] not in curr:
                        curr.append(j)
                        break
            flag = False
            i += 1
            if i == length:
                return curr, prev

    return (curr, prev)
Exemplo n.º 3
0
def first_n_words(spam, legit, classifier, wordlist):
    L = []
    to_spam, _ = find_witness(spam, legit, classifier)
    for word in wordlist:
        to_spam.append(word)
        if type(classifier) == NaiveClassifier:
            if classifier.predict("".join(to_spam)) == False:
                L.append(word)
                to_spam = to_spam[:len(to_spam)-1]
        else:
            if classifier.classify(list_to_dict(spam)) == str(0):
                L.append(word)
                to_spam = to_spam[:len(to_spam)-1]
        if len(L) == 20:
            break 
    return L
Exemplo n.º 4
0
def trick_maxent(classifier):
    WORD_LIMIT = 200
    goodWords = load_goodwords('Maxent_Data/goodwords.txt')
    spam = load_spam()
    spam = [line.split(" ")[1] for line in spam]

    avg = 0
    flag = False
    import random
    for comment in spam:
        for i in range(0, WORD_LIMIT):
            comment += " {}".format(random.choice(goodWords))
            if classifier.classify(list_to_dict(comment)) == str(0):
                avg += i
                break
            if i == WORD_LIMIT - 1:
                avg += WORD_LIMIT
    print(avg / len(spam))
Exemplo n.º 5
0
def test(filename, classifier, type="maxent"):
    testing_set = []
    with open(filename, "r") as f:
        reader = csv.reader(f)
        for row in reader:
            if row[0] == 'COMMENT_ID':
                continue
            content = re.sub(r'[^\w\s]', '', row[3]).lower()
            content = content.split(" ")
            testing_set.append([content, row[4]])

    right = 0
    wrong = 0
    wrong_items = []
    spam = []
    for item in testing_set:
        if type == "maxent":
            if classifier.classify(list_to_dict(item[0])) == item[1]:
                right += 1
            else:
                wrong += 1
                wrong_items.append([item[0], item[1]])
        else:
            string = ""
            for word in item[0]:
                string += "{} ".format(word)
            if classifier.predict(string) == int(item[1]):
                if int(item[1]) == 1:
                    spam.append(string)
                right += 1
            else:
                wrong += 1
                wrong_items.append([item[0], item[1]])
    print(f'right: {right}, wrong: {wrong}')
    if type != "maxent":
        with open("spam.txt", "w") as f:
            for comment in spam:
                f.write("{}\n".format(comment))