当前位置: 首页>>代码示例>>Python>>正文


Python markov.Markov类代码示例

本文整理汇总了Python中markov.Markov的典型用法代码示例。如果您正苦于以下问题:Python Markov类的具体用法?Python Markov怎么用?Python Markov使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Markov类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

def main(params, tracklist):
    # twitter info
    consumer_key = params.get('twitter', 'consumer_key')
    consumer_secret = params.get('twitter', 'consumer_secret')
    access_token = params.get('twitter', 'access_token')
    access_token_secret = params.get('twitter', 'access_secret')

    # auth & api
    auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)

    # markov
    corpus = params.get('markov', 'corpus')
    is_pickled = params.getboolean('markov', 'pickled')
    m = Markov(corpus)
    if is_pickled:
        m.load_db()
    else:
        m.make_db()

    # listener
    print 'Starting listener for "{}"...'.format(', '.join(tracklist))
    listener = Listener(m, is_pickled, api=api)
    stream = tweepy.Stream(auth, listener)
    stream.filter(track=tracklist)
开发者ID:Battleroid,项目名称:anangrybot,代码行数:26,代码来源:listen.py

示例2: test_fold_in

    def test_fold_in(self):
        """It should create a parody text

        """
        markov = Markov(corpus=self.sample_page1)
        key = markov.get_key()
        print(key)
        print(markov.next(key))
开发者ID:zjjw,项目名称:cut_up,代码行数:8,代码来源:test_markov.py

示例3: test_count

    def test_count(self):
        m = Markov(2)
        m.scan(['a', 'b', 'b', 'b', 'c'])
        self.assertEquals(m.chains['a']['count'], 1)
        self.assertEquals(m.chains['b']['count'], 3)

        self.assertEquals(m.chains['a']['next']['b']['count'], 1)
        self.assertEquals(m.chains['b']['next']['b']['count'], 2)
        self.assertEquals(m.chains['b']['next']['c']['count'], 1)
开发者ID:thegreatape,项目名称:mockingbard,代码行数:9,代码来源:tests.py

示例4: lambda_handler

def lambda_handler(event, context):
    """Entrypoint for AWS Lambda. Event contains the payload from the AWS Lambda
       call."""
    user_text = event['userText']
    mixin = event['mixin']
    length = int(event['length'])

    full_text = user_text + load_mixin(mixin)
    markover = Markov(full_text)
    return markover.generate(length)
开发者ID:ekatek,项目名称:nanowrinet,代码行数:10,代码来源:lambda_function.py

示例5: __init__

class Parser:
    """
    Base class for a lexer/parser that has the rules defined as methods
    """
    tokens = ()
    precedence = ()

    def __init__(self, **kw):
        self.debug = kw.get('debug', 0)
        self.sentences = []
        self.markov = Markov()
        self.clause_starter = {}
        self.para_starter = []
        self.words = kw.get('words', None)

        try:
            modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
        except:
            modname = "parser"+"_"+self.__class__.__name__
        self.debugfile = modname + ".dbg"
        self.tabmodule = modname + "_" + "parsetab"
        #print self.debugfile, self.tabmodule

        # Build the lexer and parser
        lex.lex(module=self, debug=self.debug)
        yacc.yacc(module=self,
                  debug=self.debug,
                  debugfile=self.debugfile,
                  tabmodule=self.tabmodule)

    def run(self):
        s = sys.stdin.read()
        s = s.replace('\n\n', '\x00')
        s = s.replace('\x00\x00', '\x00')
        s = s.replace('\n\n', '')
        s = s.replace('\n', ' ')
        s = s.replace('  ', ' ')
        yacc.parse(s)
        print self.sentences
        self.markov.printout()
        print
        print "clause starters"
        keys = self.clause_starter.keys()
        keys.sort()
        for k in keys:
            v = self.clause_starter[k]
            print "\t", repr(k), v
        print
        print "para starters", self.para_starter
        print
        self.markov.prepare()
        sentence = random_sentence(self.markov, 800,
                                    starters=self.clause_starter,
                                    para_starters=self.para_starter)
        print_sentence(sentence, word_filter=self.words)
开发者ID:davelab6,项目名称:telaro,代码行数:55,代码来源:telaro.py

示例6: markovComments

def markovComments(comments):
	words = comments.split(' ')
	m = Markov(words)
	startWord = random.choice(m.getStartingUnits())

	chain = m.getNextSentence(startWord, 20)
	sentence = startWord
	for word in chain:
		sentence += ' ' + word
	
	return sentence
开发者ID:Nmael,项目名称:Reddit-Markov,代码行数:11,代码来源:redditHelpers.py

示例7: newTweet

def newTweet():
    tweet_data = Markov(prefix="tweets")
    tweetline = tweet_data.generate()
    tweet = ""
    for word in tweetline:
        tweet += word + " "
    
    tweet = tweet.rstrip()
    
    if len(tweet)>140:
        return newTweet()
    else:
        return tweet
