zhangbo-NLP-plugin_eng/lib/zhangbo/NLP/engine_cl.pl
#!/usr/bin/perl
use Encode;
#use JSON::XS;
use Clone qw(clone);
use MongoDB;
use Smart::Comments;
use lib "./";
use Conn_mongo_jc;
use Data::Dumper;
use IO::File;
#use Add_info;
# http://poe.perl.org/?POE_Cookbook/TCP_Servers
# Include POE and POE::Component::Server::TCP.
our $hash=();
$hash_mn=();
my $hash_sc=();
use POE qw(Component::Server::TCP);
my $w_c_v;
#all_word_id2();
#exit;
open(FD,"allword.txt");
while(<FD>)
{
$_=~/(.*?)\t(.*)/;
$hash->{$1}=$2;
}
close FD;
our $hash_c_i;
my $col = Conn_mongo_jc->new("classifier", "classifiers");
my $alldata=$col->find();
while(my $one=$alldata->next)
{
my $name=$one->{'name'};
my $id=$one->{'_id'};
$name=encode('utf-8',$name);
$hash_c_i->{$name}->{"dir"}=$id;
#print $name,"name $dir \n";
while(($k,$v)=each(%{$one->{'classes'}}))
{
# while(($k1,$v1)=each($k))
# {
my $cn=encode('utf-8',$v->{'name'});
my $cnid=$v->{'id'};
#print "$k ---- ",$cn, $cnid,"\n";
# }
$hash_c_i->{$name}->{$cnid}=$cn;
}
}
my $n=0;
#our $f=0;
print "server start\n";
open(FD_log,">/tmp/class_.log");
POE::Component::Server::TCP->new(
Alias => "echo_server",
Port => 11215,
ClientInput => sub {
my ($session, $heap, $input) = @_[SESSION, HEAP, ARG0];
#print "Session ", $session->ID(), " got input: ".$input."\n";
#print "Session ", $session->ID(), " got input: ".length($input)."\n";
my $d;
my $randout;
$randout=rand();
my @res=classifier($input);
print "返回@res\n";
#close(FD_log);
#print scalar(@res)," num\n";
$heap->{client}->put("@res");
# $session->yield("shutdown");
$_[KERNEL]->yield("shutdown");
# return;
},
ClientDisconnected => sub {
#print "Client disconnected\n"; # log it
}
#, ClientFlushed => sub {
# my $data_source = $_[HEAP]{file_handle};
# my $read_count = sysread($data_source, my $buffer = "", 65536);
# if ($read_count) {
# $_[HEAP]{client}->put($buffer);
# }
# else {
# print FD_log "------------------error\n";
# $_[KERNEL]->yield("shutdown");
# }
#}
);
POE::Kernel->run;
#$input="坦克,我的希望非常诱人电视里有主持人,那里有大熊猫";
# $input="网络和网民的意见,现在正行驶在通州回北京的高速公路上,十评论员单仁平的文章,标题是“做大众政治焦点,茅于轼的选择”。这篇文章的核心意见是,茅于轼应该做中国社会团结的促进者,不应该";
#$input="致力于宣传 市场万能 剥削有理 汉奸人性 保钓无用 保粮错误替富人说话的茅于轼,今天下午两点在北京海淀翠宫饭店演讲顽强继续。未知海淀区委书记隋振江,宣传部长陈名杰是否到场。外媒问我是否到场,告曰:先参加央视《苦难辉煌》座谈会,或会晚到一会儿。今天要长见识了";
# $input2="茅于轼刚刚吃完胡辣汤和烧饼夹猪头肉。现在正行驶在通州回北京的高速公路上,十五分钟以后要开始腾讯微访谈直播,我和方舟子拟就昨天的热点问题,回答网友的提问。敬请各位网友提示一下,昨天有什么热点问题需要谈一谈?";
#$file=$ARGV[0];
sub classifier
{
$file=$_[0];
chomp($file);
my $class;
my $content;
my $username;
if($file=~/(.*?) (.*?) (.*)/)
{
$username=$1;
$class=$2;
$content=$3;
}
else
{
print "input line error;should be (username) (class) (content)\n";
return "input line error;should be (username) (class) (content)\n";
}
if(exists( $hash_c_i->{$class}))
{
print $hash_c_i->{$class}->{'dir'},"\n";
}
else
{
print "class error;\n";
return "no find the class :$class\n";
}
print "$class $content\n";
#print "file=$file\n";
my $f=0;
my $n=0;
my $kk;
my $thash=();
#my @input_ar=`cat $file`;
my %my_hash;
my %my_hash2;
my %thash;
my $j;
my $rand=rand();
system("scws -I -E -N -d ./dict.utf8.xdb:/home/wuyabo/stanford/dict_user.txt -c utf-8 $content -o /tmp/$rand.sctxt2");
#$cc=`cat /tmp/$rand.sctxt`;
#print $cc,"$rand------";
#exit;
open(FD_scws,"/tmp/$rand.sctxt2");
open(FD_class,">/tmp/$rand.class");
while(<FD_scws>)
#foreach my $in (@output)
{
my $in=$_;
my $article_hash;
#print FD_log "line $in \n";
#print "line $kk $in \n";
chomp($in);
#$input_ar[$f]=$in;
my $n_line;
my $tot;
my @arry=split('\ ',$in);
# while (($k,$v)=each(%my_hash2))
# {
# print FD_hash "$k,$v\t";
# }
# print FD_hash "\n";
foreach my $tp (@arry)
{
if(exists($hash->{$tp}))
{
$article_hash->{$hash->{$tp}}++;
}
}
my $k,$v;
print FD_class "0";
while(($k,$v)=each(%{$article_hash}))
{
print FD_class " $k:$v";
print " $k:$v";
}
print "\n";
print FD_class "\n";
}
close FD_class;
my $shell="cd /var/www/classifier/shell/user/$username/".$hash_c_i->{$class}->{'dir'}."; ./Maketest /tmp/$rand.class /tmp/$rand.class_test";
print "$shell\n";
system("$shell");
my @s_res;
my @resu=`cat /tmp/$rand.class_test`;
foreach my $tline (@resu)
{
my $tclass,$tcn;
my $tn;
if($tline=~/(.*?)\t(.*)/)
{
$tclass=$1;;
$tn=$2;
$tcn=$hash_c_i->{$class}->{$tclass};
push (@s_res,$tcn."\t".$tn."\n");
}
}
#unlink("/tmp/$rand.sctxt2");
#unlink("/tmp/$rand.class_test");
#unlink("/tmp/$rand.class");
@s_res;
}
# }
# );
#$poe_kernel->run();