text_tokenizer/
tokens.rs

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use unicode_properties::{
    GeneralCategoryGroup,
    UnicodeGeneralCategory,
    GeneralCategory,
};
use std::str::FromStr;
use std::collections::{VecDeque,BTreeSet};

use text_parsing::Local;

use crate::{
    EMOJIMAP,
    Token, Numerical, Number, Formatter, Separator, Struct, Word, Special, Unicode,
    TokenizerOptions, TokenizerParams, IntoTokenizer, SentenceBreaker,
    wordbreaker::{
        BasicToken,
        WordBreaker,
        one_char_word,
    },
};


impl<'t> IntoTokenizer for &'t str {
    type IntoTokens = Tokens<'t>;
    
    fn into_tokenizer<S: SentenceBreaker>(self, params: TokenizerParams<S>) -> Self::IntoTokens {
        Tokens::new(self, &params.options)
    }
}

impl<'t> Iterator for Tokens<'t> {
    type Item = Local<Token>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.buffer.len()>0 {                
                return self.next_from_buffer();
            } else {
                loop {
                    match self.bounds.next() {
                        Some(local_bt) => {
                            let sep = if let BasicToken::Separator(_) = local_bt.data() { true } else { false };
                            self.buffer.push_back(local_bt);
                            if sep {
                                return self.next();
                            }
                        },
                        None if self.buffer.len()>0 => return self.next(),
                        None => return None,
                    }
                }
            }
        }
    }
}

