ComputeVibhakti.pm 5.13 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
package ILMT::KAN::HIN::ComputeVibhakti;
#use strict;
#use warnings;
use Dir::Self;
use Data::Dumper;
use ILMT::KAN::HIN::SSFAPI::feature_filter;
use ILMT::KAN::HIN::SSFAPI::shakti_tree_api;
use ILMT::KAN::HIN::ComputeVibhakti::ComputeTAM;

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;
}

#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;