开发者ID:riordan,项目名称:ponyinthere,代码行数:13,代码来源:tweetslice.py

示例8: test_probabilities

    def test_probabilities(self):
        m = Markov(2)
        m.scan(['bacon', 'fish', 'bacon', 'lung',
                'bacon', 'lung', 'bacon', 'lung',  '.'])
        result = m.compute_probabilities(3)
        self.assertEquals(len(result), 3)

        # least likely to most
        self.assertEquals(result[0]['word'], 'fish')
        self.assertEquals(result[0]['chance'], 1.0/8.0 * (1.0 / 0.5))
        self.assertEquals(result[1]['word'], 'lung')
        self.assertEquals(result[1]['chance'], 3.0/8.0 * (1.0 / 0.5))
        self.assertEquals(result[2]['word'], 'bacon')
        self.assertEquals(result[2]['chance'], 0.5 * (1.0 / 0.5))
开发者ID:thegreatape,项目名称:mockingbard,代码行数:14,代码来源:tests.py

示例9: main

def main():
    """set-up applcation and send love"""
    api = get_handle()
    markov = Markov(get_corpus())
    people = People(api)

    love = markov.generate_markov_text(random.randrange(15, 20))
    to_email = people.get_random()
    print to_email
    print love
    if api.send_love(to_email, love):
        return 0
    else:
        return -1
开发者ID:mwhooker,项目名称:bad-love,代码行数:14,代码来源:bad_love.py

示例10: Test_Markov

class Test_Markov(unittest.TestCase):

    def setUp(self):
        self.test_text = "I have bought this bonnet.\r\nI am Mr. Darcy.\r\nI am.\r\n"
        with open("in.txt",'w') as f:
            f.write(self.test_text)
        self.test_outfile = "out.txt"
        ospl.process_data("in.txt", self.test_outfile, True)
        self.test_markov = Markov(self.test_outfile)
    
    def test_unigrams(self):
        self.assertEqual(type(self.test_markov.unigrams), defaultdict)
        self.assertEqual(round(sum(self.test_markov.unigrams.values()), 5), 1.0)
        self.assertEqual(self.test_markov.unigrams["**Beginning**"], 0.15)
    
    def test_bigrams(self):
        self.assertEqual(type(self.test_markov.bigrams), defaultdict)
        self.assertEqual(round(sum(self.test_markov.bigrams["I"].values()), 5), 1.0)
        self.assertEqual(self.test_markov.bigrams["**Beginning**"]["I"], 1.0)
        self.assertEqual(self.test_markov.bigrams["I"]["have"], 1/3.)
    
    def test_bigrams_counter(self):
        bigram_freqs = self.test_markov.make_ngram_freqs_dict(2)
        self.assertEqual(type(bigram_freqs), defaultdict)
        self.assertEqual(bigram_freqs["I"]["am"], 2)
        self.assertEqual(bigram_freqs["."]["**End**"], 3)
        
    def test_trigrams(self):
        self.assertEqual(type(self.test_markov.trigrams), defaultdict)
        self.assertEqual(round(sum(self.test_markov.trigrams[("I","am")].values()), 5), 1.0)
        self.assertEqual(self.test_markov.trigrams[("**Beginning**","I")]["am"], 2/3.)
        self.assertEqual(self.test_markov.trigrams[("I","have")]["rocketships"], 1e-5)

    def test_trigrams_counter(self):
        trigram_freqs = self.test_markov.make_ngram_freqs_dict(3)
        self.assertEqual(type(trigram_freqs), defaultdict)
        self.assertEqual(trigram_freqs[("Darcy",".")]["**End**"], 1)
        self.assertEqual(trigram_freqs[("**Beginning**","I")]["am"], 2)
        
    def test_make_sentence(self):
        self.test_markov.make_ngram_sentence(n=2)
        self.test_markov.make_ngram_sentence(n=3)
        self.assertRaises(AssertionError, self.test_markov.make_ngram_sentence, n=5)
        
    def test_score_sentence(self):
        """ make up nonsense sentence, make sure it scores worse than Markov sentence. possibly also make up word with same words as corpus but in a completly bogus order, that should also score lower"""
        nonsense = "Vestiges of cetaceous gentility."
        nonsense_score = self.test_markov.score_sentence(nonsense)
        real_trigram_sent = self.test_markov.make_ngram_sentence()
        real_trigram_score = self.test_markov.score_sentence(real_trigram_sent)
        weird_order = "Darcy . I bonnet am bought have am"
        weird_order_score = self.test_markov.score_sentence(weird_order)
        self.assertGreater(nonsense_score, real_trigram_score)
        self.assertGreater(weird_order_score, real_trigram_score)
        
    def tearDown(self):
        os.remove("in.txt")
        os.remove("out.txt")
