| 1 | //! CJK-aware tokenization for relevance scoring and near-duplicate detection. |
| 2 | //! |
| 3 | //! Mirrors `skills/last30days/scripts/lib/cjk.py` (dictionary-free bigram path). |
| 4 | |
| 5 | /// High-frequency Chinese function words that dilute overlap signal. |
| 6 | pub const CHINESE_STOPWORDS: &[&str] = &[ |
| 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 | /// Returns `true` if `ch` is a CJK ideograph, Japanese kana, or Korean hangul. |
| 83 | #[inline] |
| 84 | pub fn is_cjk_char(ch: char) -> bool { |
| 85 | matches!( |
| 86 | ch, |
| 87 | '\u{3400}'..='\u{4DBF}' |
| 88 | | '\u{4E00}'..='\u{9FFF}' |
| 89 | | '\u{F900}'..='\u{FAFF}' |
| 90 | | '\u{3040}'..='\u{30FF}' |
| 91 | | '\u{AC00}'..='\u{D7AF}' |
| 92 | ) |
| 93 | } |
| 94 | |
| 95 | /// Returns `true` if `text` contains any CJK, kana, or hangul character. |
| 96 | #[inline] |
| 97 | pub fn has_cjk(text: &str) -> bool { |
| 98 | !text.is_empty() && text.chars().any(is_cjk_char) |
| 99 | } |
| 100 | |
| 101 | /// Returns `true` if `ch` matches Python's Unicode `\w` (alphanumeric or underscore). |
| 102 | #[inline] |
| 103 | pub(crate) fn is_word_char(ch: char) -> bool { |
| 104 | ch.is_alphanumeric() || ch == '_' |
| 105 | } |
| 106 | |
| 107 | /// Returns `true` if `token` is in [`CHINESE_STOPWORDS`]. |
| 108 | #[inline] |
| 109 | pub fn is_chinese_stopword(token: &str) -> bool { |
| 110 | CHINESE_STOPWORDS.contains(&token) |
| 111 | } |
| 112 | |
| 113 | /// Pushes maximal `\w+` runs from `slice` into `out`. |
| 114 | pub(crate) fn push_word_runs(slice: &str, out: &mut Vec<String>) { |
| 115 | let mut start: Option<usize> = None; |
| 116 | for (idx, ch) in slice.char_indices() { |
| 117 | if is_word_char(ch) { |
| 118 | if start.is_none() { |
| 119 | start = Some(idx); |
| 120 | } |
| 121 | } else if let Some(s) = start.take() { |
| 122 | out.push(slice[s..idx].to_owned()); |
| 123 | } |
| 124 | } |
| 125 | if let Some(s) = start { |
| 126 | out.push(slice[s..].to_owned()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Pushes character bigrams (or the single character for a 1-char run) for a CJK run. |
| 131 | fn push_cjk_bigrams(run: &str, out: &mut Vec<String>) { |
| 132 | let mut iter = run.char_indices(); |
| 133 | let Some((first_idx, _)) = iter.next() else { |
| 134 | return; |
| 135 | }; |
| 136 | let Some((second_idx, _)) = iter.next() else { |
| 137 | out.push(run[first_idx..].to_owned()); |
| 138 | return; |
| 139 | }; |
| 140 | let mut prev_start = first_idx; |
| 141 | let mut cur_start = second_idx; |
| 142 | for (next_idx, _) in iter { |
| 143 | out.push(run[prev_start..next_idx].to_owned()); |
| 144 | prev_start = cur_start; |
| 145 | cur_start = next_idx; |
| 146 | } |
| 147 | out.push(run[prev_start..].to_owned()); |
| 148 | } |
| 149 | |
| 150 | /// Tokenizes mixed CJK and Latin text into lowercased tokens in order. |
| 151 | /// |
| 152 | /// Non-CJK runs use Unicode `\w+` words; CJK runs use dictionary-free character |
| 153 | /// bigrams (single character for a length-1 run). |
| 154 | pub fn segment(text: &str) -> Vec<String> { |
| 155 | if text.is_empty() { |
| 156 | return Vec::new(); |
| 157 | } |
| 158 | let lowered = text.to_lowercase(); |
| 159 | let mut out = Vec::new(); |
| 160 | if !has_cjk(&lowered) { |
| 161 | push_word_runs(&lowered, &mut out); |
| 162 | return out; |
| 163 | } |
| 164 | |
| 165 | let mut pos = 0; |
| 166 | let mut cjk_start: Option<usize> = None; |
| 167 | for (idx, ch) in lowered.char_indices() { |
| 168 | if is_cjk_char(ch) { |
| 169 | if cjk_start.is_none() { |
| 170 | if idx > pos { |
| 171 | push_word_runs(&lowered[pos..idx], &mut out); |
| 172 | } |
| 173 | cjk_start = Some(idx); |
| 174 | } |
| 175 | } else if let Some(start) = cjk_start.take() { |
| 176 | push_cjk_bigrams(&lowered[start..idx], &mut out); |
| 177 | pos = idx; |
| 178 | } |
| 179 | } |
| 180 | if let Some(start) = cjk_start { |
| 181 | push_cjk_bigrams(&lowered[start..], &mut out); |
| 182 | } else if pos < lowered.len() { |
| 183 | push_word_runs(&lowered[pos..], &mut out); |
| 184 | } |
| 185 | out |
| 186 | } |
| 187 | |
| 188 | /// Visits every segmented token of an already-lowercased string without allocating |
| 189 | /// an intermediate `Vec<String>`. |
| 190 | pub(crate) fn for_each_segmented_lowered<F>(lowered: &str, mut visit: F) |
| 191 | where |
| 192 | F: FnMut(&str), |
| 193 | { |
| 194 | if lowered.is_empty() { |
| 195 | return; |
| 196 | } |
| 197 | let emit_words = |slice: &str, visit: &mut F| { |
| 198 | let mut start: Option<usize> = None; |
| 199 | for (idx, ch) in slice.char_indices() { |
| 200 | if is_word_char(ch) { |
| 201 | if start.is_none() { |
| 202 | start = Some(idx); |
| 203 | } |
| 204 | } else if let Some(s) = start.take() { |
| 205 | visit(&slice[s..idx]); |
| 206 | } |
| 207 | } |
| 208 | if let Some(s) = start { |
| 209 | visit(&slice[s..]); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | if !has_cjk(lowered) { |
| 214 | emit_words(lowered, &mut visit); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | let emit_cjk = |run: &str, visit: &mut F| { |
| 219 | let mut iter = run.char_indices(); |
| 220 | let Some((first_idx, _)) = iter.next() else { |
| 221 | return; |
| 222 | }; |
| 223 | let Some((second_idx, _)) = iter.next() else { |
| 224 | visit(&run[first_idx..]); |
| 225 | return; |
| 226 | }; |
| 227 | let mut prev_start = first_idx; |
| 228 | let mut cur_start = second_idx; |
| 229 | for (next_idx, _) in iter { |
| 230 | visit(&run[prev_start..next_idx]); |
| 231 | prev_start = cur_start; |
| 232 | cur_start = next_idx; |
| 233 | } |
| 234 | visit(&run[prev_start..]); |
| 235 | }; |
| 236 | |
| 237 | let mut pos = 0; |
| 238 | let mut cjk_start: Option<usize> = None; |
| 239 | for (idx, ch) in lowered.char_indices() { |
| 240 | if is_cjk_char(ch) { |
| 241 | if cjk_start.is_none() { |
| 242 | if idx > pos { |
| 243 | emit_words(&lowered[pos..idx], &mut visit); |
| 244 | } |
| 245 | cjk_start = Some(idx); |
| 246 | } |
| 247 | } else if let Some(start) = cjk_start.take() { |
| 248 | emit_cjk(&lowered[start..idx], &mut visit); |
| 249 | pos = idx; |
| 250 | } |
| 251 | } |
| 252 | if let Some(start) = cjk_start { |
| 253 | emit_cjk(&lowered[start..], &mut visit); |
| 254 | } else if pos < lowered.len() { |
| 255 | emit_words(&lowered[pos..], &mut visit); |
| 256 | } |
| 257 | } |
| 258 |