『エンディング・ボット』

自分の書いた大量の文章をトークン化し、語彙資源化して、バイグラムの頻度分布を作成することで、自分の文章のようなランダムテキストを生成できる。
これを用いて、たとえばこれまでの自分のTwitterのポストを解析し、ランダムなトークンから始まるツイートを生成しつぶやくプログラムをcronにスケジューリングすることで、たとえ自分が死んだとしてもサーバの生きる限り自分のような文章をつぶやき続けるbotを作ることができる。
最近、『エンディング・ノート』とか流行ってるから、ソーシャルメディアのエンディング・ボットとか流行らないかな。

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

import nltk
import json
import sys

FILENAME = sys.argv[1]

def generate_model(cfdist, word, num=15):
 for i in range(num):
  print word,
  word = cfdist[word].max()

tokens = json.load(open(FILENAME,"r"))
text = nltk.Text(tokens)
bigrams = nltk.bigrams(text)
cfd = nltk.ConditionalFreqDist(bigrams) 

if __name__ = '__main__':
 print cfd['living']
 # <FreqDist: 'creature': 7, 'thing': 4, 'substance': 2, ',': 1, '.': 1, 'soul': 1>

 generate_model(cfd, 'living')
 # living creature that he said , and the land of the land of the land