Example #1
0
    def test_add(self):
        gen = MarkovGenerator([], random=self.random)

        gen.add(SAMPLE_TEXT)
        result = gen.generate(3)

        self.assertEqual('One two three', result)
class MailGenerator(object):

    NAMES = ['alice', 'bob', 'eve']

    def __init__(self, receiver, domain_name, sample_mail_list, random=None):
        self._random = random if random else Random()
        self._receiver = receiver
        self._domain_name = domain_name
        self._subjects, self._bodies = load_all_mails(sample_mail_list)

        self._potential_tags = search_for_tags(' '.join(self._bodies))
        self._subject_markov = MarkovGenerator(self._subjects,
                                               random=self._random)
        self._body_markov = MarkovGenerator(self._bodies,
                                            random=self._random,
                                            add_paragraph_on_empty_chain=True)

    def generate_mail(self):
        body = self._body_markov.generate(150)
        mail = MIMEText(body)

        mail['Subject'] = self._subject_markov.generate(8)
        mail['To'] = '%s@%s' % (self._receiver, self._domain_name)
        mail['From'] = self._random_from()
        mail['Date'] = self._random_date()
        mail['X-Tags'] = self._random_tags()
        mail['X-Leap-Encryption'] = self._random_encryption_state()
        mail['X-Leap-Signature'] = self._random_signature_state()

        return mail

    def _random_date(self):
        now = int(time.time())
        ten_days = 60 * 60 * 24 * 10
        mail_time = self._random.randint(now - ten_days, now)

        return formatdate(mail_time)

    def _random_encryption_state(self):
        return self._random.choice(['true', 'decrypted'])

    def _random_signature_state(self):
        return self._random.choice(['could not verify', 'valid'])

    def _random_from(self):
        name = self._random.choice(
            filter(lambda name: name != self._receiver, MailGenerator.NAMES))

        return '%s@%s' % (name, self._domain_name)

    def _random_tags(self):
        barrier = 0.5
        tags = set()
        while self._random.random() > barrier:
            tags.add(self._random.choice(self._potential_tags))
            barrier += 0.15

        return ' '.join(tags)
    def __init__(self, receiver, domain_name, sample_mail_list, random=None):
        self._random = random if random else Random()
        self._receiver = receiver
        self._domain_name = domain_name
        self._subjects, self._bodies = load_all_mails(sample_mail_list)

        self._potential_tags = search_for_tags(' '.join(self._bodies))
        self._subject_markov = MarkovGenerator(self._subjects, random=self._random)
        self._body_markov = MarkovGenerator(self._bodies, random=self._random, add_paragraph_on_empty_chain=True)
class MailGenerator(object):

    NAMES = ['alice', 'bob', 'eve']

    def __init__(self, receiver, domain_name, sample_mail_list, random=None):
        self._random = random if random else Random()
        self._receiver = receiver
        self._domain_name = domain_name
        self._subjects, self._bodies = load_all_mails(sample_mail_list)

        self._potential_tags = search_for_tags(' '.join(self._bodies))
        self._subject_markov = MarkovGenerator(self._subjects, random=self._random)
        self._body_markov = MarkovGenerator(self._bodies, random=self._random, add_paragraph_on_empty_chain=True)

    def generate_mail(self):
        body = self._body_markov.generate(150)
        mail = MIMEText(body)

        mail['Subject'] = self._subject_markov.generate(8)
        mail['To'] = '%s@%s' % (self._receiver, self._domain_name)
        mail['From'] = self._random_from()
        mail['Date'] = self._random_date()
        mail['X-Tags'] = self._random_tags()
        mail['X-Leap-Encryption'] = self._random_encryption_state()
        mail['X-Leap-Signature'] = self._random_signature_state()

        return mail

    def _random_date(self):
        now = int(time.time())
        ten_days = 60 * 60 * 24 * 10
        mail_time = self._random.randint(now - ten_days, now)

        return formatdate(mail_time)

    def _random_encryption_state(self):
        return self._random.choice(['true', 'decrypted'])

    def _random_signature_state(self):
        return self._random.choice(['could not verify', 'valid'])

    def _random_from(self):
        name = self._random.choice(filter(lambda name: name != self._receiver, MailGenerator.NAMES))

        return '%s@%s' % (name, self._domain_name)

    def _random_tags(self):
        barrier = 0.5
        tags = set()
        while self._random.random() > barrier:
            tags.add(self._random.choice(self._potential_tags))
            barrier += 0.15

        return ' '.join(tags)
    def __init__(self, receiver, domain_name, sample_mail_list, random=None):
        self._random = random if random else Random()
        self._receiver = receiver
        self._domain_name = domain_name
        self._subjects, self._bodies = load_all_mails(sample_mail_list)

        self._potential_tags = search_for_tags(' '.join(self._bodies))
        self._subject_markov = MarkovGenerator(self._subjects,
                                               random=self._random)
        self._body_markov = MarkovGenerator(self._bodies,
                                            random=self._random,
                                            add_paragraph_on_empty_chain=True)
Example #6
0
    def test_multiple_word_occurences(self):
        gen = MarkovGenerator(['One Two One Three One Two One Four'], random=self.random)

        result = gen.generate(2)

        self.assertEqual('Two One', result)
Example #7
0
    def test_multiple_inputs(self):
        gen = MarkovGenerator([SAMPLE_TEXT, 'Five Six seven eight'], random=self.random)

        result = gen.generate(3)

        self.assertEqual('Five Six seven', result)
Example #8
0
    def test_add_paragraph_on_empty_chain(self):
        gen = MarkovGenerator([SAMPLE_TEXT], random=self.random, add_paragraph_on_empty_chain=True)

        result = gen.generate(5)

        self.assertEqual('One two three four \n\n One', result)
Example #9
0
 def test_minimum_three_words(self):
     self.assertRaises(ValueError, MarkovGenerator([]).generate, 1)
     self.assertRaises(ValueError, MarkovGenerator, ['1'])
     self.assertRaises(ValueError, MarkovGenerator, ['1', '2'])
     self.assertRaises(ValueError, MarkovGenerator, ['1', '2', '3'])
Example #10
0
    def test_aborts_if_no_upper_letter_word_found(self):
        gen = MarkovGenerator(['all lower case'], random=self.random)

        self.assertRaises(ValueError, gen.generate, 1)
Example #11
0
    def test_starts_with_capital_case_workd(self):
        gen = MarkovGenerator(['lower Upper smaller Capital'], random=self.random)

        result = gen.generate(1)

        self.assertTrue(result.startswith('Upper'))