zhangbo-NLP-plugin_eng/lib/zhangbo/NLP/chat_server_lj.pl
#!/usr/bin/perl
use POSIX;
use Storable;
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`;
our $wordw;
our $wordcus;
our $wordt;
our $wordcust;
our $s_w_hash;
our @s_hash;
our @s_arr;
our @s_arr_cus;
our $s_arr_cus;
our $s_arr;
#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;
#our %allr :shared;
#our @allr_res :shared;
our @allr_res;
our $allr_context;
#our $iques :shared;
#print "join yg reg\n";
#my $ygid = threads->new("yinguo","yinguo",$input,$uid,$pid);
#$ygid->detach;
our $hash;
open(FDst,"stop.txt");
our $hash_st;
while(<FDst>)
{
my $line;
$line=$_;
chomp($line);
$hash_st->{$line}=1;
}
close FDst;
open(FD, "0sy.txt");
my $sytmp;
while(<FD>)
{
my $line=$_;
if($line=~/\#(.*)/)
{
$tsy=$1;
if(!exists($sytmp->{$tsy}))
{
$sytmp->{$tsy}=$tsy;
$eat=1;
# print "id=$tsy\n";
}
else
{
$eat=0;
}
}
elsif($eat==1)
{
chomp($line);
if(length($tsy)<=3)
{next;}
if(length($line)<=3)
{next;}
$hash->{$line}->{$tsy}=0.2;
# $hash->{$tsy}->{$line}=0.2;
# print "-$tsy-$line\n";
}
}
close FD;
open(FD, "0word.txt");
my $tmp=();
while(<FD>)
{
#DEF={Unit|单位:host={information|信息:belong={computer|电脑}}}
#DEF={tool|用具:modifier={able|能:scope={bring|携带:content={$}}},{listen|听:content={music|音乐},instrument={~}}}
$line=$_;
chomp($line);
if($line=~/^W_C=(.*)/)
{ $tmp=$1;
$r=1;
}
elsif($line=~/^DEF=(.*)/)
{$r=0;
$tmp_m=$1;
if(length($tmp)<=3)
{next;}
if(exists($hash_st->{$tmp}))
{next;}
my @arry=split('\|',$tmp_m);
foreach $tmps (@arry)
{
if($tmps=~/(.*?)\:/)
{ my $m=$1;
if(exists($hash_st->{$m}))
{next;}
if(($m eq "专")||($m eq "功能词")||($m eq "人")||($m eq "事情")||($m eq "时间")||($m eq "特定")||($m eq "部件")||($m eq "地方"))
{next;}
if(exists($hash_sc->{$m}))
{
if($r==0)
{
$hash->{$tmp}->{$m}=$hash_sc->{$m}->{"h"};
}
else{ $hash->{$tmp}->{$m}=$hash_sc->{$m}->{"h"}*0.2;}
if($hash->{$tmp}->{$m}<0.01){
$hash->{$tmp}->{$m}=0.1;}
}
else
{ $hash->{$tmp}->{$m}=0.1;
}
$hash_mn->{$tmp}+=0.1;
$r++; }
elsif($tmps=~/(.*?)\}/)
{ my $m=$1;
if(($m eq "专")||($m eq "功能词")||($m eq "人")||($m eq "事情")||($m eq "时间")||($m eq "特定")||($m eq "部件")||($m eq "地方"))
{next;}
if(exists($hash_st->{$m}))
{next;}
if($r==0)
{
if(exists($hash_sc->{$m}))
{
$hash->{$tmp}->{$m}=$hash_sc->{$m}->{"h"};
if($hash->{$tmp}->{$m}<0.01){
$hash->{$tmp}->{$m}=0.2;}
}
else
{
$hash->{$tmp}->{$m}=0.1;
# print FD_log $hash->{$tmp}->{$m}," $m\n";
}
$hash_mn->{$tmp}+=0.1;
}
else
{
if(exists($hash_sc->{$m}))
{
$hash->{$tmp}->{$m}=$hash_sc->{$m}->{"h"}*(0.5);
if($hash->{$tmp}->{$m}<0.01){
$hash->{$tmp}->{$m}=0.2;}
# print FD_log $hash->{$tmp}->{$m}," $m\n";
}
else
{
$hash->{$tmp}->{$m}=0.1;
# print FD_log $hash->{$tmp}->{$m}," $m\n";
}
$hash_mn->{$tmp}+=0.2;
}
$r++;
}
}
}
}
close FD;
#load_dic("gen_me_topic_all2.txt",20,1);
load_ljdic("concept_su.txt");
my @globcus=glob("/tmp/chat_*.kb");
foreach my $gone(@globcus)
{
if($gone=~/.*?chat_(.*?).kb/)
{
load_ljdic_cus($gone,$1);
}
}
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;
our $inputor;
POE::Component::Server::TCP->new(
Alias => "echo_server",
Port => 38888,
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;
$inputor=$2;
}
if(length($uid)<1)
{$uid=0;}
## $ljw
#print $allr_res[$pid],"debug $pid-------\n";
print @{$process_list}[$np]."\n";
my @child_thread;
#print "$uid |$input \n";
my $hasqa=0;
my $ljres;
my ($process_list,$ljw,$rrr3)=whatdoing($input,$uid,$pid);
$ljres .=$rrr3;
## $cont
#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;
#$child_thread[$np]->join;
#my $c=@{$process_list}[$np] ;
#eval { $c \($c,$input,$uid,$pid\);};
#my $aa="echo ";
#my $bbbb="bbbbbbbbbbb";
#eval "$c($c,$input,$uid,$pid)";
#eval "$c($c,$input,$uid,$pid)";
#eval "$aa $bbbb";
#print "执行 @{$process_list}[$np](@{$process_list}[$np],$input,$uid,$pid);\n";
# if( @{$process_list}[$np] =~/^q|^a/)
# {$hasqa=1;}
#}
#if(0)
if($input =~/(.*?)和(.*)/)
{
## $input
my $lana=length($1);;
my $lanb=length($2);;
if(($lana <12 ) &&($lanb <12 ))
{
$ljres=between_w($input,$uid);
}
## 和
## $ljres
}
if($input=~/清空记忆/)
{
delete($wordcus->{$uid});
delete($wordcust->{$uid});
delete($s_arr_cus->{$uid});
unlink("/tmp/chat_$uid.fun");
unlink("/tmp/chat_$uid.same");
unlink("/tmp/chat_$uid.dic");
unlink("/tmp/chat_$uid.kb");
unlink("/tmp/allq_$uid.kb");
unlink("/tmp/que_$uid.dic");
unlink("deep_rule/$uid.add");
system("./client_search_muti.pl reload_dic no $uid 0 ");
system("./client_search_muti.pl reload_dic no $uid".".rul 0 ");
$ljres .="全部忘记ok";
}
elsif($input=~/^推理(.*)/)
{
$ljres .=deeptl($1,$uid);
}
elsif($input=~/^想想(.*)/)
{
$ljres .=deep_search($1,$uid);
}
elsif($input=~/^推荐(.*)/)
{
$ljres .=_shoping_has($1,$uid);
}
elsif($input !~/记住/){
#my $ljresxi2=_mingyan($input,$uid);
#$ljres.=$ljresxi2;
$ljres .=$allr_res[$ppid];
### now
### $ljres
$ljres .=_lj_("_lj_",$input,$uid,$pid,$ljw,$process_list);
if(($input eq $allr_context->{$uid}->{'lastone'}) && ($allr_context->{$uid}->{'cont'}==1))
{
$allr_context->{$uid}->{'cont'}=0;
my $rand=rand();
if($rand>0.5)
{
$ljres .=_mingyan_has($input,$uid);
}
else
{
$ljres .=_shoping_has($input,$uid);
}
#
}
else
{
$allr_context->{$uid}->{'cont'}=1;
}
$allr_context->{$uid}->{'lastone'}=$input;
## $ljresxi2
## $process_list
# $ljres=eval ("_lj_",$input,$uid,$pid,$ljw);
### $ljres
}
### $rrr3
#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($ljres);
#$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 get_key_lj
{
my $in=$_[0];
my $resa;
my $resb;
my $vin;
my $alli=seg_txt($in);
my @allk=split(" ",$alli);
for(0 .. scalar(@allk)-1)
{
my $wn=$_;
my $wpos=_ispos($allk[$wn]);
if($wpos=~/v|n/)
{
## $wpos
$resa=$allk[$wn];
}
}
##1 $resa
my $alnk=scalar(@allk);
## $alnk
if(($alnk==2)&&(length($in)<=5))
{
## 88888888888888888
## $alli
return($in,"");
}
elsif(($alnk<2))
{
return($in,"");
}
## $alnk
## 999999999999
my $pas=`./client_7754.pl $in`;
my @par=sptree($pas);
## @par
my $awo=scalar(@par)-1;
for( 0 .. $awo)
{
my $nowan=$_;
print "$par[$nowan]\n";
if($par[$nowan]=~/.*?东西/)
{
next;
}
elsif($par[$nowan]=~/.*?AS (.*)/)
{
if($par[$nowan-1]=~/.*? (.*)/)
{
$resa=$1;
## no
## $resa
}
}
elsif($par[$nowan]=~/IP|NPNR|NPNN|NN/)
{
my $w11;
if($par[$nowan]=~/.*? (.*)/)
{
$w11=$1;;
}
## $w11
if($w11 =~/组成|制作|怎么办|了|用处/)
{
## $w11
my $guest_hobby=seg_txt($in);;
## $guest_hobby
my @ag=split(" ",$guest_hobby);
$resa=$ag[0];
}
else{
$resa=$w11;
}
## $resa
}
if($par[$nowan]=~/.*?VPVV (.*)/)
{
if($1 eq $resa)
{next;}
$vin=$1;
}
if($par[$nowan]=~/.*?NPNN (.*)/)
{
if($1 eq $resa)
{next;}
$vin=$1;
}
}
## $vin
if($in=~/为什么|带来|为啥/)
{
$resb .="Causes ";
}
if($in=~/是什么|谁/)
{
$resb .="IsA MotivatedByGoal ";
}
if($in=~/能|会|用处/)
{
$resb .="CapableOf ";
}
if($in=~/哪|地方|在|放|去哪里/)
{
$resb .="AtLocation ";
}
if($in=~/需要|要|怎么办/)
{
$resb .="MotivatedByGoal HasFirstSubevent ";
}
if($in=~/有/)
{
$resb .="HasSubevent HasProperty";
}
if($in=~/不|怕/)
{
$resb .="Desires CausesDesire ";
}
if($in=~/用/)
{
$resb .="UsedFor ";
}
if($vin=~/含有|做|部分|成份|组成|制作|构成|结构|成份|成分|零件|组件|元素/)
{
$resb .="PartOf Madeof ";
}
if($vin=~/表示|代表|意思|象征|象|是/)
{
$resb .="SymbolOf ";
}
if(length($resb)<3)
{$resb="PartOf SymbolOf UsedFor Madeof MotivatedByGoal HasFirstSubevent HasSubevent HasProperty Causes Desires AtLocation IsA CausesDesire CapableOf ";}
## $resa
## $resb
return($resa,$resb);
}
sub whatdoing
{
my $in=$_[0];
my $uid=$_[1];
my $pid=$_[2];
my @mres;
#my $one=sure($in);
#push(@{$allr_context->{$uid}->{'time'}},$in);
## $allr_context
#push(@{$allr_context->{$uid}->{'lastin'}},$in);
#生活,艺术,体育,科学,政治,经济,娱乐,音乐,电影
#if(length($one)>1)
#{
# $allr_res[$pid]="ans:dirct $one";
# return "";
#}
my $ljway;
my $rr3;
#my $pas=`./client_7754.pl $in`;
#if($in=~/表示|代表|意思|象征|象|是/)
#{
# $ljway .="SymbolOf ";
# 3782 UsedFor";
#}
#<b>(ROOT (IP (NP (DP (DT 什么)) (NP (NN 东西))) (VP (VV 可以) (VP (VV 制作) (NP (NN 冰淇淋))))))<b>
#if($pas=~/.*?DP \(DT 什么\).*?\(VP \(VV [部分|成份|组成|制作|构成|结构|成份|成分|零件|组件|元素]\).*NP \(NN (.*?)\)/)
# {
# $ljway .="Madeof $1";
#$ljway .="UsedFor $1";
# }
#PartOf
# if($in=~/部分|成份|组成|制作|构成|结构|成份|成分|零件|组件|元素/)
# {
# $ljway .="PartOf ";
# }
#MadeOf 字 由 组成
#3051 MotivatedByGoal
#if(($in=~/^什么$/)&&(length($in)<3)||($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");
#semtic("semtic",1,$uid,$pid);
#}
my $noen=0;
#if(($in=~/如果|因为|所以|因此|就|因为/)||($in=~/^请记住.*?推理.*/))
#{
# $noen=1;
#push(@mres,"learn_because");
#$rr3=learn_because("learn_because",1,$uid,$pid);
#}
if($in=~/关系|联系|关联|有关/)
{
#push(@mres,"between");
between("between",1,$uid,$pid);
}
if($in=~/请忘记/)
{
# push(@mres,"forgot");
forgot("forgot",1,$uid,$pid);
}
if($in=~/^是$|是的$|^对|记住/)
{
#push(@mres,"record");
if($in=~/^记住.*?推理.*/)
{$rr3=learn_because("learn_because",1,$uid,$pid);}
else
{
$rr3=record("record",1,$uid,$pid);
}
#push(@mres,"forgot");
}
#if($in=~/谁|哪.*大|小|多|少/)
#{
# algebra_comp("",1,$uid,$pid);
#}
if(($in=~/\?|\?|多远|多少|你知道|请问|什么|几|谁|哪里|哪|何时|啥|吗|怎么|怎样|多大|多高|多重|嘛|么$/)&&($in !~/记住/))
{
#代数处理系统|
if(($in=~/多少|几|多/)&&($in=~/本|斤|桶|元|两|吨|个|件|元|米|块|尺|寸|厘|毫|克|个|匹|头|条|只|套|件|张|次|台|面|捆|斤|吨|辆|扇|栋|片|扇/)&&($in !~/哪个|面积|体积/))
{
if($in=~/累计|合计/)
{
# push(@mres,"algebra_count");
algebra_count("algebra_count",1,$uid,$pid);
}
else{
push(@mres,"algebra");
algebra("algebra",1,$uid,$pid);
}
}
elsif($in=~/^求|面积/)
{
# push(@mres,"algebra_equ");
algebra_equ("algebra_equ",1,$uid,$pid);
}
elsif($in=~/加|减|乘|除/)
{
push(@mres,"cal");
cal("cal",1,$uid,$pid);
}
#elsif($in=~/哪种|那几种|哪些|哪类|哪几类|属于|包含|包括|同类|是什么|是谁|什么/)
else
{
# my $str=seg_txt($in);
# my $str=~s/ /\,/g;
my $soloway=get_mem2($in,"ques99",4,0.1);
my @solo=split("\n",$soloway);
#哪种.*可以.* reg_哪种(.*?)可以(.*) p_exp_($resout[1][0]) p_ent_has($resout[2],$resout[1][1])
my @resout;
$resout[0]=$in;
my @p=split(" ",$solo[0]);
## @p
print "$solo[0]=================================\n" ;
for(1 ..scalar(@p))
{
my $step=$_;
if($p[$step]=~/^reg_(.*?)<res>(.*)/)
{
my $reg=$1;
my $resp=$2;
if($resout[$step-1]=~/$reg/)
{
for(1 .. $resp)
{
push(@{$resout[$step]},${$_})
}
}
}
elsif($p[$step]=~/^p_(.*?)\((.*?)\)/)
{
my $func=$1;
my $arg=$2;
$arg .=",".$uid;
### $func
### $arg
### $uid
my $reseval=eval "$func($arg)";
### $reseval
$resout[$step]=$reseval;
#eval("$func($arg);");
}
else
{
print " $p[$step] errrrrrrrrrrrrr\n";
}
}
### @resout
my $laststr=scalar(@resout);
my $laststrn=length($resout[$laststr-1]);
if($laststrn >3)
{
$allr_res[$pid] ="ans:abstr $resout[$laststr-1]\n";
}
## @soloway
# chouxiang();
}
# push(@mres,"qa");
$rr3.=qa("qa",1,$uid,$pid);
### $rr3
# push(@mres,"qaf");
}
else
{
if($noen==1)
{}
else{
#push(@mres,"learn_en");
learn_en("learn_en",1,$uid,$pid);
}
#push(@mres,"qa_reg");
#push(@mres,"learn_equa");
#push(@mres,"_lj_");
#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,$ljway,$rr3);
}
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=$inputor;
#my $fin=$_[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=$inputor_;
#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=$inputor;
#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]\n";
$allr_res[$ppid] .="$tp\n";
}
sub algebra_count
{
my $all=$_[1];
my $fin=$inputor;
#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=$inputor;
#my $all=$_[1];
my $fin=$inputor;
#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 algebra_comp
{
my $all=$inputor;
#my $all=$_[1];
my $fin=$inputor;
#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=$inputor;
#my $all=$_[1];
my $fin=$inputor;
#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 $dn;
if( $conf[0]=~/1-(\d+)-/)
{
$dn=$1;
delete_kb("/tmp/chat_$uuid.dic",$dn);
}
#my $tp="确定我忘记 $conf[0] 这个记忆吗?(回答“是”执行忘记)";
my $tp="执行忘记";
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=$inputor;
#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=$inputor;
#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 $fin=$inputor;
my $uuid=$_[2];
my $ppid=$_[3];
if(length($uuid)<2)
{return;}
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=$inputor;
my $uuid=$_[2];
my $ppid=$_[3];
my $trandin=rand();
my $trand=rand();
my $tp;
print "qa $f4-$0-$$- $fin $uuid $ppid".$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;
my $s;
my $ssss=(stat "/tmp/chat_$uuid.dic")[7]."\n";
print $ssss;
if(length($uuid)<1)
{
}
elsif(!( -z "/tmp/chat_$uuid.dic"))
{
print "执行 /mnt/sdb/shell2/art_res.pl /tmp/ask_$trandin /tmp/chat_$uuid.dic /tmp/$trand\n";
$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;;
}
}
$res_tmp=~s/\n/ /g;
$tp .=$res_tmp;
}
else
{
$res_tmp=~s/\n/ /g;
$tp .=$res_tmp;
# my $best=bestans($fin,$res_tmp);
# $tp .="ans:qa best $best\n";
}
#$allr_res[$ppid] .=$tp."\n";
#print "推理机结果$tp--$ppid--$allr_res[$ppid]\n";
#$allr{$f4}=`$iqa \"$fin\"`;
# 加速1秒 qa $allr{$ppid}
$allr{$ppid}++;
#unlink("/tmp/ask_$trandin");
my $qares=$tp."\n";
$qares
}
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]=~/\+|\-|\*|\//)
{
1
}
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=$inputor;
$fin=~s/请记住//g;
$fin=~s/记住//g;
#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)
{
system("./client_search_muti.pl addone $uuid $fin");
# my $soloway=get_mem2($in,"ques99",1,0.1);
learn_soloway($fin,$uuid);
open(FDkb,">>/tmp/chat_$uuid.kb");
if($fin=~/(.*?)是一种(.*)/)
{
print FDkb "$1\t$2\t$1 是一种 $2\tIsA\n";
word_ljaddone("$1\t$2\t$1 是一种 $2\tIsA",$uuid);
# learn_shangxia($1,$2,$uuid);
}
elsif($fin=~/(.*?)是(.*?)的(.*)/)
{
print FDkb "$1\t$3\t$1 是 $3\tIsA\n";
word_ljaddone("$1\t$3\t$1 是 $3\tIsA",$uuid);
# learn_shangxia($1,$3,$uuid);
# learn_exp($1,$2,$uuid);
}
elsif($fin=~/(.*?)的(.*?)有(.*)/)
{
my $abg=$1."的".$2;
my $acg=$3;
print "学习 $abg $acg $uuid\n";
# learn_exp_sub($abg,$acg,$uuid);
}
elsif($fin=~/(.*?)的(.*?)是(.*)/)
{
my $abg=$1."的".$2;
my $acg=$3;
# learn_exp_sub($abg,$acg,$uuid);
}
elsif($fin=~/(.*?)是(.*)/)
{
print FDkb "$1\t$2\t$1 是 $2\tIsA\n";
word_ljaddone("$1\t$2\t$1 是 $2\tIsA",$uuid);
# learn_exp($1,$2,$uuid);
}
elsif($fin=~/(.*?)在(.*)/)
{
print FDkb "$1\t$2\t$1 在 $2\tAtLocation\n";
word_ljaddone("$1\t$2\t$1 在 $2\tAtLocation",$uuid);
# learn_shangxia($1,$2,$uuid);
}
elsif($fin=~/(.*?)位于(.*)/)
{
print FDkb "$1\t$2\t$1 在 $2\tAtLocation\n";
word_ljaddone("$1\t$2\t$1 在 $2\tAtLocation",$uuid);
# learn_shangxia($1,$2,$uuid);
}
elsif($fin=~/(.*?)所以(.*)/)
{
print FDkb "$1\t$2\t$1 所以 $2\tCauses\n";
word_ljaddone("$1\t$2\t$1 所以 $2\tCauses",$uuid);
}
elsif($fin=~/(.*?)能|会(.*)/)
{
print FDkb "$1\t$2\t$1 能 $2\tCapableOf\n";
word_ljaddone("$1\t$2\t$1 能 $2\tCapableOf",$uuid);
}
elsif($fin=~/(.*?)需要(.*)/)
{
# 489 $resb .="MotivatedByGoal HasFirstSubevent ";
print FDkb "$1\t$2\t$1 需要 $2\tMotivatedByGoal\n";
word_ljaddone("$1\t$2\t$1 需要 $2\tMotivatedByGoal",$uuid);
}
elsif($fin=~/(.*?)有(.*)/)
{
# $resb .="HasSubevent HasProperty";
print FDkb "$1\t$2\t$1 有 $2\tHasSubevent\n";
print FDkb "$1\t$2\t$1 有 $2\tHasProperty\n";
word_ljaddone("$1\t$2\t$1 有 $2\tHasSubevent",$uuid);
word_ljaddone("$1\t$2\t$1 有 $2\tHasProperty",$uuid);
# learn_shangxia($1,$2,$uuid);
# learn_exp($1,$2,$uuid);
}
elsif($fin=~/(.*?)怕(.*)/)
{
#$resb .="Desires CausesDesire ";
print FDkb "$1\t$2\t$1 害怕 $2\tDesires\n";
word_ljaddone("$1\t$2\t$1 害怕 $2\tDesires",$uuid);
}
elsif($fin=~/(.*?)用(.*)/)
{
# $resb .="UsedFor ";
print FDkb "$1\t$2\t$1 害怕 $2\tDesires\n";
word_ljaddone("$1\t$2\t$1 害怕 $2\tDesires",$uuid);
}
elsif($vin=~/(.*?)[含有|做|部分|成份|组成|制作|构成|结构|成份|成分|零件|组件|元素](.*)/)
{
# $resb .="PartOf Madeof ";
print FDkb "$1\t$2\t$1 是 $2 的一部分\tPartOf\n";
print FDkb "$1\t$2\t$1 是 $2 的一部分\tMadeof\n";
word_ljaddone("$1\t$2\t$1 是 $2 的一部分\tMadeof",$uuid);
word_ljaddone("$1\t$2\t$1 是 $2 的一部分\tPartOf",$uuid);
}
elsif($vin=~/(.*?)[表示|代表|意思|象征|象|是](.*)/)
{
print FDkb "$1\t$2\t$1 表示 $2\tSymbolOf\n";
word_ljaddone("$1\t$2\t$1 表示 $2\tSymbolOf",$uuid);
}
## $wordcus
## $wordcust
## $s_arr_cus
close FDkb;
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}++;
$tp;
}
sub _lj_
{
my $f4=$_[0];
my $fin=$_[1];
$fin=~s/ /\,/g;
my $uuid=$_[2];
my $ppid=$_[3];
my $ljz=$_[4];
my $other=$_[5];
## $other
my $all=$fin;
my @gol;
my $res;
my $tp;
#my $rn=scalar(@{$other})-1;
## $rn
#for (0 .. $rn)
#{
#my $c=@{$other}[$_];
#eval "$c($c,1,$uuid,$ppid)";
#print "============================= $c($c,1,$uuid,$ppid)\n";
#}
my $strone;
my @one;
my @one2;
my @cus;
# 猫怎么抓老鼠
#阿拉斯加的猫抓老鼠
#猫
#猫老鼠
my $rand=rand();
#if($rand>0.7)
# {
# $allr_res[$ppid]=_mingyan_has($fin,$uuid);
# }
# unlink("/tmp/$rand.rule");
# unlink("/tmp/$rand.fact");
#$allr_res[$ppid].=deep_search($fin,$uuid);
if(( length ( $allr_res[$ppid]) < 18))
{
my $sl=seg_txt($fin);
chomp($sl);
my @allk2=split(" ",$sl);
my ($ky1,$ywc)=delywc(@allk2);
## $ky1
## $ywc
my @allk=split("\,",$ky1);
my $alnn=scalar(@allk);
## $alnn
if($alnn <= 1)
{
my $lengthm;
if(0)
{
}
else
{
my ($keywo,$ljz)=get_key_lj($fin);
## $keywo
## $ljz
## $uuid
if(length($ljz)>0)
{
@one=get_wordljlist($keywo,"y",$ljz);
@cus=get_wordljlist_cus($keywo,"y",$ljz,$uuid);
## @cus
push(@one,@cus);
}
else
{
@one=get_wordljlist_more($keywo);
@cus=get_wordljlist_cus($keywo,"","",$uuid);
## @one
## @cus
foreach my $onek (@one)
{
my $nonek=get_log($keywo,$onek);
## $nonek
push (@one2,$nonek);
}
foreach my $onek (@cus)
{
my $cusk=get_log_cus($keywo,$onek,$uuid);
## $cusk
push (@one2,$cusk);
}
}
$strone .=join(" ",@one2);
$strone .=join(" ",@one);
my $c=_mingyan2_has($fin,"mingyan100");
my $cn=length($c);
## $c
if($cn>18)
{
$strone .=$c;
}
my $singln=length($strone);
if($singln < 10)
{
my $strone2 =wnsock("vsm_all_sock $fin",11221);
## $strone2
my @wlist=split(" ",$strone2);
foreach my $wone (@wlist)
{
if($wone=~/(.*?)\:.*/)
{
my $kw=$1;
## $kw
my @so=get_wordljlist_more($kw);
my @so1=get_wordljlist_cus($kw,"","",$uuid);;
push(@so,@so1);
my $nso=scalar(@so);
if( $nso >=1)
{
$hasone=1;
$strone .=join(" ",@so);
}
}
if($hasone==1)
{last;}
}
$strone2=~s/\:.*? /<sg>/g;
my @an=split("<sg>",$strone2);
my @c=@an[0 .. 3];
my $cstr=join("",@c);;
### $cstr
my $rand=rand();
if($rand>0.7)
{
# $strone .=deep_search($cstr,$uuid);
$strone .=_mingyan2_has($cstr,$uuid);
}
else
{
$strone .=_mingyan_has($cstr,$uuid);
}
}
my $singln=length($strone);
if($singln <20)
{
my $rand=rand();
if($rand>0.7)
{
# $strone .=deep_search($cstr,$uuid);
$strone =_mingyan2_has($fin,$uuid);
}
else
{
$strone =_mingyan_has($fin,$uuid);
}
}
}
}
else
{
## 长句子
my $st2;
#my ($ky1,$ywc)=delywc(@allk);
## $ky1
## $ywc
my $rand=rand();
if($rand>0.7)
{
#$strone=deep_search($fin,$uuid);
$strone=_mingyan2_has($fin,$uuid);
}
elsif($rand>0.5)
{
$strone=deep_search($fin,$uuid);
}
else
{
$strone=_mingyan_has($fin,$uuid);
}
my $jjn=scalar($strone);
if($jjn>20)
{
return "$allr_res[$ppid] ans:rand $strone\n";
}
my $strone2=wnsock("vsm_all_sock "."\"".$ky1."\"",11221);
#$strone .="\n";
## $strone
my $hasone=0;
my @senw;
my @wlist=split(" ",$strone2);
foreach my $wone (@wlist)
{
if($wone=~/(.*?)\:.*/)
{
my $kw=$1;
push(@senw,$kw);
}
}
## $strone2
## @senw
my $u=$uuid;
for(0 .. 20)
{
my $sn=$_;
my $s1=$senw[$sn];
for( $sn+1 .. 20)
{
my $s2=$senw[$_];
## $s1
## $s2
if(exists($wordcus->{$u}))
{
if(exists($wordcus->{$u}->{$s1}))
{
if(exists($wordcus->{$u}->{$s1}->{$s2}))
{
$strone .=get_log_cus($s1,$s2,$u);
$hasone=1;
}
}
}
if(exists($word->{$s1}))
{
if(exists($wordw->{$s1}->{$s2}))
{
$strone .=get_log($s1,$s2);
$hasone=1;
}
}
if(exists($wordcus->{$u}))
{
if(exists($wordcus->{$u}->{$s2}))
{
if(exists($wordcus->{$u}->{$s2}->{$s1}))
{
$strone .=get_log_cus($s2,$s1,$u);
$hasone=1;
}
}
}
if(exists($wordw->{$s2}))
{
if(exists($wordw->{$s2}->{$s1}))
{
$strone .=get_log($s2,$s1);
$hasone=1;
}
}
## $strone
my $lst=length($strone);
if(($hasone==1)&&($lst > 60)){
last;}
}
}
my $lstr=length($strone);
if($lstr < 12)
{
#between_w($senw[$ini],$senw[$ini2]);
my $hasr=0;
for(0 .. 20)
{
my $sn=$_;
my $s1=$senw[$sn];
for( $sn+1 .. 20)
{
my $s2=$senw[$_];
my $jstr=_mingyan2_has($s2,"mingyan100");
# my $jstr=between_w("$s1和$s2",$uuid);
## $s1
## $s2
## $jstr
# exit;
my $lstrn=length($jstr);
if($lstrn>22)
{
$strone .=$jstr;
$hasr=1;
}
if($hasr ==1)
{last;}
}
if($hasr ==1){last;}
}
}
#$strone .=$strone2;
}
my $ln=length($strone);
if ($ln <6)
{
my $rand=rand();
if($rand>0.7)
{
$strone=deep_search($fin,$uuid);
}
elsif($rand>0.5)
{
$strone=_mingyan2_has($fin,$uuid);
}
else
{
$strone=_mingyan_has($fin,$uuid);
}
}
return "$allr_res[$ppid] ans:_lj_ $strone\n";
}
else
{
return "$allr_res[$ppid]";
}
}
sub between_w
{
# learn_new_word();
my $j=$_[0];
my $uid=$_[1];
my @wlist;
#my $wl=seg_txt($j);
#=split(" ",$wl);
if($j=~/(.*?)和(.*)/)
{
$wlist[0]=$1;
$wlist[2]=$2;
}
else
{
my $wl=seg_txt($j);@wlist=split(" ",$wl);
}
my $res;
#print "pushn $j\n";
# $j=~s/\:.*//g;
# push(@gol,$j);
my $chash;
my $doublec;
my $doublec2;
my $hasres=0;
my @one=get_wordljlist($wlist[0]);
my @cus=get_wordljlist_cus($wlist[0],"","",$uid);
push(@one,@cus);
my @two=get_wordljlist($wlist[2]);
my @cus2=get_wordljlist_cus($wlist[2],"","",$uid);
push(@two,@cus2);
## comb
## @cus
## @one
## @two
$res .="关联";
foreach my $j2 (@one)
{
$doublec->{$j2}++;
if($j2 eq $wlist[2])
{
$res .="$wlist[0]>>$wlist[2]:";
$res .="(1)".get_log($wlist[0],$wlist[2]);
$res .="(1)".get_log_cus($wlist[0],$wlist[2],$uid);
$hasres=1;
}
}
#if($hasres==1){ return $res;}
foreach my $j3 (@two)
{
$doublec2->{$j3}++;
if(exists($doublec->{$j3}))
{
$res .="$wlist[0]>>$j3>>$wlist[2]:";
$res .="(1)".get_log($wlist[0],$j3);
$res .="(1)".get_log_cus($wlist[0],$j3,$uid);
$res .="(2)".get_log($j3,$wlist[2]);
$res .="(2)".get_log_cus($j3,$wlist[2],$uid);
$hasres==1;
}
}
#if($hasres=1){ return $res;}
if($hasrs==0)
{
for(0 .. scalar(@one)-1)
{
my $nnnw=$one[$_];
if($nnnw eq $wlist[0])
{next;}
my @ones=get_wordljlist($nnnw);
my @onesadd=get_wordljlist_cus($nnnw,"","",$uid);
push(@ones,@onesadd);
## @ones
foreach my $j4(@ones)
{
if($j4 eq $wlist[2])
{
$res .="$wlist[0]>>$nnnw>>$j4>>$wlist[2]:";
$res .="(1)".get_log($wlist[0],$nnnw);
$res .="(1)".get_log_cus($wlist[0],$nnnw,$uid);
$res .="(2)".get_log($nnnw,$j4);
$res .="(2)".get_log_cus($nnnw,$j4,$uid);
$res .="(3)".get_log($j4,$wlist[2]);
$res .="(3)".get_log_cus($j4,$wlist[2],$uid);
$hasres=1;
return "ans:_gllj_ $res\n";
}
if(exists($doublec2->{$j4}))
{
$res .="$wlist[0]>>$nnnw>>$j4>>$wlist[2]:";
$res .="(1)".get_log($wlist[0],$nnnw);
$res .="(1)".get_log_cus($wlist[0],$nnnw,$uid);
$res .="(2)".get_log($nnnw,$j4);
$res .="(2)".get_log_cus($nnnw,$j4,$uid);
$res .="(3)".get_log($j4,$wlist[2]);
$res .="(3)".get_log_cus($j4,$wlist[2],$uid);
$hasres=1;
return "ans:_gllj2_ $res \n";
}
}
}
}
if($hasrs==0)
{
for(0 .. scalar(@two)-1)
{
my $nnnw=$two[$_];
if($nnnw eq $wlist[2])
{next;}
my @twos=get_wordljlist($nnnw);
my @twos2=get_wordljlist_cus($nnnw,"","",$uid);
push(@twos,@twos2);
## $nnnw
## @twos
foreach my $j4(@twos)
{
if($j4 eq $wlist[0])
{
$res .="$wlist[0]>>$nnnw>>$j4>>$wlist[2]:";
$res .="(1)".get_log($wlist[0],$nnnw);
$res .="(1)".get_log_cus($wlist[0],$nnnw);
$res .="(2)".get_log($nnnw,$j4);
$res .="(2)".get_log_cus($nnnw,$j4);
$res .="(3)".get_log($j4,$wlist[2]);
$res .="(3)".get_log_cus($j4,$wlist[2]);
$hasres=1;
return "ans:_gllj3_ $res\n";
}
if(exists($doublec->{$j4}))
{
$res .="$wlist[0]>>$nnnw>>$j4>>$wlist[2]:";
$res .="(1)".get_log($wlist[0],$nnnw);
$res .="(1)".get_log_cus($wlist[0],$nnnw);
$res .="(2)".get_log($nnnw,$j4);
$res .="(2)".get_log_cus($nnnw,$j4);
$res .="(3)".get_log($j4,$wlist[2]);
$res .="(3)".get_log_cus($j4,$wlist[2]);
$hasres=1;
return "ans:_gllj4_ $res \n";
}
}
}
}
my $tp .="ans:_gllj5_ $res \n";
$tp;
#$allr_res[$ppid] ="$tp\n";
}
sub get_log
{
my $w=$_[0];
my $w1=$_[1];
my $resz;
if(exists($wordw->{$w}->{$w1}))
{
while(my ($k1,$v1)=each(%{$wordw->{$w}->{$w1}}))
{
$resz .=$s_arr[$v1]."<sg>";
}
}
elsif(exists($wordw->{$w1}->{$w}))
{
while(my ($k1,$v1)=each(%{$wordw->{$w1}->{$w}}))
{
$resz .=$s_arr[$v1]."<sg>";
}
}
$resz;
}
sub get_log_cus
{
my $w=$_[0];
my $w1=$_[1];
my $cus=$_[2];
my $resz;
if(! exists($wordcus->{$cus}->{$w}))
{
return;
}
if(exists($wordcus->{$cus}->{$w}->{$w1}))
{
while(my ($k1,$v1)=each(%{$wordcus->{$cus}->{$w}->{$w1}}))
{
$resz .=$s_arr_cus->{$cus}[$v1]."<sg>";
}
}
elsif(exists($wordcus->{$cus}->{$w1}->{$w}))
{
while(my ($k1,$v1)=each(%{$wordcus->{$cus}->{$w1}->{$w}}))
{
$resz .=$s_arr_cus->{$cus}[$v1]."<sg>";
}
}
$resz;
}
sub get_wordljlist_cus
{
my $w=$_[0];
my $w1=$_[1];
my $fx=$_[2];
my $cus=$_[3];
## $fx
my @res;
## $wordcus
## $cus
if(exists($wordcus->{$cus}->{$w}))
{
## here
while(my ($k1,$v1)=each(%{$wordcus->{$cus}->{$w}}))
{
## $k1
if($w1 eq "y")
{
while(my ($k2,$v2)=each(%{$wordcus->{$cus}->{$w}->{$k1}}))
{
if(length($fx)>1)
{
if($fx=~/.*?$k2.*/)
{
push(@res,$k1."/$k2/$v2 @{$s_arr_cus->{$cus}}[$v2]|");
## $k2
}
}
else
{
push(@res,$k1."/$k2/$v2 @{$s_arr_cus->{$cus}}[$v2]|");
}
}
}
else{
# while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
# {
# push(@res,$k1."/$k2/$v2 $s_arr[$v2]|");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]|");
# }
push(@res,$k1);
}
}
}
@res;
}
sub get_wordljlist_cus2
{
my $w=$_[0];
my $w1=$_[1];
my $fx=$_[2];
my $cus=$_[3];
## $fx
my @res;
## $wordcus
## $cus
if(exists($wordcus->{$cus}->{$w}))
{
## here
while(my ($k1,$v1)=each(%{$wordcus->{$cus}->{$w}}))
{
## $k1
if($w1 eq "y")
{
while(my ($k2,$v2)=each(%{$wordcus->{$cus}->{$w}->{$k1}}))
{
if(length($fx)>1)
{
if($fx=~/.*?$k2.*/)
{
push(@res,"@{$s_arr_cus->{$cus}}[$v2]|");
## $k2
}
}
else
{
push(@res,"@{$s_arr_cus->{$cus}}[$v2]|");
}
}
}
else{
# while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
# {
# push(@res,$k1."/$k2/$v2 $s_arr[$v2]|");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]|");
# }
push(@res,$k1);
}
}
}
@res;
}
sub get_wordljlist
{
my $w=$_[0];
my $w1=$_[1];
my $fx=$_[2];
my $cust=$_[3];
## $fx
my @res;
my @res_add;
if(exists($wordw->{$w}))
{
while(my ($k1,$v1)=each(%{$wordw->{$w}}))
{
if($w1 eq "y")
{
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
if(length($fx)>1)
{
if($fx=~/.*?$k2.*/)
{
push(@res,"$s_arr[$v2]<sg>");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]<sg>");
}
}
else
{
push(@res,"$s_arr[$v2]<sg>");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]<sg>");
}
}
}
else{
push(@res,$k1);
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
push(@res_add,$k1."$s_arr[$v2]<sg>");
#push(@res_add,$k1."/$k2/$v2 $s_arr[$v2]|");
}
}
}
}
return @res;
}
sub get_wordljlist_more2
{
my $w=$_[0];
my $w1=$_[1];
my $fx=$_[2];
my $cust=$_[3];
## $fx
my @res;
my @res_add;
my $jsq=0;
if(exists($wordw->{$w}))
{
while(my ($k1,$v1)=each(%{$wordw->{$w}}))
{
if($w1 eq "y")
{
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
if(length($fx)>1)
{
if($fx=~/.*?$k2.*/)
{
push(@res,"$s_arr[$v2]<sg>");
}
}
else
{
push(@res,"$s_arr[$v2]<sg>");
}
}
}
else{
# push(@res,$k1);
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
push(@res_add,"$s_arr[$v2]<sg>");
$jsq++;
if($jsq>$w1)
{last;}
}
}
}
}
return @res_add;
}
sub get_wordljlist_more
{
my $w=$_[0];
my $w1=$_[1];
my $fx=$_[2];
my $cust=$_[3];
## $fx
my @res;
my @res_add;
if(exists($wordw->{$w}))
{
while(my ($k1,$v1)=each(%{$wordw->{$w}}))
{
if($w1 eq "y")
{
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
if(length($fx)>1)
{
if($fx=~/.*?$k2.*/)
{
push(@res,"$s_arr[$v2]<sg>");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]<sg>");
}
}
else
{
push(@res,$k1."$s_arr[$v2]<sg>");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]|");
}
}
}
else{
# push(@res,$k1);
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
push(@res_add,"$s_arr[$v2]<sg>");
#push(@res_add,$k1."/$k2/$v2 $s_arr[$v2]|");
}
}
}
}
return @res_add;
}
sub get_wordljlist_mut_key
{
my $w=$_[0];
my $w1=$_[1];
my $fx=$_[2];
my $cust=$_[3];
## $fx
my @res;
my @res_add;
$w=seg_txt($w);
my @kl=split(" ",$w);
if(exists($wordw->{$w}))
{
while(my ($k1,$v1)=each(%{$wordw->{$w}}))
{
if($w1 eq "y")
{
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
if(length($fx)>1)
{
if($fx=~/.*?$k2.*/)
{
push(@res,"$s_arr[$v2]<sg>");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]<sg>");
}
}
else
{
push(@res,"$s_arr[$v2]<sg>");
#push(@res,$k1."/$k2/$v2 $s_arr[$v2]<sg>");
}
}
}
else{
# push(@res,$k1);
while(my ($k2,$v2)=each(%{$wordw->{$w}->{$k1}}))
{
push(@res_add,"$s_arr[$v2]<sg>");
#push(@res_add,$k1."/$k2/$v2 $s_arr[$v2]|");
}
}
}
}
return @res_add;
}
sub maxent
{
}
sub fun2
{
my $f4=$_[0];
my $fin=$inputor;
#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=$inputor;
#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 =$inputor;
#my $all =$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
my $r3;
if($all=~/记住(.*?)推理(.*)/)
#if($all=~/请记住A属于B并且B属于C推理A属于C/)
{
my $intj=$1;
$intj=seg_txt($intj);
chomp($intj);
my $inres=$2;
$inres=seg_txt($inres);
chomp($inres);
$intj=~s/并且/ \&\& /g;
$intj=~s/([a-zA-z])/ \$$1 /g;
$inres=~s/([a-zA-z])/ \$$1 /g;
open(FD,">> /mnt/sdb/shell2/deep_rule/$uuid.add");
print FD $intj;
print FD " => ";
print FD $inres."\n";
close FD;
system("./client_search_muti.pl reload_dic /mnt/sdb/shell2/deep_rule/$uuid.add $uuid.rul rule");
my $tp .="ans:learn_because $intj => $inres";
# $allr_res[$ppid] .=$tp;
$r3 .=$tp;
}
elsif(($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:learn_because ";
$allr_res[$ppid] .=$tp;
}
$r3;
}
sub learn_en
{
my $all =$inputor;
#my $all =$_[1];
my $uuid=$_[2];
my $ppid=$_[3];
## learn_en
## $all
if(0)
#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");
if(($et=~/$tp/)||($tp=~/$et/))
{
$allr_res[$ppid] .="不能相互包含";
}
else
{
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);
$txt=~s/ /_/g;
$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;
}
sub get_sim
{
my $inw=$_[0];
my ($lhash)=txt2arr3("0 $inw");
my $big=0;
my $bigw;
while(my($wo,$h)=each(%{$s_w_hash}))
{
my $n=comp_hash($s_w_hash->{$wo},$lhash);
if($n>$big)
{
$big=$n;
$bigw=$wo;
}
}
return($bigw,$big);
}
sub load_ljdic
{
open(FDb, "$_[0]");
my $lsize=$_[1];
my $zoomin=0.5;
if($_[2]>0)
{$zoomin=$_[2];}
my $tmp=();
my $sid=0;
my $cc=0;
while(<FDb>)
{
$line=$_;
chomp($line);
my $name;
my @a=split("\t",$line);
#练肌肉 运动 因为 练肌肉 所以 运动 Causes
my $dob=0;
#@b=split(" ",$m);
$f=0;
my $ccn=0;
$wordw->{$a[0]}->{$a[1]}->{$a[3]}=$sid;
$wordt->{$a[0]}->{$a[3]}->{$a[1]}=$sid;
$s_arr[$sid]=$a[2];
$sid++;
}
close (FDb);
#store $s_w_hash, "$_[0].saveobj";
#store $s_arr, "$_[0].saveobj_sarr";
#store $wordw, "$_[0].saveobj_wordw";
#store $wordt, "$_[0].saveobj_wordt";
#}
print "load concept $_[0] ok\n";
}
sub load_ljdic_cus
{
open(FDb, "$_[0]");
my $cus=$_[1];
my $tmp=();
my $sid=0;
my $cc=0;
while(<FDb>)
{
$line=$_;
chomp($line);
my $name;
my @a=split("\t",$line);
#练肌肉 运动 因为 练肌肉 所以 运动 Causes
my $dob=0;
#@b=split(" ",$m);
$f=0;
my $ccn=0;
$wordcus->{$cus}->{$a[0]}->{$a[1]}->{$a[3]}=$sid;
$wordcust->{$cus}->{$a[0]}->{$a[3]}->{$a[1]}=$sid;
$s_arr_cus->{$cus}[$sid]=$a[2];
$sid++;
## $wordcus
}
close (FDb);
#store $s_w_hash, "$_[0].saveobj";
#store $s_arr, "$_[0].saveobj_sarr";
#store $wordw, "$_[0].saveobj_wordw";
#store $wordt, "$_[0].saveobj_wordt";
}
sub txt2arr3
{
#需要改进为 先判断文本域 再根据域 重写词向量值 域向量集 可以用clone覆盖已有向量
#例如黑话库 的词依存 规则
my $input_t=$_[0];
my $exp_h=$_[1];
my $tmp_hash=();
my $tmp_hash_seq=();
my @list=split(" ",$input_t);
print FD_log "$input_t\n";
@res=();
my $og;
my $n_a=0;
my $n_b=0;
my $v_a=0;
my $v_b=0;
my $n1;
my $n2;
my $v1;
my $v2;
my $nline;
my $seq=0;
my $tmp_first=0;
my $list_len;
$list_len=scalar(@list);
foreach $tmp_res (@list)
{
if ($tmp_first==0)
{
$nline=$tmp_res;
$tmp_first++;
next;
}
my $l=length($tmp_res);
#if($l<=3)
#{next;}
if(($l<3)&&($l!~/^[a-z|A-Z]/))
{next;}
if(exists($hash_st->{$tmp_res}))
{next;}
#原始词保留
#主题放大
$w=$tmp_res;
# if($tmp_hash->{$w}>($list_len/10))
#if($tmp_hash->{$w}>($list_len/10))
# {next;}
# else
if(exists($tmp_hash->{$w}))
{
}
else
{$tmp_hash->{$w}+=2.1;
$og->{$w}->{$w}=1;
}
$tmp_hash_seq->{$w}=$seq;
$seq++;
if(exists($hash->{$w}))
{
my $t_n=0;
while(($k,$v)=each(%{$hash->{$w}}))
{
$tmp_hash->{$k}+=$v;
$tmp_hash_seq->{$k}=$seq;
$og->{$k}->{$w}=1;
# print FD_log "正常词 $w 转换",$hash_sc->{$w}->{'h'},"--",$tmp_hash->{$k}," $k seq=$seq\n";
}
}
else
{
$tmp_hash->{$w}+=0.1;
$og->{$w}->{$w}=1;
$tmp_hash_seq->{$w}=$seq;
# print FD_log "正常词 $w 转换",$hash_sc->{$w}->{'h'},"--",$tmp_hash->{$k}," $k seq=$seq\n";
}
}
return (\%{$tmp_hash},$nline,\%{$tmp_hash_seq}, \%{$og});
}
sub load_dic
{
open(FDb, "$_[0]");
my $lsize=$_[1];
my $zoomin=0.5;
if($_[2]>0)
{$zoomin=$_[2];}
my $tmp=();
while(<FDb>)
{
$line=$_;
$s=$line;
# $m=$2;
my $name;
@a=split("\t",$s);
my $dob=0;
#@b=split(" ",$m);
$f=0;
my $ccn=0;
foreach $i (@a)
{
if($f==0)
{
$f++;
$name=$i;
}
else
{
if($i=~/(.*?)\:(.*)/)
{
my $av=$1;
my $bv=$2;
#覆盖
if(exists($hash_st->{$av}))
{next;}
if($ccn>$lsize)
{last;}
$ccn++;
$hash->{$name}->{$av}=$bv*$zoomin;
}
}
}
}
close (FDb);
print "load $_[0] ok\n";
}
sub search_article
{
$file=$_[0];
chomp($file);
#print "file=$file\n";
my $showtop=$_[1]||0;
my $show_more=$_[2];
my $dic=$_[3];
my $f=0;
my $n=0;
my $kk;
my $thash=();
my @thash;
my @input_all=`cat $file`;
my %my_hash;
my %my_hash2;
my %my_hash2_seq;
my %thash;
my $j;
my $input_dis;
my %input_dis;
my $j=1;
my @inputa;
my @inputb;
my $disthash;
my @disthash;
my $dist_seq_hash;
my @dist_seq_hash;
my $kk=0;
my $kj=0;
#my $all=`cat `;
#while(<FD_scws>)
my $input_b;
my @input_b;
my %input_b;
my $in=$input_all[0];
chomp($in);
$in=seg_txt($in);
my $in_gg=$input_all[2];
chomp($in_gg);
$in_gg=seg_txt($in_gg);
#if(length($in)>12)
#{
# $in=rewrite_gx($in);
# print FD_log "rewrite == $in\n";
($d,$c,$d_seq,$og)=txt2arr_input("$kk $in",$exp_hash);
#my ($d_gg,$c,$d_seq_gg,$og)=txt2arr_input("0 $in_gg",$exp_hash);
$disthash[$kk]=clone($d);
$disthashog[$kk]=clone($og);
$dist_seq_hash[$kk]=clone($d_seq);
# $isthash_gg[$kk]=clone($d_gg);
# $dist_seq_hash_gg[$kk]=clone($d_seq_gg);
$kk++;
my $ssid=scalar(@s_hash)-1;
for (0 .. $ssid)
{
my $nnn=$_;
# print "$inputdic[$nown]";
if($dic==4)
{
$n=comp_hash($disthash[$nnn],$s_hash[$nown],$dist_seq_hash[$nnn],$my_hash2_seq,$disthashog[$nnn],$og2);
}
else
{
$n=comp_hash($disthash[$nnn],$s_hash[$nown],$dist_seq_hash[$nnn],$my_hash2_seq,$disthashog[$nnn],$og2);
}
my $shnn=$nnn+1;
#print "totle $inputdic[$nown] $shnn-$n_line=$n\n";
$thash[$nnn]->{$nnn}=$n;
}
if($showtop>0)
{
chomp($in);
if($dic==4)
{$input_dis->{$n_line}=$s_arr[$n_line];}
else
{
$input_dis->{$n_line}=$inputdic[$n_line];
}
#$input_dis->{$n_line}=$in;
#print FD_log "inputdis $n_line $input_dis->{$n_line}\n";
}
$f++;
#unlink("/tmp/$rand.sctxt2");
#close(FD_hash);
close(FD_arr);
my @m_res;
if($showtop>0)
{
for(0 .. (scalar(@disthash)-1))
{
my $nnn=$_;
my @key =sort {$thash[$nnn]->{$b} <=> $thash[$nnn]->{$a}} keys %{$thash[$nnn]};
#print @key;
my $o_n;
my $toptmp=0;
foreach $i (@key)
{
if($toptmp>=$showtop)
{last;}
# if( $thash[$nnn]->{$i}>0)
# {
my $shnnn=$nnn+1;
$jjkk=$i+1;
push (@m_res,"$shnnn-$jjkk-".$thash[$nnn]->{$i}."-".$input_dis->{$i});
#print FD_log "inputdis show $i $input_dis->{$i}\n";
#push (@m_res,"$nnn-$i-".$thash[$nnn]->{$i}.$input_ar[$i]);
# $o_n++;
# }
$toptmp++;
}
}
}
else
{
for(0 .. (scalar(@disthash)-1))
{
my $nnn=$_;
my @key =sort {$thash[$nnn]->{$b} <=> $thash[$nnn]->{$a}} keys %{$thash[$nnn]};
#print @key;
my $o_n;
foreach $i (@key)
{
# if( $thash[$nnn]->{$i}>0)
# {
my $shnnn=$nnn+1;
$jjkk=$i+1;
push (@m_res,"$shnnn-$jjkk-".$thash[$nnn]->{$i});
#push (@m_res,"$nnn-$i-".$thash[$nnn]->{$i}.$input_ar[$i]);
# $o_n++;
# }
}
}
}
# print FD_log "return ",scalar(@m_res),"\n";
@m_res;
}
sub comp_hash
{
my $h1=@_[0];
my $h2=@_[1];
my $h3=@_[2];
my $h4=@_[3];
my $org1=@_[4];
my $org2=@_[5];
#while(($k,$v)=each(%{$h3}))
# {
#print FD_log "h3 show $k, $v\n";
# }
#while(($k,$v)=each(%{$h4}))
# {
#print FD_log "h4 show $k, $v\n";
# }
my $akv=0;
my $all;
my $an_all;
my %all;
my $vall=0.001;
my $vall2=0.001;
my $k,$v;
while(($k,$v)=each(%{$h1}))
{
$all->{$k}++;
if($v>0)
{
$an_all++;
$vall+=$v;
}
# print " hash 1 k $k v $v\n";
}
my $k,$v;
while(($k,$v)=each(%{$h2}))
{
if($v>0)
{
$all->{$k}++;
$an_all++;
#$vall2++;
$vall2+=$v;
}
# print " hash 2 k $k v $v\n";
}
my $k,$v;
my $samelist;
my $std_n;
while(($k,$v)=each(%{$all}))
{
if($v >= 2)
{
# my $sum;
#if($h1->{$k}>$h2->{$k})
#{$sum=$h2->{$k};}
#else
#{$sum=$h1->{$k};}
#$kv=$sum/($vall+$vall2);
if($h1->{$k}<0)
{next;}
$an_all=$an_all-2;
# 平均算法 适用于多词找文 对词典中噪音有包容性 #$kv=($h1->{$k}+$h2->{$k})/($vall+$vall2);
#$kv=($h1->{$k}+$h2->{$k})/($vall+$vall2);
$kv=($h1->{$k}+$h2->{$k})/($vall+$vall2);
$kv2=((($h1->{$k})/$vall) + ($h2->{$k}/$vall2))/2;
$kv3=($kv+$kv2)/2;
# log2算法 适用于文找文 $kv=($h1->{$k}+$h2->{$k})/($vall+$vall2+log2($an_all));
#$kv=($h1->{$k}+$h2->{$k})/($vall+$vall2+log2($an_all)/2);
#$kv3=$kv;
#$kv2=((($h1->{$k})/$vall) + ($h2->{$k}/$vall2))/2;
#if($h1->{$k}<$h2->{$k})
#{
#}
if(($h1->{$k}>2)&&($h2->{$k}>2))
{$akv+=0.12;}
$akv+=$kv3;
# print "double $akv $k $v kv sum= $h1->{$k}+$h2->{$k} ($org1->{$k}, $org2->{$k}) kv= $kv val1= $vall val2= $vall2 kv2=$kv2 kv3 =$kv3 std_n=$std_n\n";
# print "相似语义 $k 来自前句 from ";
#while(($kc,$vc)=each($org1->{$k}))
#{
# print "$kc,";
#}
#print " 来自后句 ";
if (exists($org2->{$k}))
{
while(($kc,$vc)=each($org2->{$k}))
{
print "$kc,";
}
print "\n";
}
$samelist->{$k}=$h3->{$k};
$std_n++;
}
else{$akv -="0.001";}
}
#序列相似评估
#---------------------------------------
if(($std_n>0)&&($std_n !=1))
{
# make samelist ->std test_seq->obj_seq;
my @key =sort {$samelist->{$b} <=> $samelist->{$a}} keys %{$samelist};
#print @key;
my $std_seq;
my $t_n=1;
my $test_seq;
my $last_tmp;
my $my_fi=0;
my $std_sq;
foreach $i (@key)
{
if(exists($std_sq->{$samelist->{$i}}))
{
$std_seq->{$i}=($std_n-$t_n+1);
$test_seq->{$i}=$h4->{$i};
# print FD_log "def test_seq $i =$h4->{$i}\n";
# print FD_log "std_seq last $i seq =$std_seq->{$i} h3 $h3->{$i} std_n=$std_n\n";
}
else
{
$t_n++;
$std_sq->{$samelist->{$i}}=1;
$std_seq->{$i}=($std_n-$t_n+1);
# print FD_log "std_seq $i seq=$std_seq->{$i}\n";
# print FD_log "def test_seq $i =$h4->{$i}\n";
$test_seq->{$i}=$h4->{$i};
}
}
my @key =sort {$test_seq->{$b} <=> $test_seq->{$a}} keys %{$test_seq};
my $t_n=1;
my $last_tmp;
my $obj_tmp;
my $test_sq;
foreach $i (@key)
{
# print FD_log "$i \"$last_tmp\" == \"$test_seq->{$i}\" ??\n";
# if($last_tmp eq $test_seq->{$i})
if(exists($test_sq->{$test_seq->{$i}}))
{
$obj_seq->{$i}=($std_n-$t_n+1);
# print FD_log "test_seq last $i seq= $obj_seq->{$i} std_n=$std_n\n";
}
else
{
$t_n++;
$test_sq->{$test_seq->{$i}}=1;
$obj_seq->{$i}=($std_n-$t_n+1);
}
# test_seq 受体拮抗剂 last_tmp = 26 25
# print FD_log "test_seq $i seq= $obj_seq->{$i}\n";
}
my $diff_sum=0;
while(($k,$v)=each(%{$std_seq}))
{
$diff_sum=$diff_sum+abs($v-$obj_seq->{$k});
# print FD_log "diff_sum $diff_sum $k $v $obj_seq->{$k}\n";
}
my $sam_rate=1-($diff_sum/(($std_n-1)*($std_n-1)));
my $diff_n=$akv*(1-$sam_rate)*0.15;
$akv=$akv-$diff_n;
#print FD_log "seq diff sam_rate $sam_rate akv $akv diff_n $diff_n\n";
}
#-----------------------------------------------
# 异议评估 说多了远的,深意不在这里
# my $maxd=log2($an_all+1);
# if($maxd>100)
# {$akv=$akv*0.618;}
# elsif($maxd==0)
# {
# }
# elsif($maxd>1)
# {
# $akv=$akv*(0.8+0.2/($maxd+1));
# }
# print "---------------\n";
$akv;
}
sub seg_txt
{
my $txt=$_[0];
my $socket = IO::Socket::INET->new(
PeerAddr => "127.0.0.1",
PeerPort => "11229",
Type => SOCK_STREAM,
Proto => "tcp",
)
or die "Can not create socket connect.$@";
$socket->autoflush(1);
$sel = IO::Select->new($socket);
$socket->send("$txt\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;
}
}
}
$i;
}
sub wnsock
{
my $txt=$_[0];
my $sockn=$_[1];
my $socket = IO::Socket::INET->new(
PeerAddr => "127.0.0.1",
PeerPort => $sockn,
Type => SOCK_STREAM,
Proto => "tcp",
)
or die "Can not create socket connect.$@";
$socket->autoflush(1);
$sel = IO::Select->new($socket);
$socket->send("$txt\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;
}
}
}
#$i .=" wnsock";
$i;
}
sub sptree
{
my $a=$_[0];
my @b=split(" ",$a );
my @resa;
my $str;
for (0 .. scalar(@b)-1)
{
my $n=$_;
if($b[$n]=~/(.*?)\)/)
{
$str .=" ".$1;
push(@resa,$str);
$str=();
}
elsif($b[$n]=~/^\((.*)/)
{
#(CP (IP (IP (VP (PP (P 给) (NP (PN 我))) (VP (VV 听) (NP (NN 音乐))))) (VP (VA 好))) (SP 吗) (? ?)))
# $b[$n]=~/^\((.*)/;
$str.=$1;
next;
}
# else
# {
# if( $b[$n]=~/\((.*)/)
# {
# $str .=$1;
# }
# }
}
@resa;
}
sub deeptl
{
my $rand=rand();
my $run=` grep "$_[0]" concept_su.txt |awk -F "\t" {'print \$3 '}> /tmp/$rand `;
# $run=`cat /tmp/chat_$_[1].dic >> /tmp/$rand`;
$run=`cat /tmp/chat_$_[1].kb |awk -F "\t" {'print \$3 '} >> /tmp/$rand`;
my $deb=`cat /tmp/$rand`;
## $deb
my @glob=glob("deep_rule/*");
my $res2;
my $resl=0;
foreach my $rf(@glob)
{
## $rf
my $res=`./art_res.pl $rf /tmp/$rand /tmp/$rand.res noseg `;
my @tres=`cat /tmp/$rand.res`;
$resl +=scalar(@tres);
my $t=join("|",@tres);
$res2 .=$t."|";
if($resl>30)
{last;}
## $res2
}
unlink("/tmp/$rand");
unlink("/tmp/$rand.res");
$res2;
}
sub delywc
{
#my @gin=@_;
## @_
my $resa;
my $resb;
foreach my $in(@_)
{
## $in
my $isb=0;
if($in=~/为什么|带来|为啥/)
{
$resb .="Causes ";
$isb=1;
}
if($in=~/是什么|谁/)
{
$isb=1;
$resb .="IsA MotivatedByGoal ";
}
if($in=~/能|会|用处/)
{
$isb=1;
$resb .="CapableOf ";
}
if($in=~/哪|地方|在|放|去哪里/)
{
$isb=1;
$resb .="AtLocation ";
}
if($in=~/需要|要|怎么办/)
{
$isb=1;
$resb .="MotivatedByGoal HasFirstSubevent ";
}
if($in=~/有/)
{
$resb .="HasSubevent HasProperty";
$isb=1;
}
if($in=~/不|怕/)
{
$resb .="Desires CausesDesire ";
$isb=1;
}
if($in=~/用/)
{
$resb .="UsedFor ";
$isb=1;
}
if($in=~/有|做/)
{
$resb .="PartOf Madeof ";
$isb=1;
}
if($in=~/表示|代表|意思|象征|是/)
{
$resb .="SymbolOf ";
$isb=1;
}
if($in=~/的|可以|做|先|什么/)
{
$isb=1;
}
## $isb
if($isb==0)
{
$resa .="$in,";
}
}
return($resa,$resb);
}
sub word_ljaddone
{
my @a=split("\t",$_[0]);
my $u=$_[1];
my $n=scalar(@{$s_arr_cus->{$u}});
$wordcus->{$u}->{$a[0]}->{$a[1]}->{$a[3]}=$n;
$wordcust->{$u}->{$a[0]}->{$a[3]}->{$a[1]}=$n;
push(@{$s_arr_cus->{$u}},$a[2]);
#my $run=`./client_search_muti.pl addone `;
}
sub get_sim_mix
{
my $strone2 =wnsock("vsm_all_sock $_[0]",11221);
my @wlist=split(" ",$strone2);
my $res;
my $rn=0;
foreach my $wone (@wlist)
{
if($wone=~/(.*?)\:.*/)
{
$res .=$1.",";
$rn++;
if($rn>10)
{last;}
}
}
$res;
}
sub get_mem
{
my @ca=split("\,",$_[0]);
my $user=$_[1];
my $num=$_[2];
my $max=$_[3]||0;
my $resm;
print "/client_search_muti.pl \"$_[0]\" showtop $num n $user\n";
print "$_[0] $user $num\n";
my $alres=wnsock("$_[0] showtop $num n $user",11020);
### $alres
@alle=split("\n",$alres);
#11020
## @alle
foreach my $one (@alle)
{
if($one=~/(.*?)<inc>.*?<sort>(.*)/)
{
my $sg=$1;
if($2 >$max)
{
$resm .="$sg\n";
}
}
elsif($one=~/(.*?)<inc>.*?<ac>(.*)/)
{
my $sg=$1;
if($2 >$max)
{
$resm .="$sg\n";
}
}
}
return $resm;
}
sub get_mem2
{
#my @ca=split("\,",$_[0]);
my $user=$_[1];
my $num=$_[2];
my $max=$_[3]||0.1;
my $resm;
#my @alle=`./client_search_muti.pl "$_[0]" showtop $num n $user `;
#print "$_[0] $user $num\n";
my $alres=wnsock("$_[0] showtop $num n $user",11019);
#print "$_[0] showtop $num n $user\n";
## $alres
@alle=split("\n",$alres);
#11020
## @alle
foreach my $one (@alle)
{
if($one=~/(.*?)<inc>.*?<sort>(.*)/)
{
my $sg=$1;
if($2 >$max)
{
$resm .="$sg\n";
}
}
elsif($one=~/(.*?)<inc>.*?<ac>(.*)/)
{
my $sg=$1;
if($2 >$max)
{
$resm .="$sg\n";
}
}
}
return $resm;
}
sub get_kb
{
my @ca=split("\,",$_[0]);
## @ca
my $user=$_[1];
my $num=$_[2];
my $resm;
## @alle
my @resone;
foreach my $kkone (@ca)
{
my @one=get_wordljlist_more2($kkone,$num);
my @cus=get_wordljlist_cus2($kkone,"","",$user);
#ns## @one
## $kkone
push(@resone,@one);
push(@resone,@cus);
}
$resm=join(" ",@resone);
return $resm;
}
sub deep_search
{
my $fin=$_[0];
my $uuid=$_[1];
my $rand=rand();;
my $sl=seg_txt($fin);
my @allk2=split(" ",$sl);
my ($ky1,$ywc)=delywc(@allk2);
my $sim_key=get_sim_mix($ky1);
## $ky1
### $sim_key
my $all_mem=get_mem($sim_key,$uuid,30) || get_mem($sim_key,$uuid,30);
### $all_mem
my $all_rule=get_mem($fin."$sim_key",$uuid.".rul",30) || get_mem($fin,$uuid.".rul",30);
### $all_rule
## $sl
# my $all_kb=get_kb($ky1,$uuid,30);
my $all_kb=get_kb($ky1,$uuid,30) ;
my $lfact= length("$all_mem\n$all_kb");
my $lfactkb= length($all_kb);
## $lfact
my $lrule= length($all_rule);
## $lrule
if($lfactkb<5)
{
## $fin
$all_kb=get_kb($sim_key,$uuid,30);
}
## $all_kb
#my $all_kb=get_kb($sim_key,$uuid,30);
$all_kb=~s/\|/\n/gis;
## $all_kb
# 要求 事实规则 知识的数量
#一阶逻辑 无迭代
# open(FDr,">/tmp/$rand.rule");
# print FDr "$all_rule";
# close FDr;
# open(FDf,">/tmp/$rand.fact");
# print FDf "$all_mem\n$all_kb";
# close FDf;
# open(FDf,">/tmp/$rand.rule_org");
# print FDf "$fin";
# close FDf;
my $resl;
my $mres;
my $rescc;
my $mres2;
if(($fin=~/什么|怎|多|哪|如何|何|几/)&&($lfact>3))
{
open(FDf,">/tmp/$rand.rule_org");
print FDf "$fin";
close FDf;
open(FDf,">/tmp/$rand.fact");
print FDf "$all_mem\n$all_kb";
close FDf;
print "art_res.pl /tmp/$rand.rule /tmp/$rand.fact /tmp/$rand.res noseg";
$rescc .=`/mnt/sdb/shell2/art_res.pl /tmp/$rand.rule_org /tmp/$rand.fact /tmp/$rand.res2`;
$mres2 .= `cat /tmp/$rand.res2`;
}
if(($lfact>3)&&($lrule>2))
{
open(FDr,">/tmp/$rand.rule");
print FDr "$all_rule";
close FDr;
open(FDf,">/tmp/$rand.fact");
print FDf "$all_mem\n$all_kb";
close FDf;
print "art_res.pl /tmp/$rand.rule /tmp/$rand.fact /tmp/$rand.res noseg";
$rescc.=`/mnt/sdb/shell2/art_res.pl /tmp/$rand.rule /tmp/$rand.fact /tmp/$rand.res noseg`;
$mres2 .= `cat /tmp/$rand.res`;
}
my $mres;
$resl=length($mres2);
if($resl<4)
{
#$mres="ans:deep_search ".$all_kb;
### 问哲人
$mres=_mingyan_has($fin,$uuid);
}
else
{
$mres="ans:deep_search2 $mres2\n";
}
$mres;
}
sub ndate
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
$year=~s/1(.*)/20$1/;
$mon+=1;
if($mon=~/^\d$/)
{
$mon="0"."$mon";
}
if($mday=~/^\d$/)
{
$mday="0"."$mday";
}
if($hour=~/^\d$/)
{
$hour="0"."$hour";
}
if($min=~/^\d$/)
{
$min="0"."$min";
}
if($sec=~/^\d$/)
{
$sec="0"."$sec";
}
my $res=$year."-".$mon."-".$mday." ".$hour.":"."$min".":"."$sec";
$res;
}
sub context
{
my $in=$_[0];
my $uid=$_[1];
my $buft;
my $res;
my $res2;
if (exists($allr_context->{$uid}->{'t'}))
{
$buft= $allr_context->{$uid}->{'t'};
if($buft>2)
{
$allr_context->{$uid}->{'t'}=0;
$buft=0;
my $js=0;
my $fin=join("",@{$allr_context->{$uid}->{'lastin'}});
$res=wnsock("vsm_all_sock $fin",11221);
my @wlist=split(" ",$res);
foreach my $wone (@wlist)
{
if($wone=~/(.*?)\:.*/)
{
my $kw=$1;
$res2 .=$kw." ";
}
if ($js>8)
{last;}
$js++;
}
$allr_context->{$uid}->{'context'}=$res2;
$allr_context->{$uid}->{'hbuft'}++;
$allr_context->{$uid}->{'hobby'}[$allr_context->{$uid}->{'hbuft'}]=$res2;
# if($allr_context->{$uid}->{'hbuft'}>4)
# {
# my $hjs=0;
# $allr_context->{$uid}->{'hbuft'}=0;
# my $fin=join("",@{$allr_context->{$uid}->{'hobby'}});
# my $hres=wnsock("vsm_all_sock $fin",11221);
# my @wlist=split(" ",$res);
# foreach my $wone (@wlist)
# {
# if($wone=~/(.*?)\:.*/)
# {
# my $kw=$1;
## 抽象类 $kw
# }
# if ($hjs>30)
# {last;}
# $hjs++;
# }
# }
}
else
{
my $rand=rand();
if ($rand>0.7)
{
$res2=$in;
}
#$res2=$allr_context->{$uid}->{'context'};
}
$allr_context->{$uid}->{'t'}++;
}
else
{
$allr_context->{$uid}->{'t'}=0;
$buft=0;
}
$allr_context->{$uid}->{'lastin'}[$buft]=$in;
$res2;
}
#_mingyan(inp,uid);
sub _mingyan
{
my $input=$_[0];
my $uid=$_[1];
my $min_res;
my $cont=context($input,$uid);
my $contl=length($cont);
if($contl>4)
{
# $min_res.="";
my @conl=split(" ",$cont);;
my $conn=scalar(@conl)-1;
#$conn=$conn *0.618;
# $conn=ceil($conn)+1;
my @c2=@conl[0 .. 3];
#my @c2=@conl[0 .. $conn];
my $ostr=join(",",@c2);
$min_res .="ans:mingyan ";
$min_res .=get_mem($ostr,"mingyan99",8);
$min_res=~s/\n/<sg>/g;
}
$min_res;
#my $witet=scalar(@{$process_list});
}
sub _mingyan_has
{
my $input=$_[0];
my $uid=$_[1];
my $min_res;
$min_res .="ans:mingyan_has ";
$min_res .=get_mem($input,"mingyan99",12);
$min_res=~s/\n/<sg>/g;
#$ljres.=deep_search($cont,$uid);
$min_res;
#my $witet=scalar(@{$process_list});
}
sub _shoping_has
{
my $input=$_[0];
my $uid=$_[1];
my $min_res;
$min_res .="ans:shoping 推荐你.";
$min_res .=get_mem($input,"shoping99",12);
$min_res=~s/\n/<sg>给您推荐"/g;
#$ljres.=deep_search($cont,$uid);
$min_res;
#my $witet=scalar(@{$process_list});
}
sub _mingyan2_has
{
my $input=$_[0];
my $uid=$_[1];
my $min_res;
#$min_res .="ans:mingyan2_has ";
$min_res .=get_mem2($input,"mingyan100",12);
my @mres=split("\n",$min_res);
my @mmres;
my $mmc;
map{if($_=~/.*?( .*)/){push(@mmres,$1."<sg>");}} @mres;
#$min_res=~s/\n/<sg>/g;
#$ljres.=deep_search($cont,$uid);
$min_res="ans:mingyan2_has ";
$min_res .=join("",@mmres);
$min_res;
#my $witet=scalar(@{$process_list});
}
sub chouxiang
{
my $input=$_[0];
my $uuid=$_[1];
my $rel_rule=exp_rule($input,$uuid);
my $exp_fact=exp_fact($input,$uuid);
#my $ent_list=dairu($input,$uuid);
### $ent_list
}
sub exp_
{
### $0
### @_
my $res;
#="鸟_大雁_蝴蝶";
# 9991
my $ws=$_[0];
my $c=length($ws);
if($c<2)
{return;}
my $uid=$_[1];
my $total;
my $totaln=1000;
my @restotal;
my @tmptotal;
$tmptotal[0]=$ws;
my $exhash;
while($totaln>0)
{
$totaln--;
if(exists($exhash->{$wc})){}
else
{
push(@restotal,$wc);
$exhash->{$wc}=1;
}
$ws=shift(@tmptotal);
$res=wnsock("$ws xia $uid",9992);
## $ws
## $res
chomp($res);
my @rone=split(" ",$res);
for (0 .. scalar(@rone)-1)
{
my $wc=@rone[$_];
if(exists($exhash->{$wc})){}
else
{
push(@restotal,$wc);
push(@tmptotal,$wc);
$exhash->{$wc}=1;
$totaln --;
}
}
if($totaln <0) {last};
}
$total=join("_",@restotal);;
}
sub ent_has
{
### @_
my $in=$_[0];
my $has=$_[1];
my $uid=$_[2];
### $has
my @list=split("_",$in);
my $total;
foreach my $one (@list)
{
my $res=wnsock("$one exp $uid",9992);
if($res=~/$has/)
{
$total.=$one." ";
# if()
}
}
$total;
}
sub upshang_
{
my $res;
my @tmptotal;
$res=wnsock("$_[0] shang $_[1]",9992);
chomp($res);
$res;
}
sub upshangone_
{
my $res;
$res=wnsock("$_[0] Shangone $_[1]",9992);
chomp($res);
$res;
}
sub learn_shangxia
{
my $res;
my @tmptotal;
$res=wnsock("add_shang $_[0] $_[1] $_[2]",9992);
chomp($res);
$res;
}
sub tonglei_
{
my $res;
my @tmptotal;
$res=wnsock("$_[0] tong $_[1]",9992);
chomp($res);
$res;
}
sub learn_exp
{
my $res;
my @tmptotal;
$res=wnsock("add_exp $_[0] $_[1] $_[2]",9992);
chomp($res);
$res;
}
sub exp_self_
{
my $res;
my @tmptotal;
$res=wnsock("$_[0] exp $_[1]",9992);
chomp($res);
$res;
}
sub learn_exp_sub
{
my $res;
my $in=$_[0];
$in=~s/的/\//g;
$res=wnsock("add_exp_sub $in $_[1] $_[2]",9992);
chomp($res);
$res;
}
sub learn_soloway
{
## soloway
### @_
my $uid=$_[1];
my $soloway=get_mem2($_[0],"learn99",4,0.1);
my @solo=split("\n",$soloway);
## $soloway
my @resout;
$resout[0]=$_[0];
my @p=split(" ",$solo[0]);
for(1 ..scalar(@p))
{
my $step=$_;
if($p[$step]=~/^reg_(.*?)<res>(.*)/)
{
my $reg=$1;
my $resp=$2;
if($resout[$step-1]=~/$reg/)
{
for(1 .. $resp)
{
push(@{$resout[$step]},${$_})
}
}
}
elsif($p[$step]=~/^p_(.*?)\((.*?)\)/)
{
my $func=$1;
my $arg=$2;
$arg .=",".$uid;
### $func
### $arg
### $uid
## @resout;
my $reseval=eval "$func($arg)";
print "$func($arg)\n";
### $reseval
$resout[$step]=$reseval;
#eval("$func($arg);");
}
}
my $laststr=scalar(@resout);
my $laststrn=length($resout[$laststr-1]);
my $lres;
if($laststrn >3)
{
$lres ="ans:abstrlearn $resout[$laststr-1]\n";
}
$lres;
}