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

hi i found two pieces of code (unfortunately in python....) and i want to put them in my perl search engine....can anyone help me convert them to perl? the fist code determins the location of a term in a document (the higher the term is in the doc the more relevant is the doc to the term)
def locationscore(self,rows): locations=dict([(row[0],1000000) for row in rows]) for row in rows: loc=sum(row[1:]) if loc<locations[row[0]]: locations[row[0]]=loc return self.normalizescores(locations,smallIsBetter=1)
the second one has to do with word distance (the closer the terms of a multiple query are to a doc the more relevant the doc is to that query)
def distancescore(self,rows): # If there's only one word, everyone wins! if len(rows[0])<=2: return dict([(row[0],1.0) for row in rows]) # Initialize the dictionary with large values mindistance=dict([(row[0],1000000) for row in rows]) for row in rows: dist=sum([abs(row[i]-row[i-1]) for i in range(2,len(row))]) if dist<mindistance[row[0]]: mindistance[row[0]]=dist return self.normalizescores(mindistance,smallIsBetter=1)
thats all, pls help me ....i know nothing in python and i am a newbie in perl!!! thanks

Replies are listed 'Best First'.
Re: term weighting :convert python to perl
by pc88mxer (Vicar) on Apr 14, 2008 at 21:13 UTC
    Here's a stab at the first one:
    sub locationscore { my $self = shift; my $rows = shift; my %locations; for my $row (@$rows) { my $sum = 0; for (1..$#$row) { $sum += $row->[$_] }; if (!defined($locations{ $row->[0] } || $sum < $locations{ $row->[ +0] } ) { $locations{ $row->[0] } = $sum; } } $self->normalizescores(\%locations, ...); }
    Update: Fixed logic error in setting of $locations{ $row->[0] }. I've taken a little liberty with the logic. To make it conform strictly with the original python coding, just add
    $locations{ $_->[0] } = 1_000_000 for (@$rows);
    right before the for loop.
Re: term weighting :convert python to perl
by Anonymous Monk on Apr 15, 2008 at 08:33 UTC
    hi i found two pieces of code (unfortunately in python....) and i want to put them in my perl search engine....

    Find a description of what the code does.