pub mod coreference;
pub mod entity_extraction;
pub mod intent_recognition;
pub mod sentiment_analysis;
pub use coreference::{CoreferenceChain, CoreferenceConfig, CoreferenceResolver, Mention};
pub use entity_extraction::{EntityExtractionConfig, EntityExtractor, EntityType, ExtractedEntity};
pub use intent_recognition::{IntentRecognitionConfig, IntentRecognizer, IntentResult, IntentType};
pub use sentiment_analysis::{
Emotion, SentimentAnalyzer, SentimentConfig, SentimentPolarity, SentimentResult,
};
pub struct NLPPipeline {
intent_recognizer: IntentRecognizer,
entity_extractor: EntityExtractor,
coreference_resolver: CoreferenceResolver,
sentiment_analyzer: SentimentAnalyzer,
}
impl NLPPipeline {
pub fn new() -> anyhow::Result<Self> {
Ok(Self {
intent_recognizer: IntentRecognizer::new(IntentRecognitionConfig::default())?,
entity_extractor: EntityExtractor::new(EntityExtractionConfig::default())?,
coreference_resolver: CoreferenceResolver::new(CoreferenceConfig::default())?,
sentiment_analyzer: SentimentAnalyzer::new(SentimentConfig::default())?,
})
}
pub fn with_config(
intent_config: IntentRecognitionConfig,
entity_config: EntityExtractionConfig,
coref_config: CoreferenceConfig,
sentiment_config: SentimentConfig,
) -> anyhow::Result<Self> {
Ok(Self {
intent_recognizer: IntentRecognizer::new(intent_config)?,
entity_extractor: EntityExtractor::new(entity_config)?,
coreference_resolver: CoreferenceResolver::new(coref_config)?,
sentiment_analyzer: SentimentAnalyzer::new(sentiment_config)?,
})
}
pub fn process(&mut self, message_id: String, text: &str) -> anyhow::Result<NLPResult> {
self.coreference_resolver
.add_message(message_id.clone(), text.to_string());
let intent = self.intent_recognizer.recognize(text)?;
let entities = self.entity_extractor.extract(text)?;
let coreferences = self.coreference_resolver.resolve(&message_id)?;
let sentiment = self.sentiment_analyzer.analyze(text)?;
self.intent_recognizer
.update_history(text.to_string(), intent.primary_intent);
Ok(NLPResult {
intent,
entities,
coreferences,
sentiment,
})
}
pub fn intent_recognizer_mut(&mut self) -> &mut IntentRecognizer {
&mut self.intent_recognizer
}
pub fn coreference_resolver_mut(&mut self) -> &mut CoreferenceResolver {
&mut self.coreference_resolver
}
}
#[derive(Debug, Clone)]
pub struct NLPResult {
pub intent: IntentResult,
pub entities: Vec<ExtractedEntity>,
pub coreferences: Vec<CoreferenceChain>,
pub sentiment: SentimentResult,
}
impl Default for NLPPipeline {
fn default() -> Self {
Self::new().expect("Failed to create default NLP pipeline")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nlp_pipeline_creation() {
let pipeline = NLPPipeline::new();
assert!(pipeline.is_ok());
}
#[test]
fn test_nlp_pipeline_processing() {
let mut pipeline = NLPPipeline::new().expect("should succeed");
let result = pipeline.process("msg1".to_string(), "Show me all movies from 2023");
assert!(result.is_ok());
let nlp_result = result.expect("should succeed");
assert_eq!(nlp_result.intent.primary_intent, IntentType::Exploration);
}
}