ComputeVibhakti.pm 11.7 KB
Newer Older
priyank's avatar
priyank committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
package ILMT::TEL::HIN::ComputeVibhakti;
#use strict;
#use warnings;
use Dir::Self;
use Data::Dumper;
use ILMT::TEL::HIN::SSFAPI::feature_filter;
use ILMT::TEL::HIN::SSFAPI::shakti_tree_api;

sub process {
    my %par = @_;
    my $input = $par{'data'};
    read_story(\$input);
    my $keep = $par{'keep'};
    my $body;
    my $numbody = get_bodycount();
    for(my($bodynum)=1;$bodynum<=$numbody;$bodynum++)
    {

	$body = get_body($bodynum,$body);

	# count the number of paragraphs in the story
	my($numpara) = get_paracount($body);

	#print stderr "paras : $numpara\n";

	# iterate through paragraphs in the story
	for(my($i)=1;$i<=$numpara;$i++)
	{

	    my($para);
	    # read paragraph
	    $para = get_para($i);


	    # count the number of sentences in this paragraph
	    my($numsent) = get_sentcount($para);
	    # iterate through sentences in the paragraph
	    for(my($j)=1;$j<=$numsent;$j++)
	    {

		# read the sentence which is in ssf format
		my($sent) = get_sent($para,$j);

		#copy vibhakti info
		ComputeVibhakti($sent, $keep);

		#compute tam
		ComputeTAM($sent, $keep);
	    }
	}
    }

    open OUTFILE, '>', \$result  or die $!;
    select(OUTFILE);
    printstory();
    select(STDOUT);
    return $result;
}

sub ComputeTAM
{
    my $sent=@_[0];
    my $keep=$_[1];

    my @uns_VG_nodes = &get_nodes(3,"VGF",$sent);	#get all the VG nodes
    my @VGINF_nodes = &get_nodes(3,"VGINF",$sent);	#get all the VG nodes
    my @VGNF_nodes = &get_nodes(3,"VGNF",$sent);	#get all the VG nodes
    my @VGNN_nodes = &get_nodes(3,"VGNN",$sent);	#get all the VG nodes

    #push VGINF,VGNF,VGNN nodes to uns_VG_nodes. Thus we have single list containing all the VG nodes.
    foreach (@VGINF_nodes)
    {
	push(@uns_VG_nodes,$_);
    }
    foreach (@VGNF_nodes)
    {
	push(@uns_VG_nodes,$_);
    }
    foreach (@VGNN_nodes)
    {
	push(@uns_VG_nodes,$_);
    }

    my @remove;
    my @VG_nodes = sort {$a <=> $b}(@uns_VG_nodes);	#sorting list in ascending order
    foreach $node (@VG_nodes)
    {
	my @leaves = &get_leaves_child($node,$sent); #gets all the leaves of the the VG node
	my $parent = $node;
	my $head = 0;
	my $final_tam_aux = "";
	my $neg = "";
	$fs_array_head = "";
	$verb_leaf_present = 0;
	my $flag=0;
	my @final_tam;
	my @_leaf;
	my $position="";

	$f4=&get_field($node,4,$sent); #gets fs of the node
	my $string_fs = &read_FS($f4, $sent);
	my @head_value = &get_values("head", $string_fs, $sent);#gets head value of the node

	#checks for verb leaf, if present sets verb_leaf_present=1
	foreach $leaf (@leaves)
	{
	    $leaf_tag = &get_field($leaf, 3,$sent);#gets postag of leaf
	    if($leaf_tag =~ /^V/)
	    {
		$verb_leaf_present = 1;
	    }
	}


	foreach $leaf (@leaves)
	{
	    $leaf_tag = &get_field($leaf, 3,$sent);#gets postag of leaf
	    $leaf_lex = &get_field($leaf, 2,$sent);#gets lexical item of leaf
	    if($leaf_tag =~/^V/ and $head == 0)
	    {

		$head = 1;
		$node_head = $leaf;
		$fs = &get_field($leaf, 4,$sent);#gets feature structure
		$fs_array = &read_FS($fs,$sent);
		$fs_array_head = $fs_array;
		@tam = &get_values("vib", $fs_array,$sent);
		my @name_value= &get_values("name",$fs_array,$sent); #gets value of name attribute
		if($head_value[0] eq $name_value[0])
		{
		    $num=$leaf-$node; #gives position of the leaf with respect to the nodei

		    # modifies the value of vpos(position) in a chunk

		    if($position ne "")
		    {$position=$position."_"."tam$num";}
		    if($position eq "")
		    {$position="tam$num";}
		}
		#storing tam values
		if($tam[0] ne "")
		{
		    if($final_tam_aux ne "")
		    {
			$final_tam_aux = $final_tam_aux."_".$tam[0];
		    }
		    else
		    {
			$final_tam_aux = $tam[0];
		    }
		    #store all the tam of all interpretation in $final_tam
		}
		else
		{
		    if($final_tam_aux ne "")
		    {
			$final_tam_aux = $final_tam_aux."_".0;
		    }
		    else
		    {
			$final_tam_aux = 0;
		    }
		}
	    }
	    elsif($leaf_tag=~/^VAUX/ or $leaf_tag=~/PSP/ or $leaf_tag=~/NST/) #identifying whether a vibhakti or not.
	    {
		$flag=1;
		#modifying the value of vpos(position)
		if($position ne "")
		{
		    $num=$leaf-$node;
		    $position=$position."_".$num;
		}
		else
		{
		    $position=$leaf-$node;
		}



		my $word1=&get_field($leaf,2,$sent); #gets the word(lex)
#print "LEAF TAG--$leaf_tag--$word1\n";
		push(@remove,$leaf);
		$fs = &get_field($leaf, 4,$sent);
		$fs_array = &read_FS($fs,$sent);
		@tam = &get_values("vib", $fs_array); #gets value of vib attribute
		@lex = &get_values("lex", $fs_array); #gets lexical item(root)
		push(@_leaf,$leaf);
		my $root = $lex[0];
		my $tam_t = "";
		#line 137 to 162 modifies tam feature of fs.

		if($tam[0] ne "" and $tam[0] ne "`" and $tam[0] ne "0" and $tam[0] ne $root)
		{
		    $tam_t = $tam[0];
		    if($final_tam_aux ne "")
		    {
			$final_tam_aux = $final_tam_aux."_".$root."+".$tam_t;
		    }
		    else
		    {
			$final_tam_aux = $root."+".$tam_t;
		    }
		}
		else
		{
		    $tam_t = "0";

		    if($final_tam_aux ne "")
		    {
			$final_tam_aux = $final_tam_aux."_".$root;
		    }
		    else
		    {
			$final_tam_aux = $root;
		    }
		}
	    }
	    elsif($leaf_tag eq 'NEG' and $verb_leaf_present == 1)
	    {
=cut
if($position ne "")
				{
					$num=$leaf-$node;
					$position=$position."_"."NEG$num";
				}
				if($position eq "")
				{
					$num=$leaf-$node;
					$position="NEG$num";
				}
				$neg = &get_field($leaf, 2,$sent);

				push(@remove,$leaf);
				$flag=1;
				$fs = &get_field($leaf, 4,$sent);
				$fs_array = &read_FS($fs,$sent);
				@tam = &get_values("vib", $fs_array);
				@lex = &get_values("lex", $fs_array);
				push(@_leaf,$leaf);
				my $root = $lex[0];
				my $tam_t = "";
				if($tam[0] ne "" and $tam[0] ne "`" and $tam[0] ne "0" and $tam[0] ne $root)
				{
					$tam_t = $tam[0];
					if($final_tam_aux ne "")
					{
						$final_tam_aux = $final_tam_aux."_".$root."+".$tam_t;
					}
					else
					{
						$final_tam_aux = $root."+".$tam_t;
					}
				}
				else
				{
					$tam_t = "0";
					if($final_tam_aux ne "")
					{
						$final_tam_aux = $final_tam_aux."_".$root;

					}
					else
					{
						$final_tam_aux = $root;
					}
				}
=cut
			}


		    }

		    $fs_head = &get_field($parent, 4,$sent);
		    $fs_head_array = &read_FS($fs_head,$sent);
		    my @num,@gen,@per;
#print "-->",$#_leaf,"\n";
		    if($#_leaf>0)
		    {
			my $fs1 = &get_field($_leaf[-1], 4,$sent);
			my $fs2 = &get_field($_leaf[-2], 4,$sent);

			$fs_array1=&read_FS($fs1,$sent);
			$fs_array2=&read_FS($fs2,$sent);
			@num = &get_values("num", $fs_array1,$sent);
			@per = &get_values("per", $fs_array1,$sent);
			@gen = &get_values("gen", $fs_array2,$sent);
		    }
		    if($#_leaf==0)
		    {
			my $fs1 = &get_field($_leaf[-1], 4,$sent);
			my $pos1 = &get_field($_leaf[-1], 3,$sent);
			if($pos1 eq "VAUX" or $pos1 eq "PSP")
			{
			    $fs_array1=&read_FS($fs1,$sent);
			    @num = &get_values("num", $fs_array1,$sent);
			    @per = &get_values("per", $fs_array1,$sent);
			}
		    }
		    $tam_new[0] = $final_tam_aux;
		    &update_attr_val_2("vib", \@tam_new, $fs_head_array->[0],$sent);
		    #print "@num[0]--@per[0]--@gen[0]\n";
		    if(@gen[0] ne "")
		    {
			&update_attr_val_2("gen", \@gen, $fs_head_array->[0],$sent);
		    }
		    if(@num[0] ne "")
		    {
			&update_attr_val_2("num", \@num, $fs_head_array->[0],$sent);
		    }
		    if(@per[0] ne "")
		    {
			&update_attr_val_2("per", \@per, $fs_head_array->[0],$sent);
		    }

		    if($verb_leaf_present == 1 and $flag==1)
		    {
			$string_head = &make_string($fs_head_array,$sent);
			my ($x,$y)=split(/>/,$string_head);
			my $new_head_fs=$x." vpos=\"$position\">";
			&modify_field($parent, 4, $new_head_fs,$sent);
		    }
		    delete @num[0..$#remove];
		    delete @per[0..$#remove];
		    delete @gen[0..$#remove];
		}
		if (not defined $keep) {
		    my @sort_remove=sort{$a <=> $b} @remove;
		    my $delete=0;
		    foreach (@sort_remove)
		    {
			&delete_node($_-$delete,$sent);
			$delete++;
		    }
		    delete @remove[0..$#remove];
		    delete @sort_remove[0..$#remove];
		}

		#print "after vib comp--\n";
		#&print_tree();
}

#the module prunes multiple feature structure (NN, NNP, PRP at present), it also removes the parsarg node in the NP and adds it to its noun fs.
#$&compute_vibhakti;
sub ComputeVibhakti
{
    my $sent=@_[0];
    my $keep = @_[1];

    #my $delete;	#keeps count of all the deleted node, helps in locating node obtained before deletion.

    #get all the noun nodes in the tree, the noun will be case marked '1' if a vibhakti is present, else case is '0'

    #my @all_leaves = &get_leaves();
    #&read(@_[0]);
    my @all_children_NP =&get_nodes(3,"NP",$sent); #gets all the NP nodes
    my @all_children_RBP =&get_nodes(3,"RBP",$sent); #gets all the RBP nodes
    my @all_children = (@all_children_NP , @all_children_RBP); #contains all the NP and RBP nodes
    my @all_children = sort { $a <=> $b } @all_children;

    foreach $node(@all_children)
    {

	my @node_leaves=&get_leaves_child($node,$sent); #gets leaf nodes of NP or RBP node


	$position="";
	$nhead=0;
	$f4=&get_field($node,4,$sent); # gets feature structure
	my $string_fs = &read_FS($f4, $sent);

	#gets head and vibhakti values

	my @head_value = &get_values("head", $string_fs, $sent);
	my @vibh_value=&get_values("vib", $string_fs, $sent);
	$vibh_chunk=$vibh_value[0];
	#iterates through each leaf node and gets postag, word, fs

	foreach $NP_child(@node_leaves)
	{
	    my $pos = &get_field($NP_child,3,$sent);
	    my $word = &get_field($NP_child,2,$sent);
	    my $fs = &get_field($NP_child,4,$sent);
	    my $str_fs=&read_FS($fs,$sent);
	    my @name_value=&get_values("name",$str_fs,$sent);
	    if($pos eq "NN" or $pos eq "NNP" or $pos eq "PRP")
	    {
		$nhead=1;
		$flag=0;
		$prev_RB=0;
		$flag_NN=1
	    }
	    if($pos eq "RB")
	    {
		$flag=1;
		$prev_RB = 1;
		$flag_NN=0

	    }

	    if($head_value[0] eq $name_value[0])
	    {
		$num=$NP_child-$node; #gives position of the leaf with respect to the node

		# modifies the value of vpos(position) in a chunk

		if($position ne "")
		{$position=$position."_"."vib$num";}
		if($position eq "")
		{$position="vib$num";}

	    }


	    #Adds the RP vibhakti to vpos

	    if($pos eq "RP")
	    {
		if($position ne "")
		{
		    $position=$position."_"."RP";
		}
		else
		{
		    next;
		}
	    }


	    if($pos eq "PSP" or $pos eq "NST" and $nhead==1)
	    {
		#Adds position of vibhakti in vpos(position) value

		if($position ne "")
		{
		    $num=$NP_child-$node;
		    $position=$position."_".$num;
		}

		else
		{
		    $position=$NP_child-$node;
		}
		my $val_fs=&get_field($NP_child, 4,$sent);

		$FSreference = &read_FS($val_fs,$sent); #reads feature structure of the leaf
		my @cur_vibhakti = &get_values("lex",$FSreference); #fetches the lexical value of vibhakti
		my @cur_vib_vib = &get_values("vib",$FSreference);

		#adds the lexical value of vibhakti to vibh_chunk

		if($vibh_chunk ne "")
		{
		    $vibh_chunk=$vibh_chunk . "_" . $cur_vibhakti[0];
		}
		else
		{
		    $vibh_chunk="0_".$cur_vibhakti[0];
		}
		push(@remove,$NP_child);

	    }

	}
	if($vibh_chunk)
	{
	    my @vibh_chunk_arr=();
	    push @vibh_chunk_arr,$vibh_chunk; #pushes the value of vibh_chunk in vibh_chunk_arr

	    my $head_node=&get_field($node,4,$sent);
	    my $FSreference1 = &read_FS($head_node,$sent); #gets FS value
	    &update_attr_val("vib", \@vibh_chunk_arr,$FSreference1,$sent); #updates value of attribute vib

	    # Modifies the value of fs by adding new attribute vpos that will be in output.

	    my $string=&make_string($FSreference1,$sent);
	    my ($x,$y)=split(/>/,$string);
	    my $new_head_fs=$x." vpos=\"$position\">";
	    &modify_field($node,4,$new_head_fs,$sent);

	    undef $head_word;
	    undef $new_string;
	}
    }

    if (not defined $keep) {
	#Deletes the leaves containing vibhakti.
	$delete=0;
	foreach (@remove)
	{
	    &delete_node($_-$delete,$sent);
	    $delete++;
	}
	delete @remove[0..$#remove];
    }
}

1;