Store tokens instead of text in insert

This commit is contained in:
Andras Schmelczer 2024-11-28 20:51:05 +00:00
parent 3391d2c0ec
commit 44f9a44f63
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
10 changed files with 144 additions and 74 deletions

View file

@ -1,2 +1,6 @@
use token::Token;
pub mod token;
pub mod word_tokenizer;
pub type Tokenizer<T> = dyn Fn(&str) -> Vec<Token<T>>;

View file

@ -1,17 +1,24 @@
use std::hash::Hash;
#[derive(Debug, Clone)]
pub struct Token<T>
where
T: PartialEq + Hash + Clone,
T: PartialEq + Clone,
{
normalised: T,
original: String,
}
impl From<&str> for Token<String> {
fn from(s: &str) -> Self {
Token {
normalised: s.to_string(),
original: s.to_string(),
}
}
}
impl<T> Token<T>
where
T: PartialEq + Hash + Clone,
T: PartialEq + Clone,
{
pub fn new(normalised: T, original: String) -> Self {
Token {
@ -35,18 +42,9 @@ where
impl<T> PartialEq for Token<T>
where
T: PartialEq + Hash + Clone,
T: PartialEq + Clone,
{
fn eq(&self, other: &Self) -> bool {
self.normalised == other.normalised
}
}
impl<T> Hash for Token<T>
where
T: PartialEq + Hash + Clone,
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.normalised.hash(state);
}
}