npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

brainnet-tokenizer

v1.0.0

Published

A simple and efficient tokenizer for natural language processing tasks.

Downloads

63

Readme

Tokenizer

A simple and efficient tokenizer for natural language processing tasks. This tokenizer supports multiple languages and handles special characters effectively.

Features

  • Tokenizes text into words and special characters.
  • Encodes text into token IDs.
  • Decodes token IDs back into text.
  • Saves and loads vocabulary from a file.
  • Supports multiple languages.

Installation

To use the Tokenizer class, you need to install the package from npm:

npm install @brainnet/tokenizer

Usage

Here is an example of how to use the Tokenizer class:

const Tokenizer = require('@brainnet/tokenizer');

// Create a Tokenizer instance
const tokenizer = new Tokenizer();

// Tokenize the text
const text = "Tokenization is a fundamental step in natural language processing.";
const tokens = tokenizer.tokenize(text);
console.log("Tokens:", tokens);

// Encode the text
const encodedResult = tokenizer.encode(text);
console.log("Encoding Result:", encodedResult);

// Decode the text
const decodedResult = tokenizer.decode(encodedResult.encodedArray);
console.log("Decoding Result:", decodedResult);

// Save the vocabulary to a file
const vocabularyFilePath = 'vocabulary.json';
tokenizer.saveVocabulary(vocabularyFilePath);
console.log("Vocabulary saved to", vocabularyFilePath);

// Create a new Tokenizer instance and load the vocabulary
const newTokenizer = new Tokenizer();
newTokenizer.loadVocabulary(vocabularyFilePath);
console.log("Vocabulary loaded from", vocabularyFilePath);

// Encode and decode the text using the loaded vocabulary
const newEncodedResult = newTokenizer.encode(text);
const newDecodedResult = newTokenizer.decode(newEncodedResult.encodedArray);
console.log("New Encoding Result:", newEncodedResult);
console.log("New Decoding Result:", newDecodedResult);

// Convert token ID to token
const tokenId = tokenizer.getTokenId("Tokenization");
const token = tokenizer.getToken(tokenId);
console.log(`Token ID ${tokenId} corresponds to token: "${token}"`);

API

Tokenizer

constructor()

Creates an instance of Tokenizer.

tokenize(text: string): string[]

Tokenizes the input text into words and special characters.

getTokenId(token: string): number

Adds a token to the vocabulary if it doesn't exist, and returns its ID.

getToken(tokenId: number): string | null

Converts a token ID back to its corresponding token.

getVocabularySize(): number

Returns the size of the vocabulary.

encode(text: string): Object

Encodes the input text into an array of token IDs.

decode(encodedArray: number[]): Object

Decodes an array of token IDs back into text.

saveVocabulary(filePath: string): void

Saves the vocabulary to a file.

loadVocabulary(filePath: string): void

Loads the vocabulary from a file.

License

This project is licensed under the Apache-2.0 License.