use std::ops::Range;
const CONTINUATION_PREFIX: &str = "##";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Word {
pub text: String,
pub tokens: Range<usize>,
}
pub fn reconstruct(tokens: &[String]) -> Vec<Word> {
let mut words: Vec<Word> = Vec::new();
for (index, token) in tokens.iter().enumerate() {
match token.strip_prefix(CONTINUATION_PREFIX) {
Some(continuation) if !words.is_empty() => {
let word = words.last_mut().expect("just checked words is not empty");
word.text.push_str(continuation);
word.tokens.end = index + 1;
}
Some(continuation) => words.push(Word {
text: continuation.to_string(),
tokens: index..index + 1,
}),
None => words.push(Word {
text: token.clone(),
tokens: index..index + 1,
}),
}
}
words
}
#[cfg(test)]
mod tests;