class tweeter_markov_tests(unittest.TestCase): @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def setUp(self, _): self.corpus = path.join(path.dirname(__file__), 'data', 'tweets.txt') self.configfile = path.join(path.dirname(__file__), '..', 'bots.yaml') self.tm = TwitterMarkov('example_screen_name', [self.corpus], config=self.configfile, dry_run=True, learn=False) self.tm.log.setLevel(100) def testCheckModels(self): for m in self.tm.models.values(): self.assertIsInstance(m, markovify.text.NewlineText) @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovCompose(self, *_): response = self.tm.compose(tries=150, max_overlap_ratio=2, max_overlap_total=100) assert isinstance(response, basestring) assert len(response) < 140 @mock.patch.object(tweepy.API, 'mentions_timeline', return_value=fake_timeline()) @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovReply(self, *_): r = self.tm.reply_all(tries=75, max_overlap_ratio=2, max_overlap_total=100) @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovRecentlyTweeted(self, _): recents = self.tm.recently_tweeted assert recents[0] == TIMELINE[0]['text'] @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovCheckTweet(self, _): assert self.tm.check_tweet('') is False assert self.tm.check_tweet('badword') is False assert self.tm.check_tweet('Lorem ipsum dolor sit amet') is False assert self.tm.check_tweet('Lorem ipsum dolor sit amet!') is False assert self.tm.check_tweet('Lorem ipsum dolor set namet') is False assert self.tm.check_tweet('Random Text that should work totally') is True assert self.tm.check_tweet('@reply Random Text') is True @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovLearn(self, _): tmp = path.join(path.dirname(__file__), 'data', 'tmp.txt') self.tm.learn_parent(corpus=tmp) try: with open(tmp) as f: result = f.read() assert TIMELINE[0]['text'] in result assert TIMELINE[1]['text'] in result assert TIMELINE[2]['text'] in result finally: os.remove(tmp)
class tweeter_markov_tests(unittest.TestCase): @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def setUp(self, _): self.corpus = path.join(path.dirname(__file__), 'data', 'tweets.txt') self.configfile = path.join(path.dirname(__file__), '..', 'bots.yaml') self.tm = TwitterMarkov('example_screen_name', [self.corpus], config=self.configfile, dry_run=True, learn=False) self.tm.log.setLevel(100) @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovCompose(self, *_): response = self.tm.compose(tries=150, max_overlap_ratio=2, max_overlap_total=100) assert isinstance(response, basestring) assert len(response) < 140 @mock.patch.object(tweepy.API, 'mentions_timeline', return_value=fake_timeline()) @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovReply(self, *_): r = self.tm.reply_all(tries=75, max_overlap_ratio=2, max_overlap_total=100) @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovRecentlyTweeted(self, _): recents = self.tm.recently_tweeted assert recents[0] == TIMELINE[0]['text'] @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovCheckTweet(self, _): assert self.tm.check_tweet('') is False assert self.tm.check_tweet('badword') is False assert self.tm.check_tweet('Lorem ipsum dolor sit amet') is False assert self.tm.check_tweet('Lorem ipsum dolor sit amet!') is False assert self.tm.check_tweet('Lorem ipsum dolor set namet') is False assert self.tm.check_tweet('Random Text that should work totally') is True assert self.tm.check_tweet('@reply Random Text') is True @mock.patch.object(tweepy.API, 'user_timeline', return_value=fake_timeline()) def testTwitterMarkovLearn(self, _): tmp = path.join(path.dirname(__file__), 'data', 'tmp.txt') self.tm.learn_parent(corpus=tmp) try: with open(tmp) as f: result = f.read() assert TIMELINE[0]['text'] in result assert TIMELINE[1]['text'] in result assert TIMELINE[2]['text'] in result finally: os.remove(tmp)
length = None propoganda = (random.random() <= 0.4) if not propoganda: #state_size = coherence(5) length = 92 try: model = learn(state_size, propoganda) except IOError as e: exit(0) #tm.models = tm._setup_models(tm.corpora, state_size) print 'composing....\n' tweet = tm.compose(model=model, max_len=length) tries = 1 print 'checking...\n' while not check(tweet): if (tries % 10) == 0: state_size = coherence(state_size - 1) #coherence(state_size) try: model = learn(state_size, propoganda) except IOError as e: exit(0) #tm.models = tm._setup_models(tm.corpora, state_size) tweet = tm.compose(model=model, max_len=length)
length = None propoganda = (random.random() <= 0.4) if not propoganda: #state_size = coherence(5) length = 92 try: model = learn(state_size, propoganda) except IOError as e: exit(0) #tm.models = tm._setup_models(tm.corpora, state_size) print 'composing....\n' tweet = tm.compose(model = model, max_len=length) tries = 1 print 'checking...\n' while not check(tweet): if (tries % 10) == 0: state_size = coherence(state_size -1) #coherence(state_size) try: model = learn(state_size, propoganda) except IOError as e: exit(0) #tm.models = tm._setup_models(tm.corpora, state_size) tweet = tm.compose(model = model, max_len=length)