╔══════════════════════════════════════════════════════════════════════════════╗
║ INTEGRATED COGNITIVE SYSTEM - ARCHITECTURE ║
╚══════════════════════════════════════════════════════════════════════════════╝
┌──────────────────────────────────────────────────────────────────────────┐
│ INPUT: NATURAL LANGUAGE │
│ │
│ "Pedro is a student who lives in Madrid. Bob is his father and │
│ works at Microsoft. Pedro feels happy about his achievements." │
└────────────────────────────────┬─────────────────────────────────────────┘
│
▼
╔════════════════════════════════════════════════════╗
║ STAGE 1: NLP TO INFERENCE CONVERTER ║
║ (nlp_to_inference.py) ║
╚════════════════════════════════════════════════════╝
│
│ Uses: Gemma LLM via Ollama
│ Process: Few-shot learning prompt
│
▼
┌─────────────────────────────────────────┐
│ STRUCTURED FACTS (.inf format) │
│ │
│ (Pedro)IsA(student) │
│ (Pedro)LivesIn(Madrid) │
│ (Bob)FatherOf(Pedro) │
│ (Bob)WorksAt(Microsoft) │
│ (Pedro)FeelsHappy(achievements) │
└─────────────────┬───────────────────────┘
│
▼
╔════════════════════════════════════════════════════╗
║ STAGE 2: INFERENCE ENGINE ║
║ (engine.py) ║
╚════════════════════════════════════════════════════╝
│
│ Process:
│ - Parse infix notation
│ - Group by subject
│ - Convert to natural language
│ - Concatenate with "and"
│
▼
┌─────────────────────────────────────────┐
│ NATURAL LANGUAGE INFERENCES │
│ │
│ Pedro is a student and lives in │
│ Madrid and feels happy achievements. │
│ Bob father of Pedro and works at │
│ Microsoft. │
└─────────────────┬───────────────────────┘
│
▼
╔════════════════════════════════════════════════════╗
║ STAGE 3: EMOTIONAL ANALYZER ║
║ (emotional_analyzer.py) ║
╚════════════════════════════════════════════════════╝
│
│ Uses: Gemma LLM via Ollama
│ Process: Emotion & sentiment classification
│
▼
┌─────────────────────────────────────────┐
│ EMOTIONALLY ENRICHED KNOWLEDGE │
│ │
│ Pedro is a student and lives in │
│ Madrid and feels happy achievements. │
│ → Emotion: Joy, Sentiment: Positive │
│ │
│ Bob father of Pedro and works at │
│ Microsoft. │
│ → Emotion: Neutral, Sentiment: Neutral│
└─────────────────┬───────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ FINAL OUTPUT + SUMMARY │
│ │
│ • Complete analysis report │
│ • Emotional distribution statistics │
│ • Sentiment analysis summary │
└─────────────────────────────────────────┘
Class: NLPToInferenceConverter
Responsibility: Convert free-form text to structured inference format
Key Methods:
split_into_sentences(text)→ Breaks text into individual sentencesconvert_sentence(sentence)→ Converts one sentence using Gemmaconvert_text(text)→ Processes entire textconvert_and_save(text, file)→ Saves to .inf file
External Dependencies:
- Ollama API (http://localhost:11434)
- Gemma LLM model
Input Example:
"Pedro is a student who lives in Madrid."
Output Example:
(Pedro)IsA(student)
(Pedro)LivesIn(Madrid)
Class: InferenceEngine
Responsibility: Process structured facts and generate natural language
Key Methods:
parse_line(line)→ Parses infix notation_parse_infix(line)→ Handles (Subject)Relation(Object) formatformat_relation(text)→ Converts CamelCase/snake_case to natural languageto_natural_language()→ Generates sentences with concatenationload_file(path)→ Loads and processes .inf file
External Dependencies:
- None (pure Python)
Input Example:
(Pedro)IsA(student)
(Pedro)LivesIn(Madrid)
Output Example:
Pedro is a student and lives in Madrid.
Class: EmotionalAnalyzer
Responsibility: Analyze emotional content and sentiment
Key Methods:
analyze_sentence(sentence)→ Classifies one sentence using Gemmaanalyze_file(file)→ Analyzes all sentences in a fileget_emotional_summary(results)→ Generates statisticsanalyze_and_save(input, output)→ Saves analysis results
External Dependencies:
- Ollama API (http://localhost:11434)
- Gemma LLM model
Emotions Detected:
- Joy, Sadness, Anger, Fear, Surprise, Disgust, Neutral
Sentiments Detected:
- Positive, Negative, Neutral
Input Example:
"Pedro is a student and lives in Madrid."
Output Example:
Emotion: Neutral, Sentiment: Neutral
Class: IntegratedCognitiveSystem
Responsibility: Orchestrate the complete pipeline
Key Methods:
process_text(text)→ Runs all 3 stagesprocess_and_save(text, file)→ Saves complete analysis_cleanup_temp_files()→ Removes intermediate files
External Dependencies:
- All three modules above
- Ollama API
Workflow:
- Instantiates all three components
- Coordinates data flow between stages
- Manages temporary files
- Generates comprehensive reports
# Input
text = "Pedro is a student."
# NLPToInferenceConverter
converter.convert_sentence(text)
# Gemma processes with few-shot prompt
# Returns: "(Pedro)IsA(student)"
# Output saved to .inf file# Input from .inf file
"(Pedro)IsA(student)"
"(Pedro)LivesIn(Madrid)"
# InferenceEngine
engine.load_file("facts.inf")
# Parsing and grouping
# Subject "Pedro" has 2 relations
# Output with concatenation
"Pedro is a student and lives in Madrid."# Input
"Pedro is a student and lives in Madrid."
# EmotionalAnalyzer
analyzer.analyze_sentence(inference)
# Gemma classifies emotion and sentiment
# Output
{
'emotion': 'Neutral',
'sentiment': 'Neutral'
}Interface: .inf file format
Data Structure:
(Subject)Relation(Object)
(Subject)Relation(Object)
...
Validation: Engine validates syntax during parsing
Interface: Plain text file
Data Structure:
Natural language sentence.
Natural language sentence.
...
Format: One inference per line
- Sequential processing stages
- Each stage transforms data for the next
- Different parsing strategies (infix, n-ary, definition)
- Pluggable LLM models
- Prompt templates with placeholders
- Consistent LLM interaction pattern
IntegratedCognitiveSystemprovides simple interface- Hides complexity of individual components
- Extend
InferenceEngine._parse_line() - Add new parsing method
- Update
to_natural_language()if needed
- Update prompt in
EmotionalAnalyzer._create_prompt_template() - Add examples for the new emotion
- Update documentation
- Replace Ollama calls with new API
- Adjust prompt format if needed
- Update temperature/parameters
- Create new module with similar structure
- Integrate in
IntegratedCognitiveSystem - Update data flow
-
LLM API Calls (Stages 1 & 3)
- Solution: Batch processing, caching
-
File I/O (All stages)
- Solution: Stream processing for large files
-
Sentence Splitting (Stage 1)
- Solution: Use faster regex patterns
- Parallel Processing: Process sentences in parallel
- Caching: Cache LLM responses for repeated sentences
- Streaming: Process data streams instead of loading entire files
- Model Selection: Use smaller models for faster processing
- Connection errors: Retry with exponential backoff
- Invalid responses: Log and skip sentence
- Timeout: Use longer timeout for complex sentences
- Syntax errors: Skip invalid lines, log warning
- Empty file: Return empty list
- Encoding issues: Try multiple encodings
- Classification errors: Default to "Unknown"
- API failures: Retry or skip
- Invalid format: Parse robustly with fallbacks
╔══════════════════════════════════════════════════════════════════════════════╗
║ End of Architecture Documentation ║
╚══════════════════════════════════════════════════════════════════════════════╝