æéãªã¼ãããã³ã§ä½ãã¤ããBot
http://twitter.com/tsunku_bot
ãé¢ç½ãç¼ã®ã¹ã¬ãç´¹ä»ããã¦ããã®ã§ããããå
ã«ã¾ãTwitterBotä½ã£ã¦ã¿ãã
http://ameblo.jp/ookamya/entry-10438008424.html
ã¹ã¬ãè¦ç´ããã¨ããããããã種ã®åèªãç¹ããã ãã§ã¤ããã£ã½ãæè©ãä½ããã®ã§ã¯ãªããã¨ã®ãã¨ãã¨ããã°ããããããã¹ã¯ãªããã§ã¡ãã¡ãã£ã¨ã§ãããããã¨ãããã¨ã§ãGoogle App Engineã§ä½ã£ã¦ã¿ã¾ãããGoogle App Engineä¸ã§ã®TwitterBotã®ä½ãæ¹ã¯ä»¥ä¸åç
§ã
http://d.hatena.ne.jp/intheflight/20090611/p1
ããã§ä»åã®èã¯ã©ã®ããã«åèªãç¹ããã°ã¤ããæè©ããããªããããã®åèªç¾¤ãçºãã¦ãããããã¤ãã®ã°ã«ã¼ãã«åé¡ãããã¨ãã§ããããªã®ã§ã¾ãåé¡ãã¦ã¿ãã
- ã°ã«ã¼ã0ï¼ãç±³ãå宿ãå°çãåªåãå¹³åã幸ããæè¬ãæªæ¥ãSexyã人éãæµ®æ°ã人çãäºè¡ãæãã¦ãé½ä¼ãé空ãæ±äº¬ãããç¼ããã¢ã¤ã¹ããã¹ã¿ããè±ãçãã¦ããã¸ã§ã©ã·ã¼ããã¼ãªã³ãã»ã³ããªæãåæ ãæ£ç¾©ãã§ã£ãããæ¥æ¬ãåå¼ãç¶ãããæ¯ããã家æãã·ã£ãã ãããã¹ã¿ãåé²ãLOVEãç§ãã¨ããã¡ã¼ã«ãåã³ã³ã大好ããã®ã£ã«ãkiss
- ã°ã«ã¼ã1ï¼æãåããåãã¡åããæé£ããkissããªããããããªãã§ãå§ã¾ããªããæ³£ãã¡ããã夢ãè¨ããããã«ã
- ã°ã«ã¼ã2ï¼ã¤ã§ã¼ã¤ã§ã¼ãLALALAããã¤ãã¼ãã¢ã¼ã³ãyeah!ãwaoãuhãahãNANANAããã£ã¼ããã£ããï¼ãã¦ã¼ã³ãã¤ã§ã¤ã¤ã§ã¤ï¼ãã«ã¢ã³ãOhãSha la laãSha la la...
- ã°ã«ã¼ã3ï¼ãã¨ãªãããã ãããã®ããããããã
次ã«ããããç¹ããããã«ã¯å©è©ãå¿ è¦ã ãªã¨æããã®ã§ãå©è©ã®ã°ã«ã¼ãã追å ã
- ã°ã«ã¼ã4ï¼ããã®ãããã«ãã¸ãã¨ãããããããã§ãã
ããã§ã²ã¨ã¤ã®åèªã°ã«ã¼ããç¶æ
ã¨ã¿ãªãã°ãæéãªã¼ãããã³ã¨ãã¦è¡¨ç¾ã§ããã®ã§ã¯ãªããã試è¡é¯èª¤ããçµæåºæ¥ãã®ãä¸ã®ç¶æ
é·ç§»å³ãç¶æ
ã®æ°åã¨åèªã°ã«ã¼ãã®æ°åã対å¿ãã¾ããéå§ç¶æ
ã0ã§ãçµäºç¶æ
ã5ã
次ã«é·ç§»ã®æ¡ä»¶ã確çã§è¡¨ç¾ããç¶æ
é·ç§»å³ãç¾å¨ã®ç¶æ
ã®åèªã°ã«ã¼ãããåèªé¸æã次ã®ç¶æ
ã«é·ç§»ã¨ããæµãã
ãããéå§ç¶æ
ããä¹±æ°ã§é·ç§»ããã¦çµäºç¶æ
ã«ãªã£ãã¨ãã®åºåãã¤ããæè©ãããããåç´ãªåèªé¸æã ã¨æ¥æ¬èªãããï¼ã¤ãããããï¼ï¼æè©ã«ãªããªãã®ã§ãå©è©ã®é¸æé¨åã ãã1ã¤ã®æã§åãå©è©ãè¤æ°ååºãªãããã«æ¹è¯ããã®ã以ä¸ã®ã½ã¼ã¹ã³ã¼ãã
# ã¤ããåèªç¾¤ word0 = [ã°ã«ã¼ã0ã®åèª] word1 = [ã°ã«ã¼ã1ã®åèª] word2 = [ã°ã«ã¼ã2ã®åèª] word3 = [ã°ã«ã¼ã3ã®åèª] word4 = [ã°ã«ã¼ã4ã®åèª] tsunku_words = [word0, word1, word2, word3, word4] # ã¤ããçµäºç¶æ tsunku_end_state = 5 # ã¤ããæéãªã¼ãããã³ # ç¾å¨ã®ç¶æ ã¨æ¬¡ã®ç¶æ ã¸ã®ç¢ºçã対å¿ãããããã·ã¥ tsunku_state_machine = { 0: { 3: 0.1, 4: 0.9 }, 1: { 0: 0.5, 2: 0.3, 5: 0.2 }, 2: { 0: 0.5, 5: 0.5 }, 3: { 2: 0.5, 5: 0.5 }, 4: { 0: 0.6, 1: 0.4 } } class TsunkuPost(webapp.RequestHandler): def get(self): tsunku_text = self._make_tsunku_poem() if len(tsunku_text) > 140: return url = 'http://twitter.com/statuses/update.xml' payload = urllib.urlencode({'status': tsunku_text.encode('utf-8')}) base64string =b64encode("%s:%s" % (username, password)) headers = {"Authorization": "Basic %s" % base64string} urlfetch.fetch(url, payload=payload, method=urlfetch.POST, headers=headers) def _make_tsunku_poem(self): tsunku_text = [] tsunku_state = 0 used_words = [] while True: if tsunku_state == tsunku_end_state: break elif tsunku_state == 4: # å©è©ã®å ´åã1ã¤ã®æã§åãå©è©ãè¤æ°ååºãªãããã«ãã words = tsunku_words[tsunku_state] while True: word = words[random.randint(0, len(words)-1)] if word not in used_words: tsunku_text.append(word) used_words.append(word) break else: # ç¾å¨ã®ç¶æ ã®åèªç¾¤ããã©ã³ãã ã«é¸ã¶ words = tsunku_words[tsunku_state] tsunku_text.append(words[random.randint(0, len(words)-1)]) # 次ã®ç¶æ ã®é¸æ r = random.random() current_state = tsunku_state_machine[tsunku_state] c = 0 for next_state, prob in current_state.iteritems(): c += prob if r <= c: tsunku_state = next_state # å©è©ã®å¦ç if next_state == 1: used_words = [] break return ''.join(tsunku_text)