20

我尝试了各种方法来从 Project Gutenberg 文本中剥离许可证,以用作语言学习项目的语料库,但我似乎无法提出一种无监督、可靠的方法。到目前为止,我想出的最好的启发式方法是剥离前 28 行和最后 398 行,这适用于大量文本。关于我可以自动剥离文本的方法的任何建议(这对于许多文本来说非常相似,但在每种情况下略有不同,还有一些不同的模板),以及关于如何验证文本已被准确剥离,将非常有用。

4

3 回答 3

12

多年来,我还想要一个工具来剥离 Project Gutenberg 的页眉和页脚,以便在不污染与 etxt 混合的样板文件的情况下使用自然语言处理。读完这个问题后,我终于抽出手指写了一个 Perl 过滤器,您可以将它通过管道传递到任何其他工具中。

它是使用每行正则表达式作为状态机制作的。它的编写易于理解,因为速度不是 etext 的典型大小的问题。到目前为止,它适用于我在这里拥有的几十个 etext,但在野外肯定会有更多的变体需要添加。希望代码足够清晰,任何人都可以添加:


#!/usr/bin/perl

# stripgutenberg.pl < in.txt > out.txt
#
# designed for piping
# Written by Andrew Dunbar (hippietrail), released into the public domain, Dec 2010

use strict;

my $debug = 0;

my $state = 'beginning';
my $print = 0;
my $printed = 0;

while (1) {
    $_ = <>;

    last unless $_;

    # strip UTF-8 BOM
    if ($. == 1 && index($_, "\xef\xbb\xbf") == 0) {
        $_ = substr($_, 3);
    }

    if ($state eq 'beginning') {
        if (/^(The Project Gutenberg [Ee]Book( of|,)|Project Gutenberg's )/) {
            $state = 'normal pg header';
            $debug && print "state: beginning -> normal pg header\n";
            $print = 0;
        } elsif (/^$/) {
            $state = 'beginning blanks';
            $debug && print "state: beginning -> beginning blanks\n";
        } else {
            die "unrecognized beginning: $_";
        }
    } elsif ($state eq 'normal pg header') {
        if (/^\*\*\*\ ?START OF TH(IS|E) PROJECT GUTENBERG EBOOK,? /) {
            $state = 'end of normal header';
            $debug && print "state: normal pg header -> end of normal pg header\n";
        } else {
            # body of normal pg header
        }
    } elsif ($state eq 'end of normal header') {
        if (/^(Produced by|Transcribed from)/) {
            $state = 'post header';
            $debug && print "state: end of normal pg header -> post header\n";
        } elsif (/^$/) {
            # blank lines
        } else {
            $state = 'etext body';
            $debug && print "state: end of normal header -> etext body\n";
            $print = 1;
        }
    } elsif ($state eq 'post header') {
        if (/^$/) {
            $state = 'blanks after post header';
            $debug && print "state: post header -> blanks after post header\n";
        } else {
            # multiline Produced / Transcribed
        }
    } elsif ($state eq 'blanks after post header') {
        if (/^$/) {
            # more blank lines
        } else {
            $state = 'etext body';
            $debug && print "state: blanks after post header -> etext body\n";
            $print = 1;
        }
    } elsif ($state eq 'beginning blanks') {
        if (/<!-- #INCLUDE virtual=\"\/include\/ga-books-texth\.html\" -->/) {
            $state = 'header include';
            $debug && print "state: beginning blanks -> header include\n";
        } elsif (/^Title: /) {
            $state = 'aus header';
            $debug && print "state: beginning blanks -> aus header\n";
        } elsif (/^$/) {
            # more blanks
        } else {
            die "unexpected stuff after beginning blanks: $_";
        }
    } elsif ($state eq 'header include') {
        if (/^$/) {
            # blanks after header include
        } else {
            $state = 'aus header';
            $debug && print "state: header include -> aus header\n";
        }
    } elsif ($state eq 'aus header') {
        if (/^To contact Project Gutenberg of Australia go to http:\/\/gutenberg\.net\.au$/) {
            $state = 'end of aus header';
            $debug && print "state: aus header -> end of aus header\n";
        } elsif (/^A Project Gutenberg of Australia eBook$/) {
            $state = 'end of aus header';
            $debug && print "state: aus header -> end of aus header\n";
        }
    } elsif ($state eq 'end of aus header') {
        if (/^((Title|Author): .*)?$/) {
            # title, author, or blank line
        } else {
            $state = 'etext body';
            $debug && print "state: end of aus header -> etext body\n";
            $print = 1;
        }
    } elsif ($state eq 'etext body') {
        # here's the stuff
        if (/^<!-- #INCLUDE virtual="\/include\/ga-books-textf\.html" -->$/) {
            $state = 'footer';
            $debug && print "state: etext body -> footer\n";
            $print = 0;
        } elsif (/^(\*\*\* ?)?end of (the )?project/i) {
            $state = 'footer';
            $debug && print "state: etext body -> footer\n";
            $print = 0;
        }
    } elsif ($state eq 'footer') {
        # nothing more of interest
    } else {
        die "unknown state '$state'";
    }

    if ($print) {
        print;
        ++$printed;
    } else {
        $debug && print "## $_";
    }
}
于 2010-12-03T04:06:46.067 回答
4

你不是在开玩笑。这几乎就像他们试图让这项工作完成人工智能一样。我只能想到两种方法,它们都不是完美的。

1) 在 Perl 中设置一个脚本来处理最常见的模式(例如,寻找短语“produced by”,继续向下到下一个空白行并剪切到那里),但要对什么是预期(例如,下一个文本应该是标题或作者)。这样,当模式失败时,你就会知道。模式第一次失败时,请手动执行。第二次,修改脚本。

2) 试试亚马逊的 Mechanical Turk

于 2009-08-13T17:50:34.983 回答
0

哇,这个问题现在太老了。尽管如此,R 中的 gutenbergr 包似乎在删除标题方面做得很好,包括标题“官方”结束后的垃圾。

首先你需要安装 R/Rstudio,然后

install.packages('gutenbergr')
library(gutenbergr)
t <- gutenberg_download('25519')  # give it the id number of the text

strip_headers 参数默认为 T。您可能还想删除插图:

library(data.table)
t <- as.data.table(t)  # I hate tibbles -- datatables are easier to work with
head(t)  # get the column names

# filter out lines that are illustrations and joins all lines with a space
# the \\[ searches for the [ character, the \\ are used to 'escape' the special [ character
# the !like() means find rows where the text column is not like the search string
no_il <- t[!like(text, '\\[Illustration'), 'text']
# collapse the text into a single character string
t_cln <- do.call(paste, c(no_il, collapse = ' '))
于 2017-11-01T18:48:08.057 回答