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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
//! Rust port of [pySBD] v3.1.0 and Ruby [pragmatic_segmenter]. **[Documentations]**
//!
//! rust-pragmatic-segmenter is rule-based SBD. It uses a lot of regular expressions to separate
//! sentences.
//!
//! ```rust
//! use pragmatic_segmenter::Segmenter;
//!
//! let segmenter = Segmenter::new()?;
//! let result: Vec<_> = segmenter.segment("Hi Mr. Kim. Let's meet at 3 P.M.").collect();
//! println!("{:?}", result); // ["Hi Mr. Kim. ", "Let\'s meet at 3 P.M."]
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! [pySBD]: https://github.com/nipunsadvilkar/pySBD
//! [pragmatic_segmenter]: https://github.com/diasks2/pragmatic_segmenter
//! [Documentations]: https://docs.rs/pragmatic-segmenter
mod abbreviation_replacer;
mod list_item_replacer;
mod rule;
mod util;
use std::borrow::Cow;
use std::error::Error;
use std::iter::Iterator;
use onig::{Captures, Regex};
use abbreviation_replacer::AbbreviationReplacer;
use list_item_replacer::ListItemReplacer;
use rule::Rule;
use util::re;
const PUNCTUATIONS: [char; 7] = ['。', '.', '.', '!', '!', '?', '?'];
/// Segmenter type. It stores the compilation results of regular expressions used internally by
/// pragmatic-segmenter in memory.
///
/// ```rust
/// use pragmatic_segmenter::Segmenter;
///
/// let segmenter = Segmenter::new()?;
/// let result: Vec<_> = segmenter.segment("Hi Mr. Kim. Let's meet at 3 P.M.").collect();
/// assert_eq!(result, vec!["Hi Mr. Kim. ", "Let's meet at 3 P.M."]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Segmenter {
list_item_replacer: ListItemReplacer,
abbreviation_replacer: AbbreviationReplacer,
number_rules: [Rule; 5],
continuous_punctuation_regex: Regex,
numbered_reference: Rule,
abbreviation_with_multiple_periods_and_email_regex: regex::Regex,
misc_rules: [Rule; 2],
parens_between_double_quotes_regex: Regex,
parens_between_double_quotes_0: Rule,
parens_between_double_quotes_1: Rule,
ellipsis_rules: [Rule; 5],
exclamation_regex: Regex,
sub_escaped_regex_reserved_characters: [Rule; 5],
word_with_leading_apostrophe: Regex,
trailing_apostrophe: Regex,
between_single_quotes_regex: Regex,
between_single_quote_slanted_regex: Regex,
between_double_quotes_regex_2: Regex,
between_square_brackets_regex_2: Regex,
between_parens_regex_2: Regex,
between_quote_arrow_regex_2: Regex,
between_em_dashes_regex_2: Regex,
between_quote_slanted_regex_2: Regex,
double_punctuation: Regex,
question_mark_in_quotation_and_exclamation_point_rules: [Rule; 4],
replace_parens: Rule,
sentence_boundary_regex: Regex,
post_process_regex: Regex,
quotation_at_end_of_sentence_regex: Regex,
split_space_quotation_at_end_of_sentence_regex: Regex,
}
impl Segmenter {
/// Create a new Segmenter instance. The regular expressions used internally by
/// pragmatic-segmenter are compiled here.
///
/// ```rust
/// use pragmatic_segmenter::Segmenter;
///
/// let segmenter = Segmenter::new()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn new() -> Result<Self, Box<dyn Error>> {
Ok(Segmenter {
list_item_replacer: ListItemReplacer::new()?,
abbreviation_replacer: AbbreviationReplacer::new()?,
number_rules: [
// PeriodBeforeNumberRule
// Example: https://rubular.com/r/oNyxBOqbyy
Rule::new(r"\.(?=\d)", "∯")?,
// NumberAfterPeriodBeforeLetterRule
// Example: https://rubular.com/r/EMk5MpiUzt
Rule::new(r"(?<=\d)\.(?=\S)", "∯")?,
// NewLineNumberPeriodSpaceLetterRule
// Example: https://rubular.com/r/rf4l1HjtjG
Rule::new(r"(?<=\r\d)\.(?=(\s\S)|\))", "∯")?,
// StartLineNumberPeriodRule
// Example: https://rubular.com/r/HPa4sdc6b9
Rule::new(r"(?<=^\d)\.(?=(\s\S)|\))", "∯")?,
// StartLineTwoDigitNumberPeriodRule
// Example: https://rubular.com/r/NuvWnKleFl
Rule::new(r"(?<=^\d\d)\.(?=(\s\S)|\))", "∯")?,
],
// Example: https://rubular.com/r/mQ8Es9bxtk
continuous_punctuation_regex: re(r"(?<=\S)(!|\?){3,}(?=(\s|\Z|$))")?,
// Example: https://rubular.com/r/UkumQaILKbkeyc
numbered_reference: Rule::new(
r"(?<=[^\d\s])(\.|∯)((\[(\d{1,3},?\s?-?\s?)*\b\d{1,3}\])+|((\d{1,3}\s?)?\d{1,3}))(\s)(?=[A-Z])",
r"∯\2\r\7",
)?,
// English.Abbreviation.WithMultiplePeriodsAndEmailRule,
//
// NOTE: pySBD와 루비 구현체가 다른 정규표현식을 쓴다. pySBD의 동작을 따라간다.
//
// Example: https://rubular.com/r/EUbZCNfgei
abbreviation_with_multiple_periods_and_email_regex: regex::Regex::new(
r"([a-zA-Z0-9_])(?:\.)([a-zA-Z0-9_])",
)?,
misc_rules: [
// English.GeoLocationRule,
Rule::new(r"(?<=[a-zA-z]°)\.(?=\s*\d+)", "∯")?,
// English.FileFormatRule,
Rule::new(
r"(?<=\s)\.(?=(jpe?g|png|gif|tiff?|pdf|ps|docx?|xlsx?|svg|bmp|tga|exif|odt|html?|txt|rtf|bat|sxw|xml|zip|exe|msi|blend|wmv|mp[34]|pptx?|flac|rb|cpp|cs|js)\s)",
"∯",
)?,
],
// Example: https://rubular.com/r/6flGnUMEVl
parens_between_double_quotes_regex: re(r#"["\”]\s\(.*\)\s["\“]"#)?,
parens_between_double_quotes_0: Rule::new(r"\s(?=\()", "\r")?,
parens_between_double_quotes_1: Rule::new(r"(?<=\))\s", "\r")?,
// NOTE: 이부분은 pySBD 구현과 루비 구현이 동작이 다르다. pySBD의 동작을 따른다.
// 이 부분을 고치게 되면 ReinsertEllipsisRules도 함께 고쳐야한다.
ellipsis_rules: [
// ThreeSpaceRule
// Example: https://rubular.com/r/YBG1dIHTRu
Rule::new(r"(\s\.){3}\s", "♟♟♟♟♟♟♟")?,
// FourSpaceRule
// Example: https://rubular.com/r/2VvZ8wRbd8
Rule::new(r"(?<=[a-z])(\.\s){3}\.($|\\n)", "♝♝♝♝♝♝♝")?,
// FourConsecutiveRule
// Example: https://rubular.com/r/Hdqpd90owl
Rule::new(r"(?<=\S)\.{3}(?=\.\s[A-Z])", "ƪƪƪ")?,
// ThreeConsecutiveRule
// Example: https://rubular.com/r/i60hCK81fz
Rule::new(r"\.\.\.(?=\s+[A-Z])", "☏☏.")?,
// OtherThreePeriodRule
Rule::new(r"\.\.\.", "ƪƪƪ")?,
],
exclamation_regex: re(
r"!Xũ|!Kung|ǃʼOǃKung|!Xuun|!Kung\-Ekoka|ǃHu|ǃKhung|ǃKu|ǃung|ǃXo|ǃXû|ǃXung|ǃXũ|!Xun|Yahoo!|Y!J|Yum!",
)?,
// NOTE: pySBD에 구현 실수가 있어 루비 구현체와 동작이 전혀 다르지만, pySBD의 동작을
// 따르기 위해 버그를 유지하겠다.
sub_escaped_regex_reserved_characters: [
// SubLeftParen
Rule::new(r"\\\(", "(")?,
// SubRightParen
Rule::new(r"\\\)", ")")?,
// SubLeftBracket
Rule::new(r"\\\[", "[")?,
// SubRightBracket
Rule::new(r"\\\]", "]")?,
// SubDash
Rule::new(r"\\\-", "-")?,
],
// Example: https://rubular.com/r/mXf8cW025o
word_with_leading_apostrophe: re(r"(?<=\s)'(?:[^']|'[a-zA-Z])*'\S")?,
trailing_apostrophe: re(r"'\s")?,
// Example: https://rubular.com/r/2YFrKWQUYi
between_single_quotes_regex: re(r"(?<=\s)'(?:[^']|'[a-zA-Z])*'")?,
between_single_quote_slanted_regex: re(r"(?<=\s)‘(?:[^’]|’[a-zA-Z])*’")?,
// Example: https://regex101.com/r/r6I1bW/1
//
// NOTE: pySBD에선 파이썬 regex의 기능 한계로 인해 원본인 루비 pragmatic_segmenter와
// 동작이 다른데, 우리는 Oniguruma regex engine을 쓰고있으므로 루비 구현을 재현할 수
// 있다. 그러나 pySBD와 동작을 맞추기 위해 의도적으로 pySBD 정규표현식을 사용한다.
//
// NOTE: Python regex와 Oniguruma regex는 named capture group과 backreference 문법이
// 다르다. 주의
//
// Reference: https://stackoverflow.com/a/13577411/13977061
between_double_quotes_regex_2: re(r#""(?=(?<tmp>[^\"\\]+|\\{2}|\\.)*)\k<tmp>""#)?,
between_square_brackets_regex_2: re(r#"\[(?=(?<tmp>[^\]\\]+|\\{2}|\\.)*)\k<tmp>\]"#)?,
between_parens_regex_2: re(r"\((?=(?<tmp>[^\(\)\\]+|\\{2}|\\.)*)\k<tmp>\)")?,
between_quote_arrow_regex_2: re(r"\«(?=(?<tmp>[^»\\]+|\\{2}|\\.)*)\k<tmp>\»")?,
between_em_dashes_regex_2: re(r"--(?=(?<tmp>[^--]*))\k<tmp>--")?,
between_quote_slanted_regex_2: re(r"\“(?=(?<tmp>[^”\\]+|\\{2}|\\.)*)\k<tmp>\”")?,
double_punctuation: re(r"^(?:\?!|!\?|\?\?|!!)")?,
question_mark_in_quotation_and_exclamation_point_rules: [
// QuestionMarkInQuotationRule
// Example: https://rubular.com/r/aXPUGm6fQh
Rule::new(r#"\?(?=(\'|\"))"#, "&ᓷ&")?,
// InQuotationRule
// Example: https://rubular.com/r/XS1XXFRfM2
Rule::new(r#"\!(?=(\'|\"))"#, "&ᓴ&")?,
// BeforeCommaMidSentenceRule
// Example: https://rubular.com/r/sl57YI8LkA
Rule::new(r"\!(?=\,\s[a-z])", "&ᓴ&")?,
// MidSentenceRule
// Example: https://rubular.com/r/f9zTjmkIPb
Rule::new(r"\!(?=\s[a-z])", "&ᓴ&")?,
],
// Example: https://rubular.com/r/GcnmQt4a3I
replace_parens: Rule::new(
// ROMAN_NUMERALS_IN_PARENTHESES
r"\(((?=[mdclxvi])m*(c[md]|d?c*)(x[cl]|l?x*)(i[xv]|v?i*))\)(?=\s[A-Z])",
r"&✂&\1&⌬&",
)?,
// added special case: r"[。..!!?].*" to handle intermittent dots, exclamation, etc.
sentence_boundary_regex: re(
r#"((?:[^)])*)(?=\s?[A-Z])|「(?:[^」])*」(?=\s[A-Z])|\((?:[^\)]){2,}\)(?=\s[A-Z])|\'(?:[^\'])*[^,]\'(?=\s[A-Z])|\"(?:[^\"])*[^,]\"(?=\s[A-Z])|\“(?:[^\”])*[^,]\”(?=\s[A-Z])|[。..!!??].*|\S.*?[。..!!??ȸȹ☉☈☇☄]"#,
)?,
post_process_regex: re(r"\A[a-zA-Z]*\Z")?,
// Example: https://rubular.com/r/NqCqv372Ix
quotation_at_end_of_sentence_regex: re(r#"[!?\.-][\"\'“”]\s{1}[A-Z]"#)?,
// Example: https://rubular.com/r/JMjlZHAT4g
split_space_quotation_at_end_of_sentence_regex: re(
r#"(?<=[!?\.-][\"\'“”])\s{1}(?=[A-Z])"#,
)?,
})
}
/// Separate sentences from given input. Although it is a function that returns an Iterator,
/// not all processing is done by streaming. After pre-processing the entire input once,
/// processing is performed for each sentence by streaming.
///
/// ```rust
/// use pragmatic_segmenter::Segmenter;
///
/// let segmenter = Segmenter::new()?;
/// let mut iter = segmenter.segment("Hi Mr. Kim. Let's meet at 3 P.M.");
///
/// assert_eq!(iter.next(), Some("Hi Mr. Kim. "));
/// assert_eq!(iter.next(), Some("Let's meet at 3 P.M."));
/// assert_eq!(iter.next(), None);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn segment<'a>(&'a self, original_input: &'a str) -> impl Iterator<Item = &'a str> {
// NOTE: 루비 버전에는 이런 처리가 없으나, pySBD 3.1.0에 이 처리가 들어갔다. pySBD와 동작을
// 맞추기위해 동일하게 처리해준다.
let text = original_input.replace('\n', "\r");
let text = self.list_item_replacer.add_line_break(&text);
// replace_abbreviations()
let mut text = self.abbreviation_replacer.replace(&text);
// replace_numbers()
for rule in &self.number_rules {
text = rule.replace_all(&text);
}
// replace_continuous_punctuation()
let text = self
.continuous_punctuation_regex
.replace_all(&text, |c: &Captures| {
let mat = c.at(0).unwrap(); // Must exists
mat.replace('!', "&ᓴ&").replace('?', "&ᓷ&")
});
// replace_periods_before_numeric_references()
//
// Reference:
// https://github.com/diasks2/pragmatic_segmenter/commit/d9ec1a35
let text = self.numbered_reference.replace_all(&text);
let mut text = self
.abbreviation_with_multiple_periods_and_email_regex
.replace_all(&text, "$1∮$2");
for rule in &self.misc_rules {
text = Cow::Owned(rule.replace_all(&text));
}
//
// split_into_segments()
//
// check_for_parens_between_quotes()
let text = self
.parens_between_double_quotes_regex
.replace_all(&text, |c: &Captures| {
let mat = c.at(0).unwrap(); // Must exists
let mat = self.parens_between_double_quotes_0.replace_all(mat);
self.parens_between_double_quotes_1.replace_all(&mat)
});
let mut prior_start_char_idx = 0;
// TODO: flat_map() 에서 임시 Vec, String 할당 줄이기
text.split('\r')
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.collect::<Vec<_>>() // String을 own하는 버전의 새 split 함수를 만들면 이부분을 제거할 수 있음
.into_iter()
.flat_map(move |sent| {
// English.SingleNewLineRule
let mut sent = sent.replace(r"\n", "ȹ");
// English.EllipsisRules.All
for rule in &self.ellipsis_rules {
sent = rule.replace_all(&sent);
}
// check_for_punctuation()
if PUNCTUATIONS.iter().any(|&p| sent.contains(p)) {
// process_text()
if !sent.ends_with(&PUNCTUATIONS[..]) {
sent += "ȸ";
}
// ExclamationWords.apply_rules()
sent = self
.exclamation_regex
.replace_all(&sent, self.replace_punctuation(false));
// between_punctuation()
if self.word_with_leading_apostrophe.find(&sent).is_none()
|| self.trailing_apostrophe.find(&sent).is_some()
{
sent = self
.between_single_quotes_regex
.replace_all(&sent, self.replace_punctuation(true));
}
sent = self
.between_single_quote_slanted_regex
.replace_all(&sent, self.replace_punctuation(false));
sent = self
.between_double_quotes_regex_2
.replace_all(&sent, self.replace_punctuation(false));
sent = self
.between_square_brackets_regex_2
.replace_all(&sent, self.replace_punctuation(false));
sent = self
.between_parens_regex_2
.replace_all(&sent, self.replace_punctuation(false));
sent = self
.between_quote_arrow_regex_2
.replace_all(&sent, self.replace_punctuation(false));
sent = self
.between_em_dashes_regex_2
.replace_all(&sent, self.replace_punctuation(false));
sent = self
.between_quote_slanted_regex_2
.replace_all(&sent, self.replace_punctuation(false));
// handle text having only doublepunctuations
if self.double_punctuation.find(&sent).is_none() {
sent = sent
.replace(r"?!", "☉")
.replace(r"!?", "☈")
.replace(r"??", "☇")
.replace(r"!!", "☄");
}
for rule in &self.question_mark_in_quotation_and_exclamation_point_rules {
sent = rule.replace_all(&sent);
}
// ListItemReplacer(sent).replace_parens()
sent = self.replace_parens.replace_all(&sent);
// sentence_boundary_punctuation()
// retain exclamation mark if it is an ending character of a given text
sent = sent.replace(r"&ᓴ&$", "!");
self.sentence_boundary_regex
.find_iter(&sent)
.map(|r| sent[r.0..r.1].to_string())
.collect::<Vec<_>>()
} else {
vec![sent]
}
})
.flat_map(move |mut sent| {
// SubSymbolsRules
sent = sent
.replace('∯', ".")
.replace('♬', "،")
.replace('♭', ":")
.replace(r"&ᓰ&", "。")
.replace(r"&ᓱ&", ".")
.replace(r"&ᓳ&", "!")
.replace(r"&ᓴ&", "!")
.replace(r"&ᓷ&", "?")
.replace(r"&ᓸ&", "?")
.replace('☉', "?!")
.replace('☇', "??")
.replace('☈', "!?")
.replace('☄', "!!")
.replace(r"&✂&", "(")
.replace(r"&⌬&", ")")
.replace('ȸ', "")
.replace('ȹ', "\n");
// post_process_segments()
//
// NOTE: post_process_segments 함수는 pySBD와 루비 pragmatic_segmenter의 동작이 전혀
// 다르다. pySBD를 따라간다.
if sent.len() > 2 && self.post_process_regex.find(&sent).is_some() {
return vec![sent];
}
// ReinsertEllipsisRules
// NOTE: 이부분은 pySBD 구현과 루비 구현이 동작이 다르다. pySBD의 동작을 따른다.
sent = sent
.replace(r"ƪƪƪ", "...")
.replace(r"♟♟♟♟♟♟♟", " . . . ")
.replace(r"♝♝♝♝♝♝♝", ". . . .")
.replace(r"☏☏", "..")
.replace('∮', ".");
if self
.quotation_at_end_of_sentence_regex
.find(&sent)
.is_some()
{
self.split_space_quotation_at_end_of_sentence_regex
.split(&sent)
.map(|s| s.to_string())
.collect()
} else {
vec![sent.replace('\n', "").trim().to_string()]
}
})
.map(|sent| sent.replace(r"&⎋&", "'"))
// NOTE: pySBD에만 이하의 처리가 존재하고, 원본 루비코드에는 이런 동작이 없다. 일단
// 동작을 맞추기 위해 동일한 처리를 해주지만, 아래 코드때문에 성능손실이 크다.
.flat_map(move |sent| -> Vec<_> {
// since SENTENCE_BOUNDARY_REGEX doesnt account
// for trailing whitespaces \s* & is used as suffix
// to keep non-destructive text after segments joins
// NOTE: escape 한 뒤 compile했기 때문에, 실패의 여지가 없다.
let re = regex::Regex::new(&format!(r"{}\s*", regex::escape(&sent))).unwrap();
re.find_iter(original_input)
.filter_map(|mat| {
let match_str = mat.as_str();
let match_start_idx = mat.start();
if match_start_idx >= prior_start_char_idx {
prior_start_char_idx = match_start_idx;
Some(match_str)
// making sure if curren sentence and its span
// is either first sentence along with its char spans
// or current sent spans adjacent to prior sentence spans
} else {
None
}
})
.collect()
})
}
fn replace_punctuation(&self, is_match_type_single: bool) -> impl Fn(&Captures) -> String + '_ {
move |c: &Captures| {
let mat = c.at(0).unwrap(); // Must exists
// NOTE: 원래 이 자리에서 EscapeRegexReservedCharacters.All 규칙이 적용되어야
// 하나, pySBD의 구현 버그로 인해 EscapeRegexReservedCharacters.All가 아무일도
// 하지 않는다. 버그이지만, pySBD의 동작을 따라가기위해 버그를 유지하겠다.
let mut mat = mat.replace('.', "∯");
mat = mat.replace('。', "&ᓰ&");
mat = mat.replace('.', "&ᓱ&");
mat = mat.replace('!', "&ᓳ&");
mat = mat.replace('!', "&ᓴ&");
mat = mat.replace('?', "&ᓷ&");
mat = mat.replace('?', "&ᓸ&");
if !is_match_type_single {
mat = mat.replace('\'', "&⎋&");
}
for rule in &self.sub_escaped_regex_reserved_characters {
mat = rule.replace_all(&mat);
}
mat
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
type TestResult = Result<(), Box<dyn Error>>;
#[test]
fn regex_should_be_compiled() -> TestResult {
let _seg = Segmenter::new()?;
Ok(())
}
#[test]
fn empty_string() -> TestResult {
let seg = Segmenter::new()?;
let expected: [String; 0] = [];
let actual: Vec<_> = seg.segment("").collect();
assert_eq!(actual, expected);
Ok(())
}
}