そらのあお (2) : 辞書型を使ってみた

pythonmatlabにくらべて色々変数の型があるんだなーということで、辞書型を使って反応部分を書きなおしてみた。これで、反応を(比較的簡単に)追加できる、はず。
とはいえ、ちびめりも、反応パターンだけでなんパターンなるんだろう…?まぁいいや
(発信:id:showyouさん、お時間ある時に良かったら見ていただけませんか!)

#!/usr/bin/env python
# coding: utf-8

# Project : Aya
# aoiro v2
# 2009-07-21 : by allegro

# initialize
import twitter
import random

me = aboutme()
USERNAME = me['userid']
PASSWORD = me['password']

api = twitter.Api(USERNAME, PASSWORD)

# pre-process

# process (1) : make reply
timeline = api.GetFriendsTimeline()
for post in timeline:
  if post.in_reply_to_status_id:
  # TODO : 誰かへのリプライエントリであった場合の処理
  # TODO : 自分へのリプライエントリであった場合の処理
    continue
  else:
    makeActionReplyText(post)

# ######## ######## ######## サブ関数群 ######## ######## ########
# ######## about me
def aboutme():
  me = {
    'userid'  :'aoiro_',
    'password':'xxxxx',
    'me'      :'あお',
    }

  return me
  
# ######## ActionReply (他の人の独り言に反応)
def makeActionReplyText(post):
  entry = post.text
  replyToUID = post.user.screen_name # UserID
  replyToSID = post.id # StatusID

  replyBody = makeReplyBody(entry)

  if replyBody:
    replyheader = u'@',+replyToUID + ' '
    replytext = replyheader+replybody
    twitter.PostUpdate(replytext, replyToEID)
    # TODO : registerReply(replytext, replytoEID)

# ######## 反応パターン辞書からReplyTextを作る
def makeReplyBody(entry):
  patternDict = getPatternDict()

  flg = []
  for key in patternDict:
    # 各パターンについて
    ptrs = patternDict[key]['patterns']
    for ptr in ptrs:
      # 各パターンの反応辞書について反応するかcheck
      if ptr in entry:
        flg[end+1] = key # TODO : end+1に相当する書き方にする
        break

  if len(flg) == 1:
    # 反応が一種類の場合
    replies  = patternDict[flg[1]]['patterns']
    dest     = patternDict[flg[1]]['dest']
    replyBody = selectReplyfromReplies(replies, dest)
  elif len(flg) > 1:
    ind
    replies  = patternDict[flg[ind]]['patterns']
    dest     = patternDict[flg[ind]]['dest']
    replyBody = selectReplyfromReplies(replies, dest)

# ###  Repliesから適当に/確率から一つReply選ぶ
def selectReplyfromReplies(replies, dest):
  ind = random.randint(1,len(replies))
  ind = ind -1
  reply = replies[ind]

  return reply

# ######## 確率分布からindexを一つ選ぶ
def indexFromPDest(pdest):
  #sumdest = []
  #th = random.random()

  #for i in range(len(dest)):
  #  t0 = sum(dest([0:i+1])) / float(sum(dest))
  #  sumdest.append(t0)

  #for i = in sumdest:
  #  if th<=  i:
  #    ind = i
  #    break

  th = random.random()
  print th

  for i in range(len(pdest)):
    t0 = sum(pdest[0:i+1]) / float(sum(pdest))
    # 0からpdest[i]までの確率の和
    if th <= t0:
      # threshold-valueをt0が超えたところがindxになる
      ind = i
      break

  return ind



# ######## ######## ######## 反応辞書 ######## ######## ########
def getPatternDictionary():
  patternDictionary = dict()

# ######## ohayou : おはよう
  patternDictionary['ohayou'] = {
    'patterns':
      [u'おはよ',
      u'オハヨ'],

    'replies':
      [u'おはようなの',
      u'おきてーおきてーなの'],

    'dest':
      [100,
      100]
  }

# ######## banterine : バンテリーン
  patternDictionary['banterine'] = {
    'patterns':
      [u'バンテリーン',
      u'バンテリン'],

    'replies':
      [u'キンカーン!!',
      u'キンカーーーーーーーン!!!'],

    'dest':
      [100,
      100]
  }

  return patternDictionary
  # fed getPatternDictionary