Skip to main content

Library to interface with Project Gutenberg

Project description

https://github.com/c-w/gutenberg/workflows/CI/badge.svg https://github.com/c-w/gutenberg/workflows/Daily/badge.svg https://codecov.io/gh/c-w/gutenberg/branch/master/graph/badge.svg https://img.shields.io/pypi/v/gutenberg.svg https://img.shields.io/pypi/pyversions/gutenberg.svg

Overview

This package contains a variety of scripts to make working with the Project Gutenberg body of public domain texts easier.

The functionality provided by this package includes:

  • Downloading texts from Project Gutenberg.

  • Cleaning the texts: removing all the crud, leaving just the text behind.

  • Making meta-data about the texts easily accessible.

The package has been tested with Python 2.7 and 3.5+.

An HTTP interface to this package exists too. Try it out!

Installation

This project is on PyPI, so I’d recommend that you just install everything from there using your favourite Python package manager.

pip install gutenberg

If you want to install from source or modify the package, you’ll need to clone this repository:

git clone https://github.com/c-w/Gutenberg.git

Now, you should probably install the dependencies for the package and verify your checkout by running the tests.

cd Gutenberg

virtualenv --no-site-packages virtualenv
source virtualenv/bin/activate
pip install -r requirements-dev.pip
pip install .

nose2

Alternatively, you can also run the project via Docker:

docker build -t gutenberg -f Dockerfile-py3 .

docker run -it -v /some/mount/path:/data gutenberg python

Python 3

This package depends on BSD-DB. The bsddb module was removed from the Python standard library since version 2.7. This means that if you wish to use gutenberg on Python 3, you will need to manually install BSD-DB.

If getting BSD-DB to run on your platform is difficult, take a look at gutenbergpy which only depends on SQLite or MongoDB.

Linux

On Linux, you can usually install BSD-DB using your distribution’s package manager. For example, on Ubuntu, you can use apt-get:

sudo apt-get install libdb++-dev
export BERKELEYDB_DIR=/usr
pip install .

MacOS

On Mac, you can install BSD-DB using homebrew:

brew install berkeley-db4
pip install .

Windows

On Windows, it’s easiest to download a pre-compiled version of BSD-DB from pythonlibs which works great for Python 3.5+.

For example, if you have Python 3.5 on a 64-bit version of Windows, you should download bsddb3‑6.2.1‑cp35‑cp35m‑win_amd64.whl.

After you download the wheel, install it and you’re good to go:

pip install bsddb3‑6.2.1‑cp35‑cp35m‑win_amd64.whl
pip install .

License conflicts

Since its v6.x releases, BSD-DB switched to the AGPL3 license which is stricter than this project’s Apache v2 license. This means that unless you’re happy to comply to the terms of the AGPL3 license, you’ll have to install an ealier version of BSD-DB (anything between 4.8.30 and 5.x should be fine). If you are happy to use this project under AGPL3 (or if you have a commercial license for BSD-DB), set the following environment variable before attempting to install BSD-DB:

YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=1

Apache Jena Fuseki

As an alternative to the BSD-DB backend, this package can also use Apache Jena Fuseki for the metadata store. The Apache Jena Fuseki backend is activated by setting the GUTENBERG_FUSEKI_URL environment variable to the HTTP endpoint at which Fuseki is listening. If the Fuseki server has HTTP basic authentication enabled, the username and password can be provided via the GUTENBERG_FUSEKI_USER and GUTENBERG_FUSEKI_PASSWORD environment variables.

For local development, the Fuseki server can be run via Docker:

docker run \
    --detach \
    --publish 3030:3030 \
    --env ADMIN_PASSWORD=some-password \
    --volume /some/mount/location:/fuseki \
    stain/jena-fuseki:3.6.0 \
    /jena-fuseki/fuseki-server --loc=/fuseki --update /ds

export GUTENBERG_FUSEKI_URL=http://localhost:3030/ds
export GUTENBERG_FUSEKI_USER=admin
export GUTENBERG_FUSEKI_PASSWORD=some-password

Usage

Downloading a text

from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers

text = strip_headers(load_etext(2701)).strip()
print(text)  # prints 'MOBY DICK; OR THE WHALE\n\nBy Herman Melville ...'
python -m gutenberg.acquire.text 2701 moby-raw.txt
python -m gutenberg.cleanup.strip_headers moby-raw.txt moby-clean.txt

Looking up meta-data

A bunch of meta-data about ebooks can be queried:

from gutenberg.query import get_etexts
from gutenberg.query import get_metadata

print(get_metadata('title', 2701))  # prints frozenset([u'Moby Dick; Or, The Whale'])
print(get_metadata('author', 2701)) # prints frozenset([u'Melville, Hermann'])

print(get_etexts('title', 'Moby Dick; Or, The Whale'))  # prints frozenset([2701, ...])
print(get_etexts('author', 'Melville, Hermann'))        # prints frozenset([2701, ...])

You can get a full list of the meta-data that can be queried by calling:

from gutenberg.query import list_supported_metadatas

print(list_supported_metadatas()) # prints (u'author', u'formaturi', u'language', ...)

Before you use one of the gutenberg.query functions you must populate the local metadata cache. This one-off process will take quite a while to complete (18 hours on my machine) but once it is done, any subsequent calls to get_etexts or get_metadata will be very fast. If you fail to populate the cache, the calls will raise an exception.

To populate the cache:

from gutenberg.acquire import get_metadata_cache
cache = get_metadata_cache()
cache.populate()

If you need more fine-grained control over the cache (e.g. where it’s stored or which backend is used), you can use the set_metadata_cache function to switch out the backend of the cache before you populate it. For example, to use the Sqlite cache backend instead of the default Sleepycat backend and store the cache at a custom location, you’d do the following:

from gutenberg.acquire import set_metadata_cache
from gutenberg.acquire.metadata import SqliteMetadataCache

cache = SqliteMetadataCache('/my/custom/location/cache.sqlite')
cache.populate()
set_metadata_cache(cache)

Limitations

This project deliberately does not include any natural language processing functionality. Consuming and processing the text is the responsibility of the client; this library merely focuses on offering a simple and easy to use interface to the works in the Project Gutenberg corpus. Any linguistic processing can easily be done client-side e.g. using the TextBlob library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Gutenberg-0.8.2.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

Gutenberg-0.8.2-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file Gutenberg-0.8.2.tar.gz.

File metadata

  • Download URL: Gutenberg-0.8.2.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for Gutenberg-0.8.2.tar.gz
Algorithm Hash digest
SHA256 b5703b6348bce8b63eb044044ea0877da79915e8c263c2758a51d6595a6189fc
MD5 23c371156dee994840afcd0a29c0600c
BLAKE2b-256 da732d2e1b34d50b9d6023d7ee8b643ed325e0e447d5f07460c93ff370643d41

See more details on using hashes here.

File details

Details for the file Gutenberg-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: Gutenberg-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for Gutenberg-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6b4833c372013ba2600906ccc0c5623d23df0ffc5fdea6dfdb28948272be032d
MD5 1c29b8ce0b50fcd7a5e5010e5fd9ae56
BLAKE2b-256 59632a763e65eeff6ced347a19fb7511e38b569fb5de1fff644ffc206bc34064

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page