mercredi 10 janvier 2018

How to select randomly a string from tuple and insert randomly on each line in a text?

I have this text("prediction.txt"):

email01
email02
email03
email04
email05

I want to as classify randomly a type("SPAM", "OK") to each of the emails so that I will have something like this:

email01 OK
email02 SPAM
email03 OK
email04 OK
email05 SPAM

My code is the below:

import os
import random


class BaseFilter:

    def __init__(self, mail, em_type):
        with open(mail, 'r') as f:
            self.em_type = f.read().replace('\n', em_type)


class RandomFilter(BaseFilter):
    "Returns randomly either SPAM or OK"
    types = ('SPAM', 'OK')
    rand_type = random.choice(types)

    def __init__(self, mail):

        types = ('SPAM', 'OK')
        rand_type = random.choice(types)
        super().__init__(mail, em_type= ' ' + rand_type + '\n')

When testing it through following:

random = RandomFilter('prediction.txt')
print(random.em_type)

But what I actually receive is either this:

email01 SPAM
email02 SPAM
email03 SPAM
email04 SPAM
email05 SPAM

Or this:

email01 OK
email02 OK
email03 OK
email04 OK
email05 OK

Therefore how can i receive randomly OK or SPAM on each line?




Aucun commentaire:

Enregistrer un commentaire