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
,但我一直無法弄清楚。