package ILMT::TEL::HIN::Repair::modify; use Exporter qw(import); use ILMT::TEL::HIN::SSFAPI::feature_filter; use ILMT::TEL::HIN::SSFAPI::shakti_tree_api; our @EXPORT = qw(modify_tree_rhs); #version 4 sub modify_tree_rhs { my $ref_nodes=$_[0]; my $index=$_[1]; my $sent=$_[2]; my $ref_rule=$_[3]; my $skip=$_[4]; my @nodes=@$ref_nodes; my @_rule_=($$ref_rule); my @_lhs_= $_rule_->[0]->[0]; my @_rhs_= $_rule_->[0]->[1]; my $i=$index; my $k=0; my $delete=0; #$log->info("#######DELETION OF THE CHUNKS######\n"); ########### DELETION Of CHUNKS ####################### for($k=0;$_lhs_->[0]->[$k]->{"chunk"} ne "";$k++) { if($_lhs_->[0]->[$k]->{"drop"}==1) { #$log->info("Deletion Of $_lhs->[0]->[$k]->{\"chunk\"}\n"); my @children=&get_children($nodes[$i]-$delete,$sent); my $num_childs=@children+2; print ERR $nodes[$i],"-delete-",$delete,"\n"; &delete_node($nodes[$i]-$delete,$sent); $delete=$delete+$num_childs; } $i++; } #$log->info("#######REORDER OF THE NODES######\n"); ###################################################### @nodes=&get_children(0,$sent); ###################################################### ##### REORDER OF THE NODES ########################### my $l=0; my $i=$index; my $cflag=0; my $add_count=0; my $drop_count=0; my @address_nodes; my $add_ind=0; my @mod_nodes=&get_children(0,$sent); my @move_lhs; my @move_rhs; for($k=0;$_lhs_->[0]->[$k]->{"chunk"} ne "";$k++) { if($_lhs_->[0]->[$k]->{"drop"}==1) { $drop_count++; next; } else { @move_lhs[$add_ind]=$lhs_node=$_lhs_->[0]->[$k]->{"chunk"}."~".$_lhs_->[0]->[$k]->{"token_id"}; @address_nodes[$add_ind]=$mod_nodes[$i]; $add_ind++; $i++; } } my $add_ind=0; for($l=0;$_rhs_->[0]->[$l]->{"chunk"} ne "";$l++) { if($_rhs_->[0]->[$l]->{"add"} eq 1) { $add_count++; next; } else { @move_rhs[$add_ind]=$_rhs_->[0]->[$l]->{"chunk"}."~".$_rhs_->[0]->[$l]->{"token_id"}; $add_ind++; } } ################ my $i=$index; my $pos_count=0; my $shift_count=0; my $l=0; my $pcount=0; my $prev_l=0; for($k=0;@move_lhs[$k] ne "";) { $pos_count=0; $prev_l=$l; for(;$l<@move_rhs;$l++) { #$log->info("Comparing: @move_lhs[$k]--@move_rhs[$l]\n"); if(@move_lhs[$k] eq @move_rhs[$l]) { if($pos_count==0) { # #$log->info("Done: @move_lhs[$k] Next\n"); $k++; $l++; $pcount++; last; } else { &move_node(@address_nodes[$k-$shift_count],@address_nodes[$k-$shift_count+$pos_count],1,$sent); push(@move_lhs,"dummy"); for($shift=@move_lhs-1;($shift)!=($k+$pos_count);$shift--) { #$log->info("Shifting:--$move_lhs[$shift-1]-to-$move_lhs[$shift]\n"); $move_lhs[$shift]=$move_lhs[$shift-1]; } $move_lhs[$shift+1]=$move_lhs[$k]; my @mod_nodes=&get_children(0,$sent); $add_ind=0; $i=$index; while($add_ind<=@move_rhs) { @address_nodes[$add_ind]=$mod_nodes[$i]; $add_ind++; $i++; } $k++; $l=$prev_l; $shift_count++; last; } } else { $pos_count++; } } } #$log->info("####DELETION OF WORDS#####\n"); #############Deletion of Words####################### #################################################### @nodes=&get_children(0,$sent); my $i=$index; for($k=0;$_rhs_->[0]->[$k]->{"chunk"} ne "";$k++) { #$log->info("Node:$nodes[$i] Chunk:$_rhs_->[0]->[$k]->{\"chunk\"}\n"); if($nodes[$i] eq "") { last; } my ($f0,$f1,$f2,$f3,$f4)=&get_fields($nodes[$i],$sent); if($_rhs_->[0]->[$k]->{"head_val"} eq 1) { } if($_rhs_->[0]->[$k]->{"add"} eq 1) { next; } else { if($_rhs_->[0]->[$k]->{"drop_childs"}->[0] ne "") { $sent=&delete_children($nodes[$i],$sent,\$_rhs_->[0]->[$k]->{"drop_childs"}); @nodes=&get_children(0,$sent); } } $i++; } ############################# ############################# @nodes=&get_children(0,$sent); ############################################################### ############REORDERING OF WORDS ############################### #$log->info("####REORDERING OF WORDS#####\n"); my $i=$index; my @rhs_pos; my @lhs_pos; for($l=0;$_rhs_->[0]->[$l]->{"chunk"} ne "";$l++) { my $rhs_chunk=$_rhs_->[0]->[$l]->{"chunk"}."~".$_rhs_->[0]->[$l]->{"token_id"}; $i=$index+$l; my @lhs_words; my @rhs_words; if($_rhs_->[0]->[$l]->{"add"}==1) { next; } for($k=0;$_lhs_->[0]->[$k]->{"chunk"} ne "";$k++) { my $lhs_chunk=$_lhs_->[0]->[$k]->{"chunk"}."~".$_lhs_->[0]->[$k]->{"token_id"}; if($_lhs_->[0]->[$k]->{"drop"}==1) { next; } if($lhs_chunk eq $rhs_chunk) { # #$log->info("Checking in $rhs_chunk\n"); my $ref_rhs=$_rhs_->[0]->[$l]->{"fs"}; my @check_rhs=@$ref_rhs; my $ref_lhs=$_lhs_->[0]->[$k]->{"fs"}; my @check_lhs=@$ref_lhs; my $ref_drop_arr=$_rhs_->[0]->[$l]->{"drop_childs"}; my $ref_add_arr=$_rhs_->[0]->[$l]->{"add_childs"}; my @drop_arr=@$ref_drop_arr; my @add_arr=@$ref_add_arr; # #$log->info("Rhs -",$#check_rhs,"\n"); # #$log->info("Lhs -",$#check_lhs,"\n"); my $drop_flag=0; for($m=0;$m<=$#check_lhs;$m++) { # #$log->info("$m---",$_lhs_->[0]->[$k]->{"fs"}->[$m]->{"word_var"},"\n"); if($_lhs_->[0]->[$k]->{"fs"}->[$m]->{"word_var"} ne "") { $drop_flag=0; foreach (@drop_arr) { if($m==($_-1)) {$drop_flag=1;} } if($drop_flag!=1) { #$log->info("Lhs Array---",$_lhs_->[0]->[$k]->{"fs"}->[$m]->{"word_var"},"\n"); push(@lhs_words,$_lhs_->[0]->[$k]->{"fs"}->[$m]->{"word_var"}); } } else{last;} } my $add_flag=0; for($m=0;$m<=$#check_rhs;$m++) { # #$log->info("$m---",$_rhs_->[0]->[$l]->{"fs"}->[$m]->{"word_var"},"\n"); if($_rhs_->[0]->[$l]->{"fs"}->[$m]->{"word_var"} ne "") { $add_flag=0; foreach (@add_arr) { if($m==($_-1)) { $add_flag=1; } } if($add_flag!=1) { #$log->info("Rhs Array ---",$_rhs_->[0]->[$l]->{"fs"}->[$m]->{"word_var"},"\n"); push(@rhs_words,$_rhs_->[0]->[$l]->{"fs"}->[$m]->{"word_var"}); } } else{last;} } #$log->info("LHS WORDS\n"); foreach (@lhs_words) { #$log->info("$_-"); } # log->info("\n"); #$log->info("RHS WORDS\n"); foreach (@rhs_words) { #$log->info("$_-"); } # #$log->info("\n"); last; } } my $n=0; my $pos_count_word=0; my $prev_n=0; my $pcount=0; my $shift_count=0; for($m=0;@lhs_words[$m] ne "";) { $pos_count_word=0; $prev_n=$n; #$log->info("LHS WORDS--",@lhs_words[$m],"\n"); #$log->info("RHS WORDS-",@rhs_words,"\n"); for(;$n<=$#rhs_words;$n++) { #$log->info("Comparing--",@lhs_words[$m],"--",@rhs_words[$n],"--","\n"); if($lhs_words[$m] eq $rhs_words[$n]) { if($pos_count_word==0) { $m++; $n++; $pcount++; last; } else { #$log->info("Moving Words ",@nodes[$i]+$m-$shift_count,"--",@nodes[$i]+$m-$shift_count+$pos_count_word,"\n"); &move_node(@nodes[$i]+$m+1-$shift_count,@nodes[$i]+$m+1-$shift_count+$pos_count_word,1,$sent); push(@lhs_words,"dummy"); for($shift=@lhs_words-1;($shift)!=$m+$pos_count_word;$shift--) { #$log->info("Shifting $lhs_words[$shift-1] to $lhs_words[$shift]\n"); $lhs_words[$shift]=$lhs_words[$shift-1]; } $lhs_words[$shift+1]=$lhs_words[$m]; #$log->info("LHS array is @lhs_words\n"); #$log->info("RHS array is @rhs_words\n"); $m++; $n=$prev_n; $shift_count++; last; } } else { $pos_count_word++; } } } } #####Modifying the RHS################################# #$log->info("####Modifying RHS Children\n"); my $i=$index; #$log->info("Children $i $index \n"); @nodes=&get_children(0,$sent); for($k=0;$_rhs_->[0]->[$k]->{"chunk"} ne "";$k++) { #$log->info("$i Node:$nodes[$i] Chunk:$_rhs_->[0]->[$k]->{\"chunk\"}\n"); if($nodes[$i] eq "") { last; } my ($f0,$f1,$f2,$f3,$f4)=&get_fields($nodes[$i],$sent); if($_rhs_->[0]->[$k]->{"head_val"} eq 1) { #$log->info("enter::$nodes[$i]::$_rhs->[0]->[$k]->{\"chunk\"}\n"); $sent=&head_modify($nodes[$i],$sent,\$_rhs_->[0]->[$k]->{"head_fs"}); } if($_rhs_->[0]->[$k]->{"add"} eq 1) { #$log->info("Add Node::$nodes[$i]::$_rhs->[0]->[$k]->{\"chunk\"}\n"); $sent=&add_function($nodes[$i],$sent,\$_rhs_->[0]->[$k]); @nodes=&get_children(0,$sent); $i=$i+$_rhs_->[0]->[$k]->{"add_val"}; next; } else { #$log->info("test $nodes[$i] \n"); if($_rhs_->[0]->[$k]->{"add_childs"}->[0] ne "") { $sent=&add_children($nodes[$i],$sent,\$_rhs_->[0]->[$k]->{"add_childs"},\$_rhs->[0]->[$k]); @nodes=&get_children(0,$sent); } #$log->info("Modifying Children $nodes[$i] \n"); $sent=&children_modify($nodes[$i],$sent,\$_rhs_->[0]->[$k]->{"fs"}); # if($_rhs_->[0]->[$k]->{"operator"} eq 1) # { # $i=$i+$skip; # } $i++; } } } sub add_function { my $index=@_[0]; my $sent=@_[1]; my $ref_rhs_chunk=@_[2]; my @temp_array_rhs1=($$ref_rhs_chunk); (undef my @_small_TREE_); my $k; $_small_TREE_[0][1]="0"; $_small_TREE_[0][2]="(("; $_small_TREE_[0][3]=$$ref_rhs_chunk->{"chunk"}; my @array_fs; # #$log->info("enter-check \n"); for($k=0;$temp_array_rhs1->[0]->{"fs"}->[$k]->{"word"} ne "";$k++) { $_small_TREE_[$k+1][1]=$k+1; push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"word"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"pos"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"root"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"lcat"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"num"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"gend"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"pers"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"case"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"cm"}); push(@array_fs,$temp_array_rhs1->[0]->{"fs"}->[$k]->{"tam"}); my $l=0; $num=@array_fs; while($l<$num) { if($array_fs[$l]=~/\$/) { if($array_fs[$l]=~/(\$[a-zA-Z0-9]+)$/) { $array_fs[$l]="$var_hash{$1}"; } elsif($array_fs[$l]=~/([^\$]*\.)?(\$[a-zA-Z0-9])\.(.*)?/) { my $temp1=$1; my $var1=$2; my $temp2=$3; $array_fs[$l]=$temp1."$var_hash{$var1}".$temp2; } } $l++; } my $vv='"'.$array_fs[2].'"'; $_small_TREE_[$k+1][2]=$array_fs[0],"\n"; $_small_TREE_[$k+1][3]=$array_fs[1],"\n"; $_small_TREE_[$k+1][4]=""; } $_small_TREE_[$k+1][2]="))"; $_small_TREE_[0][0]=$k+2; &add_node(\@_small_TREE_,$index,0,$sent); return ($sent); } sub head_modify { my $node=@_[0]; my $sent=@_[1]; my $ref_temp_fs_hash=@_[2]; my $ref_temp_hash=$$ref_temp_fs_hash; my %temp_hash=%$ref_temp_hash; my ($f0,$f1,$f2,$f3,$f4)=&get_fields($node,$sent); my $fs_head_array = &read_FS($f4,$sent); $sent=&modify_fs(\%temp_hash,$node,$sent); return $sent; } sub add_children { my $node=@_[0]; my $sent=@_[1]; my $ref_temp_children_array=@_[2]; my $ref_temp_array=$$ref_temp_children_array; my @temp_array=@$ref_temp_array; my $ref_rhs_chunk=@_[3]; #$log->info("ADDING CHILDREN\n"); my @temp_array_rhs1=($$ref_rhs_chunk); my $add=0; foreach (@temp_array) { my @array; push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"word"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"pos"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"root"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"lcat"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"gend"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"num"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"pers"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"case"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"cm"}); push(@array,$temp_array_rhs1->[0]->{"fs"}->[$_]->{"tam"}); my $l=0; my $num=@array; while($l<$num) { if($array[$l]=~/\$/) { if($array[$l]=~/(\$[a-zA-Z0-9]+)$/) { $array[$l]="$var_hash{$1}"; } elsif($array[$l]=~/([^\$]*\.)?(\$[a-zA-Z0-9])\.(.*)?/) { my $temp1=$1; my $var1=$2; my $temp2=$3; $array[$l]=$temp1."$var_hash{$var1}".$temp2; } } $l++; } my $fs=""; &add_leaf($node+$_+$add,0,$array[0],$array[1],$fs,$sent); #$log->info("@array[0],@array[1],$fs,\n"); $add++; } return ($sent); } sub delete_children { my $node=@_[0]; my $sent=@_[1]; my $ref_temp_children_array=@_[2]; my $ref_temp_array=$$ref_temp_children_array; my @temp_array=@$ref_temp_array; my $delete=0; my @sort_remove=sort{$a <=> $b} @temp_array; my $delete=0; foreach (@sort_remove) { &delete_node($node+$_-$delete,$sent); $delete++; } return ($sent); } sub modify_fs { my $ref_hash=@_[0]; my %temp_hash=%$ref_hash; my $node=@_[1]; my $sent=@_[2]; my ($f0,$f1,$f2,$f3,$f4)=&get_fields($node,$sent); my $fs_head_array = &read_FS($f4,$sent); foreach $uu(%temp_hash) { # @cat[0]=@array[3]; my @var; # @.$uu.[0]; if($temp_hash{$uu} eq "") { $array[$l]="NULL"; } else { #$log->info("guess $temp_hash{$uu} $uu \n"); if($temp_hash{$uu} eq "NULL") { # $array[$l]=""; @var[0]=""; } #for variable of the form $x=>list and filename type elsif($temp_hash{$uu}=~/(\$[a-zA-Z0-9]+)?=>(.*)?$/){ # $var=$3; @var[0]=$var_list{$1}; # #$log->info("Val $l - $array[$l]\n"); # #$log->info("testing-1 $temp_hash{$uu} $uu @var[0] \n"); } #for variable of the type $x,$y elsif($temp_hash{$uu}=~/(^\$[a-zA-Z0-9]+)$/) { $try=$1; @var[0]=$var_hash{$1}; #$log->info("testing $uu @var[0] \n"); } #for variable of the form $x.ani elsif($temp_hash{$uu}=~/([^\$]*\.)?(\$[a-zA-Z0-9])\.(.*)?/) { my $temp1=$1; my $var1=$2; my $temp2=$3; #$log->info("Vadsdsdl $l - $array[$l] $temp1 $var1 $temp2\n"); $array[$l]=$temp1."$var_hash{$var1}".$temp2; #$log->info("Vadsdsdl $l - $array[$l] $temp1 $var1 $temp2\n"); @var[0]=$array[$l]; #$log->info("test @var[0] \n") # if($temp1 ne '' and $temp2 ne ''){ # @var[0]=$temp1.$var_hash{$var1}.$temp2;} # if($temp1 eq '' and $temp2 ne ''){ # @var[0]='"'.$var_hash{$var1}.$temp2;} # if($temp1 ne '' and $temp2 eq ''){ # @var[0]=$temp1.$var_hash{$var1}.'"' ;} # if($temp1 eq '' and $temp2 eq ''){ # @var[0]='"'.$var_hash{$var1}.'"' ;} } #for variable of the form wunn.$x elsif($temp_hash{$uu}=~/(.*)?\.(\$[a-zA-Z0-9])?/) { my $temp1=$1; my $var1=$2; #$log->info("reverse $l - $array[$l] $temp1 $var1 \n"); $array[$l]=$temp1."$var_hash{$var1}"; #$log->info("recer $l - $array[$l] $temp1 $var1\n"); @var[0]=$array[$l]; #$log->info("rever @var[0] $uu \n"); } else{ @var[0]=$temp_hash{$uu}; #$log->info("batao-errir \n"); } # &update_attr_val("cat", \@cat,$fs_head_array,$sent);} if($uu eq "cm" or $uu eq "tam") { # @check=&get_values("vib",$fs_array); $uu="vib"; } if($uu eq "suff") { # @check=&get_values("tam",$fs_array); $uu="suff"; # $uu="tam"; } if($uu eq "root"){ $uu="lex" ;} if($uu eq "pers"){ $uu="per" ;} if($uu eq "lcat"){ $uu="cat" ;} if($uu eq "gend"){ $uu="gen" ;} if($uu eq "case"){ $uu="cas" ;} if($uu eq "drel"){ $uu="drel" ;} if($uu eq "word") { &modify_field($node,2,@var[0],$sent); } if($uu eq "pos") { &modify_field($node,3,@var[0],$sent); } #$log->info("iupdate @var[0] $uu $fs_head_array[\@var{$uu}]\n"); &update_attr_val($uu, \@var,$fs_head_array,$sent); } } my $string=&make_string($fs_head_array,$sent); #$log->info("test $string\n"); &modify_field($node,4,$string,$sent); return $sent; } sub children_modify { my $node=@_[0]; my $sent=@_[1]; my $ref_temp_fs_array=@_[2]; my $ref_temp_array=$$ref_temp_fs_array; my @temp_array=@$ref_temp_array; my @children=&get_children($node,$sent); for($l=0;$l<=$#children;$l++) { #$log->info("Modifying FS of Child $l+1 \n"); my ($f0,$f1,$f2,$f3,$f4)=&get_fields($children[$l],$sent); my $fs_head_array = &read_FS($f4,$sent); my $ref_temp_hash=$temp_array[$l]; my %temp_hash=%$ref_temp_hash; $sent=&modify_fs(\%temp_hash,$children[$l],$sent); } return $sent; } 1;