プロジェクト・グーテンベルクのテキストからヘッダー/フッターを削除する方法?

StackOverflow https://stackoverflow.com/questions/1269146

質問

私は言語学習プロジェクトのためのコーパスとして使用するために、プロジェクト・グーテンベルクのテキストからライセンスを除去するためにさまざまな方法を試してみたが、私は教師なし、信頼性の高いアプローチを考え出すように見えることはできません。私がこれまでに作ってみた最高のヒューリスティックは、テキストの大多数のために働いた最初の28行と最後の398を、ストリッピングされます。ことを確認する方法については、任意の(だけでなく、それぞれの場合のわずかな違い、およびいくつかの異なるテンプレートで、テキストの多くのために非常に似ている)私は、自動的にテキストを取り除くことができる方法についての提案、などの提案テキストは非常に有用であろう、正確に取り除かれています。

役に立ちましたか?

解決

あなたは冗談ではなかったです。彼らは仕事AI完全をしようとしたかのようにそれはほとんどです。私はそれらのどちらも完璧な、たった2つのアプローチを考えることができます。

1)最も一般的なパターンに取り組むために、Perlの、たとえば、中にスクリプトを設定(例えば、次の空白行にまで続けるとそこにカット)が、たくさんに入れ、「製」フレーズを探します期待されているものに関するアサーション(たとえば、次のテキストは、タイトルや著者でなければなりません)。その方法は、パターンが失敗したとき、あなたはそれを知っていますよ。パターンが失敗した最初の時間は、手でそれを行います。二回目は、スクリプトを変更します。

2) Amazonの機械トルコ人を試してみてください。

他のヒント

私はまた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 "## $_";
    }
}

うわー、この質問は今とても古いです。それにも関わらず、Rでgutenbergrパッケージは、ヘッダの「公式」終了後にジャンク含めて、ヘッダを除去OK仕事をするようです。

まず、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 = ' '))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top