【问题标题】:inverted index generation using perl for large data set使用 perl 为大型数据集生成倒排索引
【发布时间】:2013-12-07 10:53:38
【问题描述】:

我的目标是在 perl 中构建一个倒排索引文件:我的文件格式为 1000 万+行:

document id:  citing document 1; citing document 2; 

示例:

document 56: document 12, document 45
document 117: document 12, document 22, document 99

我想在表单中创建另一个文件:

document 12: document 117, document 56 
...

目前我正在逐行读取源文件,并为每个引用附加索引文件(每个文档一行)。但是为每个引用附加索引文件(In Perl, how do I change, delete, or insert a line in a file, or append to the beginning of a file?)非常慢。任何替代/更有效的方法?谢谢。

【问题讨论】:

  • 在显示示例输入(和代码)时请使用代码格式化选项。
  • 我想说,如果您有 1000 万行文件,那么您要么需要大量内存来存储所有行,然后才能打印,要么您需要随时添加行。由于这样的查找和打印会很昂贵,至少可以说,后一种解决方案的唯一选择是Tie::File
  • perlmonks.org/?node_id=484831 如果您不坚持自己的实施。
  • 非常感谢您的所有建议:vogomatix 和 user1534668 的回答让我朝着正确的方向前进。我将创建一个大哈希;然后将源文件解析为更小的块(以适应内存中的哈希)为每个块创建多个倒排索引文件。最后合并它们很容易。
  • mpapec:确实,lucene 方法会更明智。我通过解析 20+ Gb 的压缩 XML 文件创建了我的源索引文件,我怀疑使用 lucene 可能会更容易(我使用 XML::Simple)

标签: perl


【解决方案1】:

您想读入文件并使用数据构建散列。这应该让你开始

use strict;
use warnings;
use 5.010;

my %cited; # results go here

while (<DATA>) { # really read from your file
    chomp;
    my ($doc, @cites) = split /:\s+|,\s+/;
    for (@cites) {
        push @{$cited{$_}}, $doc;
    }
}
for (sort keys %cited) {
    say "$_ cited in: ", join ", ", sort @{$cited{$_}};
}

__DATA__
document 56: document 12, document 45
document 117: document 12, document 22, document 99
document 17: document 67, document 22, document 1

【讨论】:

    【解决方案2】:

    采用以下算法代替修改索引文件:

    1. 将倒排索引文件加载到哈希结构中
    2. 读取每个文档并将引用添加到哈希结构中
    3. 编写倒排索引文件。

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多