Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Created August 25, 2023 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughdbrown/e57321964d779cc9a0f6cb9f41c1f6da to your computer and use it in GitHub Desktop.
Save hughdbrown/e57321964d779cc9a0f6cb9f41c1f6da to your computer and use it in GitHub Desktop.
Group lines of text into paragraphs
use itertools::Itertools;
pub fn split_paragraphs(lines: &Vec<&str>) -> Vec<String> {
let mut paragraphs: Vec<String> = vec![];
for (key, group) in &lines.into_iter().group_by(|x| !x.is_empty()) {
if key {
paragraphs.push(group.map(|s: &&str| *s).collect::<Vec<&str>>().join(" "));
}
}
paragraphs
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn split_paragraphs_1() {
let lines: Vec<&str> = vec![
"asdasd asd", "zxc zxc", "",
"qwe qwe", "ert ert", "ty ty", "",
"sdf sdf", "iopio",
];
let paragraphs: Vec<String> = split_paragraphs(&lines);
assert_eq!(paragraphs.len(), 3);
assert_eq!(paragraphs[0], lines[..2].to_vec().join(" "));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment