smilly has asked for the wisdom of the Perl Monks concerning the following question:

Dear friends, after trying to write my simmat.pl to get a corpus n returns the similarity matrix, in output file(similarity matrix), i get something like this : ARRAY(0x9976d30)ARRAY(0x9976ab4)ARRAY(0x9c52bc8)ARRAY(0x9a00908), while I expect the 4X4 similarity matrix. how to reach that? my input file is a small copus of 4 words, one in each line.here is my codes:
#! /usr/local/bin/perl -w use strict; use warnings; use WordNet::QueryData; use WordNet::Similarity::random; use WordNet::Similarity::path; use WordNet::Similarity::wup; use WordNet::Similarity::lch; use WordNet::Similarity::jcn; use WordNet::Similarity::res; use WordNet::Similarity::lin; use WordNet::Similarity::hso; use WordNet::Similarity::lesk; use WordNet::Similarity::vector; use WordNet::Similarity::vector_pairs; my $Infile = shift; my $Outfile = shift; my $Measure = shift; unless (defined $Infile and defined $Outfile and defined $Measure) { print STDERR "Undefined input\n"; print STDERR "Usage: simmat.pl inputfile outputfile measure()\n"; exit 1; } print STDERR "Loading WordNet... "; my $wn = WordNet::QueryData->new; die "Unable to create WordNet object.\n" if(!$wn); print STDERR "done.\n"; open (FILE, "$Infile"); @words = <FILE>; #print $words[0]; close(FILE); for my $i (0 .. $#words) { for my $j ( ($i+1) .. $#words) { $sim[$i][$j] = similarity( $words[$i], $words[$j]) $sim[$j][$i] = $sim[$i][$j]; } } sub similarity { my ( $w1, $w2 ) = @_; my $sim = 1; my $obj = WordNet::Similarity::"$Measure"->new($wn); my $sim = $obj->getRelatedness($w1, $w2); return sim; } open (OUTPUT, ">$Outfile"); print OUTPUT @sim; close(OUTPUT);

Replies are listed 'Best First'.
Re: how to get the matrix in output?
by toolic (Bishop) on Feb 06, 2008 at 02:30 UTC
    use Data::Dumper; might give you a clearer picture of whatever data structure you are working with. For example:
    print Dumper(\@sim);

    However, I do not believe the code you posted is the actual code that you are using. The code you posted would not compile without errors since you have not declared several variables with my, such as @words and @sim.

    It is also a good practice to check the success of open, for example:

    open my $in_fh, '<', $Infile or die "Can not open file $Infile: !$\n"
      Dear Friend, U were right, I re arranged my codes but I sent the older version to you. It's my newer version of my codes. I followed your advice and i could get the matrix but i dont know why all values are "undef". any idea?
      #! /usr/local/bin/perl -w use strict; use warnings; use WordNet::QueryData; use WordNet::Similarity::random; use WordNet::Similarity::path; use WordNet::Similarity::wup; use WordNet::Similarity::lch; use WordNet::Similarity::jcn; use WordNet::Similarity::res; use WordNet::Similarity::lin; use WordNet::Similarity::hso; use WordNet::Similarity::lesk; use WordNet::Similarity::vector; use WordNet::Similarity::vector_pairs; use Data::Dumper; my $Infile = shift; my $Outfile = shift; my $Measure = shift; my (@sim , $simi); unless (defined $Infile and defined $Outfile and defined $Measure) { print STDERR "Undefined input\n"; print STDERR "Usage: simmat.pl inputfile outputfile measure()\n"; exit 1; } print STDERR "Loading WordNet... "; my $wn = WordNet::QueryData->new; die "Unable to create WordNet object.\n" if(!$wn); print STDERR "done.\n"; open (INPUT, "$Infile") || die "can't open the input file"; my @words = <INPUT>; close (INPUT) ; for my $i (0 .. $#words) { for my $j ( ($i+1) .. $#words) { $sim[$i][$j] = similarity( $words[$i], $words[$j]); $sim[$j][$i] = $sim[$i][$j]; } } sub similarity { my ( $w1, $w2 ) = @_; $simi = 1; my $obj = $Measure -> new($wn); my $simi = $obj-> getRelatedness("$w1#n#1", "$w2#n#1"); return $simi; } open (OUTPUT, ">$Outfile"); print OUTPUT Dumper(\@sim); close(OUTPUT);
Re: how to get the matrix in output?
by hipowls (Curate) on Feb 06, 2008 at 02:35 UTC

    @sim is an array of arrays, you need to do

    foreach my $row ( @sim ) { print OUTPUT @{ $row }, "\n"; }
    you should also turn on warnings and strict.

      totally misleading...