Start New Discussion within our Software Development Community

Hi ,
I am looking for some directions with this and posted the same question in one more forum.

am trying to get the word type as an output from an input file (input.txt) which looks like this,

Input file

John    N   N
loved   V
Mary    N   N
.   S

He  N
was V
eating  V
in  P   P
a   A   A
restaurant  N
.   S

The A   A
cats    N   N
killed  V
the A   A
rats    N   N
 .  S

I   N
am  V
playing V
.   S

The input file has three columns. The first column are the words and the next two columns are the word types. All the columns are separated by tab and there are blank lines in between. The objective of the program is to match the word endings like "ing","ed" and "s" and print the word types in the fourth column. The expected output will be

Expected output

John    N   N
loved   V       V
Mary    N   N
.   S

He  N
was V
eating  V       V
in  P   P
a   A   A
restaurant  N
.   S

The A   A
cats    N   N   N
killed  V       V
the A   A
rats    N   N   N
.   S

I   N
am  V
playing V       V
.   S

The code that I am using is this,

!/usr/bin/perl
use warnings;
use strict;

open my $fh, '<' , 'input.txt' or die $!;
while (<$fh>) {
    chomp ;
    print $_;
    if (/ing\s*$/ or /ed\s*$/) {
        print '  V';
    } 
    if (/s\s*$/) {
        print '  N';
    }
    print "\n";
    }
close($fh);

However I believe I am doing something wrong as I am getting the input file itself as the output. Please help me with some directions. Thanks in advance.

Hi,

Why your program is print the the input lines is because of line 8 in your script. Also your regex is not matching like you wanted.

Well, looking at your input file and expected output, I think what your regex should do is take the last alphabeth of required line and reprint it.

Something like thus:

use warnings;
use strict;

while(<DATA>) {
    chomp;
    if (/^.+(ed|ing|s)\s+?(.+?\s+?)?(.)$/ig) {
        print join '  ' => $_, $3, $/;
    }
    else {
        print $_, $/;
    }
}

__DATA__
John    N   N
loved   V
Mary    N   N
.   S

He  N
was V
eating  V
in  P   P
a   A   A
restaurant  N
.   S

The A   A
cats    N   N
killed  V
the A   A
rats    N   N
 .  S

I   N
am  V
playing V
.   S

Your Output Becomes:

John    N   N
loved   V  V  
Mary    N   N
.   S

He  N
was V  V  
eating  V  V  
in  P   P
a   A   A
restaurant  N
.   S

The A   A
cats    N   N  N  
killed  V  V  
the A   A
rats    N   N  N  
 .  S

I   N
am  V
playing V  V  
.   S