pub struct FullTokenizer { /* private fields */ }
Expand description

A FullTokenizer that runs basic tokenization and WordPiece tokenization.

Example

A full tokenizer can be built from a vocabulary as HashMap.

use bert_tokenizer::{FullTokenizer, Tokenizer, Vocab};

let mut vocab = Vocab::new();
vocab.insert("hello".to_string(), 0);
vocab.insert("world".to_string(), 1);
vocab.insert("!".to_string(), 2);
vocab.insert(",".to_string(), 3);
vocab.insert("##,".to_string(), 4);
vocab.insert("##!".to_string(), 5);
vocab.insert("##world".to_string(), 6);
vocab.insert("##hello".to_string(), 7);

let tokenizer = FullTokenizer::new().vocab(vocab).do_lower_case(true).build();
let tokens = tokenizer.tokenize("Hello, World!");
assert_eq!(tokens, vec!["hello", ",", "world", "!"]);

Or from a vocabulary file.

use bert_tokenizer::{FullTokenizer, Tokenizer};
let tokenizer = FullTokenizer::new().vocab_from_file("tests/cased_L-12_H-768_A-12/vocab.txt").build();
let tokens = tokenizer.tokenize("Hello, World!");
assert_eq!(tokens, vec!["Hello", ",", "World", "!"]);

You can also specify whether to do lower case.

use bert_tokenizer::{FullTokenizer, Tokenizer};
let tokenizer = FullTokenizer::new().vocab_from_file("tests/uncased_L-12_H-768_A-12/vocab.txt").do_lower_case(true).build();
let tokens = tokenizer.tokenize("Hello, World!");
assert_eq!(tokens, vec!["hello", ",", "world", "!"]);

Implementations§

source§

impl FullTokenizer

source

pub fn new() -> FullTokenizerBuilder

source

pub fn convert_tokens_to_ids(&self, tokens: &Vec<String>) -> Vec<u32>

Converts a sequence of tokens to a sequence of ids.

Example
use bert_tokenizer::{FullTokenizer, Tokenizer, Vocab};

let mut vocab = Vocab::new();
vocab.insert("hello".to_string(), 0);
vocab.insert("world".to_string(), 1);
vocab.insert("!".to_string(), 2);
vocab.insert("##!".to_string(), 3);
vocab.insert("##world".to_string(), 4);
vocab.insert("##hello".to_string(), 5);

let tokenizer = FullTokenizer::new().vocab(vocab).build();
let ids = tokenizer.convert_tokens_to_ids(&vec!["hello".to_string(), "!".to_string(), "world".to_string(), "##!".to_string()]);
assert_eq!(ids, vec![0, 2, 1, 3]);
Arguments
  • tokens - A sequence of tokens.
Returns

A sequence of ids.

source

pub fn convert_ids_to_tokens(&self, ids: &Ids) -> Vec<String>

Converts a sequence of ids to a sequence of tokens.

Example
use bert_tokenizer::{FullTokenizer, Tokenizer, Vocab};

let mut vocab = Vocab::new();
vocab.insert("hello".to_string(), 0);
vocab.insert("world".to_string(), 1);
vocab.insert("!".to_string(), 2);
vocab.insert("##!".to_string(), 3);
vocab.insert("##world".to_string(), 4);
vocab.insert("##hello".to_string(), 5);

let tokenizer = FullTokenizer::new().vocab(vocab).build();
let tokens = tokenizer.convert_ids_to_tokens(&vec![0, 2, 4, 3]);
assert_eq!(tokens, vec!["hello", "!", "##world", "##!"]);
Arguments
  • ids - A sequence of ids.
Returns

A sequence of tokens.

source

pub fn convert_tokens_to_string(&self, tokens: &[String]) -> String

Converts a sequence of subword tokens to a single text string.

Example
use bert_tokenizer::{FullTokenizer, Tokenizer, Vocab};

let mut vocab = Vocab::new();
vocab.insert("hello".to_string(), 0);
vocab.insert("world".to_string(), 1);
vocab.insert("!".to_string(), 2);
vocab.insert("##!".to_string(), 3);
vocab.insert("##world".to_string(), 4);
vocab.insert("##hello".to_string(), 5);

let tokenizer = FullTokenizer::new().vocab(vocab).build();
let text = "hello, world!";
let tokens = tokenizer.tokenize(text);
let text2 = tokenizer.convert_tokens_to_string(&tokens);
println!("Before: {} -> Tokens: {:?} -> After: {}", text, tokens, text2);
Arguments
  • tokens - A sequence of tokens.
Returns

A single text string.

source

pub fn get_vocab_words(&self) -> Vec<String>

Get subword tokens from the vocabulary.

Example
use bert_tokenizer::{FullTokenizer, Tokenizer, Vocab};

let mut vocab = Vocab::new();
vocab.insert("hello".to_string(), 0);
vocab.insert("world".to_string(), 1);
vocab.insert("!".to_string(), 2);
vocab.insert("##!".to_string(), 3);
vocab.insert("##world".to_string(), 4);
vocab.insert("##hello".to_string(), 5);

let tokenizer = FullTokenizer::new().vocab(vocab).build();
let tokens = tokenizer.get_vocab_words();
assert_eq!(tokens, vec!["hello", "world", "!", "##!", "##world", "##hello"]);
Returns

A sequence of subword tokens.

Trait Implementations§

source§

impl Tokenizer for FullTokenizer

source§

fn tokenize(&self, text: &str) -> Vec<String>

Tokenize by applying basic and wordpiece tokenization.

Example
use bert_tokenizer::{FullTokenizer, Tokenizer, Vocab};

let mut vocab = Vocab::new();
vocab.insert("hello".to_string(), 0);
vocab.insert("world".to_string(), 1);
vocab.insert("!".to_string(), 2);
vocab.insert("##!".to_string(), 3);
vocab.insert("##world".to_string(), 4);
vocab.insert("##hello".to_string(), 5);

let tokenizer = FullTokenizer::new().vocab(vocab).build();
let text = "hello, world!";
let tokens = tokenizer.tokenize(text);
println!("Text: {} -> Tokens: {:?}", text, tokens);

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.