Chunker.pm 3.09 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
package ILMT::HIN::PAN::Chunker;
use strict;
use warnings;
use Dir::Self;
use Data::Dumper;
use IPC::Run qw(run);

my @dispatch_seq = (
    "ssf2tnt",
    "crf_test",
    "bio2ssf",
);

sub process {
    my %args = @_;
    foreach my $submodule (@dispatch_seq) {
        $args{'data'} = __PACKAGE__->can($submodule)->(%args);
    }
    return $args{"data"};
}

sub ssf2tnt {
    my %par = @_;
    my $data = $par{'data'};
    my $result = "";
    open my $fh, '<', \$data  or die $!;
    while (my $line = <$fh>)
    {
        chomp($line);
        if($line=~/<\/S/)
        {
            $result .= "\n";
            next;
        }
        if($line =~ /^\s*$/)  # if the line has all space charcters
        {
            $result .= "\n";
            next;
        }
        $line=~s/[ ]+/___/g;
        my ($att1,$att2,$att3,$att4) = split (/[\t]+/, $line);
        if($att1 =~ /\<.*/ || $att2 eq "((" || $att2 eq "))") #unwanted lines
        {
            next;
        }
        else
        {
            $result .= "$att2\t$att3\t$att4\n";
        }
    }
    return $result;
}

sub bio2ssf {
    my %par = @_;
    my $data = $par{'data'};
    my $result = "";
    open my $fh, '<', \$data  or die $!;

    my $line = "";
    my $startFlag = 1;
    my $wno = 1;
    my $prevCTag = "";
    my $error = "";
    my $lno = 0;
    my $sno = 1;
    my $cno=0;

    while($line = <$fh>)
    {
        $lno ++;
        if($line =~ /^\s*$/)
        {       # start of a sentence

            $result .= "\t))\t\t\n";
            $result .= "</Sentence>\n\n";
            $startFlag = 1;
            $wno = 1;
            $prevCTag = "";
            $sno ++;
            next;
        }

        if($startFlag == 1)
        {
            $result .= "<Sentence id=\"$sno\">\n";
        }
        chomp($line);
        my @cols = split(/\s+/,$line);

        if($cols[3] =~ /^B-(\w+)/)
        {
            my $ctag = $1;
            if($prevCTag ne "O" && $startFlag == 0)
            {
                $result .= "\t))\t\t\n";
                $wno++;
            }
            $cno++;
            $result .= "$cno\t((\t$ctag\t\n";
            $wno=1;
            $prevCTag = $ctag;
        }
        elsif($cols[3] =~ /^O/)
        {
            if($prevCTag ne "O" && $startFlag == 0)
            {
                $result .= "\t))\t\t\n";
                $wno++;
            }
            $prevCTag = "O";
        }

        if($cols[3] =~ /I-(\w+)/ )
        {       # check for inconsistencies .. does not form a chunk if there r inconsistencies
            my $ctag = $1;
            if($ctag ne $prevCTag)
            {
                $error =$error . "Inconsistency of Chunk tag in I-$ctag at Line no:$lno : There is no B-$ctag to the prev. word\n";
            }
        }
        $cols[2]=~s/___/ /g;
        $result .= "$cno.$wno\t$cols[0]\t$cols[1]\t$cols[2]\n";
        $wno ++;
        $startFlag = 0;
    }
    return $result;
}

sub crf_test {
    my %par = @_;
    my $data = $par{'data'};
    my $result = "";
    run ["/usr/local/bin/crf_test", "-m", __DIR__ . "/Chunker/models/300k_hin_chunker.model"], \$data, \$result;
    return $result;
}