2011-12-08 2 views
3

Perl의 Text::DocumentCollection에 문서 모음이 있으면 Text::Document을 사용하여 컬렉션의 두 문서간에 cosine similarity을 계산하고 싶습니다.Perl의 Text :: DocumentCollection에서 모든 문서를 다른 문서와 비교하십시오.

아마도 이것은 EnumerateV 및 콜백을 사용하여 수행 할 수 있다고 생각하지만 구체적인 사항을 파악하는 데 문제가 있습니다. (This SO question 도움이 될 것입니다,하지만, 난 여전히 붙어있어.)

는 다음과 같이 수집 test.db에 저장되어있는 가정, 구체적으로 :

#!/usr/bin/perl -w 
use Text::DocumentCollection; 
use Text::Document; 

$c = Text::DocumentCollection->new(file => 'test.db'); 

my $text = 'Stack Overflow is a programming | Q & A site that’s free. Free to ask | questions, free to answer questions|, free to read, free to index'; 

my @strings = split /\|/, $text; 
my $i=0; 

foreach (@strings) { 
    my $doc = Text::Document->new(); 
    $doc->AddContent($_); 
    $c->Add(++$i,$doc); 
} 

지금 내가 test.db에서 읽고 코사인 유사도를 계산해야 가정 모든 문서 조합에 대해 (위의 코드에서 저장된 데이터베이스 파일 이외의 다른 문서에 액세스 할 수 없습니다.)

대답은 콜백을 사용하여 EnumerateV에 액세스하는 서브 루틴을 구성하는 것입니다. 서브 루틴도 EnumerateV이라고 부르는 것을 추측하지만 나는 그것을 파악할 수 없었다.

답변

2

당신은 이런 일을 시작 할 수 있습니다 :

$c->EnumerateV(sub { 
    my ($c, $k1, $d1) = @_; 
    $c->EnumerateV(sub { 
     my ($c, $k2, $d2) = @_; 
    return if exists $dist{$k1.$k2}; 
    $dist{$k1.$k2} = $dist{$k2.$k1}= cosine_dist($d1, $d2); 
    }); 
}); 
관련 문제