Skip to main content

rougers/
lib.rs

1//! Fast ROUGE metrics whose scores are identical to Google's
2//! [`rouge-score`](https://github.com/google-research/google-research/tree/master/rouge)
3//! Python package, including its tokenizer, NLTK's Porter stemmer and the
4//! summary-level `rougeLsum` variant.
5//!
6//! ```
7//! use rougers::{RougeScorer, RougeType};
8//!
9//! let scorer = RougeScorer::new(["rouge1", "rouge2", "rougeLsum"], true)?;
10//! let scores = scorer.score("the cat sat on the mat", "a cat was sitting on the mat");
11//! for (rouge_type, score) in scores.iter() {
12//!     println!("{rouge_type}: {:.4}", score.fmeasure);
13//! }
14//! # Ok::<(), rougers::Error>(())
15//! ```
16
17mod metrics;
18mod porter;
19mod rouge_type;
20mod score;
21mod scorer;
22mod tokenizer;
23
24use std::fmt;
25
26pub use rouge_type::RougeType;
27pub use score::{fmeasure, Score};
28pub use scorer::{Document, RougeScorer, Scores};
29pub use tokenizer::Tokenizer;
30
31#[derive(Clone, Debug, PartialEq, Eq)]
32pub enum Error {
33    /// The name is not `rouge1`..`rouge9`, `rougeL` or `rougeLsum`.
34    InvalidRougeType(String),
35    /// `rouge0` was requested.
36    NonPositiveN(String),
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Error::InvalidRougeType(name) => write!(f, "Invalid rouge type: {name}"),
43            Error::NonPositiveN(name) => write!(f, "rougen requires positive n: {name}"),
44        }
45    }
46}
47
48impl std::error::Error for Error {}