1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::borrow::Cow;
use std::cmp::{Eq, PartialEq};
use std::fmt;

/// Encapsulates the string term of a token.
///
/// The term that the token encapsulates is wrapped in a `Cow` (copy-on-write),
/// so that the string slice is only copied if it is modified.
pub struct Token<'a> {
    term: Cow<'a, str>,

    /// The index of the token within the original un-tokenized string slice.
    pub offset: Option<usize>,
}

impl<'a> Token<'a> {
    /// Create a new `Token` object.
    pub fn new(term: &'a str, offset: Option<usize>) -> Self {
        Token {
            term: Cow::Borrowed(term),
            offset,
        }
    }

    /// Initializes a `Token` from a string slice.
    pub fn from(term: &'a str) -> Self {
        Token {
            term: Cow::Borrowed(term),
            offset: None,
        }
    }

    /// Extracts the string slice contained in the `Token`.
    ///
    /// # Examples
    ///
    /// ```
    /// let s = "Hello";
    /// let token = tokenizers::Token::from(s);
    /// assert_eq!(token.as_str(), s);
    /// ```
    pub fn as_str(&self) -> &str {
        self.term.as_ref()
    }

    /// Returns a mutable reference to the contained string slice.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut token = tokenizers::Token::from("Hello");
    /// token.as_mut_str().make_ascii_lowercase();
    /// assert_eq!(token.as_str(), "hello");
    /// ```
    pub fn as_mut_str(&mut self) -> &mut str {
        self.term.to_mut()
    }

    /// Clones the contained string slice if it is not already owned (i.e. if it has not already
    /// been modified) and returns it as a `String`.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut token = tokenizers::Token::from("Hello");
    /// assert_eq!(token.into_string(), String::from("Hello"));
    /// ```
    pub fn into_string(self) -> String {
        self.term.into_owned()
    }
}

impl<'a> fmt::Display for Token<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<'a> PartialEq for Token<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl<'a> Eq for Token<'a> {}

impl<'a> PartialEq<String> for Token<'a> {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other.as_str()
    }
}

impl<'a> PartialEq<&str> for Token<'a> {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

/// A tokenizer is simply a struct implementing a `tokenize` function that takes a
/// string slice and returns an iterator of `Token`s.
pub trait Tokenizer<'a> {
    type TokenIter: Iterator<Item = Token<'a>>;

    fn tokenize(&self, input: &'a str) -> Self::TokenIter;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_token_change_case() {
        let s = "Hello";
        let mut token = Token::from(s);
        assert_eq!(token.as_str(), s);
        token.term.to_mut().make_ascii_lowercase();
        assert_ne!(token.as_str(), s);
        assert_eq!(token.as_str(), "hello");
    }

    #[test]
    fn test_token_display() {
        let s = "Hello";
        let token = Token::from(s);
        assert_eq!(format!("{}", token), "Hello");
    }

    #[test]
    fn test_token_comp_token() {
        let token1 = Token::from("foo");
        let token2 = Token::from("foo");
        let token3 = Token::from("bar");
        assert_eq!(token1 == token2, true);
        assert_eq!(token1 == token3, false);
        assert_eq!(token1 != token3, true);
    }

    #[test]
    fn test_token_comp_string() {
        let token = Token::from("foo");
        assert_eq!(token == String::from("foo"), true);
        assert_eq!(token == String::from("bar"), false);
        assert_eq!(token != String::from("bar"), true);
    }

    #[test]
    fn test_token_comp_string_slice() {
        let token = Token::from("foo");
        assert_eq!(token == "foo", true);
        assert_eq!(token == "bar", false);
        assert_eq!(token != "bar", true);
    }
}