#!/usr/bin/perl -w use strict; use IO::Socket; use LWP::Simple; use Text::Wrapper; use Term::ANSIColor; my $wrapper = Text::Wrapper->new(columns => 16, body_start => ''); #my $test = get("http://www.alltheweb.com/search?avkw=fogg&cat=img&cs=utf-8&q=meat") || die "$!"; #print "$test\n"; #while($test =~ m/(http[^>]*?\.jpg)'/gsi){ # print "$1\n"; #} #my $wait = ; #open(HOME,">/home/~jgr225/perl/imageurls/meat.urls") or die "$!"; $SIG{'PIPE'} = 'IGNORE'; my @actions; my $text; my @lines; my %diction; my @firstwords; my %advlines; my %advlinelength; my %inverse; my @allfirstwords; my %specificword; my @specificwords; my %specificq; my @specificqs; my @matchingwords; my $lastimageword; my $socketnum = $ARGV[0] || 10188; my $color; $color = "BLUE" if $socketnum eq "10188"; $color = "RED" if $socketnum eq "10212"; (my $aliasnum = $socketnum) =~ s/\d{2}(\d)\d{2}/$1/gsi; system("clear"); collate(); define(); print "Sorting arrays...\n"; @lines = sort @lines if $ARGV[0] && $ARGV[0] eq "sort"; @firstwords = sort @firstwords; print "\n$#lines lines indexed.\n"; print "\nListening on port $socketnum...\n"; my $server = new IO::Socket::INET ( Proto => 'tcp', LocalPort => $socketnum, Listen => '1', Reset => '1' ); unless($server) { #close($socketnum); die "Can't create server on port $socketnum\n"; } $server->autoflush(1); my $handler; while ($handler = $server->accept()) { $handler or die "accept: $!\n"; my $peeraddr = $handler->peeraddr; printf "accept %s\n\n", $handler->peerhost; my $alias = pickname(); print "\n$alias has logged in from " . $handler->peerhost . "\nHello $alias!\n"; transcribe("\n[ $alias($aliasnum) enters ]\n\n"); for(my $w = 0; $w < 4; $w++){ sendout("Hi, $alias. Welcome to Truly Interactive TV!"); } sleep(4); my $line; my $lastoutput; my $firstword; my $lastline = 1; my @array; my $linecount; my @specialwords; my $lastphrase; while(defined ($line = <$handler>)){ @array = @firstwords; #my @advlines = split(/\|/,$advlines{$array[$line]} unless $firstword; @array = split(/\|/,$advlines{$firstword}) if $firstword; #@array = sort @array; #if($firstword){ # srand; # my @new = (); # for( @array ){ # my $r = rand @new+1; # push(@new,$new[$r]); # $new[$r] = $_; # } # @array = @new; #} #if(@matchingwords) { unshift(@array,"[related]") unless $firstword; } #my @special; my %seen3; my $relcount; my $relqcount; my @openers = qw(HELLO HI HEY); #unshift(@array,@openers) unless @matchingwords || $firstword; if($linecount && $linecount == 1 && @matchingwords) { undef $advlines{"[related]"}; foreach(@matchingwords){ next if length($_) < 4; #print "$specificword{$_}\n"; $advlines{"[related]"} .= $specificword{$_} unless $specificword{$_} eq $lastphrase; #$advlines{"[related] $_"} .= $specificword{$_}; #next if length($_) < 4; #print "$_: $specificword{$_}\n"; #my @new = split(/\|/,$specificword{$_}); #foreach(@new) { # push(@special,$_) unless $seen3{$_}; # $seen3{$_}++; #} } #print $advlines{"[related]"} . "\n"; my @ed = split(/\|/,$advlines{"[related]"}) if($advlines{"[related]"}); undef $advlines{"[related]"}; #undef $advlines{"[questions]"}; foreach(@ed){ next unless $_; next if $seen3{$_}; if(m/\?$/gsi) { #print "q: $_\n"; $advlines{"[questions]"} .= "$_|"; $relqcount++; } unless($advlines{"[related]"}) { $advlines{"[related]"} = "$_|"; } else { $advlines{"[related]"} .= "$_|"; } $seen3{$_}++; $relcount++; } #shuffle("[related]"); $advlinelength{"[related]"} = $relcount; $advlinelength{"[questions]"} = $relqcount; $advlinelength{"[actions]"} = $#actions; } #@specialwords = qw(HELLO) unless $firstword || $lastoutput; @array = @actions if $firstword && $firstword eq "[actions]"; @specialwords = reverse sort @specialwords; #print $advlines{"[related]"} . "\n" if $advlines{"[related]"}; unless($firstword) { unless(@matchingwords){ unshift(@array,@openers); } else { unshift(@array,"NO"); unshift(@array,"YES"); } foreach(@specialwords) { #print "$_ " . length($advlines{$_}) . "\n"; next if length($advlines{$_}) > 2000; #print "$_\n"; unshift(@array,$_); } unshift(@array,"[related]") if($advlines{"[related]"}); unshift(@array,"[questions]") if($advlines{"[questions]"}); unshift(@array,"[actions]") if @matchingwords; } else { # srand; # my @new = (); # for( @array ){ # my $r = rand @new+1; # push(@new,$new[$r]); # $new[$r] = $_; # } # @array = @new; } #if($advlines{"[related questions]"}) { unshift(@array,"[related]") unless $firstword; } unshift(@array," "); undef @specialwords if $firstword; #@array = (@specialwords,@array) if @specialwords; #undef @specialwords; $line =~ s/\D*(\d*)\D*/$1/gsi; my $flag = $line; #$line = $line || $lastline; if($line && $line == $lastline){ $linecount ++; } else { $linecount = 0; } next if $linecount > 3; $line = $line || $lastline; #$line = $line - 1 if $line > 1 && $linecount == 3; #print STDOUT "$line/$lastline/$linecount : $array[$line]\n"; my $output; $output .= "$firstword " if $firstword && $firstword ne "[related]" && $firstword ne "[questions]" && $firstword ne "[actions]"; $line = $#array if $line > $#array; $output .= $array[$line]; #my @test = $advlines{$array[$line]}; #print "$array[$line]\n"; #my @advlines = split(/\|/,$advlines{$array[$line]}) unless $firstword; $output .= " ($advlinelength{$array[$line]})" unless $firstword; #$firstword = $firstwords[$line]; #for(my $x = length($output); $x < 66; $x++){ # $output .= " "; #} if($flag) { #print $handler "xx$output"; #print "sent $line ($output)\n"; } else { if($firstword){ print "$alias (" . $handler->peerhost . "): $lastoutput\n"; unless($lastoutput =~ m/^\[/gsi){ transcribe("$alias($aliasnum): $lastoutput\n"); } else { transcribe("\n$lastoutput\n\n"); } my $lastsaid; open(LAST,"last.txt"); while(){ #print "$_\n"; chomp; $lastsaid .= $_; } close(LAST); $lastsaid .= " $lastoutput"; print "$lastsaid\n"; #my @imagewords; #while($lastsaid =~ m/([A-z\-']*?)[\s,\.\?!]/gsi){ # next unless $specificword{$1}; # print "$1 (" . length($specificword{$1}) . ")\n"; #rint $specificword{"HELLO"}; #next if length($specificword{$1}) > 2000; #} #getimage($lastsaid); #print "$lastoutput\n"; ink($lastoutput); foreach(@allfirstwords) { my $test = $_; if($lastsaid =~ m/[^A-Z'-]$test[^A-Z'-]/gsi){ push(@specialwords,$_); #print "$_\n"; #push(@specialwords,$inverse{$_}) if $inverse{$_}; #print "$_\n"; } } undef $advlines{"[related]"} if $firstword eq "[related]"; undef $advlines{"[questions]"} if $firstword eq "[questions]"; undef $firstword; undef @matchingwords; foreach(@specificwords) { my $test2 = $_; if($lastsaid =~ m/\W$test2\W/gsi){ push(@matchingwords,$_); } } srand; my @new = (); for( @actions ){ my $r = rand @new+1; push(@new,$new[$r]); $new[$r] = $_; } @actions = @new; #$firstword = $array[$line]; } else { $firstword = $array[$line]; shuffle($firstword); shuffle("[related]"); #srand; #my @new = (); #for( @actions ){ # my $r = rand @new+1; # push(@new,$new[$r]); # $new[$r] = $_; #} #@actions = @new; } } #for(my $e = 0; $e < 2; $e++){ print $handler getready($output); #next START; #} #print "sent $line ($output)\n"; #print $wrapper->wrap($output); $lastoutput = $output; unless($firstword){ $lastphrase = $array[$line]; } else { #ink($lastphrase); } $lastline = $line; } print "Bye $alias!\n"; transcribe("\n[ $alias($aliasnum) leaves ]\n\n"); undef @matchingwords; } sub collate { dictionary(); my %seen; open(TEXT,"transcript.txt") or die "$!"; print "Collating lines...\n"; my $totaltext; TEXT: foreach(){ chomp; $_ = uc($_); if(m/\[(.*?)\]/gsi){ my $keep = "yes"; my $action = $1; $action =~ s/^\s*//gsi; $action =~ s/SFX: //gsi; $action =~ s/\s*$//gsi; $action .= " "; while($action =~ m/([A-z\-']*?)[,\s\.\?!]/gsi){ #print "$1\n"; my $testing = $1; $keep = "no" unless $diction{$testing} || $testing =~ m/ing$/gsi || $testing =~ m/s/gsi; } $action = "[ $action]"; push(@actions,$action) if $keep eq "yes"; #print "$action $keep\n" if $keep eq "no"; } my $testtext = $_; #print "$_:\n"; # while($testtext =~ m/([A-z\-']*?)[\s\.]/gsi){ # print "$1 ...\n"; # next TEXT unless $diction{$1}; # print " OK.\n"; # } #print "OK\n"; #s/^(>>)?\s*//gsi; #s/>//gsi; s/.\[m//gsi; next unless m/\w/gsi; s/\s*$//gsi; s/\[.*?\]//gsi; s/\(.*?\)//gsi; s/\s\s\s/ /gsi; s/\s\s/ /gsi; s/([A-Z])\.([A-Z])\.([A-Z])\.([A-Z])\./$1$2$3$4/gsi; s/([A-Z])\.([A-Z])\.([A-Z])\./$1$2$3/gsi; s/([A-Z])\.([A-Z])\./$1$2/gsi; s/MR\./MR/gs; s/MRS\./MRS/gs; s/MS\./MS/gs; s/\.\.\./\./gsi; next if m/o\/~\s/; #ext if m/[<>]/gsi; my $testtext = $_; #print "Original: $testtext\n"; #while($testtext =~ m/([A-z\-']*?)[\s\.\W]/gsi){ # print "$1 ...\n"; # my $testword = $1; # next unless $testword =~ m/\w/gsi; #(my $testwords = $testword) =~ s/s$//gsi; #print "#$testword#\n" unless $diction{$testword}; #next TEXT unless($diction{$testword} || $testword =~ m/ED$/gs || $testword =~ m/MEN$/gs || $testword =~ m/S$/gs); # next TEXT unless($diction{$testword}); # print " OK.\n"; #} #next unless $_; #print "Passed: $_\n"; $text .= "$_ "; } close(TEXT); #print $text; #my $wait = ; $advlines{"[actions]"} = $#actions; print "$#actions indexed.\n"; @actions = sort @actions; # while($totaltext =~ m/(\[[^\]]*?\])/gsi){ # print $1; # } print "Cleaning text...\n"; TEXT: while($text =~ m/\s([A-Z][A-Z\-,"\s'&]*[\.\?!])/gs){ #while($text =~ m/([^\.\?!]*[\.\?!])/gs){ my $rawline = $1; #print "\nOriginal: $rawline\n"; #$rawline =~ s/^m\W*//gsi; #$rawline =~ s/^m\s//gsi; $rawline =~ s/^\W*([A-Z])/$1/gs; $rawline =~ s/^.*?: //gs; $rawline =~ s/\s\s/ /gsi; $rawline =~ s/\s\s/ /gsi; #$rawline =~ s/[^[A-z]\-",\.]//gsi; if($rawline =~ m/^[^"]*"[^"]*$/) { $rawline = "$rawline\""; } #print "Too long!\n" if length($rawline) > 63; my $testline = getready($rawline); next if length($testline) > 68; #next if($rawline =~ m/\WM\W/gsi); while($rawline =~ m/([A-z\-']*?)[,\s\.\?!]/gsi){ #print "$1 ...\n"; my $testword = $1; next unless $testword =~ m/\w/gsi; next TEXT if $testword =~ m/^[^AIO]$/gsi; if($testword =~ m/^(.*)S$/gsi){ next TEXT unless($diction{$1}); }elsif($testword =~ m/^(.*)ED$/gsi){ next TEXT unless($diction{$1}); }else{ #next TEXT unless($diction{$testword}); #(my $testwords = $testword) =~ s/s$//gsi; #print "#$testword#\n" unless $diction{$testword}; #next TEXT unless($diction{$testword} || $testword =~ m/ED$/gs || $testword =~ m/MEN$/gs || $testword =~ m/S$/gs); next TEXT unless($diction{$testword}); } #print " OK.\n"; } #print "Passed: $rawline\n"; #my $wait = ; push(@lines,$rawline) unless($seen{$rawline}); $seen{$rawline} = "y"; } my @lines2 = @lines; my %seen1; print "Choosing first words...\n"; LINE: foreach(@lines){ if(m/^(.*?,?)"?\s(.*)$/gsi){ my $found = $1; my $text = $2; $found =~ s/,$//gsi; $seen1{$found}++; push(@firstwords,$found) if $seen1{$found} == 100; push(@allfirstwords,$found) if $seen1{$found} == 1; $advlines{$found} .= "$text|"; } } my %seen2; print "Creating meta-word lists...\n"; foreach(@lines2){ my $line = $_; if($line =~ m/[^A-Z']([A-Z].*?)"?\s/gsi){ my $word = $1; $seen2{$word}++; $specificword{$word} .= "$line|"; #if($line =~ m/\?$/gsi){ #print "$word $line\n"; # $specificq{$word} .= "$line|" if m/\?$/gsi; #} #push(@specificqs,$1) if $seen2{$1} == 1; push(@specificwords,$word) if $seen2{$word} == 1; #print "$1\n"; } } foreach(@allfirstwords) { my @advlines = split(/\|/,$advlines{$_}); $advlinelength{$_} = $#advlines + 1; } my %seenact = (); @actions = grep { ! $seenact{$_} ++ } @actions; } sub dictionary { print "Building dictionary list ...\n"; my $definitions; #open(WORDS,"/home/jgr225/poetry/mpos/mobyposi.i") || die "$!"; #$definitions .= $_ foreach(); #close(WORDS); #my @defs = split(/[\n\r]/,$definitions); #DEF: foreach(@defs) { # if(m/^(.*?)\W(\w)\w*?$/gsi){ # my $word = uc($1); # next DEF if $word =~ m/^[^AIO]$/gsi; # $diction{$word} = "y"; # } #} my $wordcount; my @dics = ("unabr.dic","D8.DIC","6of12.txt","ALLUP&R.DIC"); foreach(@dics){ print "$_\n"; open(WORDS,"/home/jgr225/perl/$_") || die "$!"; my $dic = $_; foreach() { #print "$_\n" if m/gonna/gsi; chomp; my $word = uc($_); $word =~ s/\W*$//gsi; #print "$word\n"; next if $word =~ m/^[^AIO]$/gsi; next if $diction{$word}; $diction{$word} = "y" unless $diction{$word}; #print "$word\n" if $word =~ m/gonna/gsi; $wordcount++; } close(WORDS); } print "$wordcount words indexed.\n"; #my $wait = ; } sub define { $inverse{"YOU"} = "I"; $inverse{"I"} = "YOU"; } sub transcribe { #if($_[0] =~ m/^\[/gsi){ # $_[0] =~ s/\w*?: //gsi; #} open(OUT,">>text.txt"); print OUT "$_[0]"; close(OUT); #while($_[0] =~ m/([A-Z'-]*?)\s/gsi){ #print "$1\n"; #} #while($_[0] =~ m/([A-z\-']*?)[\s,\.\?!]/gsi){ # next unless $specificword{$1}; # print "$1 (" . length($specificword{$1}) . ")\n"; #rint $specificword{"HELLO"}; #next if length($specificword{$1}) > 2000; # } } sub pickname { print "Indexing names...\n"; my @names; open(NAMES,"names.htm") || die "$!"; my $count = 1000; foreach(){ while(m/([A-Z].*?)<\/TD>/gsi){ next NAME if length($1) > 12; for(my $c=$count; $c > 0; $c--){ push(@names,uc($1)); } $count = $count - 1; } } close(NAMES); return $names [ rand @names ]; } sub sendout { #if(m/^\[/gsi){ # $_[0] =~ s/\w*?: //gsi; #} print $handler getready($_[0]); } sub getready { my $text = $_[0]; my $ready; $text = $wrapper->wrap($text); while($text =~ m/(.*?)\n/gsi){ $ready .= $1; for(my $d = 16; $d > length($1); $d--){ $ready .= " "; } } for(my $x = length($ready); $x < 66; $x++){ $ready .= " "; } #print length($ready) . "\n"; $ready = "xx$ready"; return $ready; } sub ink { open(OUT,">last.txt"); print OUT $_[0]; close(OUT); } sub getimage { my @imagewords; while($_[0] =~ m/([A-z\-']*?)[\s,\.\?!]/gsi){ next unless $specificword{$1} && length($specificword{$1}) < 5000 && length($specificword{$1}) > 20; push(@imagewords,$1); print "$1 (" . length($specificword{$1}) . ")\n"; #rint $specificword{"HELLO"}; #next if length($specificword{$1}) > 2000; } return unless @imagewords; my $imageword = $imagewords [ rand @imagewords ]; print "Searching for \"$imageword+$lastimageword\" image\n"; $imageword = lc($imageword); my $results; my @images; #if(-e "/home/jgr225/perl/imageurls/$imageword.urls"){ # print "Already indexed...\n"; # open(IMAGEFILE,"/home/jgr225/perl/imageurls/$imageword.urls") || die "1: $!"; # foreach(){ # push(@images,$_); # } # close(IMAGEFILE); # # } else { print "Looking on the web...\n"; $results = get("http://www.alltheweb.com/search?avkw=fogg&cat=img&cs=utf-8&q=$imageword+$lastimageword") || die "$!"; #print "$results\n"; while($results =~ m/(http[^>]*?\.jpg)'/gsi){ push(@images,$1); } open(IMAGEFILE,">/home/jgr225/perl/imageurls/$imageword.urls") || die "2: $!"; print IMAGEFILE "$_\n" foreach(@images); close(IMAGEFILE); # } my $image = $images [ rand @images ]; open(OUTPUT,">/home/jgr225/public_html/tv.html") || die "3: $!"; print OUTPUT "
$imageword+$lastimageword
"; close(OUTPUT); print "...written.\n"; $lastimageword = $imageword; } sub shuffle { #print "$advlines{$_[0]}\n"; my @array = split(/\|/,$advlines{$_[0]}); srand; my @new = (); for( @array ){ my $r = rand @new+1; push(@new,$new[$r]); $new[$r] = $_; } @array = @new; undef $advlines{$_[0]}; foreach(@array){ $advlines{$_[0]} .= "$_|"; } }