//#[derive(Debug)]
pub struct Tokens<'t> {
    bounds: WordBreaker<'t>,
    buffer: VecDeque<Local<BasicToken<'t>>>,
    allow_structs: bool,
}
impl<'t> Tokens<'t> {
    pub(crate) fn new<'a>(s: &'a str, options: &BTreeSet<TokenizerOptions>) -> Tokens<'a> {
        Tokens {
            bounds: WordBreaker::new(s, &options),
            buffer: VecDeque::new(),
            allow_structs: if options.contains(&TokenizerOptions::StructTokens) { true } else { false },
        }
    }
    fn basic_separator_to_pt(&mut self, c: char) -> Token {
        Token::Special(Special::Separator(match c {
            ' ' => Separator::Space,
            '\n' => Separator::Newline,
            '\t' => Separator::Tab,
            _ => Separator::Char(c),
        }))
    }
    fn basic_formater_to_pt(&mut self, c: char) -> Token {        
        Token::Unicode(Unicode::Formatter(match c {
            '\u{200d}' => Formatter::Joiner,
            _ => Formatter::Char(c),
        }))
    }   
    fn basic_number_to_pt(&mut self, s: &str) -> Token {
        Token::Word(match i64::from_str(s) {
            Ok(n) => Word::Number(Number::Integer(n)),
            Err(_) => {
                match f64::from_str(s) {
                    Ok(n) => Word::Number(Number::Float(n)),
                    Err(..) => {
                        #[cfg(feature = "strings")]
                        { Word::Word(s.to_string()) }
                        #[cfg(not(feature = "strings"))]
                        { Word::Word }  
                    },
                }
            }
        })
    }
    fn basic_mixed_to_pt(&mut self, s: &str) -> Token {
        let mut word = true;
        let mut has_word_parts = false;
        let mut first = true;
        let mut same = false;
        let mut one_c = ' ';
        for c in s.chars() {
            match c.is_alphanumeric() || c.is_digit(10) || (c.general_category_group() == GeneralCategoryGroup::Punctuation) || (c == '\u{0060}') {
                true => { has_word_parts = true; },
                false => { word = false; },
            }
            match first {
                true => { one_c = c; first = false; same = true; },
                false => if one_c != c { same = false; }
            }
        }
        if !first && same && (one_c.is_whitespace() || (one_c.general_category() == GeneralCategory::Format)) {
            if one_c.is_whitespace() {
                return self.basic_separator_to_pt(one_c);                       
            } else {
                return self.basic_formater_to_pt(one_c)
            }
        }
        if word {
            #[cfg(feature = "strings")]
            { Token::Word(Word::StrangeWord(s.to_string())) }
            #[cfg(not(feature = "strings"))]
            { Token::Word(Word::StrangeWord) }  
        } else {
            let rs = s.replace("\u{fe0f}","");
            match EMOJIMAP.get(&rs as &str) {
                Some(em) => Token::Word(Word::Emoji(em)),
                None => match one_char_word(&rs) {
                    //Some(c) if c.general_category() == GeneralCategory::ModifierSymbol => Token::UnicodeModifier(c),
                    Some(c) if c.general_category_group() == GeneralCategoryGroup::Symbol => Token::Special(Special::Symbol(c)),
                    Some(_) | None => match has_word_parts {
                        true => {
                            #[cfg(feature = "strings")]
                            { Token::Word(Word::StrangeWord(s.to_string())) }
                            #[cfg(not(feature = "strings"))]
                            { Token::Word(Word::StrangeWord) }  
                        },
                        false => {
                            #[cfg(feature = "strings")]
                            {
                                Token::Unicode(Unicode::String({
                                    let mut us = "".to_string();
                                    for c in rs.chars() {
                                        if us!="" { us += "_"; }
                                        us += "u";
                                        let ns = format!("{}",c.escape_unicode());
                                        us += &ns[3 .. ns.len()-1];
                                    }
                                    us
                                }))
                            }
                            #[cfg(not(feature = "strings"))]
                            { Token::Unicode(Unicode::String) }
                        },
                    }
                },
            }
        }        
    }
    fn basic_alphanumeric_to_pt(&mut self, s: &str) -> Token {
        /*
        Word
        StrangeWord
        pub enum Numerical {
            Date(String),
            Ip(String),
            DotSeparated(String),
            Countable(String),
            Measures(String),
            Alphanumeric(String),
        }*/
        //let mut wrd = true;
        let mut digits = false;
        let mut digits_begin_only = false;
        let mut dots = false;
        let mut alphas_and_apos = false;
        let mut other = false;

        let mut start_digit = true;
        for c in s.chars() {
            if start_digit && (!c.is_digit(10)) { start_digit = false; }
            match c {
                c @ _ if c.is_digit(10) => {
                    digits = true;
                    if start_digit { digits_begin_only = true; }
                    else { digits_begin_only = false; }
                },
                c @ _ if c.is_alphabetic() => { alphas_and_apos = true; },
                '\'' => { alphas_and_apos = true; },
                '.' => { dots = true; },
                _ => { other = true; },
            }
        }
        Token::Word(match (digits,digits_begin_only,dots,alphas_and_apos,other) {
            (true,false,true,false,false) => {
                // TODO: Date, Ip, DotSeparated
                #[cfg(feature = "strings")]
                { Word::Numerical(Numerical::DotSeparated(s.to_string())) }
                #[cfg(not(feature = "strings"))]
                { Word::Numerical(Numerical::DotSeparated) }
            },
            (true,true,_,true,false) => {
                // TODO: Countable or Measures
                #[cfg(feature = "strings")]
                { Word::Numerical(Numerical::Measures(s.to_string())) }
                #[cfg(not(feature = "strings"))]
                { Word::Numerical(Numerical::Measures) }
            },
            (true, _, _, _, _) => {
                // Numerical trash, ids, etc.
                #[cfg(feature = "strings")]
                { Word::Numerical(Numerical::Alphanumeric(s.to_string())) }
                #[cfg(not(feature = "strings"))]
                { Word::Numerical(Numerical::Alphanumeric) }
            }
            (false,false,_,true,false) => {
                // Word
                #[cfg(feature = "strings")]
                { Word::Word(s.to_string()) }
                #[cfg(not(feature = "strings"))]
                { Word::Word }  
            },
            (false,false,_,_,_) => {
                // Strange                    
                #[cfg(feature = "strings")]
                { Word::StrangeWord(s.to_string()) }
                #[cfg(not(feature = "strings"))]
                { Word::StrangeWord }   
            },
            (false,true,_,_,_) => unreachable!(),
        })
    }
    fn basic_punctuation_to_pt(&mut self, c: char) -> Token {
        Token::Special(Special::Punctuation(c))
    }
    /*fn check_url(&mut self) -> Option<PositionalToken> {
        if !self.allow_structs { return None; }
        let check = if self.buffer.len()>3 {
            match (&self.buffer[0],&self.buffer[1],&self.buffer[2]) {
                (BasicToken::Alphanumeric("http"),BasicToken::Punctuation(":"),BasicToken::Punctuation("//")) |
                (BasicToken::Alphanumeric("https"),BasicToken::Punctuation(":"),BasicToken::Punctuation("//")) => true,
                _ => false,
            }
        } else { false };
        if check {
            let mut url = "".to_string();
            let tag_bound = None;
            loop {
                if let Some(b) = tag_bound {
                    if (self.offset + url.len()) >= b { break; }
                }
                match self.buffer.pop_front() {
                    None => break,
                    Some(BasicToken::Separator(s)) => {
                        self.buffer.push_front(BasicToken::Separator(s));
                        break;
                    },
                    Some(BasicToken::Alphanumeric(s)) |
                    Some(BasicToken::Number(s)) |
                    Some(BasicToken::Punctuation(s)) |
                    Some(BasicToken::Formatter(s)) |
                    Some(BasicToken::Mixed(s)) => {
                        url += s;
                    },
                }
            }
            let len = url.len();
            let tok = PositionalToken {
                offset: self.offset,
                length: len,
                token: Token::Url(url),
            };
            self.offset += len;
            Some(tok)
        } else { None }
    }*/
    fn check_hashtag(&mut self) -> Option<Local<Token>> {
        if !self.allow_structs || (self.buffer.len() < 2) { return None; }

        let (loc1,s1) = self.buffer[0].into_inner();
        let (loc2,s2) = self.buffer[1].into_inner();
        match (s1,s2) {
            (BasicToken::Punctuation('#'),BasicToken::Alphanumeric(_s)) |
            (BasicToken::Punctuation('#'),BasicToken::Number(_s)) => match Local::from_segment(loc1,loc2) {
                Ok(local) => {
                    self.buffer.pop_front();
                    self.buffer.pop_front();
                    
                    Some(local.local(Token::Struct({
                        #[cfg(feature = "strings")]
                        { Struct::Hashtag(_s.to_string()) }
                        #[cfg(not(feature = "strings"))]
                        { Struct::Hashtag }
                    })))
                },
                Err(_) => None,                    
            },
            _ => None,
        }
    }
    fn check_mention(&mut self) -> Option<Local<Token>> {
        if !self.allow_structs || (self.buffer.len() < 2) { return None; }

        let (loc1,s1) = self.buffer[0].into_inner();
        let (loc2,s2) = self.buffer[1].into_inner();
        match (s1,s2) {
            (BasicToken::Punctuation('@'),BasicToken::Alphanumeric(_s)) |
            (BasicToken::Punctuation('@'),BasicToken::Number(_s)) => match Local::from_segment(loc1,loc2) {
                Ok(local) => {
                    self.buffer.pop_front();
                    self.buffer.pop_front();
                    
                    Some(local.local(Token::Struct({
                        #[cfg(feature = "strings")]
                        { Struct::Mention(_s.to_string()) }
                        #[cfg(not(feature = "strings"))]
                        { Struct::Mention }
                    })))
                },
                Err(_) => None,                    
            },
            _ => None,
        }
    }
    fn next_from_buffer(&mut self) -> Option<Local<Token>> {
        //if let Some(t) = self.check_url() { return Some(t); }
        if let Some(t) = self.check_hashtag() { return Some(t); }
        if let Some(t) = self.check_mention() { return Some(t); }
        match self.buffer.pop_front() {
            Some(local_tok) => {
                let (local,tok) = local_tok.into_inner();
                Some(local.local(match tok {
                    BasicToken::Alphanumeric(s) => self.basic_alphanumeric_to_pt(s),
                    BasicToken::Number(s) => self.basic_number_to_pt(s),
                    BasicToken::Punctuation(s) => self.basic_punctuation_to_pt(s),
                    BasicToken::Mixed(s) => self.basic_mixed_to_pt(s),
                    BasicToken::Separator(s) => self.basic_separator_to_pt(s),
                    BasicToken::Formatter(s) => self.basic_formater_to_pt(s),
                }))
            },
            None => None,
        }
    }
}