Lightweight NLP library in pure Python - currently implements a text classifier
Go to file
2022-05-21 14:02:20 -07:00
gptc New CLI tool 2022-05-21 14:02:20 -07:00
models Recompile example model 2021-11-07 11:54:24 -08:00
utils Make pack a function 2022-05-21 13:09:53 -07:00
.gitignore Use good gitignore 2020-08-14 16:21:22 -07:00
GPL-3.0 Switch to LGPL v3 or later 2022-03-05 10:17:17 -08:00
LGPL-3.0 Switch to LGPL v3 or later 2022-03-05 10:17:17 -08:00
LICENSE Switch to LGPL v3 or later 2022-03-05 10:17:17 -08:00
README.md New CLI tool 2022-05-21 14:02:20 -07:00
setup.py Add setup.py 2021-12-06 10:04:09 -08:00

GPTC

General-purpose text classifier in Python

GPTC provides both a CLI tool and a Python library.

CLI Tool

Classifying text

python -m gptc classify <compiled model file>

This will prompt for a string and classify it, then print (in JSON) a dict of the format {category: probability, category:probability, ...} to stdout.

Alternatively, if you only need the most likely category, you can use this:

python -m gptc classify [-c|--category] <compiled model file>

This will prompt for a string and classify it, outputting the category on stdout (or "None" if it cannot determine anything).

Compiling models

python -m gptc compile <raw model file>

This will print the compiled model in JSON to stdout.

Library

gptc.Classifier(model)

Create a Classifier object using the given compiled model (as a dict, not JSON).

Classifier.confidence(text)

Classify text. Returns a dict of the format {category: probability, category:probability, ...}

Classifier.classify(text)

Classify text. Returns the category into which the text is placed (as a string), or None when it cannot classify the text.

gptc.compile(raw_model)

Compile a raw model (as a list, not JSON) and return the compiled model (as a dict).

Model format

This section explains the raw model format, which is how you should create and edit models.

Raw models are formatted as a list of dicts. See below for the format:

[
    {
        "text": "<text in the category>",
        "category": "<the category>"
    }
]

GPTC handles models as Python lists of dicts of strs (for raw models) or dicts of strs and floats (for compiled models), and they can be stored in any way these Python objects can be. However, it is recommended to store them in JSON format for compatibility with the command-line tool.

Example model

An example model, which is designed to distinguish between texts written by Mark Twain and those written by William Shakespeare, is available in models. The raw model is in models/raw.json; the compiled model is in models/compiled.json.