にゃーんBOT(@nya_n_)
β版です。
多分何もエラーがでなければβのままです。
後は定期ポストの語彙を増やすだけぐらいかな。
機能は
1.発言
2.リプライに反応
3.フォロワーの発言を収集してマルコフ連鎖して発言
のみです。
大した発言もしないですし、大した返しもしてくれません。
あ、後
4.猫で癒される
猫ってだけで癒されますね!!
今大したエラーがおきてます
直ったー…かな
ソース
application.py
#coding: utf-8 from flask import Flask app = Flask(__name__) app.debug = True from google.appengine.ext import db from google.appengine.api import urlfetch from flask import redirect, url_for, request, render_template, abort, flash, get_flashed_messages import tweepy import random from markov import markov , morph CONSUMER_KEY = '自分で登録した値' CONSUMER_SECRET = '上に同じ' TOKEN_KEY = '上に同じ' TOKEN_SECRET = '上に同じ' class Contexts(db.Model): context = db.StringProperty() class ReplyContexts(db.Model): context = db.StringProperty() class LatestReplyId(db.Model): reply_id = db.IntegerProperty() class MyModel(db.Model): updated = db.StringProperty() @app.route('/post_form/') def post_form(context=None): return render_template('form.html') @app.route('/post_update/',methods=['GET','POST']) def post_update(): if request.method == 'POST': context = request.form['context'] update(context) return redirect(url_for('post_form')) return 'Sample' @app.route('/rnd_update/') def rnd_update(): query = Contexts.all() results = query.fetch(1000) result = results[random.randint(0,len(results)-1)] update(result.context) return result.context @app.route('/insert_form/',methods=['GET','POST']) def insert_form(context = None): if request.method == 'POST': context_str = request.form['context'] cont = Contexts(context=context_str) db.put(cont) else: pass return render_template('insert_form.html') def update(context = None,consumer_key = CONSUMER_KEY, \ consumer_secret = CONSUMER_SECRET,token_key = TOKEN_KEY, token_secret = TOKEN_SECRET): if context != None: context = context.encode('utf-8') auth = tweepy.OAuthHandler(consumer_key,consumer_secret) auth.set_access_token(token_key,token_secret) api = tweepy.API(auth) api.update_status(context) return True return False def get_API(consumer_key = CONSUMER_KEY,consumer_secret=CONSUMER_SECRET,\ token_key = TOKEN_KEY,token_secret = TOKEN_SECRET): api = None auth = tweepy.OAuthHandler(consumer_key,consumer_secret) auth.set_access_token(token_key,token_secret) api = tweepy.API(auth) return api @app.route('/markov/') def twi_markov(context = None): api = get_API() timeline = api.friends_timeline(count=5) context = timeline[random.randint(0,len(timeline) -1 )].text for tweet in timeline: context = context + tweet.text context = markov(morph(sentence = context)) context.replace('@',' ') update(context = context) return context @app.route('/rnd_reply/') def rnd_reply(): api = get_API() query = ReplyContexts.all() db_reply_id = query.fetch(10).pop() latest_reply_id = db_reply_id.reply_id timelines = api.friends_timeline(latest_reply_id) if len(timelines) > 0: for status in timelines: return None # set the secret key. keep this really secret: app.secret_key = '自分で設定した値' if __name__ == '__main__': app.run()
markov.py
# coding:utf-8 import urllib,urllib2 from BeautifulSoup import BeautifulSoup import random appid = "アプリケーションid" pageurl = "http://jlp.yahooapis.jp/MAService/V1/parse" def morph(sentence = None, appid=appid, results="ma", app_filter="1|2|3|4|5|6|7|8|9|10|11|12|13"): ret = {} sentence = urllib.quote_plus(sentence.encode('utf-8')) query = "%s?appid=%s&results=%s&filter=%s&sentence=%s" % (pageurl, appid, results, app_filter, sentence) #c = urlfetch.fetch(query,deadline=10) c = urllib2.urlopen(url=query) soup = BeautifulSoup(c.read()) surfaces = [] print soup for w in soup.ma_result.word_list: surfaces.append(w.surface.string) return surfaces def get_important_word(sentence = None, appid=appid): #http://jlp.yahooapis.jp/KeyphraseService/V1/extract?appid=<あなたのアプリケーションID>&sentence=<対象のテキスト> sentence = urllib.quote_plus(sentence.encode('utf-8')) query = "http://jlp.yahooapis.jp/KeyphraseService/V1/extract?appid=%s&sentence=%s" % (appid,sentence) c = urllib2.urlopen(url=query) soup = BeautifulSoup(c.read()) print soup.results results = soup.findAll('result') important_words = [] return soup def markov(word_list=None): MAXGEN = 15 nonword = "\n" w1 = w2 = nonword markov = {} content = "" for word in word_list: if not markov.has_key((w1,w2)): markov[(w1,w2)] = [] markov[(w1,w2)].append(word) w1,w2 = w2,word if not markov.has_key((w1,w2)): markov[(w1,w2)] = [] markov[(w1,w2)].append(nonword) w1 = w2 = nonword for i in range(MAXGEN): suf = markov[(w1,w2)] r = random.randint(0,len(suf) - 1) t = suf[r] if t == nonword: break content = content + t w1,w2 = w2,t return content if __name__=='__main__': sentence = u'隣の庭ではニワトリを二羽飼っている' #result = morph(sentence) result = get_important_word(sentence=sentence)
マルコフ連鎖の部分はこのサイト様を参考にさして頂きました http://d.hatena.ne.jp/pylet/20060501/markov