zhangbo-NLP-plugin_eng/lib/zhangbo/NLP/chat_server.pl
#!/usr/bin/perl
use POSIX;
use Smart::Comments;
our $jieba="/usr/local/lib//python2.7/dist-packages/jieba/dict.txt";
our $ch2n="/mnt/sdb/shell2/chnumtoa.pl";
our $shangxia="/mnt/sdb/shell2/client_shang9991.pl";
our $key_word="/mnt/sdb/shell2/test_key.pl";
our $ner="/mnt/sdb/shell2/NER.pl";
our $showtop="/mnt/sdb/shell2/client_gongan.pl";
our $iqa="/mnt/sdb/shell2/client_qa.pl";
our $cal="/mnt/sdb/shell2/cal.pl";
our $funtalk="/home/htc/stanford-parser-full-2014-06-16/htc/because_so/client_because.pl";
our $funtalk2="/mnt/sdb/shell2/client_tfidf.pl";
#our $funtalk2="/mnt/sdb/shell2/client_talk.pl";
our $zj="cd /home/htc/nord/whoosh/vm/;./whoosh_direct_import.py";
our @talk_add=`cat talk_add.txt`;
#use POE qw(Component::Server::TCP);
use Clone qw(clone);
use IO::Socket;
use threads;
use threads::shared;
use IO::Socket::INET;
use Socket;
use IO::Handle;
use IO::Select;
use IO::Socket::INET;
use IO::Socket;
use Data::Dumper;
use lib "./";
use plugin_eng;
use POE qw(Component::Server::TCP);
#use JSON::XS;
use Clone qw(clone);
use Encode;
our @rewrite_g;
our @rewrite_l;
our @rewrite_n;
our %rewrite_all;
our $rewrite_all;
our $qahs;
#实体的自创立
#切词词典
#相似度词典
#玩无树词典
#做问答的筛选,主动筛选,主人启动的筛选
#规则的学习 凡是XXX,都是
# 如果XXX,那么
#
# Include POE and POE::Component::Server::TCP.
our $hash=();
our $hashid=();
$hash_mn=();
my $hash_sc=();
#open(FD_log, ">/tmp/engine_ask.txt");
#open(FD, "zhidao_qa.txt");
my $sytmp;
#our %allr;
our %allr :shared;
our @allr_res :shared;
#our $iques :shared;
#print "join yg reg\n";
#my $ygid = threads->new("yinguo","yinguo",$input,$uid,$pid);
#$ygid->detach;
open(FDq,"./qahs.txt");
my $q;
while(<FDq>)
{
my $line=$_;
chomp($line);
my $a;
if($line=~/^q:(.*)/)
{
$q=$1;
chomp($q);
}
elsif($line=~/^a:(.*)/)
{
$a=$1;
chomp($a);
push(@{$qahs->{$q}},$a);
print "记录 $q =》 $a\n";
}
}
close FDq;
POE::Component::Server::TCP->new(
Alias => "echo_server",
Port => 18888,
ClientInput => sub {
#my %allr;
#my %allr :shared;
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 $pid=$session->ID();
$allr{$pid}=0;
$allr{"n"}=0;
my $d;
my $d_seq;
if(length($input)<3){
$_[KERNEL]->yield("shutdown");
return;
}
my $uid;
print "input $input\n";
if($input=~/(.*?) (.*)/)
{
$uid=$1;
$input=$2;
}
if(length($uid)<1)
{$uid=0;}
my $process_list=whatdoing($input,$uid,$pid);
#print $allr_res[$pid],"debug $pid-------\n";
print @{$process_list}[$np]."\n";
my @child_thread;
#print "$uid |$input \n";
my $hasqa=0;
for(0 .. scalar(@{$process_list})-1)
{
my $np=$_;
print "join ".@{$process_list}[$np]." $np\n";
$child_thread[$np] = threads->new(@{$process_list}[$np],@{$process_list}[$np],$input,$uid,$pid);
$child_thread[$np]->detach;
if( @{$process_list}[$np] =~/^q|^a/)
{$hasqa=1;}
}
my $witet=scalar(@{$process_list});
print "pid $allr{$pid}-----totaltime $witet+$hasqa----\n";
while($allr{$pid}<($witet+$hasqa))
{
sleep 1;
# print "0000sleep $allr{$pid}\n";
$allr{$pid}++;
}
my @res;
print $allr_res[$pid],"debug2-$pid------\n";
open(FDans,">>/tmp/allsay_$uid.dic");
print FDans $allr_res[$pid],"\n";
close FDans;
$heap->{client}->put($allr_res[$pid]);
#$heap->{client}->put(@{$allr_ress[$pid]});
#again 继续思考
$_[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;
print "server start\n";
sub whatdoing
{
my $in=$_[0];
my $uid=$_[1];
my $pid=$_[2];
my @mres;
my $one=sure($in);
print "$one sureone\n";
if(length($one)>1)
{
$allr_res[$pid]="ans:dirct $one";
return "";
}
#if($in=~/^我要|^我想|^帮我/)
#{
# push(@mres,"Iwish");
# return \@mres;
#}
if(($in=~/什么/)&&(length($in)<12)||($in=~/刚刚说什么/)||($in=~/再说一次|遍/)||($in=~/没听清/))
{
push(@mres,"repet");
return \@mres;
}
my $lastat=getlast($uid);
if($lastat=~/^wait_ans/)
{
print "主动聊天模式\n";
push(@mres,"ask_master");
return \@mres;
}
if($in=~/心情|觉得|说|真|有点|认为|感觉|想/)
{
push(@mres,"semtic");
}
my $noen=0;
if($in=~/如果|因为|所以|因此|就|因为/)
{
$noen=1;
push(@mres,"learn_because");
}
if($in=~/关系|联系|关联|有关/)
{
push(@mres,"between");
}
if($in=~/^是$|是的$|^对|请忘记/)
{
push(@mres,"forgot");
}
elsif(($in=~/\?|\?|多远|多少|你知道|请问|什么|几|谁|哪里|哪|啥|吗|怎么|怎样|多大|多高|多重|嘛|么$/)&&($in !~/请记住/))
{
#代数处理系统|
if(($in=~/多少|几|多/)&&($in=~/本|斤|桶|元|两|吨|个|件|元|米|块|尺|寸|厘|毫|克|个|匹|头|条|只|套|件|张|次|台|面|捆|斤|吨|辆|扇|栋|片|扇/)&&($in !~/哪个|面积|体积/))
{
if($in=~/累计|合计/)
{
push(@mres,"algebra_count");
}
else{
push(@mres,"algebra");
}
}
elsif($in=~/^求|面积/)
{
push(@mres,"algebra_equ");
}
elsif($in=~/加|减|乘|除/)
{
push(@mres,"cal");
}
else
{
}
push(@mres,"qa");
# push(@mres,"qaf");
}
else
{
if($noen==1)
{}
else{
push(@mres,"learn_en");
}
push(@mres,"qa_reg");
push(@mres,"learn_equa");
push(@mres,"record");
#push(@mres,"fun2");
#push(@mres,"fun3");
#push(@mres,"ask_master");
#@mres=("fun","fun2","command","learn_en","record");
}
#对文本找合适的函数库
my @othery=glob("./qa_*.dic");
for(0 .. scalar(@othery)-1)
{
my $tc=$_;
if($othery[$tc]=~/qa_(.*).dic/)
{
my $func=$1;
my $inpf=rand();
open(FD,">/tmp/$inpf");
print FD $in;
close FD;
my @conf=`$ner /tmp/$inpf $othery[$tc]`;
chomp($conf);
if($conf[0]=~/.*?-.*?-(.*?)-(.*)/)
{
my $n=$1;
my $aameq=$2;
chomp($sameq);
if($n > 0.4)
{
push(@mres,"$func");
}
}
}
}
#对文本找合适的答案库
my @othery=glob("./qans_*.dic");
for(0 .. scalar(@othery)-1)
{
my $tc=$_;
if($othery[$tc]=~/qans_(.*).dic/)
{
my $func=$1;
my $inpf=rand();
print "faq $func\n";
open(FD,">/tmp/$inpf");
print FD $in;
close FD;
my @conf=`$ner /tmp/$inpf $othery[$tc]`;
if($conf[0]=~/.*?-.*?-(.*?)-(.*)\n/)
{
my $n=$1;
my $sameq=$2;
chomp($sameq);
if($n > 0.4)
{
# push(@mres,"$func");
# $allr_res[$pid] ="ans:file ";
#$allr_res[$pid] ="ans:file $sameq";
# print $allr_res[$pid]."000000000000000\n";
$allr_res[$pid] ="ans:file $sameq\n";
# print $allr_res[$pid]."000000000000000\n";
}
}
}
}
return \@mres;
}
sub repet
{
my $all=$_[1];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $lasts=`tail -5 /tmp/allsay_$uuid.dic`;
$allr_res[$ppid] .="ans:repet $lasts\n";
}
sub sure
{
my $all=$_[0];
my $res;
chomp($all);
print " qahs $all \n";
if(exists($qahs->{$all}))
{
my $tr=rand(10);
my $tall=scalar(@{$qahs->{$all}});
my $t=$tall*$tr/10;
$t=ceil($t)-1;
$res=@{$qahs->{$all}}[$t];
}
return $res;
}
sub Iwish
{
my $all=$_[1];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
#推荐
#搜索
#建议
#闲聊
#获取实体
#获取谓词/动词
#赞扬函数
}
sub ask_master
{
my $all=$_[1];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $rand=rand();
my $lastat=getlast($uuid);
my $tp;
if(! -e "/tmp/status_$uuid.txt")
{my $c=`touch /tmp/status_$uuid.txt`;
system("echo \"------\" >> /tmp/status_$uuid.txt");
}
my $ra=rand();
print "lastat 状态显示 $lastat\n";
if($lastat =~/^---/)
{
my $rand=rand();
if($rand>0.4)
{
open(FDb,">>/tmp/status_$uuid.txt");
print FDb "nothing\n";
close FDb;
}
}
if($lastat eq "nothing")
{
$tp .="ans:master 我可以问个问题吗?";
open(FDb,">>/tmp/status_$uuid.txt");
print FDb "wait_allow\n";
close FDb;
}
elsif($lastat eq "wait_ans")
{
my $lastque=getlastque($uuid);
if(1)
{
save_q_a($fin,$lastque);
open(FDb,">>/tmp/status_$uuid.txt");
print FDb "--------\n";
close FDb;
$tp .="ans:master ok";
}
}
elsif($lastat eq "wait_allow")
{
if($fin=~/可|是|行|好|ok|OK/)
{
print "开始提问\n";
}
else
{
open(FDb,">>/tmp/status_$uuid.txt");
print FDb "--------\n";
close FDb;
return;
$tp .="ans:master 哦";
}
my $type="";
#问题类型
print "wait_allow ----\n";
my $myask=getoneque($uuid,$type);
open(FDb,">>/tmp/askmaster_$uuid.txt");
print FDb "$myask\n";
close FDb;
open(FDb,">>/tmp/status_$uuid.txt");
print FDb "wait_ans\n";
close FDb;
$tp .="ans:master $myask";
}
else
{
print "else $lastat----------\n";
}
$allr_res[$ppid] .="$tp\n";
}
sub save_q_a
{}
sub getoneque
{
my $uuid=$_[0];
my $res;
my $type=$_[1];
my $rq=rand();
if ($rq> 0.5)
{
my $ran2=`tail -5 /tmp/chat_$uuid.dic`;
$ran2=~s/\n//gis;
open(FDm,">/tmp/$rq");
print FDm "$ran2";
close FDm;
my $hb=`$ner /tmp/$rq hobby.dic`;
chomp($hb);
print "rand que $hb $ran2 |$ner /tmp/$rq hobby.dic\n";
if($hb=~/\d-\d+-.*?-(.*)/)
{
$res="你喜欢 $1 ?";
}
#unlink("/tmp/$rq");
}
else
{
my $near=`tail -6 /tmp/chat_$uuid.dic`;
$near=~s/\n//gis;
$near=~s/ //g;
# my $trand=rand();
my $guest_hobby=key_word($near);;
my @ag=split(" ",$guest_hobby);
$res="你能说说关于 $ag[0]的事情吗?";
}
print " get one que $res\n";
$res;
}
sub getlastque
{
my $id=$_[0];
my $lasts=`tail -1 /tmp/askmaster_$id.txt`;
chomp($lasts);
$lasts;
}
sub getlast
{
my $id=$_[0];
my $lasts=`tail -1 /tmp/status_$id.txt`;
chomp($lasts);
$lasts;
}
sub algebra_equ
{
my $all=$_[1];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $trand=rand();
open(FDq,">/tmp/$trand.2");
open(FDqr,"/tmp/chat_$uuid.fun");
print FDq $fin;
print FDq "\n-------\n";
while(<FDqr>)
{
my $line=$_;
print FDq "$line";
print "输历史公式$line\n";
}
close FDq;
my $rline=`cat /tmp/$trand.2`;
my $most=`$showtop /tmp/$trand.2 showtop 1`;
chomp($most);
print "most $most|$showtop /tmp/$trand.2 showtop 1|$rline\n";
my $goodfun;
my $rd5=rand();;
my $hastj;
if($most=~/1-.*?-.*?-(.*)/)
{
$goodfun=$1;
#底 $v0 && 高 $v1 | $v0 nu && $v1 nu => 三角形面积 f( $v0 * $v1 / 2
if($goodfun=~/(.*?)\|/)
{
my $tj=$1;
my @cj=split("&&",$tj);
my $talln=scalar(@cj)-1;
my @cjs;
foreach my $one (@cj)
{
my @tarr=split(" ",$one);
foreach my $j (@tarr)
{
if($j=~/\$/)
{next;}
$j=~s/\*//g;
push(@cjs,$j);
print " $j tttttttttttttttjjjjjjjjjj\n";
}
}
# my $nd=`tail -$sl /tmp/chat_$uuid.dic > /tmp/$rd5`;
my @c=`cat /tmp/chat_$uuid.dic`;
my @cc;
my $fulltj=0;
my $st=scalar(@c)-1;
## $st
for(0 .. $st)
{
my $nli=$_;
print "$nli nlinlinlinlinli\n";;
my $ns=$c[$st-$nli];
my $have_tj=0;
## @cjs
for(0 .. scalar(@cjs)-1)
{
my $tj=$cjs[$_];
print "tjtj $tj\n";
if($tj=~/\$/)
{
next;
}
if(($ns=~/$tj是/) ||($ns=~/$tj为/)||($ns=~/$tj\d/))
{
print "搜索历史聊天中的条件 $tj $ns\n";
$have_tj=1;
if(! exists($hastj->{$tj}))
{
$talln--;
$hastj->{$tj}=1;
}
};
if($talln <0)
{$fulltj=1;}
}
if($have_tj==1)
{
push(@cc,$ns);
}
if($fulltj==1)
{last;}
}
open(FDrd,">/tmp/$rd5");
foreach my $more (@cc)
{
my @tj=parser_tj($more);
for(0 .. scalar(@tj)-1)
{ print FDrd $tj[$_],"\n";
print "过滤后的条件 $tj[$_]\n";
}
}
close FDrd;
}
}
open(FDq,">/tmp/$trand.3");
print FDq $goodfun,"\n";
if(-e "/tmp/chat_$uuid.same")
{
my $nd=`cat /tmp/chat_$uuid.same`;
print FDq $nd."\n";
}
close FDq;
my $ss=`cat /tmp/$rd5`;
print "cat $ss\n";
my $s=`/mnt/sdb/shell2/art_res.pl /tmp/$trand.3 /tmp/$rd5 /tmp/$trand 1>/dev/null`;
print "推理结束、n\n";
print "_equ /mnt/sdb/shell2/art_res.pl /tmp/$trand.3 /tmp/$rd5 /tmp/$trand 1>/dev/null";
my $res_tmp= `cat /tmp/$trand`;
$allr_res[$ppid] .="ans:algebra_equ $res_tmp\n";
print "运算结束、n\n";
unlink("/tmp/$trand");
}
sub between
{
my $all=$_[1];
my $fin=$_[1];
my $uid=$_[2];
my $ppid=$_[3];
$fin=~s/关系//;
$fin=~s/之间//;
$fin=~s/什么//;
$fin=~s/关联//;
$fin=~s/联系//;
$fin=~s/的//;
my $allen=all_en($fin);
print "$allen allen\n";
#my $w=key_word($allen);
#print "$w key_word\n";
#my $a,$b;
my $w=$allen;
my $rall;
if(length($w)>3)
{
my @list=split(" ",$w);;
$a=$list[0];
$b=$list[1];
my $rand=rand();
open(FD,">/tmp/$rand");
print FD "$a\n--------------\n$b\n";
close FD;
my $lres=`$showtop /tmp/$rand simple_path`;
my @all2;
my @all=split(" ",$lres);
foreach my $oneli (@all)
{
if($oneli eq "same")
{
next;
}
push(@all2,$oneli);
}
$rall=join("",@all2);
unlink("/tmp/$rand");
}
print "rall $rall-----------------))))))))))))))))))))\n";
my $sw=key_word($rall);
my @asw=split(" ",$sw);
my $tp;
$tp .="ans:between $asw[0],$asw[1],$asw[2]";
$allr_res[$ppid] .="$tp\n";
}
sub algebra_count
{
my $all=$_[1];
my $fin=$_[1];
my $uid=$_[2];
my $ppid=$_[3];
#取出 未知条件 所有实体和数量
# my $w=plugin_eng::seg_txt($fin);
my ($wen,$what)=want_en($fin);
#需要全部替换成名词
my $tp .="ans:algebra_count ";
# $tp .=`$cal /tmp/$rnd`;
# chomp($tp);
#输出结果
$allr{$ppid}++;
## 加速1秒 algebra
my @alr=split(" ",$wen);
my $rd=rand();
for(0 .. 0)
#for(0 .. scalar(@alr)-1)
{
my $nown=$_;
my @n=`grep $alr[$nown] /tmp/chat_$uid.dic > /tmp/$rd`;
}
my $n=`cat /tmp/$rd`;
open(FD,"/tmp/$rd");
my $funa;
while(<FD>)
{
my $line=$_;
my $rc=rand();
open(FDw,">/tmp/$rc");
print FDw $line;
close FDw;
my $nn=`$ner /tmp/$rc numu`;
unlink("/tmp/$rc");
chomp($nn);
$nn=~s/(.*?)_.*/$1/g;
$funa .=" $nn";
if($line=~/卖出|出货|减去|剪掉|减掉|退|减|掉|少/)
{
$funa .=" -";
}
else{$funa .=" +";}
}
close FD;
$funa=~s/\-$/$1/g;
$funa=~s/\+$/$1/g;
my $r2=rand();
open(FD33,">/tmp/$r2");
print FD33 "$funa";
close FD33;
my $cres=`$cal /tmp/$r2`;
$allr_res[$ppid] .=$tp."$cres\n";
unlink("/tmp/$r2");
}
sub algebra
{
my $all=$_[1];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
#取出 未知条件 所有实体和单位价格
my $w=plugin_eng::seg_txt($fin);
my $want=want($w);
#print "get want $want\n";
my $entn=list_ent_per($w,$uuid,$want);
for(0 .. scalar(@{$entn})-1)
{
print "@{$entn}[$_] 单位价格、\n";
}
#列方程 转公式 交给cal
my ($fun,$dw)=que2fun($fin,$entn);
#print "fun=$fun\n";
my $rnd=rand();
open(FDrn,">/tmp/$rnd");
print FDrn $fun;
close FDrn;
my $tp .="ans:algebra ";
$tp .=`$cal /tmp/$rnd`;
chomp($tp);
#输出结果
$allr{$ppid}++;
## 加速1秒 algebra
$allr_res[$ppid] .=$tp."单位 $dw\n";
unlink("/tmp/$rnd");
}
sub want
{
my $q=$_[0];
my $uq=decode('utf-8',$q);
my @uta=split("",$uq);
my $qw;
for(0 .. scalar(@uta)-1)
{
my $tn=$_;
my $tc=encode('utf-8',$uta[$tn]);
if($tc eq "多")
{
my $tcn=encode('utf-8',$uta[$tn+1]);
if($tcn eq "少")
{$qw=encode('utf-8',$uta[$tn+2]);
}
}
elsif($tc eq "几")
{
$qw=encode('utf-8',$uta[$tn+1]);
}
}
return $qw;
}
sub want_en
{
my $q=$_[0];
my $w=plugin_eng::seg_txt($q);
my @arr=split(" ",$w);
my $lasten;
for(0 .. scalar(@arr)-1)
{
my $tn=$_;
my $tc=_ispos($arr[$tn]);
print "$tc $arr[$tn] pos-------------------------------------\n";
if($tc=~/n/)
{$lasten=$arr[$tn];$res=$lasten." ";}
if($arr[$tn]=~/多远|哪个|多少|你知道|请问|什么|几|谁|哪里|哪|啥|吗|怎么|怎样|多大|多高|多重|嘛|么$/)
{
my $next1=_ispos($arr[$tn+1]);
my $next2=_ispos($arr[$tn+2]);
if($next1=~/n/)
{
if($tc=~/谁/)
{$res = "人名";}
$res .=$arr[$tn+1];
print "return $res\n";
return ($res,$arr[$tn+1]);
}
elsif($next2=~/n/)
{
if($tc=~/谁/)
{$res = "人名";}
$res .=$arr[$tn+2];
print "return2 $res\n";
return ($res,$arr[$tn]);
}
else
{
return ($res,$arr[$tn]);
}
}
}
$qw=$lasten;
return $qw;
}
sub all_en
{
my $q=$_[0];
my $w=plugin_eng::seg_txt($q);
my @arr=split(" ",$w);
my $lasten;
for(0 .. scalar(@arr)-1)
{
my $tn=$_;
my $tc=_ispos($arr[$tn]);
if($tc=~/n|r/)
{$lasten .=$arr[$tn]." ";}
}
$qw=$lasten;
return $qw;
}
sub que2fun
{
my $q=$_[0];
#我问你两斤的苹果再加上四斤鸭梨等于多少
my $dj=$_[1];
my $qw;
my $qw1;
my $uq=decode('utf-8',$q);
my @uta=split("",$uq);
for(0 .. scalar(@uta)-1)
{
my $tn=$_;
my $tc=encode('utf-8',$uta[$tn]);
if($tc eq "多")
{
my $tcn=encode('utf-8',$uta[$tn+1]);
if($tcn eq "少")
{$qw=encode('utf-8',$uta[$tn+2]);
}
}
elsif($tc eq "几")
{
$qw=encode('utf-8',$uta[$tn+1]);
}
}
if($qw eq "钱")
{
$qw ="元";
}
print "$qw1 qw1111111\n";
my $rd=rand();
open(FDD,">/tmp/$rd");
print FDD "$q";
close FDD;
my $nu=`$ner /tmp/$rd numu`;
print "qf $nu -dj-\n";
my @sa=split(" ",$nu);
for(0 .. scalar(@sa)-1)
{
my $on=$_;
my @asa=split("_",$sa[$on]);
my $nn;
if($asa[0]=~/\d/)
{
$nn=$asa[0];
}
else{
$nn=`$ch2n $asa[0]`;
}
my $a0=$asa[0];
my $a1=$asa[1];
chomp($nn);
print "------$a0 $a1 $nn asa0$asa[0] asa1$asa[1] $sa[$on]------ a0 nn\n";
$q=~s/$a0$a1/ \* $nn \* /g;
}
#消除重复
#$q=~s/$qw//g;
my $hash_dj;
my $fincal;
for(0 .. scalar(@{$dj})-1 )
{
my $n=$_;
my @a=split(" ",@{$dj}[$n]);
$hash_dj->{$a[0]}->{$a[2]}=$a[1];
my $a0=$a[0];
my $a1=$a[1];
print "单价单价 0 $a[0] 2 $a[2] 1 $a[1] |$qw a0-$a0-a1-$a1\n";
#my $bw=belongwhat($a[0]);
#if(exists($fincal->{$bw}))
#{
#next;
#}
#else{
#$fincal->{$bw}=1;
#}
print "question now $q\n";
$q=~s/多少.*?$qw//g;
$q=~s/几.*?$qw//g;
#$q=~s/多少.*?$a[0]//g;
#$q=~s/几.*?$a[0]//g;
if($a[2]=~/^$qw/)
{
$q=~s/$a0/ \* 1\/$a1 \* /g;
print "逆向 $a[2] $qw a$a0 $a1 \n";
#* 2.5 * 能买多少克黄金?
}
else
{
print "正向 $a[2] $qw\n";
$q=~s/$a0/ \* $a1 \* /g;
}
print $q," chang\n";
}
$q=~s/乘以/ \* /g;
$q=~s/乘/ \* /g;
$q=~s/除以/ \/ /g;
$q=~s/除/ \/ /g;
$q=~s/加上/ \+ /g;
$q=~s/加/ \+ /g;
$q=~s/减去/ \- /g;
$q=~s/减/ \- /g;
print $q," chang\n";
#苹果 2 斤/元 单位价格、
#鸭梨 10 斤/元 单位价格、
#qf 五_斤 二十八点六_斤
# ARRAY(0x3baa838)
#苹果 斤/元 2
#鸭梨 斤/元 10
#苹果五斤加上二十八点六斤鸭梨等于多少钱
my @ta=split("",$q);
my @m_res;
my $gres;
my $start=0;
my @tmp_str;
my $las;
for(0 .. scalar(@ta)-1)
{
my $tn=$_;
if($ta[$tn]=~/ /){next;}
if($ta[$tn] eq "*"){
if($las eq "*")
{
push(@tmp_str,"$ta[$tn]");
}
$las=$ta[$tn];
}
elsif($ta[$tn] =~/\+|\-|\/|\d|\./)
{
push(@tmp_str,"$ta[$tn]");
$las=$ta[$tn];
}
}
my $rstr=join("",@tmp_str);
print "qw $qw1|$qw\n";
return ($rstr,$qw);
}
sub list_ent_per
{
my $w=$_[0];
my $uid=$_[1];
my $want=$_[2];
my @wl=split(" ",$w);
my @res;
print "listt $in @wl\n";
for(0 .. scalar(@wl)-1)
{
my $j=$_;
my $dj;
my $ww;
if($wl[$j] eq "钱"){next;}
my $pos=_ispos($wl[$j]);
print "pos $pos $wl[$j]\n";
if($pos=~/n/)
{
# push(@res,"$wl[$j]");
my $rd=rand();
my @n=`grep $wl[$j] /tmp/chat_$uid.dic > /tmp/$rd`;
my $nu=`$ner /tmp/$rd numu` ;
print "grep nu $nu\n";
chomp($nu);
my @ld=split(" ",$nu);
my $ldmax=scalar(@ld)-1;
if($ld[0]=~/吨|个|米|块|尺|本|寸|厘|毫|克|个|匹|块|头|条|只|套|件|张|次|台|面|捆|斤|吨|辆|扇|栋|片|扇/)
{
my @tw=split("_",$ld[0]);
$ww .=$tw[1];
my $dw=$tw[0];
print "$dw $ld[0] dwwwww\n";
my $gj;
if($dw=~/\d/)
{$gj=$dw;}
else{
$gj=`$ch2n $dw`;
chmod($gj);
}
print "nuuuuuuuuuuuuuuuu $ch2n $ld[0] dw $dw|$gj\n";
print "$ld[1] ld\n";
if(length($ld[$ldmax])>=1)
{
my @tw=split("_",$ld[$ldmax]);
$ww .="/".$tw[$ldmax];
$ld[$ldmax]=~s/_元.*//;
my $d7=$tw[1];
$ld[$ldmax]=~s/_$d7.*//;
print "wang $want\n";
my $tn;
if($ld[$ldmax]=~/\d/)
{$tn=$ld[$ldmax];}
else{
$tn=`$ch2n $ld[$ldmax]`;
chmod($tn);
}
print "$tn $gj 元\n";
$dj=$tn/$gj;
}
# $dj=$a/$b;
}
elsif($ld[$ldmax]=~/吨|本|个|米|块|尺|寸|厘|毫|克|个|匹|块|头|条|只|套|件|张|次|台|面|捆|斤|吨|辆|扇|栋|片|扇/)
{
my @tw=split("_",$ld[$ldmax]);
my $dw=$tw[0];
$ww .=$tw[1];
my $gj;if($dw=~/\d/){$gj=$dw}
else{ $gj=`$ch2n $dw`;
chmod($gj);}
my $money;
if(length($ld[0])>=1)
{
my @tw=split("_",$ld[0]);
$ww .="/".$tw[1];
$ld[0]=~s/_元.*//;
my $d7=$tw[1];
$ld[0]=~s/_$d7.*//;
if($ld[0]=~/\d/){$tn=$ld[0];}
else{
$tn=`$ch2n $ld[0]`;
chmod($tn);}
$dj=$tn/$gj;
}
}
else
{
$ld[0]=~s/_元//;
if($ld[0]=~/\d/){$dj=$ld[0];}
else{
$dj=`$ch2n $ld[0]`;
chmod($dj);
}
$ww="元";
}
print "$wl[$j] $dj nu----\n";
push(@res,"$wl[$j] $dj $ww");
}
}
return \@res;
}
sub forgot
{
my $all=$_[1];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
if($all=~/请忘记|你要忘记/)
{
my $say=$all;
chomp($say);
$say=~s/请忘记|你要忘记//;
# @res=ask_conferm("$1");
my $inpf=rand();
open(FD,">/tmp/$inpf");
print FD $say;
close FD;
my @conf=`$ner /tmp/$inpf /tmp/chat_$uuid.dic`;
chomp($conf[0]);
print "$ner /tmp/$inpf /tmp/chat_$uuid.dic";
open(FD,">>/tmp/que_$uuid.dic");
print FD $say."\tID=$conf[0]\n";
close(FD);
my $tp="确定我忘记 $conf[0] 这个记忆吗?(回答“是”执行忘记)";
open(FD,">>/tmp/allq_$uuid.dic");
print FD $say,"\n";
close FD;
$allr{$ppid}++;
# 加速1秒 forget
$allr_res[$ppid] .=$tp;
}elsif($all=~/^是|^对/)
{
my $lashq=`tail -1 /tmp/que_$uuid.dic`;
my $say=$lashq;
print "$say say\n";
if($say=~/ID=1-(\d+)-.*/)
{
my $delid=$1;
delete_kb("/tmp/chat_$uuid.dic",$delid);
my $tp="我已经忘记了 第$delid条记忆 $say\n";
}
elsif($say=~/^reg/)
{
save_reg($say,$uuid,$ppid);
}
elsif($say=~/^dreg/)
{
save_dreg($say,$uuid,$ppid);
}
$allr{$ppid}++;
# 加速1秒 forget yes
$allr_res[$ppid] .=$tp;
open(FD,">>/tmp/allq_$uuid.dic");
print FD $all,"\n";
close FD;
}
}
sub cal
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $tp;
my $rnd=rand();
$q=decode('utf-8',$fin);
my @ta=split("",$q);
my @m_res;
my $gres;
my $start=0;
my @tmp_str;
my $las;
for(0 .. scalar(@ta)-1)
{
my $nw=$_;
my $w=encode('utf-8',$ta[$nw]);
if($w=~/\d|\+|\-|\*|\/|加|减|乘|除|点|\.|一|二|两|三|四|五|六|七|八|九|十|百|千|万|亿/)
{push(@tmp_str,$w);}
}
$fin=join("",@tmp_str);
open(FDrn,">/tmp/$rnd");
print FDrn $fin;
close FDrn;
$tp .="ans:cal ";
$tp .=`$cal /tmp/$rnd`;
$allr_res[$ppid] .=$tp;
$allr{$ppid}++;
# 加速1秒 cal
unlink("/tmp/$rnd");
}
sub fun
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $tp;
print "fun $f4-$0-$$-".$allr{"n"}."\n";
$allr{"n"}++;
#$allr{$f4}=0;
my $rnd=rand();
open(FDrn,">/tmp/$rnd");
print FDrn $fin;
close FDrn;
$tp .="ans:fun ";
$tp .=`$funtalk /tmp/$rnd`;
$allr_res[$ppid] .=$tp;
$allr{$ppid}++;
# 加速1秒 fun $allr{$ppid}
unlink("/tmp/$rnd");
}
sub qa_reg
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $trandin=rand();
my $trand=rand();
chomp($fin);
#my $ne=`tail -1 /tmp/chat_$uuid.dic|grep -v "因为"|grep -v "所以"|grep -v "$fin"`;
my $ne;
print ")))))))))))))))))))ne $ne\n";
$ne .=$fin;
open(FDi,">/tmp/near_$trandin");
#print "((((((((((调用 输入 $ne\n";
print FDi $ne;
close FDi;
my $tp;
my $nd;
if(-e "/tmp/chat_$uuid.same")
{
$nd=`cat /tmp/chat_$uuid.same`;
}
$nd .=`cat /tmp/reg_$uuid.dic`;
print "ndddd0000000000000 $nd cat /tmp/reg_$uuid.dic\n";
my $rd5=rand();
open(FDrd5,">/tmp/$rd5");
print FDrd5 $nd;
close FDrd5;
my $s=`/mnt/sdb/shell2/art_res.pl /tmp/$rd5 /tmp/near_$trandin /tmp/$trand se 1>/dev/null`;
my $s0=`cat /tmp/$trand `;
print "$s0 444$nd 44444444444444444444444444sososososos\n";
my $ne=`cat /tmp/chat_$uuid.dic|grep -v "因为"|grep -v "所以"|grep -v "$fin"`;
$ne .=$fin;
open(FDi,">/tmp/near_$trandin");
print FDi $ne;
close FDi;
my $nd;
if(-e "/tmp/chat_$uuid.same")
{
$nd=`cat /tmp/chat_$uuid.same`;
}
$nd .=`cat /tmp/regm_$uuid.dic`;
my $rd5=rand();
open(FDrd5,">/tmp/$rd5");
print FDrd5 $nd ;
close FDrd5;
my $s=`/mnt/sdb/shell2/art_res.pl /tmp/$rd5 /tmp/near_$trandin /tmp/$trand se 1>/dev/null`;
$s0 .=`cat /tmp/$trand `;
if(length($s0)>0)
{
$s0=~s/ //g;
$tp="ans:qa_reg 所以$s0";
}
print "reg /mnt/sdb/shell2/art_res.pl /tmp/reg_$uuid.dic /tmp/near_$trandin /tmp/$trand se 1>/dev/null\n";
#所有规则参与计算
#unlink("/tmp/near_$trandin");
#unlink("/tmp/$trand");
$allr_res[$ppid] .=$tp;
}
sub qa
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $trandin=rand();
my $trand=rand();
my $tp;
print "qa $f4-$0-$$-".$allr{"n"}."\n";
$allr{"n"}++;
#$allr{$f4}=0;
my $trandin=rand();
my $trand=rand();
open(FDi,">/tmp/ask_$trandin");
print "调用 输入 $fin、\n";
print FDi $fin,"\n";
if(-e "/tmp/chat_$uuid.same")
{my $c=`cat /tmp/chat_$uuid.same`;
print FDi $c;
}
close FDi;
$tp .="ans:qa ";
my @lashq=`tail -20 /tmp/que_$uuid.dic`;
foreach my $lq (@lashq)
{
chomp($lq);
if($lq eq $fin)
{
$tp .="您最近问过相似的问题哦 ";
last;
}
}
open(FDjj,">>/tmp/que_$uuid.dic");
print FDjj $fin."\n";
close FDjj;
print "执行 /mnt/sdb/shell2/art_res.pl /tmp/ask_$trandin /tmp/chat_$uuid.dic /tmp/$trand\n";
my $s=`/mnt/sdb/shell2/art_res.pl /tmp/ask_$trandin /tmp/chat_$uuid.dic /tmp/$trand 1>/dev/null`;
$res_tmp= `cat /tmp/$trand`;
if($res_tmp=~/\d-\d+-(\d.*)-/)
{
my $ras=$1;
if($res>=0.97)
{
$tp .=$res_tmp;
$tp .=" 您之前说过类似的话";}
elsif($ras>0.4)
{
$tp .=$res_tmp;
#my $best=bestans($fin,$ans);
#$tp .="ans:qa best $best\n";
# push(@res,"在机器人的记忆近似的答案\n");
$tp .=" 近似的答案";
}
elsif(0)
{
# push(@res,"记忆里没有接近的答案\n");
my $ans=`$iqa \"$fin\"`;
if(length($ans)>5)
{
#$allr{$pid}-=4;
$tp .="ans:qa 来自互联网\n".$ans."\n";
my $best=bestans($fin,$ans);
$tp .="ans:qa best $best\n";
}
#需要继续精简抽取 做一个实体抽取 和问题相似度匹配
# push (@res,"$ans \n这个答案满意么?不满意请输入'不'\n");
$allr{$ppid}+=3;;
}
}
else
{
$tp .=$res_tmp;
# my $best=bestans($fin,$res_tmp);
# $tp .="ans:qa best $best\n";
}
$allr_res[$ppid] .=$tp;
#print "推理机结果$tp--$ppid--$allr_res[$ppid]\n";
#$allr{$f4}=`$iqa \"$fin\"`;
# 加速1秒 qa $allr{$ppid}
$allr{$ppid}++;
#unlink("/tmp/ask_$trandin");
}
sub qaf
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $ans=`$iqa \"$fin\"`;
#$allr{$pid}-=4;
my $tp;
if(length($ans)>5)
{
$tp .="ans:qa 来自互联网\n".$ans."\n";
my $best=bestans($fin,$ans);
#需要继续精简抽取 做一个实体抽取 和问题相似度匹配
$tp .="ans:qa best $best\n";
}
$allr_res[$ppid] .=$tp;
}
sub bestans
{
my $in=$_[0];
my $more=$_[1];
$more2=decode("utf-8",$more);
$more=substr($more2,0,160);
$more=encode("utf-8",$more);
my $res;
my ($wen,$what)=want_en($in);
$more=~s/\n//gis;
$more=~s/ //gis;
my $w=plugin_eng::seg_txt($more);
my $w2=plugin_eng::seg_txt($in);
my @nol=split(" ",$w2);
my $ehas;
foreach my $rr (@nol)
{
print "exists $rr\n";
$ehas->{$rr}=1;
}
my $rf=rand();
open(FD,">/tmp/$rf");
print FD $more;
close FD;
my @arr;
my @arr2;
if($what=~/哪里|哪国/)
{
my $a2=`$ner /tmp/$rf place`;
chomp($a2);
@arr2=split(" ",$a2);
}
elsif($what=~/谁/)
{
my $a2=`$ner /tmp/$rf name`;
chomp($a2);
@arr2=split(" ",$a2);
}
elsif($what=~/多高|多远|几|多少|多大/)
{
my $a2=`$ner /tmp/$rf numu`;
chomp($a2);
@arr2=split(" ",$a2);
}
elsif($in=~/哪/)
{
my $a2=`$ner /tmp/$rf place`;
print "哪 $a2\n";
print "哪 $a3\n";
my $a3=`$ner /tmp/$rf name`;
chomp($a2);
chomp($a3);
@arr2=split(" ",$a2);
my @arr3=split(" ",$a2);
@arr2=(@arr3,@arr2);
}
else
{
print "未知 $what\n";
@arr=split(" ",$w);
}
print "place name wen $wen what $what |@arr| @arr2\n";
# @arr=(@arr,@arr2,@arr3);
print "all @arr\n";
my $red=rand();
$in=~s/谁//g;
$in=~s/哪里//g;
$in=~s/什么//g;
#my @resa=`$showtop /tmp/$red showtop 6`;
#print "$showtop /tmp/$red showtop 6\n";
my @a3=(@arr2,@arr);
print @a3,"a3\n";
foreach my $j (@a3)
#foreach my $j (@resa)
{
print "检查 $j是否为\n";
if(exists($ehas->{$j}))
{
print "tiaoguo $j\n";
next;}
my $nn=_ispos($j);
print "$j $nn pos\n";
if($nn !~/n/)
{
next;}
if($j=~/$wen/)
{
next;
}
print "$j j\n";
if($j=~/\d-\d+?-(.*?)-/)
{
my $ra=$1;
print "$ra ra\n";
if($ra >0.3)
{
next;
}
#$res .="$j";
}
$res .="$j";
last;
}
$res;
}
sub learn_rule_form_rule
{
#rn_rule_form_rule小明 经常 吃 剩饭 && 不卫生 -> 小明 不停 闹肚子
#小光 吃 剩饭 && 不卫生 -> 小光 整夜 闹肚子
# $v0 吃 剩饭 && 不卫生 => $v0 $v1 闹肚子
#上下不对称 左右对称的是 |词性相同 变量
#上下对称 左右不对称是 常量
my $f4=$_[0];
my $fin=$_[1];
my $fin2=$_[2];
my $uuid=$_[3];
my $ppid=$_[4];
my @f1=split("=>",$fin);
my @f2=split("=>",$fin2);
my @f1a=split(" ",$f1[0]);
my @f1b=split(" ",$f1[1]);
my @f2a=split(" ",$f2[0]);
my @f2b=split(" ",$f2[1]);
my $vn=0;
my @ys;
my $left;
my $right;
my $chash;
for(0 .. scalar(@f1a)-1)
{
my $lr=0;
my $ud=0;
my $now=$_;
my $one1=$f1a[$now];
if($one1 eq "&&")
{
$left .=$one1;
next;
}
for(0 .. scalar(@f1b)-1)
{
my $now2=$_;
if($one1 eq $f1b[$now2])
{
print "左右对称 $one1\n";
$lr=1;
}
}
for(0 .. scalar(@f2a)-1)
{
my $now2=$_;
if($one1 eq $f2a[$now2])
{
print "上下对称 $one1\n";
$ud=1;
}
}
### $lr
### $ud
if(($lr ==1)&&($ud ==0))
{
if(!exists($chash->{$one1}))
{
$left .=" \$v$vn ";
$chash->{$one1}="\$v$vn";
my $pos=_ispos($one1);
push(@ys," \$v$vn $pos");
$vn++;
}
else
{
$left .=" ".$chash->{$one1}." ";
}
}
elsif(($lr ==0)&&($ud ==1))
{
$left .=" $one1 ";
}
elsif(($lr ==0)&&($ud ==0))
{
if(!exists($chash->{$one1}))
{
$left .=" \$v$vn ";
$chash->{$one1}="\$v$vn";
my $pos=_ispos($one1);
push(@ys," \$v$vn $pos");
$vn++;
}
}
elsif(($lr ==1)&&($ud ==1))
{
$left .=" $one1 ";
}
}
### $left
#print "@f1b f1bbbbbbbbbbb\n";
for(0 .. scalar(@f1b)-1)
{
my $lr=0;
my $ud=0;
my $now=$_;
my $one1=$f1b[$now];
for(0 .. scalar(@f1a)-1)
{
my $now2=$_;
if($one1 eq $f1a[$now2])
{
print "右边 左右对称 $one1\n";
$lr=1;
}
}
for(0 .. scalar(@f2b)-1)
{
my $now2=$_;
if($one1 eq $f2b[$now2])
{
print "右边 上下对称 $one1\n";
$ud=1;
}
}
if(($lr ==1)&&($ud ==0))
{
if(exists($chash->{$one1}))
{
$right .=" ".$chash->{$one1}." ";
$vn++;
}
else
{
$right .=" $one1 ";
}
}
elsif(($lr ==0)&&($ud ==1))
{
$right .=" $one1 ";
}
}
### $right
my $al;
my $allr;
if((length($left)>0)&&(length($right)>0))
{
if(scalar(@ys)>0)
{
my $ysj=" | ".join(" && ",@ys);
$al=$left.$ysj;
}
else
{
$al=$left;
}
$allr=$al." => ".$right;
}
$allr;
}
sub learn_ifthen
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
if(($all=~/如果(.*?)[那么|所以](.*)/)||($all=~/(.*?)[那么|所以](.*)/))
{
my $tp .="ans:because 确定这个规则吗?$1 推出 $2";
open(FDq,">>/tmp/que_$uuid.dic");
print FDq "reg $1 => $2\n";
close FDq;
open(FDq,">>/tmp/status_$uuid.txt");
print FDq "------\n";
close FDq;
$allr_res[$ppid] .=$tp;
}
elsif($all=~/所以(.*)/)
{
my $lastsay=`tail -5 /tmp/chat_$uuid.dic`;
$lastsay=~s/\n/&&/gis;
$lastsay .=$all;
print "$lastsay lastsay\n";
if($lastsay=~/.*因为(.*?)所以(.*)/)
{
my $tp .="ans:because 确定这个规则吗? $1 推出 $2";
$allr_res[$ppid] .=$tp;
open(FDq,">>/tmp/que_$uuid.dic");
print FDq "reg $1 => $2\n";
close FDq;
open(FDq,">>/tmp/status_$uuid.txt");
print FDq "------\n";
close FDq;
}
else
{
}
}
else
{
my $tp .="ans:because ";
$allr_res[$ppid] .=$tp;
}
}
sub learn_equa
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
if($fin=~/(.*?)等于(.*)/)
{
my $p=$1;
my $p2=$2;
my $fun;
my $goal;
#不学习的例子
# if($p=~/长|宽|高/)
if($p=~/加|减|乘|除/)
{
$fun=$p;
$goal=$p2;
}
elsif($p2=~/加|减|乘|除/)
{
$fun=$p2;
$goal=$p;
}
$fun=~s/加上/ \+ /g;
$fun=~s/加/ \+ /g;
$fun=~s/减去/ \- /g;
$fun=~s/减/ \- /g;
$fun=~s/乘以/ \* /g;
$fun=~s/乘/ \* /g;
$fun=~s/除以/ \/ /g;
$fun=~s/除以/ \/ /g;
my @arl=split(" ",$fun);
my $funexp;
my $funfact;
my $funys;
my $blhash;
my $blnow=0;
for(0 .. scalar(@arl)-1)
{
my $now=$_;
if($arl[$now]=~/和|然后/)
{
$arl[$now]=~s/的//g;
$arl[$now]=~s/然后//g;
$arl[$now]=~s/值//g;
$arl[$now]=~s/和//g;
if(exists($blhash->{$arl[$now]}))
{
$funexp .=" \$v".$blhash->{$arl[$now]};
}
else
{
$funfact .="&& $arl[$now]* "."\$v"."$blnow ";
$funys .="&& \$v$blnow nu ";
$funexp .=" \$v".$blnow;
$blhash->{$arl[$now]}=$blnow;
$blnow++;
}
$funexp=" ( ".$funexp." ) ";
}
elsif($arl[$now]=~/\+|\-|\*|\//)
{
$funexp .=" ".$arl[$now]." ";
}
else
{
if($arl[$now]=~/\d|零|一|两|二|三|四|五|六|七|八|九|十|百|千|万|亿/)
{
my $n9=tonum($arl[$now]);
$n9=`$ch2n $n9`;
chomp($n9);
$funexp .=" $n9";
}
elsif(exists($blhash->{$arl[$now]}))
{
$funexp .=" \$v".$blhash->{$arl[$now]};
}
else
{
$funfact .="&& $arl[$now]* "."\$v"."$blnow ";
$funys .="&& \$v$blnow nu ";
$funexp .=" \$v".$blnow;
$blhash->{$arl[$now]}=$blnow;
$blnow++;
}
}
}
$funys=~s/^\&\&//g;
$funfact=~s/^\&\&//g;
my $total= $funfact." | ".$funys ." => ".$goal." f( ". $funexp." )\n";
open(FDf,">>/tmp/chat_$uuid.fun");
print FDf $total;
close FDf;
$allr_res[$ppid] .="ans:learn_eque 学习到公式 $total";
}
}
sub record
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $all=$fin;
my $tp;
if(! (-e "/tmp/chat_$uuid.dic"))
{
system("touch /tmp/chat_$uuid.dic");
}
my $inpf=rand();
open(FD,">/tmp/$inpf");
print FD $fin;
close FD;
my @conf=`$ner /tmp/$inpf /tmp/chat_$uuid.dic`;
foreach my $oc (@conf)
{
if($oc=~/\d+-\d+-(.*?)-(.*)/)
{
my $rt=$1;
my $s=$2;
if($rt>0.5)
{
$tp .="ans:record 发现相似记忆 如果需要忘记请输入 请忘记$s ";
}
}
#1-6-0.0608665468756944-请我喝酒
}
my $list=system("grep \"^$all\" /tmp/chat_$uuid.dic");
if($list ==256)
{
open(FD,">>/tmp/chat_$uuid.dic");
print FD $all,"\n";
close FD;
$tp="ans:record .\n";
}
open(FD,">>/tmp/allq_$uuid.dic");
print FD $all,"\n";
close FD;
$allr_res[$ppid] .=$tp;
### 加速1秒 record
$allr{$ppid}++;
}
sub fun2
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $tp;
print "fun2 $f4-$0-$$-".$allr{"n"}."\n";
$allr{"n"}++;
#$allr{$f4}=0;
#my $rnd=rand();
#open(FDrn,">/tmp/$rnd");
#print FDrn $fin;
#close FDrn;
$tp .="ans:fun2 ";
$tp .=`$funtalk2 $fin`;
#$tp .=`$funtalk2 /tmp/$rnd`;
$allr_res[$ppid] .=$tp;
$allr{$ppid}++;
### 加速1秒 fun2
unlink("/tmp/$rnd");
}
sub fun3
{
my $f4=$_[0];
my $fin=$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $tp;
$allr{"n"}++;
#$allr{$f4}=0;
my $rnd=rand();
#open(FDrn,">/tmp/$rnd");
#print FDrn $fin;
#close FDrn;
$tp .="ans:fun3 ";
#my $w=plugin_eng::tfidf(2,"ti",$fin);
my $w=key_word($fin);
if(length($w)<3)
{
$w=plugin_eng::tfidf(2,"ti",$fin);
}
print "key_word $w\n";
my @wlist=split(" ",$w);
#my @gol;
#my @gol2;
my $fw=0;
foreach my $j (@wlist)
{
print "pushn $j\n";
# if(($j=~/\:0$/)&&($fw == 0))
# {
# $w=`$kew_word $fin`;
# }
# else
# {
$j=~s/\:.*//g;
push(@gol,$j);
# }
}
my $nowg=scalar(@gol);
print "$nowg noworg\n";
if($nowg==0)
{
$wlist[0]=~s/\:.*//g;
push(@gol,$wlist[0]);
$nowg=1;
}
#my $tr=rand(10);
#$nowg=$nowg*$tr/10;
#$nowg=ceil($nowg)-1;
my $nowg=0;
print "$nowg his_grep $gol[$nowg]----- \n";
my @s;
#if(scalar(@gol)>0)
#{
#@s=`grep $gol[$nowg] babel_all_path_fin20.txt `;
# if(scalar(@s)<=0)
# {
#@s=`grep $gol[$nowg] base_total.txt`;
# }
#}else
#{
#@s=`grep $gol2[$nowg] babel_all_path_fin20.txt `;
# if(scalar(@s)<=0)
# {
# @s=`grep $gol2[0] base_total.txt`;
# }
#}
print "push @gol |$nowg |$gol[$nowg]\n";
my $alls=get_rela($gol[$nowg]);
print "get $alls------\n";
my @fu;
if(length($alls)<=3)
{
if(scalar(@gol)>0)
{
@s=`grep $gol[$nowg] babel_add_baidu_abc.txt`;
if(scalar(@s)<=0)
{
@s=`grep $gol[$nowg] base_total.txt`;
}
}else
{
#@s=`grep $gol2[$nowg] babel_all_path_fin20.txt `;
# if(scalar(@s)<=0)
# {
# @s=`grep $gol2[0] base_total.txt`;
# }
}
print "scalar",scalar(@s),"\n";
foreach my $jj (@s)
{
if($jj=~/(.*?)\t/)
{
push(@fu,$1);
}
}
}
else{
@fu=split(" ",$alls);
}
#print "scalar",scalar(@s),"\n";
#foreach my $jj (@s)
#{
# if($jj=~/(.*?)\t/)
# {
# push(@fu,$1);
# }
#}
my $t=scalar(@fu);
if($t>0)
{
my $tr=rand(10);
print "t值 $t\n";
$t=$t*$tr/10;
$tr=ceil($t)-1;
print "t $t $tr $fu[$tr] \n";
my $add=randt($fu[$tr],$gol[0]);
chomp($add);
$tp .="$add";
$allr_res[$ppid] .=$tp;
$allr{$ppid}++;
### 加速1秒 fun2
unlink("/tmp/$rnd");
}
}
sub randt
{
my $res;
my $in=$_[0];
my $in2=$_[1];
my $tr=rand(10);
my $tall=scalar(@talk_add);
my $t=$tall*$tr/10;
$t=ceil($t)-1;
#print "randt $t $talk_add[$t]\n";
my $c=rand();
if($c>0.5){$res .=$in2."? ";}
my $c=rand();
if($c<0.8){
my $cc=`$shangxia $in2 shang`;
print "上下位 $in $in2\n";
my @ccl=split(" ",$cc);
$res .=$ccl[0]."? ";}
elsif($c>0.8){
my $cc=`$shangxia $in2 tong`;
print "同位 $in $in2\n";
my @ccl=split(" ",$cc);
$res .=$ccl[0]."? ";
}
my $tr=$talk_add[$t];
chomp($tr);
$res .=$tr."$in";
$res;
}
sub command
{
my $f4=$_[0];
my $uuid=$_[2];
print "command $f4-$0-$$-".$allr{"n"}."\n";
$allr{"n"}++;
}
sub semtic
{
my $fin=$_[1];
my $ppid=$_[3];
my $rnd=rand();
open(FDrn,">/tmp/$rnd");
print FDrn $fin;
close FDrn;
my $tp .="ans:semtic ";
my @sres=`$ner /tmp/$rnd 2q.dic`;
print "$ner /tmp/$rnd 2q.dic";
my $gbv;
if($sres[0]=~/^1-1-(\d\.\d+)/)
{
$tp .="坏心情 $1";
$gbv=-$1;
$sres[1]=~/^1-2-(\d\.\d+)/
}
elsif($sres[0]=~/^1-2-(\d\.\d+)/)
{
$tp .="好心情 $1";
$gbv=$1;
}
if($gbv < -0.8)
{$tp .="心情糟透了吗";}
elsif($gbv < -0.6)
{$tp .="心情很差";}
elsif($gbv < -0.4)
{$tp .="主人很不爽";}
elsif($gbv < -0.2)
{$tp .="主人在生气?";}
elsif($gbv < -0.01)
{$tp .="主人似乎不高兴?";}
elsif($gbv < 0)
{
$tp .="不算很糟";
}
elsif($gbv < 0.1)
{
$tp .="不赖";
}
elsif($gbv < 0.2)
{
$tp .="感觉挺好";
}
elsif($gbv < 0.3)
{
$tp .="赞 很棒";
}
$allr_res[$ppid] .=$tp;
$allr{"n"}++;
#unlink("/tmp/$rnd");
}
sub learn_because
{
my $all =$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
if(($all=~/因为(.*?)所以(.*)/)||($all=~/(.*?)所以(.*)/))
{
my $tp .="ans:because 确定这个规则吗?$1 推出 $2";
open(FDq,">>/tmp/que_$uuid.dic");
print FDq "reg $1 => $2\n";
close FDq;
open(FDq,">>/tmp/status_$uuid.txt");
print FDq "------\n";
close FDq;
$allr_res[$ppid] .=$tp;
}
elsif($all=~/所以(.*)/)
{
my $lastsay=`tail -5 /tmp/chat_$uuid.dic`;
$lastsay=~s/\n/&&/gis;
$lastsay .=$all;
print "$lastsay lastsay\n";
if($lastsay=~/.*因为(.*?)所以(.*)/)
{
my $tp .="ans:because 确定这个规则吗? $1 推出 $2";
$allr_res[$ppid] .=$tp;
open(FDq,">>/tmp/que_$uuid.dic");
print FDq "reg $1 => $2\n";
close FDq;
open(FDq,">>/tmp/status_$uuid.txt");
print FDq "------\n";
close FDq;
}
else
{
}
}
elsif(($all=~/如果(.*?)那么(.*)/)||($all=~/如果(.*?)则(.*)/))
{
my $a1=$1;
my $a2=$2;
#代指消解
$a1=~s/一.*?东西/ \$v0 /g;
$a1=~s/一.*?人/ \$v0 /g;
$a1=~s/它|他|她/ \$v0 /g;
$a1=~s/某.*?东西/ \$v0 /g;
$a2=~s/这.*?东西/ \$v0 /g;
$a2=~s/它|他|她/ \$v0 /g;
if($a1=~/人/)
{ $ys .=" $v0 nr ";
}
my $tp .="ans:because 确定这个规则吗?$a1 推出 $a2";
open(FDq,">>/tmp/que_$uuid.dic");print FDq "reg $a1 => $a2\n";
close FDq;
open(FDq,">>/tmp/status_$uuid.txt");
print FDq "------\n";
close FDq;
$allr_res[$ppid] .=$tp;
}
else
{
my $tp .="ans:because ";
$allr_res[$ppid] .=$tp;
}
}
sub learn_en
{
my $all =$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
if($all=~/请记住(.*)是(.*)/)
{
my $et=$1;
my $tp=$2;
my $sp="n";;
if($tp=~/人名|人|男|女/)
{$sp="nr";}
elsif($tp=~/地名|地方|城市|村子|村名/)
{
$sp="ns";
}
print "uuid--------------- $uuid\n";
my $list=system("grep \"^$et是$tp\" /tmp/chat_$uuid.dic");
if($list ==256)
{
open(FD,">>/tmp/chat_$uuid.dic");
print FD "$et是$tp","\n";
close FD;
}
if(saveseg($et,$sp)==1)
{
restartseg();
}
$allr_res[$ppid] .="机器人说 :好的我记住了\n";
print "记住 $et $tp\n";
}
elsif($all=~/(.*)是(.*)/)
{
my $et=$1;my $tp=$2;
if($et=~/长|宽|高/)
{
return;
}
my $sp="n";
my $ccc=`touch /tmp/chat_$uuid.same`;
my $list=system("grep \"^$et是$tp\" /tmp/chat_$uuid.same");
open(FDio,">>/tmp/chat_$uuid.same");
if($list ==256)
{
print FDio "*".$tp."* => *".$et."*\n";
}
close FDio;
$allr_res[$ppid] .="机器人说 :好的我记住了\n";
}
$allr{$ppid}++;
}
exit;
my $n=0;
#our $f=0;
my $conf;
my $id;
$input=$ARGV[1];
#print "Session ", $session->ID(), " got input: ".$input."\n";
#print "Session ", $session->ID(), " got input: ".length($input)."\n";
my $d;
my $d_seq;
#open(FD_log,">/tmp/engine_ask.log");
#print FD_log "$input\n";
#system("cp $input /tmp/");
print "input file $input",length($input),"\n";
open(FDo,">$ARGV[2]");
my @res;
my $smn;
my $res2;
my $smn=0;
$res1=0;
my $status;
my $randin=rand();
my $all;
my $id=$ARGV[0];
if(0)
{
$id=$1;
$conf=$2;
#my $all=`cat $conf`;
$all=$conf;
}
elsif(length($id)<=0)
{@res="无用户id 输入个昵称吧\n";}
else
{$all=$input;}
#忘记知识的处理
#钢铁是怎样练成的是一本书
#实体学习
#
if($all=~/请记住(.*)是(.*)/)
{
my $et=$1;
my $tp=$2;
my $sp="n";;
if($tp=~/人名|人|男|女/)
{$sp="nr";}
elsif($tp=~/地名|地方|城市|村子|村名/)
{
$sp="ns";
}
my $list=system("grep \"^$et是$tp\" /tmp/chat_$id.dic");
if($list ==256)
{
open(FD,">>/tmp/chat_$id.dic");
print FD "$et是$tp","\n";
close FD;
}
if(saveseg($et,$sp)==1)
{
restartseg();
}
$res[0]="$ARGV[0] $ARGV[1]机器人说 :好的我记住了\n";
print FDo @res;
print @res;
close FDo;
exit;
}
if($all=~/^不/)
{
my $lashq=`tail -1 /tmp/que_$id.dic`;
my $say=$lashq;
my $lan=`tail -1 /tmp/allq_$id.dic`;
my $lsay=$lan;
if($lsay=~/不/)
{
#exec("/home/htc/stanford-parser-full-2014-06-16/htc/because_so/client_because.pl ".$tmptxt, $out);
#exec("/mnt/sdb/shell2/client_talk.pl ".$tmptxt, $out);
my $rnd=rand();
open(FDrn,">/tmp/$rnd");
print FDrn $say;
close FDrn;
my $fun1=`$funtalk /tmp/$rnd`;
if(length($fun1)<2)
{
$fun1=`$funtalk2 /tmp/$rnd`;
}
print FDo "$fun1 \n那这个答案满意么?不满意请输入'不'\n";
close FDo;
open(FDrn,">>/tmp/allq_$id.dic");
print FDrn $say;
close FDrn;
exit;
}
else
{
my $ans=`$iqa $say`;
print FDo "$ans \n这个答案满意么?不满意请输入'不'\n";
close FDo;
open(FD,">>/tmp/allq_$id.dic");
print FD $all;
close FD;
exit;
}
}
if($all=~/^是|^对/)
{
my $lashq=`tail -1 /tmp/que_$id.dic`;
my $say=$lashq;
print "$say say\n";
if($say=~/ID=1-(\d+)-.*/)
{
my $delid=$1;
delete_kb("/tmp/chat_$id.dic",$delid);
print FDo "我已经忘记了 第$delid条记忆 $say\n";
close FDo;
}
elsif($say=~/^reg/)
{
save_reg($say);
}
open(FD,">>/tmp/allq_$id.dic");
print FD $all,"\n";
close FD;
exit;
}
if($all=~/请忘记|你要忘记/)
{
my $say=$all;
$say=~s/请忘记|你要忘记//;
# @res=ask_conferm("$1");
my $inpf=rand();
open(FD,">/tmp/$inpf");
print FD $say;
close FD;
my @conf=`$ner /tmp/$inpf /tmp/chat_$id.dic`;
print "$ner /tmp/$inpf /tmp/chat_$id.dic";
open(FD,">>/tmp/que_$id.dic");
chomp($conf[0]);
print FD $say."\tID=$conf[0]\n";
print "写入que $conf[0]";
close(FD);
print FDo "确定我忘记 $conf[0] 这个记忆吗?(回答“是”执行忘记)";
close FDo;
open(FD,">>/tmp/allq_$id.dic");
print FD $say,"\n";
close FD;
exit;
}
#确认上个命令
#{}
#if()
#delete()
if($all=~/\?|\?|多远|多少|你知道|请问|什么|几|谁|哪里|哪|啥|吗|怎么|怎样|多大|多高|多重|嘛|么$/)
{
$status="ask";
my $trand=rand();
my $trandin=rand();
#非标准问题转换
$all=~s/多大/几岁/g;
#问题预处理
#是否和上个问题相关
#如果相关 则组合起来形成规则
#提问主人获得答案后在形成新问题
#pro_process
open(FD,">>/tmp/que_$id.dic");
print FD $all."\n";
close(FD);
open(FDi,">/tmp/ask_$trandin");
print FDi $all;
if(-e "/tmp/chat_$id.same")
{my $c=`cat /tmp/chat_$id.same`;
print FDi $c;
}
close FDi;
print "执行 /mnt/sdb/shell2/art_res.pl /tmp/ask_$trandin /tmp/chat_$id.dic /tmp/$trand\n";
my $s=`/mnt/sdb/shell2/art_res.pl /tmp/ask_$trandin /tmp/chat_$id.dic /tmp/$trand 1>/dev/null`;
@res_tmp= `cat /tmp/$trand`;
#print "tmp 000000000000$res_tmp[0]\n";
if($res_tmp[0]=~/\d-\d+-(\d\.\d+)/)
{
my $ras=$1;
if($ras>0.4)
{
push(@res,$res_tmp[0]);
push(@res,"在机器人的记忆近似的答案\n");
}
else
{
push(@res,"记忆里没有接近的答案\n");
# push(@res,"记忆里没有接近的答案\n");
my $ans=`$iqa $all`;
push (@res,"$ans \n这个答案满意么?不满意请输入'不'\n");
}
}
elsif($res_tmp[0]=~/结果/)
{
@res=@res_tmp;
}
push(@res,"对这个答案满意么?不满意请输入“不”");
open(FD,">>/tmp/allq_$id.dic");
print FD $all,"\n";
close FD;
open(FD,">>/tmp/que_$id.dic");
print FD $all,"\n";
close FD;
#答案结果处理
#是否和之前个问题相关
#除了 不算 处理否定处理
#unlink($trand);
#unlink($trandin);
}
elsif(1)
{
#添加临时规则的处理
my $list=system("grep \"^$all\" /tmp/chat_$id.dic");
if($list ==256)
{
open(FD,">>/tmp/chat_$id.dic");
print FD $all,"\n";
close FD;
}
$res[0]="$ARGV[0] $ARGV[1]机器人说 :好的我记住了\n";
$status="stad";
}
else
{
$status="fun";
}
print "机器人回答:";
print FDo @res;
print @res;
close FDo;
open(FD,">>/tmp/allq_$id.dic");
print FD $all,"\n";
close FD;
# ($res2,$smn)=search_sim($input,"last");
# if($smn<0.5)
# {
# my $res3=search_baike($input);
# print FD_log "search baike $input\n";
# @res=$res3;
# }
# else
# {
#print FD_log "find sim in kb $res2,$smn\n";
# }
# @res=($res2);
# }
# else
# {
# $res="最近答案通过直接题库 $smn $res2\n";
#print FD_log "题库答案 $res2\n";
# @res=$res3;
# }
#$file=$ARGV[0];
sub save_dreg
{
my $in=$_[0];
my $uuid=$_[1];
my $ppid=$_[2];
chomp($in);
#reg 我爱吃水果&&菠萝是水果&& => 我爱吃菠萝
#reg 我 爱 吃 水果 && 菠萝 是 水果 && = > 我 爱 吃 菠萝
if($in=~/dreg (.*?) \=\> (.*)/)
{
my $my=$1;
my $mg=$2;
### $my
### $mg
# my $my=plugin_eng::seg_txt($y);
# my $mg=plugin_eng::seg_txt($g);
# $my=~s/\&\& $//;
# $my=~s/\&\&$//;
print "my $my mg $mg\n";
if($my=~/\&\&/)
{
open(FDo,">>/tmp/dregm_$uuid.dic");
print FDo "$my => $mg\n";
#print FDo "$mg => 可能因为 $my\n";
close FDo;
}
else
{
open(FDo,">>/tmp/dreg_$uuid.dic");
print FDo "$my => $mg\n";
#print FDo "$mg => 可能因为 $my\n";
close FDo;
}
$allr_res[$ppid] .="ans:qa_dreg 我记住了 $my 推出 $mg\n";
}
}
sub save_reg
{
my $in=$_[0];
my $uuid=$_[1];
my $ppid=$_[2];
chomp($in);
#reg 我爱吃水果&&菠萝是水果&& => 我爱吃菠萝
#reg 我 爱 吃 水果 && 菠萝 是 水果 && = > 我 爱 吃 菠萝
if($in=~/reg (.*?) \=\> (.*)/)
{
my $y=$1;
my $g=$2;
my $my=plugin_eng::seg_txt($y);
my $mg=plugin_eng::seg_txt($g);
$my=~s/\$ /\$/g;
$mg=~s/\$ /\$/g;
$my=~s/\&\& $//;
$my=~s/\&\&$//;
print "my $my mg $mg\n";
if($my=~/\&\&/)
{
open(FDo,">>/tmp/regm_$uuid.dic");
print FDo "$my => $mg\n";
# print FDo "$mg => 可能因为 $my\n";
close FDo;
}
else
{
open(FDo,">>/tmp/reg_$uuid.dic");
print FDo "$my => $mg\n";
# print FDo "$mg => 可能因为 $my\n";
close FDo;
}
$allr_res[$ppid] .="ans:qa_reg 我记住了 $my 推出 $mg\n";
my @dreg;
my @dregm;
# open(FDt,"/tmp/reg_$uuid.dic");
# while(<FDt>)
# {
# my $line=$_;
# chomp($line);
# if($line eq "$my => $mg")
# {next;}
# my $deepreg=learn_rule_form_rule("","$my => $mg",$line,$uuid,$ppid);
#print "deepreg $deepreg $my => $mg| $line\n";
# if(length($deepreg)>0)
# {
# push(@dreg,$deepreg);
# last;
# }
# }
my $rg=rand();
open(FDrg,">/tmp/$rg");
print FDrg "$my => $mg";
print FDrg "\n--------\n";
my $tj=`tail -5 /tmp/reg_$uuid.dic`;
print FDrg $tj;
close FDrg;
my @ang=`$showtop /tmp/$rg showtop 4`;
print "相似=========规则 an $ner /tmp/$rg /tmp/reg_$uuid.dic\n";
my $deepreg;
foreach my $an (@ang)
{
chomp($an);
if($an=~/.*?-.*?-(.*?)-(.*)/)
{
my $nn=$1;
my $rline=$2;
print "nnnnnnnn $nn $rline\n";
if(($nn>0.23)&&($nn<0.96))
{
$deepreg=learn_rule_form_rule("","$my => $mg",$rline,$uuid,$ppid);
print "学习到规则 $deepreg\n";
}
print "长度 ".length($deepreg)."\n";
if(length($deepreg)>0)
{
push(@dreg,$deepreg);
print "dreg have ----@dreg\n";
last;
}
}
}
#open(FDt,"/tmp/regm_$uuid.dic");
# while(<FDt>)
# {
# my $line=$_;
# chomp($line);
# if($line eq "$my => $mg")
# {next;}
# my $deepreg=learn_rule_form_rule("","$my => $mg",$line,$uuid,$ppid);
# if(length($deepreg)>0)
# {
# push(@dregm,$deepreg);
# last;
# }
# }
my $rg=rand();
open(FDrg,">/tmp/$rg");
print FDrg "$my => $mg";
print FDrg "\n--------\n";
my $tj=`tail -5 /tmp/regm_$uuid.dic`;
print FDrg $tj;
close FDrg;
my @ang=`$showtop /tmp/$rg showtop 4`;
print "相似=========规则 an $ner /tmp/$rg /tmp/regm_$uuidm.dic\n";
my $deepreg;
foreach my $an (@ang)
{
if($an=~/.*?-.*?-(.*?)-(.*)/)
{
my $nn=$1;
my $rline=$2;
if(($nn>0.23)&&($nn<0.96))
{
$deepreg=learn_rule_form_rule("","$my => $mg",$rline,$uuid,$ppid);
print "学习到规则 $deepreg\n";
}
if(length($deepreg)>0)
{
push(@dregm,$deepreg);
last;
}
}
}
print "@dregm|@dreg\n";
if((scalar(@dregm)>0)||(scalar(@dreg)>0))
{
open(FDq,">>/tmp/que_$uuid.dic");
foreach my $org (@dregm,@dreg)
{
print FDq "dreg $org\n";
}
close FDq;
print "输出 学习到规律 @dregm @dreg 记录请输入是\n";
$allr_res[$ppid] .=" 学习到规律 @dregm @dreg 记录请输入是\n";
}
}
}
sub search_faq
{
my $s=$_[0];
open(FD0,"$s");
my $qtxt;
if($qtxt=<FD0>)
{
chomp($qtxt);
print FD_log "qtxt=$qtxt\n";}
close FD0;
if($qtxt=~/(.*?)和(.*?)的关系/)
{
my $a=$1;
my $b=$2;
my $rand=rand();
open(FD,">/tmp/$rand");
print FD "$a\n--------------\n$b\n";
close FD;
my @all=`/mnt/sdb/shell2/client_gongan.pl /tmp/$rand path`;
my $rall=join("",@all);
unlink($rand);
return $rall;
}
if(exists($hash->{$qtxt}))
{
return $hash->{$qtxt}->{"a"}."\n";
}
}
sub search_sim
{
# while(($k,$v)=$hash->{$q})
# {
# }
my $s=$_[0];
my $s1=$_[1];
my $rand=rand();
if($s1=~/last/)
{
open(FD0,"$s");
my $qtxt;
if($qtxt=<FD0>)
{print FD_log "qtxt=$qtxt\n";}
open(FD,">$rand.txt");
print FD "$qtxt--------\n";
open(FD2,"zhidao_q.txt");
my $line;
while(<FD2>)
{
$line=$_;
}
print FD "$line";
close(FD);
close(FD2);
}
else
{
open(FD0,"$s");
my $qtxt;
if($qtxt=<FD0>)
{print FD_log "qtxt=$qtxt\n";}
open(FD,">$rand.txt");
print FD "$qtxt--------\n";
open(FD2,"zhidao_q.txt");
while(<FD2>)
{
my $line=$_;
print FD "$line";
}
close(FD);
close(FD2);
}
my $rsim;
my $simn;
print "./client_gongan.pl $rand.txt\n";
my @sim=`./client_gongan.pl $rand.txt`;
print FD_log "sim--".$sim[0],"\n";
foreach my $one (@sim)
{
print FD_log "one=$one\n";
if($one=~/(.*?)\-(.*?)\-(0\.\d\d\d\d).*/)
{
my $rid=$2;
my $rn=$3;
$simn=$rn;
print FD_log "准确率=$simn\n";
$rsim="$rid ".$hashid->{$rid}." ".$hash->{$hashid->{$rid}}->{"a"};
#push(@rsim,"$rid ".$hashid->{$rid}." ".$hash->{$hashid->{$rid}}->{"a"});
# print FD_log "rid =$rid".$hashid->{$rid}." ".$hash->{$hashid->{$rid}}->{"a"}."\n";
last;
}
}
#unlink("$rand.txt");
$rsim,$simn;
}
sub search_zhidao
{
my $s=$_[0];
open(FD0,"$s");
my $qtxt;
if($qtxt=<FD0>)
{print FD_log "qtxt=$qtxt\n";}
my $tres;
$tres=`./get_zhidao.pl $qtxt`;
my $res=`tail -1 zhidao_qa.txt`;
# reloaddic();
# $qtxt;
$res;
}
sub search_baike
{
my $s=$_[0];
open(FD0,"$s");
my $qtxt;
if($qtxt=<FD0>)
{print FD_log "qtxt=$qtxt\n";}
my $tres;
$tres=`./get_baike_com_all.pl $qtxt`;
# reloaddic();
# $qtxt;
my $res=`tail -1 zhidao_qa.txt`;
$res;
# $tres;
}
sub delete_kb
{
my $f=$_[0];
my $dl=int($_[1]);
my $rnew=rand();
open(FDtp,"$f");
open(FDto,">/tmp/$rnew");
my $ii=1;
while(<FDtp>)
{
my $line=$_;
if($ii == $dl)
{
print "跳过 $dl $ii\n";
$ii++;
next;}
$ii++;
print FDto $line;
}
close FDtp;
close FDto;
system("cp -f /tmp/$rnew $f");
print "刷新文件、n\n";
unlink("/tmp/$rnew");
}
sub restartseg
{
my $socket = IO::Socket::INET->new(
PeerAddr => "127.0.0.1",
PeerPort => "11222",
Type => SOCK_STREAM,
Proto => "tcp",
)
or die "Can not create socket connect.$@";
$socket->autoflush(1);
my $sel = IO::Select->new($socket);
$socket->send("1 1 restart\n",0); ##发送消息至服务器端。
#print "分词 $txt\n";
my $i;
#print "$i socket\n";
while (my @ready = $sel->can_read) { ##等待服务端返回的消息
foreach my $fh (@ready) {
if ($fh == $socket) {
while (<$fh>) {
$i=$_;
print "read====$i==================\n";
}
$sel->remove($fh);
close $fh;
}
}
}
chomp($i);
if($i eq "ok")
{return 1;}
else
{return 0;}
}
sub saveseg
{
my $inw=$_[0];
my $int=$_[1];
#$ena=decode('utf-8',$inw);
$ena=$inw;
print("grep \"^$ena\\s\" $jieba\n");
my $list=system("grep \"^$ena\\s\" $jieba");
if($list ==256)
{
open(FD89 ,">>$jieba");
print FD89 "$inw 2500 $int\n";
close FD89;
return 1;
}
return 0;
}
sub get_rela
{
my $txt=$_[0];
my $socket = IO::Socket::INET->new(
PeerAddr => "127.0.0.1",
PeerPort => "11221",
Type => SOCK_STREAM,
Proto => "tcp",
)
or die "Can not create socket connect.$@";
$socket->autoflush(1);
$sel = IO::Select->new($socket);
$socket->send("$txt rela\n",0); ##发送消息至服务器端。
#print "分词 $txt\n";
my $i;
#print "$i socket\n";
while (my @ready = $sel->can_read) { ##等待服务端返回的消息
foreach my $fh (@ready) {
if ($fh == $socket) {
while (<$fh>) {
$i=$_;
}
$sel->remove($fh);
close $fh;
}
}
}
chomp($i);
$i=~s/.*?\/(.*?) /$1/g;
return $i;
}
sub _ispos
{
my $txt=$_[0];
my $socket = IO::Socket::INET->new(
PeerAddr => "127.0.0.1",
PeerPort => "11222",
Type => SOCK_STREAM,
Proto => "tcp",
)
or die "Can not create socket connect.$@";
$socket->autoflush(1);
$sel = IO::Select->new($socket);
$socket->send("$txt 1 pos\n",0); ##发送消息至服务器端。
#print "分词 $txt\n";
my $i;
#print "$i socket\n";
while (my @ready = $sel->can_read) { ##等待服务端返回的消息
foreach my $fh (@ready) {
if ($fh == $socket) {
while (<$fh>) {
$i=$_;
}
$sel->remove($fh);
close $fh;
}
}
}
chomp($i);
$i=~s/.*?\/(.*?) /$1/g;
return $i;
}
sub key_word
{
my $txt=$_[0];
my $socket = IO::Socket::INET->new(
PeerAddr => "127.0.0.1",
PeerPort => "11222",
Type => SOCK_STREAM,
Proto => "tcp",
)
or die "Can not create socket connect.$@";
$socket->autoflush(1);
$sel = IO::Select->new($socket);
$socket->send("$txt 1 key\n",0); ##发送消息至服务器端。
#print "分词 $txt\n";
my $i;
#print "$i socket\n";
while (my @ready = $sel->can_read) { ##等待服务端返回的消息
foreach my $fh (@ready) {
if ($fh == $socket) {
while (<$fh>) {
$i=$_;
}
$sel->remove($fh);
close $fh;
}
}
}
chomp($i);
$i=~s/.*?\/(.*?) /$1/g;
return $i;
}
sub tonum
{
my $cont=$_[0];
$cont=decode("utf-8",$cont);
my @ta=split("",$cont);
my @m_res;
my $gres;
my $start=0;
my @tmp_str;
for(0 .. scalar(@ta)-1)
{
my $nnt=$_;
# print "ont $start $ta[$nnt] $ctmp\n";
my $ctmp=encode("utf-8",$ta[$nnt]);
if($ctmp=~/\d|零|一|两|二|三|四|五|六|七|八|九|十|百|千|万|亿/)
{$start=1;}
#print "now $ctmp ";
if(($start==1)&&($ctmp=~/\d|块|点|零|一|二|三|四|五|六|七|八|九|十|百|千|万|亿|本|头|两|斤|吨|个|元|微|纳|米|块|尺|寸|厘|毫|克|个|桶||匹|条|只|>套|件|张|次|台|面|捆|斤|吨|辆|扇|栋|片|扇|\./))
{
if($ctmp=~/斤|本|头|块|吨|个|元|块|克|个|匹|条|只|套|件|张|次|台|面|捆|斤|吨|辆|扇|栋|片|米|扇|尺|寸|厘|毫|桶/)
{$start=0;push (@tmp_str,"_");push (@tmp_str,$ctmp); my $onestr=join("",@tmp_str);push(@m_res,$onestr); @tmp_str=();}
#{$start=0;push (@tmp_str,"_");push (@tmp_str,$ctmp);push (@tmp_str,"_$nnt"); my $onestr=join("",@tmp_str);push(@m_res,$onestr); @tmp_str=();}
else{
# print " in $ctmp \n";
push (@tmp_str,$ctmp);
}
}
elsif(($start==1)&&($ctmp !~/\d|块|零|点|头|一|二|三|四|五|六|七|八|九|十|百|千|万|亿|本|两|斤|元|块|吨|个|克|个|匹|桶|条|只|厘|毫|套|件|张|次|>台|面|捆|斤|吨|辆|扇|栋|米|尺|寸|片|扇|\./))
{$start=0;
# print "stop $ctmp \n";
# my $one=scalar(@tmp_str);
# if(($one >$la)&&($one <$lb))
if(($ctmp !~/斤|本|头|吨|块|个|元|块|克|个|匹|微|纳|条|只|套|件|张|次|台|面|捆|斤|吨|辆|扇|栋|片|米|扇|尺|寸|桶|厘|毫/))
{
#@tmp_str=();
my $onestr=join("",@tmp_str);push(@m_res,$onestr);@tmp_str=();
next;
}
else
{
my $onestr=join("",@tmp_str);push(@m_res,$onestr);@tmp_str=();
}
#{ my $onestr=join("",@tmp_str);push(@m_res,$jid." ".$onestr)}
}
# else{print "other $ctmp\n";}
}
my $onestr=join("",@tmp_str);push(@m_res,$onestr);@tmp_str=();
my $g_res=join(" ",@m_res);
#print $g_res,"gres \n";
$g_res;
}
sub parser_tj
{
my $in=$_[0];
$in=decode("utf-8",$in);
my @ta=split("",$in);
my $s=0;
my @tmp_str;
my @mres;
for(0 .. scalar(@ta)-1)
{
my $nn=$_;
my $ns=encode("utf-8",$ta[$nn]);
if($ns=~/\d|零|点|\.|一|两|二|三|四|五|六|七|八|九|十|百|千|万|亿/)
{
$s++;
push (@tmp_str,$ns);
}
elsif($s>0)
{
my $onestr=join("",@tmp_str);push(@mres,$onestr);@tmp_str=();
push (@tmp_str,$ns);
$s=0;
}
else{
push (@tmp_str,$ns);
}
#push (@tmp_str,$ns);
if($nn == (scalar(@ta)-1))
{my $onestr=join("",@tmp_str);push(@mres,$onestr);}
}
@mres;
}