我尝试了各种方法从project Gutenberg文本中剥离许可证,以用作语言学习项目的语料库,但我似乎想不出一个无监督的、可靠的方法。到目前为止,我想出的最好的启发式方法是剥离前28行和最后398行,这适用于大量文本。任何关于如何自动剥离文本的建议(这对于许多文本非常相似,但在每种情况下都略有不同,以及一些不同的模板),以及如何验证文本是否已被准确剥离的建议,都将非常有用。
发布于 2009-08-13 17:50:35
你不是在开玩笑。这几乎就像他们试图让这项工作完成人工智能一样。我只能想到两种方法,它们都不是完美的。
1)在Perl中设置一个脚本来处理最常见的模式(例如,查找短语"produced by",一直到下一个空行,然后在那里剪切),但是输入大量关于预期内容的断言(例如,下一个文本应该是标题或作者)。这样,当模式失败时,您就会知道。当一个模式第一次失败时,请手动完成。第二次,修改脚本。
发布于 2010-12-03 12:06:46
多年来,我一直想要一个工具来剥离Project Gutenberg的页眉和页脚,用于处理自然语言处理,而不会污染与etxt混合在一起的样板文件的分析。读完这个问题后,我终于拔出手指,编写了一个Perl过滤器,您可以通过管道将其插入到任何其他工具中。
它是一个使用每行正则表达式的状态机。它的编写是为了易于理解,因为速度不是etexts的典型大小的问题。到目前为止,它可以在我这里的几十个etexts上工作,但在野外,肯定会有更多的变体需要添加。希望代码足够清晰,任何人都可以添加:
#!/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 "## $_";
}
}发布于 2017-11-02 02:48:08
哇,这个问题太老了。尽管如此,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 = ' '))https://stackoverflow.com/questions/1269146
复制相似问题