Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating an output with a Tab / Space

Tags:

perl

I am working with three text documents. The first one is the main input (Input 1) with words and the word type (Noun, Verb, etc.) separated by a tab.

Input 1

John    N
goes    V
to      P
school  N
.       S
Mary    N
comes   V
from    P
home    N
.       S

The second and third input text files look like this:

Input 2

John
Mary

Input 3

to
from

My objective is to compare and match the second and third text files with the main input and get an output like this:

Expected output:

John    N   N
goes    V
to      P   P
school  N
.       S
Mary    N   N
comes   V
from    P   P
home    N
.       S

All the three columns should be separated by tab or space. However, I am getting an output like this:

John N  
N
goes    
V
to P    
P
school  
N
.   
S
Mary N  
N
comes   
V
from P  
P
home    
N
.   
S

I believe this is happening as I took the input of the first text file into an array and printed the values. Please suggest a way to get the desired output.

The program coding that I have used is below:

#!/usr/bin/perl

use warnings;
use strict;

my @file = ('Input 1.txt');

open my $word_fh, '<', 'Input 2.txt' or die $!;
open my $word2_fh, '<', 'Input 3.txt' or die $!;

my %words_to_match = map {chomp $_; $_ => 0} <$word_fh>;
my %words_to_match2 = map {chomp $_; $_ => 0} <$word2_fh>;

close $word_fh;
close $word2_fh;

check($_) for @file;

sub check {
    my $file = shift;

open my $fh, '<', $file or die $!;

while (<$fh>){
    chomp;
    my @words_in_line = split;

    for my $word (@words_in_line){
        $word =~ s/[(\.,;:!)]//g;
        $word .= '  N' if exists $words_to_match{$word};
        $word .= '  P' if exists $words_to_match2{$word};
        
        print "$word\n";
    }
    print "\n";
}

Again, the objective is to have an output with all the three columns separated by tab or space.

like image 448
Nandini143 Avatar asked Jan 21 '26 10:01

Nandini143


1 Answers

It makes things a lot easier if you read all your reference files and build data structures from them first, and then read your primary input file and transform it

You're using two hashes, %words_to_match and %words_to_match2 and storing every element with a value of zero. That's a waste of information, and the best thing here is to build a single hash that relates the words in each reference file to their part of speech. The words in Input 2.txt are nouns, so they get an N, while those in Input 3.txt are prepositions, so they get a P

Then you just have to check to see whether there exists a hash element that matches each word in Input 1.txt and append its value before printing the record if so

The program below creates a hash %pos looking like this, which relates every word in the two reference files to its part of speech

( from => "P", John => "N", Mary => "N", to => "P" )

and in the final input loop I've used a subtitution s/// to replace all trailing whitespace (which includes newlines) with three spaces and the part of speech. Tabs aren't useful things for laying out tables, firstly because no one can agree where the tab stops should be, and secondly because a single tab won't always line up columns. Depending how many characters there were in the preceding data, you may sometimes need two or more

I hope it's clear

use strict;
use warnings 'all';
use autodie;

my %words;

my %files = (
    'input 2.txt' => 'N',   
    'input 3.txt' => 'P',   
);

while ( my ( $file, $pos ) = each %files ) {
    open my $fh, '<', $file;

    while ( <$fh> ) {
        s/\s+\z//;
        $words{$_} = $pos;
    }
}

{
    open my $fh, '<','input 1.txt';

    while ( <$fh> ) {
        next unless /\S/;
        my ($word) = split;
        my $pos = $words{$word};
        s/\s+\z/   $pos\n/ if $pos;
    }
    continue {
        print;
    }
}

output

John    N   N
goes    V
to      P   P
school  N
.       S
Mary    N   N
comes   V
from    P   P
home    N
.       S
like image 190
Borodin Avatar answered Jan 23 '26 07:01

Borodin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!