Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement max character check #398

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/tokenizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ class WordPieceTokenizer extends TokenizerModel {
* @param {Object} config.vocab A mapping of tokens to ids.
* @param {string} config.unk_token The unknown token string.
* @param {string} config.continuing_subword_prefix The prefix to use for continuing subwords.
* @param {number} [config.max_input_chars_per_word=100] The maximum number of characters per word.
*/
constructor(config) {
super(config);
Expand All @@ -291,6 +292,12 @@ class WordPieceTokenizer extends TokenizerModel {
*/
this.unk_token = config.unk_token;

/**
* The maximum number of characters allowed per word.
* @type {number}
*/
this.max_input_chars_per_word = config.max_input_chars_per_word ?? 100;

/**
* An array of tokens.
* @type {string[]}
Expand All @@ -310,10 +317,10 @@ class WordPieceTokenizer extends TokenizerModel {
let outputTokens = [];
for (let token of tokens) {
let chars = [...token];
// TODO add
// if len(chars) > self.max_input_chars_per_word:
// output_tokens.append(self.unk_token)
// continue
if (chars.length > this.max_input_chars_per_word) {
outputTokens.push(this.unk_token);
continue;
}

let isUnknown = false;
let start = 0;
Expand Down