Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created July 18, 2010 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/480069 to your computer and use it in GitHub Desktop.
Save hitode909/480069 to your computer and use it in GitHub Desktop.
tilyさんのforkして,twitterにポストするようにした
// ==UserScript==
// @name AutoGhiblize
// @namespace http://d.hatena.ne.jp/tily
// @include http://faithrm-zero-zero.blog.so-net.ne.jp/archive/*
// @require http://www.chasen.org/~taku/software/TinySegmenter/tiny_segmenter-0.1.js
// ==/UserScript==
//----[Markov]---------------------------------------
var Markov = function() {
this.chain = {}
this.segmenter = new TinySegmenter()
}
Markov.prototype = {
saveAsChain: function(sentence) {
var words = this.segmenter.segment(sentence)
this.push('BOS', words[0])
for(var i = 0; i < words.length - 1; i++) {
this.push(words[i], words[i+1])
}
this.push(words[i], 'EOS')
},
push: function(first, second) {
if(!this.chain[first]) {
this.chain[first] = [second]
} else {
this.chain[first].push(second)
}
},
generate: function() {
var sentence = ''
var word = this.next('BOS')
while(word != 'EOS') {
sentence += word
word = this.next(word)
}
return sentence
},
next: function(word) {
var nexts = this.chain[word]
return nexts[this.rand(nexts.length)]
},
rand: function(num) {
return Math.floor(Math.random() * num)
}
}
//----[Main]-----------------------------------------
var div = document.querySelector('div.articles-body')
var lines = div.textContent.split("\n")
lines = lines.filter(function(e) { return e != '' })
var actors = []
var markov = new Markov()
for(var i = 0; i < lines.length; i++) {
lines[i].match(/(.+)「(.+)」/)
var actor = RegExp.$1, sentence = RegExp.$2
if(actor && sentence) {
markov.saveAsChain(sentence)
actors.push(actor)
}
}
var iframe = document.createElement('iframe')
iframe.name = 'hidden-iframe'
iframe.style.display = 'none'
document.body.appendChild(iframe)
var form = document.createElement('form')
form.action = 'http://twitter.com/statuses/update.xml'
form.method = 'post'
form.target = 'hidden-iframe'
document.body.appendChild(form)
var input = document.createElement('input')
input.type = 'hidden'
input.name = 'status'
form.appendChild(input)
var post = function() {
var actor = actors[markov.rand(actors.length)]
var sentence = markov.generate()
var line = actor + '「' + sentence + '」'
if(window.Minibuffer) { window.Minibuffer.message(line, 3000) }
if(console) { console.log(line) }
input.value = line
form.submit()
}
post();
setInterval(function() {
post();
}, 60 * 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment