Skip to main content

Chinese Text Project API wrapper

Project description

ctext is a simple Python wrapper and set of helper functions for the CTP API, which fetches data from the Chinese Text Project database, a digital library of pre-modern Chinese literature. Developed for Python 3.

Development status

This software is currently experimental. See http://ctext.org/tools/api for API details.

Installation

pip install ctext

Usage

Textual items are identified by CTP URNs. Each URN identifies a text or part of a text. You can get these manually by visiting the http://ctext.org website (bottom-right of each page), or programmatically using the searchtexts() function. To use this library, first:

from ctext import *

Some API functions (like getting the full structure of a text, or downloading a lot of data) may require an API key. If you have one, before calling any other functions, do this:

setapikey("your-api-key-goes-here")

You can also set the interface language (“en” for English, “zh” for Chinese):

setlanguage("zh")

Similarly, automatic remapping to simplified Chinese can be done with:

setremap("gb")

getstats

stats = getstats()

Simple wrapper around the getstats API call.

getstatus

status = getstatus()

Simple wrapper around the getstatus API call.

gettexttitles

titles = gettexttitles()

Simple wrapper around the gettexttitles API call.

getcapabilities

capabilities = getcapabilities()

Simple wrapper around the getcapabilities API call.

gettext

passages = gettext("ctp:analects/xue-er")

Simple wrapper around the gettext API call. Note that the API gettext function needs to be called recursively to get the full text of an entire book; the Python helper functions gettextasparagrapharray, gettextasstring, and gettextasobject call gettext repeatedly to extract all corresponding textual data.

gettextasobject

data = gettextasobject("ctp:analects/xue-er")

Returns the full text of the requested URN as an object with a nested structure representing what each gettext API call returns.

gettextasparagrapharray

passages = gettextasparagrapharray("ctp:analects/xue-er")

Returns the full text of the requested URN as a simple array of strings, each corresponding to one passage of text. Titles are omitted.

gettextasstring

string = gettextasstring("ctp:analects/xue-er")

Returns the full text of the requested URN as a single string. Each paragraph is separated with “\n\n”.

gettextinfo

data = gettextinfo("ctp:analects")

Simple wrapper around the gettextinfo API call.

searchtexts

data = searchtexts("論語")

Simple wrapper around the searchtexts API call.

setapikey

setapikey("your-api-key-goes-here")

This sets an API key which is then supplied to the CTP API with all subsequent API requests.

setlanguage

setlanguage("zh")

This sets the “if” (interface language) parameter, which is then supplied to the CTP API with all subsequent API requests.

setremap

setlanguage("gb")

This sets the “remap” (character remapping) parameter, which is then supplied to the CTP API with all subsequent API requests.

License

Copyright 2016 Donald Sturgeon. This code is licensed under the MIT License: http://opensource.org/licenses/mit-license.html

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

ctext-0.21.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file ctext-0.21.tar.gz.

File metadata

  • Download URL: ctext-0.21.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ctext-0.21.tar.gz
Algorithm Hash digest
SHA256 b215a9cb0c062c0f6ec0247743b7cd16c9ede28cab2c524fcce5ea24ed13124c
MD5 4e1c21c22e9e92837571f5dd0d4ea924
BLAKE2b-256 619acc4b8960da3fe79c06e15f9bbeec79d5256eff7c86831e55b86a8bf15eb9

See more details on using hashes here.

Supported by

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