Last active
December 16, 2015 19:38
-
-
Save hiragram/5486065 to your computer and use it in GitHub Desktop.
あるユーザのツイートを抽出してマルコフ連鎖によって文章を生成
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use Text::MeCab; | |
| use Algorithm::MarkovChain; | |
| use List::Util qw(shuffle); | |
| use Net::Twitter::Lite; | |
| use Encode; | |
| use utf8; | |
| #binmode(STDOUT,":utf8"); | |
| my $t = Net::Twitter::Lite->new( | |
| consumer_key=>'こんしゅーまーきー', | |
| consumer_secret=>'こんしゅーまーしーくれっと', | |
| access_token=>'あくせすとーくん', | |
| access_token_secret=>'あくせすとーくんしーくれっと', | |
| legacy_lists_api=>1 | |
| ); | |
| my $status = $t->user_timeline({screen_name => '元にしたいユーザーのID',count => 200}); | |
| my @tweets ; | |
| my $tmp; | |
| foreach (@$status){ | |
| $tmp = $_->{'text'}; | |
| #print $tmp."\n"; | |
| push(@tweets,$tmp); | |
| } | |
| my $tweet_connected = ""; | |
| @tweets = shuffle @tweets; | |
| foreach(@tweets){ | |
| $tweet_connected .= "$_ "; | |
| } | |
| $tweet_connected =~ s/@\S*//gm; | |
| $tweet_connected =~ s/http:\S*//gm; | |
| #print $tweet_connected; | |
| my $mecab = Text::MeCab->new({}); | |
| my @symbols; | |
| for(my $node = $mecab->parse($tweet_connected); $node; $node = $node->next){ | |
| #print $node->surface; | |
| #print "\n"; | |
| push @symbols , $node->surface; | |
| } | |
| my $chain = Algorithm::MarkovChain->new; | |
| $chain->seed( | |
| symbols=>\@symbols, | |
| longest=>7, | |
| ); | |
| my @newness = $chain->spew( | |
| #length=>int(rand 30)+1, | |
| length=>10, | |
| strict_start=>1, | |
| ); | |
| my $post; | |
| for my $chunk(@newness){ | |
| print $chunk; | |
| $post = $post.$chunk; | |
| #print "\n\n"; | |
| } | |
| $post = decode('utf-8',$post); | |
| $t->update({status=>$post}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment