R
R
ruboss2015-09-18 21:35:42
Perl
ruboss, 2015-09-18 21:35:42

How to understand Perl or translate to another language (java, c++, php, javascript)?

Hello everyone, gentlemen! There are multilingual people?)
I am writing an application related to image recognition. It is necessary to implement the LSH algorithm - for hashing points from a multidimensional space. I found a good implementation, but I don't know Perl, I don't have time to learn
Who knows how to translate the do_hashing function into any of the languages: java, c++, php, javascript?

sub do_hashing {
    my $self   = shift;
    my $vector = shift;
    if ( ref $vector ne 'ARRAY' ) {
        carp("args should be an array_ref") and return;
    }
    my $d = $self->d || sub { $self->d( int @$vector ) }
      ->();
    if ( $d != scalar @$vector ) {
        carp("invalid dimention number") and return;
    }
    my $L          = $self->L;
    my $unary_code = $self->_unarize($vector);
    my @hashes;
    for my $i ( 0 .. $L - 1 ) {
        my @array;
        for my $j ( @{ $self->_pickuped_indexes->[$i] } ) {
            push( @array, $unary_code->[$j] );
        }
        push( @hashes, \@array );
    }
    return \@hashes;
}

sub _pickuped_indexes {
    my $self = shift;
    $self->_indexes or sub {
        my @indexes;
        my $L = $self->L;
        for my $i ( 0 .. $L - 1 ) {
            my %seen;
            while (1) {
                my $rand = int( rand( $self->d * $SCALE ) );
                if ( !$seen{$rand} ) {
                    $seen{$rand} = 1;
                    last if keys %seen == $self->k * $SCALE;
                }
            }
            push( @indexes, [ sort { $a <=> $b } keys %seen ] );
        }
        $self->_indexes( \@indexes );
      }
      ->();
}

sub _unarize {
    my $self   = shift;
    my $vector = shift;
    my $max    = max(@$vector);
    my $n      = $SCALE / $max;
    my @unary;
    for (@$vector) {
        my $i = int( $_ * $n );
        my $j = $SCALE - $i;
        for ( 1 .. $i ) {
            push( @unary, 1 );
        }
        for ( 1 .. $j ) {
            push( @unary, 0 );
        }
    }
    return \@unary;
}

Maybe it will be useful to someone - a link to the algorithm https://metacpan.org/pod/release/MIKI/Algorithm-LS...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadz Goldman, 2015-09-19
@ruboss

And the module itself also needs to be translated, right? =)
Do not engage in nonsense. Either write it yourself or translate it yourself.
The most excellent option has already been suggested: calling a script from your Java application.
But I'll give you one very simple piece of advice: read the perl script aloud. As ordinary text (omitting iterations and so on).
You will immediately understand everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question