Lightweight NLP library in pure Python - currently implements a text classifier
Go to file
2021-10-25 18:32:54 -07:00
gptc Remove Spacy dependency, remove unneeded files, improve tokenizer 2021-10-25 08:06:46 -07:00
models Remove Spacy dependency, remove unneeded files, improve tokenizer 2021-10-25 08:06:46 -07:00
.gitignore Use good gitignore 2020-08-14 16:21:22 -07:00
LICENSE Create LICENSE 2020-03-16 11:00:39 -07:00
README.md Remove documentation for auto-compiling 2021-10-25 18:32:54 -07:00

GPTC

General-purpose text classifier in Python

CLI Tool

If you just want to do some simple classification on the command line, use the CLI tool. To use an existing model, use gptc <modelfile>. It will prompt for a string, and classify it, outputting the category on stdout (or "unknown" if it cannot determine anything) See "Model format" for a description of the model. To compile a model, use gptc <rawmodelfile> -c|--compile <compiledmodelfile>.

Library

If you want to use GPTC programmatically, use the library.

gptc.Classifier(model)

Create a Classifier object using the given model (as a Python list/dict, not as JSON). If the model is raw (a list), it will print a big warning on stderr.

Classifier.classify(text)

Classify text with GPTC using the model used to instantiate the Classifier. Returns the category into which the text is placed (as a string), or None when it cannot classify the text.

Model format

Since you never really need to mess with compiled models, I won't discuss them. You can read the code if you really need to figure them out.

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>"
    }
]

Although GPTC handles models as Python lists (for raw models) or dicts (for compiled models), I recommend storing them in JSON format, mainly because the command-line tool uses JSON.

Example models

I provide an example model trained to distinguish between texts written by Mark Twain and those written by William Shakespeare. I chose them because their works have all gone into the public domain, and their writing style is so different that GPTC can easily tell the difference, making it a good demonstration.

The raw model is in models/raw.json; the compiled model is in models/compiled.json.