开发者ID:astrosilverio,项目名称:PandPandWhales,代码行数:58,代码来源:test_ngrams.py

示例11: setUp

 def setUp(self):
     self.test_text = "I have bought this bonnet.\r\nI am Mr. Darcy.\r\nI am.\r\n"
     with open("in.txt",'w') as f:
         f.write(self.test_text)
     self.test_outfile = "out.txt"
     ospl.process_data("in.txt", self.test_outfile, True)
     self.test_markov = Markov(self.test_outfile)
开发者ID:astrosilverio,项目名称:PandPandWhales,代码行数:7,代码来源:test_ngrams.py

示例12: main

def main():
    twitter = Twython(KEY, access_token=TOKEN)
    try:
        tweets = twitter.get_user_timeline(screen_name=raw_input("twitter username: "), count=200)
    except:
        print "User does not exist."
        return
    try:
        count = int(raw_input("number of tweets to generate: "))
        text = ""
        for tw in tweets:
            text += re.sub("https?:\/\/([^\s]+)", "", tw['text']) + ". "
        mk = Markov(text)
        sentences = ""
        for i in range(0, count):
            print mk.sentence()

    except:
        print "Please enter a valid number."
开发者ID:tylerbonnell,项目名称:Markov,代码行数:19,代码来源:lazy_tweeter.py

示例13: TestParsing

class TestParsing(unittest.TestCase):
    def setUp(self):
        self.markov = Markov()

    def test_basic_tokenize(self):
        result = self.markov.tokenize("grraaaaa brainss monkeybutter")
        self.assertEquals(result, ["grraaaaa", "brainss", "monkeybutter"])

    def test_basic_scan(self):
        self.markov.scan(['a', 'b', 'c', 'd', 'e'])
        self.assertTrue('a' in self.markov.chains)
        self.assertTrue('b' in self.markov.chains)
        self.assertTrue('c' in self.markov.chains)

        self.assertTrue('b' in self.markov.chains['a']['next'])
        self.assertTrue('c' in self.markov.chains['a']['next']['b']['next'])
        self.assertTrue('d' in self.markov.chains['b']['next']['c']['next'])

    def test_count(self):
        m = Markov(2)
        m.scan(['a', 'b', 'b', 'b', 'c'])
        self.assertEquals(m.chains['a']['count'], 1)
        self.assertEquals(m.chains['b']['count'], 3)

        self.assertEquals(m.chains['a']['next']['b']['count'], 1)
        self.assertEquals(m.chains['b']['next']['b']['count'], 2)
        self.assertEquals(m.chains['b']['next']['c']['count'], 1)

    def test_probabilities(self):
        m = Markov(2)
        m.scan(['bacon', 'fish', 'bacon', 'lung',
                'bacon', 'lung', 'bacon', 'lung',  '.'])
        result = m.compute_probabilities(3)
        self.assertEquals(len(result), 3)

        # least likely to most
        self.assertEquals(result[0]['word'], 'fish')
        self.assertEquals(result[0]['chance'], 1.0/8.0 * (1.0 / 0.5))
        self.assertEquals(result[1]['word'], 'lung')
        self.assertEquals(result[1]['chance'], 3.0/8.0 * (1.0 / 0.5))
        self.assertEquals(result[2]['word'], 'bacon')
        self.assertEquals(result[2]['chance'], 0.5 * (1.0 / 0.5))
开发者ID:thegreatape,项目名称:mockingbard,代码行数:42,代码来源:tests.py

示例14: __init__

	def __init__ (self, jid, password, room, nick, fileName):
		sleekxmpp.ClientXMPP.__init__(self, jid, password)
		self.room = room
		self.nick = nick

		#initialize markov chain with input file
		inputFile = open(fileName)
		self.markov = Markov(inputFile)

		self.add_event_handler("session_start", self.start)
		self.add_event_handler("groupchat_message", self.messageHandler)
开发者ID:dcw329,项目名称:alfred,代码行数:11,代码来源:alfred.py

示例15: main

def main():
    r = praw.Reddit(user_agent="Markov Comment Generator by /u/officialdovahkiin")
    name = raw_input("reddit username: ")
    user = r.get_redditor(name)
    if (valid_redditor(user)):
        try:
            comment_amount = int(raw_input("number of comments to consider: "))
            sentence_amount = int(raw_input("number of sentences to generate: "))
            comments = user.get_comments(limit=(None if comment_amount <= 0 else comment_amount))
            text = ""
            for c in comments:
                text += format_comment(c.body) + ". "
            mk = Markov(text)
            sentences = ""
            for i in range(0, sentence_amount):
                sentences += mk.sentence() + ". "
            print sentences
        except:
            print "Please enter a valid number"
    else:
        print "No comments found for /u/" + name
开发者ID:tylerbonnell,项目名称:Markov,代码行数:21,代码来源:reddit_comment.py


注:本文中的markov.Markov类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。