• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python markov.Markov类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: send_markov

def send_markov(user, password, recipient, subject, host, files, key_size,
                limit, limit_words, paragraph):
    # Initialize Markov chain.
    m = Markov(files, key_size)

    # Build message.
    verse = m.generate_text(limit, not limit_words, paragraph)
    message = MIMEText(verse)
    message["subject"] = subject
    message["to"] = recipient
    message = message.as_string()

    # Connect
    smtp = SMTP(host)
    smtp.starttls()
    smtp.login(user, password)

    # Send.
    smtp.sendmail(user, recipient, message)

    # Disconnect.
    smtp.close()
开发者ID:spurll,项目名称:markov,代码行数:22,代码来源:send_markov.py


示例17: __init__

 def __init__(self, name, bus, user_id=1):
     self.name = name
     self.bus = bus
     # Set the weather API key from env vars
     # For openweathermap API key
     # self.api_key = "&APPID={}".format(os.environ.get('APPID'))
     # For Wunderground API key
     self.api_key = os.environ.get('WUNDERGROUND_API_KEY')
     # self.server_path = 'http://localhost:5001/api'
     self.user_id = user_id
     self.marky = Markov(
         limit = 600,
         ngram = 6,
         paths = ['static/markov_text/alice_in_wonderland.txt',
                 'static/markov_text/through_the_looking_glass.txt'])
开发者ID:nija,项目名称:hb-chatty,代码行数:15,代码来源:listener.py


示例18: index_feed

def index_feed(target):
	# load config
	if os.path.isfile("credentials.json"):
		try:
			with open ("credentials.json", "r") as f:
				credentials = json.load(f)
		except ValueError:
			print "credentials.json is malformed."
			exit()
	else:
		empty_credentials = {u'consumer-key': u'', u'consumer-secret': u'', u'access-token': u'', u'access-token-secret': '', u'user-name': u''}
		with open ("credentials.json", "w") as f:
			json.dump(empty_credentials, f, indent=4, separators=(',', ':'))
		print "credentials.json is missing. i've created a blank one for you."
		exit()

	# authenticate
	auth = tweepy.OAuthHandler(credentials["consumer-key"], credentials["consumer-secret"])
	auth.set_access_token(credentials["access-token"], credentials["access-token-secret"])

	# connect to the api
	api = tweepy.API(auth)

	#add tweets to redis
	tweet_data = Markov(target)

	#show my tweets
	try:
		my_tweets = api.user_timeline(target, count=20000)
		for tweet in my_tweets:
			cleaned_tweet = clean_tweet(tweet.text)
			tweet_string = " ".join(cleaned_tweet)
			print 'indexing tweet: "' + tweet_string + '"'
			tweet_data.add_line_to_index(cleaned_tweet)
	except Exception as e:
		print e
开发者ID:jordanribera,项目名称:song-serpent,代码行数:36,代码来源:song-serpent.py


示例19: FullTextMarkovEvaluator

class FullTextMarkovEvaluator(HaikuEvaluator):
    """
    Evaluate the full text of a haiku using Markov chains, determine how it 
    """
    def __init__(self, weight=1, prefix=None):
        self.prefix = prefix or getattr(settings, "MARKOV_DATA_PREFIX", "goodlines")
        self.data = Markov(prefix=prefix, **getattr(settings, 'REDIS', {}))
        super(FullTextMarkovEvaluator, self).__init__(weight=weight)
    
    def evaluate(self, haiku):
        haiku_text = []
        for line in haiku.get_lines():
            haiku_text += line.split()
        score = self.data.score_for_line(haiku_text)
        return score
开发者ID:wieden-kennedy,项目名称:django-haikus,代码行数:15,代码来源:evaluators.py


示例20: MarkovTweet

class MarkovTweet(object):

    def __init__(self, url):
        "Generate the markov chain stuff first."
        file_ = open(url, 'r')
        self.mkov = Markov(file_)

    def tweet(self, words=('', '')):
        "Capitalise the first word and trim to the end of a sentence."
        twit = self.mkov.generate_markov_text(50, words)
        twit = twit[0].upper() + twit[1:]
        while twit != '' and twit[-1] not in ';:?.,!':
            twit = twit[:-1]
        
        if twit == '':
            twit = self.tweet()
        else:
            if twit[-1] in ';:,':
                twit = twit[:-1] + '.'
        return twit
开发者ID:markfiend,项目名称:markov-wiki-ircbot,代码行数:20,代码来源:markovtweet.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python markup.page函数代码示例发布时间:2022-05-27
下一篇:
Python connection.Connection类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap