vibhakti_compute.pl 3.91 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
#!/usr/bin/perl
#use strict;
#use GDBM_File;
#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 $vibh_home = @_[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;
			}
	}
        #Deletes the leaves containing vibhakti.
	$delete=0;
	foreach (@remove)
	{
		&delete_node($_-$delete,$sent);
		$delete++;
	}
	delete @remove[0..$#remove];
}
1;