use crate::error::TokenizerError;
use crate::vocab::base_vocab::{swap_key_values, Vocab};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct ProphetNetVocab {
pub values: HashMap<String, i64>,
pub indices: HashMap<i64, String>,
pub unknown_value: &'static str,
pub special_values: HashMap<String, i64>,
pub special_indices: HashMap<i64, String>,
}
impl ProphetNetVocab {
pub fn pad_value() -> &'static str {
"[PAD]"
}
pub fn cls_value() -> &'static str {
"[CLS]"
}
pub fn sep_value() -> &'static str {
"[SEP]"
}
pub fn x_sep_value() -> &'static str {
"[X_SEP]"
}
pub fn mask_value() -> &'static str {
"[MASK]"
}
}
impl Vocab for ProphetNetVocab {
fn unknown_value() -> &'static str {
"[UNK]"
}
fn get_unknown_value(&self) -> &'static str {
"[UNK]"
}
fn values(&self) -> &HashMap<String, i64> {
&self.values
}
fn indices(&self) -> &HashMap<i64, String> {
&self.indices
}
fn special_values(&self) -> &HashMap<String, i64> {
&self.special_values
}
fn special_indices(&self) -> &HashMap<i64, String> {
&self.special_indices
}
fn from_file(path: &str) -> Result<ProphetNetVocab, TokenizerError> {
let values = ProphetNetVocab::read_vocab_file(path)?;
let mut special_values = HashMap::new();
let unknown_value = ProphetNetVocab::unknown_value();
ProphetNetVocab::_register_as_special_value(unknown_value, &values, &mut special_values)?;
let pad_value = ProphetNetVocab::pad_value();
ProphetNetVocab::_register_as_special_value(pad_value, &values, &mut special_values)?;
let cls_value = ProphetNetVocab::cls_value();
ProphetNetVocab::_register_as_special_value(cls_value, &values, &mut special_values)?;
let sep_value = ProphetNetVocab::sep_value();
ProphetNetVocab::_register_as_special_value(sep_value, &values, &mut special_values)?;
let mask_value = ProphetNetVocab::mask_value();
ProphetNetVocab::_register_as_special_value(mask_value, &values, &mut special_values)?;
let indices = swap_key_values(&values);
let special_indices = swap_key_values(&special_values);
Ok(ProphetNetVocab {
values,
indices,
unknown_value,
special_values,
special_indices,
})
}
fn token_to_id(&self, token: &str) -> i64 {
self._token_to_id(
token,
&self.values,
&self.special_values,
self.unknown_value,
)
}
fn id_to_token(&self, id: &i64) -> String {
self._id_to_token(id, &self.indices, &self.special_indices, self.unknown_value)
}
}
#[cfg(test)]
mod tests {
use super::*;
extern crate anyhow;
use std::io::Write;
#[test]
fn test_create_object() {
let values: HashMap<String, i64> = HashMap::new();
let special_values: HashMap<String, i64> = HashMap::new();
let indices: HashMap<i64, String> = HashMap::new();
let special_indices: HashMap<i64, String> = HashMap::new();
let unknown_value = ProphetNetVocab::unknown_value();
let base_vocab = ProphetNetVocab {
values,
indices,
unknown_value,
special_values,
special_indices,
};
assert_eq!(base_vocab.unknown_value, "[UNK]");
assert_eq!(base_vocab.unknown_value, ProphetNetVocab::unknown_value());
assert_eq!(ProphetNetVocab::pad_value(), "[PAD]");
assert_eq!(ProphetNetVocab::sep_value(), "[SEP]");
assert_eq!(ProphetNetVocab::x_sep_value(), "[X_SEP]");
assert_eq!(ProphetNetVocab::mask_value(), "[MASK]");
assert_eq!(base_vocab.values, *base_vocab.values());
assert_eq!(base_vocab.special_values, *base_vocab.special_values());
}
#[test]
fn test_create_object_from_file() -> anyhow::Result<()> {
let mut vocab_file = tempfile::NamedTempFile::new()?;
write!(
vocab_file,
"hello \n world \n [UNK] \n ! \n [X_SEP] \n [SEP] \n [MASK] \n [PAD] \n [CLS]"
)?;
let path = vocab_file.into_temp_path();
let target_values: HashMap<String, i64> = [
("hello".to_owned(), 0),
("world".to_owned(), 1),
("[UNK]".to_owned(), 2),
("!".to_owned(), 3),
("[X_SEP]".to_owned(), 4),
("[SEP]".to_owned(), 5),
("[MASK]".to_owned(), 6),
("[PAD]".to_owned(), 7),
("[CLS]".to_owned(), 8),
]
.iter()
.cloned()
.collect();
let special_values: HashMap<String, i64> = [
("[UNK]".to_owned(), 2),
("[SEP]".to_owned(), 5),
("[MASK]".to_owned(), 6),
("[PAD]".to_owned(), 7),
("[CLS]".to_owned(), 8),
]
.iter()
.cloned()
.collect();
let base_vocab = ProphetNetVocab::from_file(path.to_path_buf().to_str().unwrap())?;
assert_eq!(base_vocab.unknown_value, "[UNK]");
assert_eq!(base_vocab.values, target_values);
assert_eq!(base_vocab.special_values, special_values);
drop(path);
Ok(())
}
#[test]
#[should_panic]
fn test_create_object_from_file_without_unknown_token() {
let mut vocab_file = tempfile::NamedTempFile::new().unwrap();
write!(vocab_file, "hello \n world \n [X_SEP] \n ! \n [SEP]").unwrap();
let path = vocab_file.into_temp_path();
let _base_vocab = ProphetNetVocab::from_file(path.to_path_buf().to_str().unwrap()).unwrap();
}
#[test]
fn test_encode_tokens() -> anyhow::Result<()> {
let mut vocab_file = tempfile::NamedTempFile::new()?;
write!(
vocab_file,
"hello \n world \n [UNK] \n ! \n [X_SEP] \n [SEP] \n [MASK] \n [PAD] \n [CLS]"
)?;
let path = vocab_file.into_temp_path();
let base_vocab = ProphetNetVocab::from_file(path.to_path_buf().to_str().unwrap())?;
assert_eq!(base_vocab.token_to_id("hello"), 0);
assert_eq!(base_vocab.token_to_id("world"), 1);
assert_eq!(base_vocab.token_to_id("!"), 3);
assert_eq!(base_vocab.token_to_id("[UNK]"), 2);
assert_eq!(base_vocab.token_to_id("oov_value"), 2);
assert_eq!(base_vocab.token_to_id("[PAD]"), 7);
assert_eq!(base_vocab.token_to_id("[MASK]"), 6);
assert_eq!(base_vocab.token_to_id("[X_SEP]"), 4);
assert_eq!(base_vocab.token_to_id("[SEP]"), 5);
assert_eq!(base_vocab.token_to_id("[CLS]"), 8);
drop(path);
Ok(())
}
#[test]
fn test_decode_tokens() -> anyhow::Result<()> {
let mut vocab_file = tempfile::NamedTempFile::new()?;
write!(
vocab_file,
"hello \n world \n [UNK] \n ! \n [X_SEP] \n [SEP] \n [MASK] \n [PAD] \n [CLS]"
)?;
let path = vocab_file.into_temp_path();
let bert_vocab = ProphetNetVocab::from_file(path.to_path_buf().to_str().unwrap())?;
assert_eq!(bert_vocab.id_to_token(&(0_i64)), "hello");
assert_eq!(bert_vocab.id_to_token(&(1_i64)), "world");
assert_eq!(bert_vocab.id_to_token(&(3_i64)), "!");
assert_eq!(bert_vocab.id_to_token(&(2_i64)), "[UNK]");
assert_eq!(bert_vocab.id_to_token(&(7_i64)), "[PAD]");
assert_eq!(bert_vocab.id_to_token(&(6_i64)), "[MASK]");
assert_eq!(bert_vocab.id_to_token(&(4_i64)), "[X_SEP]");
assert_eq!(bert_vocab.id_to_token(&(5_i64)), "[SEP]");
assert_eq!(bert_vocab.id_to_token(&(8_i64)), "[CLS]");
drop(path);
Ok(())
}
}