DefaultFeatures.pm 4.61 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
package ILMT::HIN::PAN::DefaultFeatures;
#use strict;
#use warnings;
use Dir::Self;
use Data::Dumper;
use ILMT::HIN::PAN::SSFAPI::feature_filter;
use ILMT::HIN::PAN::SSFAPI::shakti_tree_api;

my $ruleFile = __DIR__ . "/DefaultFeatures/rule.rl";
my %rule_hash = ();

sub preprocess {
    open(RULE,$ruleFile);
    @rules = <RULE>;

    %rule_hash = ();

    foreach $rule (@rules)
    {
        if($rule !~ /^\s*$/)
        {
            chomp($rule);
            ($fea,$array_in) = split(/\s+/,$rule);
            $rule_hash{ $fea } = $array_in;
        }
    }
}

sub process {
    my %par = @_;
    my $input = $par{'data'};
    utf8::encode($input);
    read_story(\$input);
    my $result;
    my $numBody = &get_bodycount();
    my $body;
    for(my($bodyNum)=1;$bodyNum<=$numBody;$bodyNum++)
    {

        $body = &get_body($bodyNum,$body);

        my($numPara) = &get_paracount($body);

        for(my($i1)=1;$i1<=$numPara;$i1++)
        {

            my($para);
            $para = &get_para($i1);


            my($numSent) = &get_sentcount($para);

            for(my($j1)=1;$j1<=$numSent;$j1++)
            {
                my($sent) = &get_sent($para,$j1);

                @nodes=&get_leaves($sent);
                foreach $node (@nodes)
                {
                    $fsSt = &get_field($node,4,$sent);
                    $fs_array = &read_FS($fsSt,$sent);
                    @lex = &get_values("lex", $fs_array,$sent);
                    @cat = &get_values("cat", $fs_array,$sent);
                    @tam = &get_values("vib", $fs_array,$sent);
                    @gen = &get_values("gen", $fs_array,$sent);
                    @num = &get_values("num", $fs_array,$sent);
                    @per = &get_values("per", $fs_array,$sent);

                    if($cat[0] eq "")
                    {
                        my @per_chunk_arr=();
                        push @per_chunk_arr,$rule_hash{"cat"};

                        my $head_node=&get_field($node,4,$sent);
                        my $FSreference1 = &read_FS($head_node,$sent);
                        &update_attr_val("cat", \@per_chunk_arr,$FSreference1,$sent);
                        my $string=&make_string($FSreference1,$sent);
                        &modify_field($node,4,$string,$sent);
                    }
                    if($tam[0] eq "")
                    {
                        my @per_chunk_arr=();
                        push @per_chunk_arr,$rule_hash{"tam"};

                        my $head_node=&get_field($node,4,$sent);
                        my $FSreference1 = &read_FS($head_node,$sent);
                        &update_attr_val("tam", \@per_chunk_arr,$FSreference1,$sent);
                        my $string=&make_string($FSreference1,$sent);
                        &modify_field($node,4,$string,$sent);
                    }
                    if($gen[0] eq "")
                    {
                        my @per_chunk_arr=();
                        push @per_chunk_arr,$rule_hash{"gen"};

                        my $head_node=&get_field($node,4,$sent);
                        my $FSreference1 = &read_FS($head_node,$sent);
                        &update_attr_val("gen", \@per_chunk_arr,$FSreference1,$sent);
                        my $string=&make_string($FSreference1,$sent);
                        &modify_field($node,4,$string,$sent);
                    }
                    if($num[0] eq "")
                    {
                        my @per_chunk_arr=();
                        push @per_chunk_arr,$rule_hash{"num"};

                        my $head_node=&get_field($node,4,$sent);
                        my $FSreference1 = &read_FS($head_node,$sent);
                        &update_attr_val("num", \@per_chunk_arr,$FSreference1,$sent);
                        my $string=&make_string($FSreference1,$sent);
                        &modify_field($node,4,$string,$sent);
                    }
                    if($per[0] eq "")
                    {
                        my @per_chunk_arr=();
                        push @per_chunk_arr,$rule_hash{"per"};

                        my $head_node=&get_field($node,4,$sent);
                        my $FSreference1 = &read_FS($head_node,$sent);
                        &update_attr_val("per", \@per_chunk_arr,$FSreference1,$sent);
                        my $string=&make_string($FSreference1,$sent);
                        &modify_field($node,4,$string,$sent);
                    }
                }
            }
        }
    }

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

preprocess();

1;