use super::types::KeywordAlgorithm;
use serde::{Deserialize, Serialize};
fn default_max_keywords() -> usize {
10
}
fn default_ngram_range() -> (usize, usize) {
(1, 3)
}
#[cfg(feature = "keywords-yake")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YakeParams {
pub window_size: usize,
}
#[cfg(feature = "keywords-yake")]
impl Default for YakeParams {
fn default() -> Self {
Self { window_size: 2 }
}
}
#[cfg(feature = "keywords-rake")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RakeParams {
pub min_word_length: usize,
pub max_words_per_phrase: usize,
}
#[cfg(feature = "keywords-rake")]
impl Default for RakeParams {
fn default() -> Self {
Self {
min_word_length: 1,
max_words_per_phrase: 3,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeywordConfig {
#[serde(default)]
pub algorithm: KeywordAlgorithm,
#[serde(default = "default_max_keywords")]
pub max_keywords: usize,
#[serde(default)]
pub min_score: f32,
#[serde(default = "default_ngram_range")]
pub ngram_range: (usize, usize),
pub language: Option<String>,
#[cfg(feature = "keywords-yake")]
#[serde(skip_serializing_if = "Option::is_none")]
pub yake_params: Option<YakeParams>,
#[cfg(feature = "keywords-rake")]
#[serde(skip_serializing_if = "Option::is_none")]
pub rake_params: Option<RakeParams>,
}
impl Default for KeywordConfig {
fn default() -> Self {
Self {
algorithm: KeywordAlgorithm::default(),
max_keywords: 10,
min_score: 0.0,
ngram_range: (1, 3),
language: Some("en".to_string()),
#[cfg(feature = "keywords-yake")]
yake_params: None,
#[cfg(feature = "keywords-rake")]
rake_params: None,
}
}
}
impl KeywordConfig {
#[cfg(feature = "keywords-yake")]
pub fn yake() -> Self {
Self {
algorithm: KeywordAlgorithm::Yake,
..Default::default()
}
}
#[cfg(feature = "keywords-rake")]
pub fn rake() -> Self {
Self {
algorithm: KeywordAlgorithm::Rake,
..Default::default()
}
}
pub fn with_max_keywords(mut self, max: usize) -> Self {
self.max_keywords = max;
self
}
pub fn with_min_score(mut self, score: f32) -> Self {
self.min_score = score;
self
}
pub fn with_ngram_range(mut self, min: usize, max: usize) -> Self {
self.ngram_range = (min, max);
self
}
pub fn with_language(mut self, lang: impl Into<String>) -> Self {
self.language = Some(lang.into());
self
}
#[cfg(feature = "keywords-yake")]
pub fn with_yake_params(mut self, params: YakeParams) -> Self {
self.yake_params = Some(params);
self
}
#[cfg(feature = "keywords-rake")]
pub fn with_rake_params(mut self, params: RakeParams) -> Self {
self.rake_params = Some(params);
self
}
}