Skip to main content

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; Python 2 is supported since version 0.263.

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 gettextasparagraphlist, gettextasstring, and gettextasobject call gettext repeatedly to extract all corresponding textual data.

gettextaschapterlist

chapters = gettextaschapterlist("ctp:analects")

Returns the full text of the requested URN as an object simple list of strings, each corresponding to one chapter of text. Titles are omitted.

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.

gettextasparagraphlist

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

Returns the full text of the requested URN as a simple list 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

setremap("gb")

This sets the “remap” (character remapping) parameter, which is then supplied to the CTP API with all subsequent API requests. Currently the only valid value is “gb”, which returns data in simplified Chinese.

License

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

Metadata

Release files for ctext 0.265

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ctext 0.265
File Size Uploaded
ctext-0.265.tar.gz 4.0 kB Details

Release files / ctext-0.265.tar.gz

Download URL ctext-0.265.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
600304c744f4b41f58a08d68989efb7abb855c1504bb46e3797fb16e79b5586d
BLAKE2b-256 checksum
How to use checksums
614c837ff5fdc8736cd7d3e407088cc39a5d8d2d0ade30885343b03100b708cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.265 This release

1 release file

0.264

1 release file

0.263

1 release file

0.262

1 release file

0.261

1 release file

0.26

1 release file

0.25

1 release file

0.24

1 release file

0.23

1 release file

0.22

1 release file

0.21

1 release file

0.20

1 release file

0.19

1 release file

0.18

1 release file

0.17

1 release file

0.16

1 release file

0.15

1 release file

0.14

1 release file

0.13

1 release file

0.12

0.11

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page