Skip to main content

vectorize some text

Project description

vecxx

Python Build Status Node.js Build Status

C++ implementations of vectorizers to convert strings to integers. There are bindings available in Python and JS/TS.

This includes a straighforward C++ implementation of common approaches to vectorization to integers, required for most types of DNNs to convert sentences into tensors. It also supports native subword BPE based on fastBPE with additional support for preprocessing transforms of strings during decode, either as native functors or from the bound languages. It also supports extra (special) tokens that can be passed through.

Python bindings

The Python bindings are written with pybind11.

Using BPE vectorizer from Python

Converting sentences to lower-case subword BPE tokens as integers from the vocabulary. Note that a python native string transform can be used to transform each token prior to subword tokenization. Tokens from either the BPE vocab or special tokens (like <GO> and <EOS>) can be applied to the beginning and end of the sequence. If a second argument is provided to convert_to_ids this will indicate a padded length required for the tensor

    from vecxx import *
    bpe = BPEVocab(
        vocab_file=os.path.join(TEST_DATA, "vocab.30k"),
        codes_file=os.path.join(TEST_DATA, "codes.30k")
    )
    vec = VocabVectorizer(bpe, transform=str.lower, emit_begin_tok=["<GO>"], emit_end_tok=["<EOS>"])
    padd_vec, unpadded_len = vec.convert_to_ids("My name is Dan . I am from Ann Arbor , Michigan , in Washtenaw County".split(), 256)

The result of this will be:

[1, 30, 265, 14, 2566, 5, 8, 158, 63, 10940, 525, 18637, 7, 3685, 7, 18, 14242, 1685, 2997, 4719, 2, 0, ..., 0]

JS/TS bindings

The Javascript bindings are provided by using the Node-API API.

A thin TypeScript wrapper provides a typed API that closely matches the underlying (and Python) APIs.

Using BPE vectorizer from TypeScript

import { BPEVocab, VocabVectorizer } from 'vecxx';
import { join } from 'path';

const testDir = join(__dirname, 'test_data');
const bpe = new BPEVocab(join(testDir, 'vocab.30k'), join(testDir, 'codes.30k'));
const vectorizer = new VocabVectorizer(bpe, {
    transform: (s: string) => s.toLowerCase(),
    emitBeginToken: ['<GO>'],
    emitEndToken: ['<EOS>']
});
const sentence = `My name is Dan . I am from Ann Arbor , Michigan , in Washtenaw County`;
const { ids, size } = vectorizer.convertToIds(sentence.split(/\s+/), 256);

Docker

Sample Dockerfiles are provided that can be used for sandbox development/testing.

docker build -t vecxx-python -f py.Dockerfile .
docker run -it vecxx-python
docker build -t vecxx-node -f node.Dockerfile .
docker run -it vecxx-node
# ...
var vecxx = require('dist/index.js')

Project details


Download files

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

Files for vecxx, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size vecxx-0.0.1.tar.gz (8.7 kB) File type Source Python version None Upload date Hashes View
Filename, size vecxx-0.0.1-cp39-cp39-manylinux2010_x86_64.whl (1.9 MB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size vecxx-0.0.1-cp39-cp39-manylinux2010_i686.whl (1.9 MB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size vecxx-0.0.1-cp39-cp39-macosx_10_9_x86_64.whl (166.3 kB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size vecxx-0.0.1-cp38-cp38-win_amd64.whl (121.9 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size vecxx-0.0.1-cp38-cp38-win32.whl (104.5 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size vecxx-0.0.1-cp38-cp38-manylinux2010_x86_64.whl (1.9 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size vecxx-0.0.1-cp38-cp38-manylinux2010_i686.whl (1.9 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size vecxx-0.0.1-cp38-cp38-macosx_10_9_x86_64.whl (166.1 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size vecxx-0.0.1-cp37-cp37m-win_amd64.whl (122.7 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size vecxx-0.0.1-cp37-cp37m-win32.whl (105.3 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size vecxx-0.0.1-cp37-cp37m-manylinux2010_x86_64.whl (2.0 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size vecxx-0.0.1-cp37-cp37m-manylinux2010_i686.whl (2.0 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size vecxx-0.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (164.2 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size vecxx-0.0.1-cp36-cp36m-win_amd64.whl (122.7 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size vecxx-0.0.1-cp36-cp36m-win32.whl (105.3 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size vecxx-0.0.1-cp36-cp36m-manylinux2010_x86_64.whl (2.0 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size vecxx-0.0.1-cp36-cp36m-manylinux2010_i686.whl (2.0 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size vecxx-0.0.1-cp36-cp36m-macosx_10_9_x86_64.whl (164.2 kB) File type Wheel Python version cp36 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page