oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
Documentation
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! # SPARQL Extension for Advanced Embedding-Enhanced Queries
//!
//! This module provides advanced SPARQL extension operators and functions that integrate
//! knowledge graph embeddings for semantic query enhancement, vector similarity search,
//! and intelligent query expansion.
//!
//! ## Features
//!
//! - **Vector Similarity Operators**: Compute similarity between entities and relations
//! - **Semantic Query Expansion**: Automatically expand queries with similar concepts
//! - **Approximate Matching**: Find entities even with typos or variations
//! - **Embedding-based Filtering**: Filter results by embedding similarity
//! - **Hybrid Queries**: Combine symbolic SPARQL with semantic vector operations
//!
//! ## Example Usage
//!
//! ```sparql
//! PREFIX vec: <http://oxirs.ai/vec#>
//!
//! # Find entities similar to "alice" with similarity > 0.7
//! SELECT ?entity ?similarity WHERE {
//!   ?entity vec:similarTo <http://example.org/alice> .
//!   BIND(vec:similarity(<http://example.org/alice>, ?entity) AS ?similarity)
//!   FILTER(?similarity > 0.7)
//! }
//!
//! # Find nearest neighbors
//! SELECT ?neighbor ?distance WHERE {
//!   ?neighbor vec:nearestTo <http://example.org/alice> .
//!   BIND(vec:distance(<http://example.org/alice>, ?neighbor) AS ?distance)
//! } LIMIT 10
//!
//! # Semantic query expansion
//! SELECT ?s ?o WHERE {
//!   ?s ?p ?o .
//!   FILTER(vec:semanticMatch(?p, <http://example.org/knows>, 0.8))
//! }
//! ```

use crate::{EmbeddingModel, Vector};
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, trace};

/// Configuration for SPARQL extension behavior
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SparqlExtensionConfig {
    /// Default similarity threshold for approximate matching
    pub default_similarity_threshold: f32,
    /// Maximum number of expansions per query element
    pub max_expansions_per_element: usize,
    /// Enable query rewriting optimizations
    pub enable_query_rewriting: bool,
    /// Enable semantic caching
    pub enable_semantic_caching: bool,
    /// Cache size for semantic query results
    pub semantic_cache_size: usize,
    /// Enable fuzzy matching for entity names
    pub enable_fuzzy_matching: bool,
    /// Minimum confidence for query expansion
    pub min_expansion_confidence: f32,
    /// Enable parallel processing for similarity computations
    pub enable_parallel_processing: bool,
}

impl Default for SparqlExtensionConfig {
    fn default() -> Self {
        Self {
            default_similarity_threshold: 0.7,
            max_expansions_per_element: 10,
            enable_query_rewriting: true,
            enable_semantic_caching: true,
            semantic_cache_size: 1000,
            enable_fuzzy_matching: true,
            min_expansion_confidence: 0.6,
            enable_parallel_processing: true,
        }
    }
}

/// Advanced SPARQL extension engine
pub struct SparqlExtension {
    model: Arc<RwLock<Box<dyn EmbeddingModel>>>,
    config: SparqlExtensionConfig,
    semantic_cache: Arc<RwLock<SemanticCache>>,
    query_statistics: Arc<RwLock<QueryStatistics>>,
}

impl SparqlExtension {
    /// Create new SPARQL extension with embedding model
    pub fn new(model: Box<dyn EmbeddingModel>) -> Self {
        Self {
            model: Arc::new(RwLock::new(model)),
            config: SparqlExtensionConfig::default(),
            semantic_cache: Arc::new(RwLock::new(SemanticCache::new(1000))),
            query_statistics: Arc::new(RwLock::new(QueryStatistics::default())),
        }
    }

    /// Create with custom configuration
    pub fn with_config(model: Box<dyn EmbeddingModel>, config: SparqlExtensionConfig) -> Self {
        let cache_size = config.semantic_cache_size;
        Self {
            model: Arc::new(RwLock::new(model)),
            config,
            semantic_cache: Arc::new(RwLock::new(SemanticCache::new(cache_size))),
            query_statistics: Arc::new(RwLock::new(QueryStatistics::default())),
        }
    }

    /// Compute similarity between two entities
    ///
    /// # Arguments
    /// * `entity1` - First entity URI
    /// * `entity2` - Second entity URI
    ///
    /// # Returns
    /// Cosine similarity score between 0.0 and 1.0
    pub async fn vec_similarity(&self, entity1: &str, entity2: &str) -> Result<f32> {
        trace!("Computing similarity between {} and {}", entity1, entity2);

        // Check cache first
        if self.config.enable_semantic_caching {
            let cache = self.semantic_cache.read().await;
            let cache_key = format!("sim:{}:{}", entity1, entity2);
            if let Some(cached_result) = cache.get(&cache_key) {
                debug!("Cache hit for similarity computation");
                return Ok(cached_result);
            }
        }

        let model = self.model.read().await;
        let emb1 = model.get_entity_embedding(entity1)?;
        let emb2 = model.get_entity_embedding(entity2)?;

        let similarity = normalized_cosine_similarity(&emb1, &emb2)?;

        // Cache result
        if self.config.enable_semantic_caching {
            let mut cache = self.semantic_cache.write().await;
            let cache_key = format!("sim:{}:{}", entity1, entity2);
            cache.put(cache_key, similarity);
        }

        // Update statistics
        let mut stats = self.query_statistics.write().await;
        stats.similarity_computations += 1;

        Ok(similarity)
    }

    /// Find k nearest neighbors for an entity
    ///
    /// # Arguments
    /// * `entity` - Target entity URI
    /// * `k` - Number of neighbors to return
    /// * `min_similarity` - Minimum similarity threshold (optional)
    ///
    /// # Returns
    /// Vector of (entity_uri, similarity_score) pairs
    pub async fn vec_nearest(
        &self,
        entity: &str,
        k: usize,
        min_similarity: Option<f32>,
    ) -> Result<Vec<(String, f32)>> {
        info!("Finding {} nearest neighbors for {}", k, entity);

        let model = self.model.read().await;
        let target_emb = model.get_entity_embedding(entity)?;
        let all_entities = model.get_entities();

        let threshold = min_similarity.unwrap_or(self.config.default_similarity_threshold);

        // Compute similarities in parallel if enabled
        let mut similarities: Vec<(String, f32)> = if self.config.enable_parallel_processing {
            self.compute_similarities_parallel(&all_entities, &target_emb, entity)
                .await?
        } else {
            self.compute_similarities_sequential(&all_entities, &target_emb, entity, &**model)
                .await?
        };

        // Filter by threshold and sort by similarity
        similarities.retain(|(_, sim)| *sim >= threshold);
        similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Take top k
        let result: Vec<(String, f32)> = similarities.into_iter().take(k).collect();

        // Update statistics
        let mut stats = self.query_statistics.write().await;
        stats.nearest_neighbor_queries += 1;

        Ok(result)
    }

    /// Find entities similar to a given entity above threshold
    ///
    /// # Arguments
    /// * `entity` - Target entity URI
    /// * `threshold` - Minimum similarity threshold
    ///
    /// # Returns
    /// Vector of (entity_uri, similarity_score) pairs
    pub async fn vec_similar_entities(
        &self,
        entity: &str,
        threshold: f32,
    ) -> Result<Vec<(String, f32)>> {
        debug!(
            "Finding entities similar to {} (threshold: {})",
            entity, threshold
        );

        let model = self.model.read().await;
        let target_emb = model.get_entity_embedding(entity)?;
        let all_entities = model.get_entities();

        let similarities = if self.config.enable_parallel_processing {
            self.compute_similarities_parallel(&all_entities, &target_emb, entity)
                .await?
        } else {
            self.compute_similarities_sequential(&all_entities, &target_emb, entity, &**model)
                .await?
        };

        let result: Vec<(String, f32)> = similarities
            .into_iter()
            .filter(|(_, sim)| *sim >= threshold)
            .collect();

        Ok(result)
    }

    /// Find relations similar to a given relation above threshold
    ///
    /// # Arguments
    /// * `relation` - Target relation URI
    /// * `threshold` - Minimum similarity threshold
    ///
    /// # Returns
    /// Vector of (relation_uri, similarity_score) pairs
    pub async fn vec_similar_relations(
        &self,
        relation: &str,
        threshold: f32,
    ) -> Result<Vec<(String, f32)>> {
        debug!(
            "Finding relations similar to {} (threshold: {})",
            relation, threshold
        );

        let model = self.model.read().await;
        let target_emb = model.get_relation_embedding(relation)?;
        let all_relations = model.get_relations();

        let mut similarities = Vec::new();
        for rel in &all_relations {
            if rel == relation {
                continue; // Skip self
            }

            let rel_emb = model.get_relation_embedding(rel)?;
            let sim = cosine_similarity(&target_emb, &rel_emb)?;

            if sim >= threshold {
                similarities.push((rel.clone(), sim));
            }
        }

        similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(similarities)
    }

    /// Perform semantic query expansion
    ///
    /// # Arguments
    /// * `query` - Original SPARQL query
    ///
    /// # Returns
    /// Expanded query with similar entities and relations
    pub async fn expand_query_semantically(&self, query: &str) -> Result<ExpandedQuery> {
        info!("Performing semantic query expansion");

        let mut stats = self.query_statistics.write().await;
        stats.query_expansions += 1;
        drop(stats);

        let model = self.model.read().await;

        // Parse query to extract entities and relations
        let parsed = parse_sparql_query(query)?;

        let mut entity_expansions = HashMap::new();
        let mut relation_expansions = HashMap::new();

        // Expand entities
        for entity in &parsed.entities {
            let similar = self
                .vec_similar_entities(entity, self.config.min_expansion_confidence)
                .await?;

            let expansions: Vec<Expansion> = similar
                .into_iter()
                .take(self.config.max_expansions_per_element)
                .map(|(uri, confidence)| Expansion {
                    original: entity.clone(),
                    expanded: uri,
                    confidence,
                    expansion_type: ExpansionType::Entity,
                })
                .collect();

            if !expansions.is_empty() {
                entity_expansions.insert(entity.clone(), expansions);
            }
        }

        // Expand relations
        for relation in &parsed.relations {
            let similar = self
                .vec_similar_relations(relation, self.config.min_expansion_confidence)
                .await?;

            let expansions: Vec<Expansion> = similar
                .into_iter()
                .take(self.config.max_expansions_per_element)
                .map(|(uri, confidence)| Expansion {
                    original: relation.clone(),
                    expanded: uri,
                    confidence,
                    expansion_type: ExpansionType::Relation,
                })
                .collect();

            if !expansions.is_empty() {
                relation_expansions.insert(relation.clone(), expansions);
            }
        }

        drop(model);

        let expanded_query = if self.config.enable_query_rewriting {
            self.rewrite_query_with_expansions(query, &entity_expansions, &relation_expansions)
                .await?
        } else {
            query.to_string()
        };

        let expansion_count = entity_expansions.len() + relation_expansions.len();

        Ok(ExpandedQuery {
            original_query: query.to_string(),
            expanded_query,
            entity_expansions,
            relation_expansions,
            expansion_count,
        })
    }

    /// Perform fuzzy entity matching
    ///
    /// # Arguments
    /// * `entity_name` - Entity name (possibly with typos)
    /// * `k` - Number of candidates to return
    ///
    /// # Returns
    /// Vector of (entity_uri, match_score) pairs
    pub async fn fuzzy_match_entity(
        &self,
        entity_name: &str,
        k: usize,
    ) -> Result<Vec<(String, f32)>> {
        if !self.config.enable_fuzzy_matching {
            return Ok(vec![]);
        }

        debug!("Performing fuzzy match for entity: {}", entity_name);

        let model = self.model.read().await;
        let all_entities = model.get_entities();

        let mut matches = Vec::new();

        for entity in &all_entities {
            let score = fuzzy_match_score(entity_name, entity);
            if score > 0.5 {
                // Minimum fuzzy match threshold
                matches.push((entity.clone(), score));
            }
        }

        matches.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(matches.into_iter().take(k).collect())
    }

    /// Get query statistics
    pub async fn get_statistics(&self) -> QueryStatistics {
        self.query_statistics.read().await.clone()
    }

    /// Clear semantic cache
    pub async fn clear_cache(&self) {
        let mut cache = self.semantic_cache.write().await;
        cache.clear();
        info!("Semantic cache cleared");
    }

    // Helper methods

    async fn compute_similarities_parallel(
        &self,
        entities: &[String],
        target_emb: &Vector,
        exclude_entity: &str,
    ) -> Result<Vec<(String, f32)>> {
        use rayon::prelude::*;

        let model = self.model.read().await;
        let embeddings: Vec<_> = entities
            .iter()
            .filter(|e| e.as_str() != exclude_entity)
            .filter_map(|e| {
                model
                    .get_entity_embedding(e)
                    .ok()
                    .map(|emb| (e.clone(), emb))
            })
            .collect();
        drop(model);

        let target_emb_clone = target_emb.clone();
        let similarities: Vec<(String, f32)> = embeddings
            .par_iter()
            .filter_map(|(entity, emb)| {
                cosine_similarity(&target_emb_clone, emb)
                    .ok()
                    .map(|sim| (entity.clone(), sim))
            })
            .collect();

        Ok(similarities)
    }

    async fn compute_similarities_sequential(
        &self,
        entities: &[String],
        target_emb: &Vector,
        exclude_entity: &str,
        model: &dyn EmbeddingModel,
    ) -> Result<Vec<(String, f32)>> {
        let mut similarities = Vec::new();

        for entity in entities {
            if entity == exclude_entity {
                continue;
            }

            if let Ok(entity_emb) = model.get_entity_embedding(entity) {
                if let Ok(sim) = cosine_similarity(target_emb, &entity_emb) {
                    similarities.push((entity.clone(), sim));
                }
            }
        }

        Ok(similarities)
    }

    async fn rewrite_query_with_expansions(
        &self,
        original_query: &str,
        entity_expansions: &HashMap<String, Vec<Expansion>>,
        relation_expansions: &HashMap<String, Vec<Expansion>>,
    ) -> Result<String> {
        // This is a simplified query rewriting
        // In production, would use a proper SPARQL parser and rewriter
        let mut rewritten = original_query.to_string();

        // Add UNION clauses for entity expansions
        for (original, expansions) in entity_expansions {
            if let Some(first_expansion) = expansions.first() {
                let union_clause = format!(
                    "\n  UNION {{ # Semantic expansion for {}\n    # Similar entity: {} (confidence: {:.2})\n  }}",
                    original, first_expansion.expanded, first_expansion.confidence
                );
                rewritten.push_str(&union_clause);
            }
        }

        // Add comments for relation expansions
        for (original, expansions) in relation_expansions {
            if let Some(first_expansion) = expansions.first() {
                let comment = format!(
                    "\n  # Relation '{}' can be expanded to '{}' (confidence: {:.2})",
                    original, first_expansion.expanded, first_expansion.confidence
                );
                rewritten.push_str(&comment);
            }
        }

        Ok(rewritten)
    }
}

/// Semantic cache for query results
struct SemanticCache {
    cache: HashMap<String, f32>,
    max_size: usize,
    access_count: HashMap<String, u64>,
}

impl SemanticCache {
    fn new(max_size: usize) -> Self {
        Self {
            cache: HashMap::new(),
            max_size,
            access_count: HashMap::new(),
        }
    }

    fn get(&self, key: &str) -> Option<f32> {
        self.cache.get(key).copied()
    }

    fn put(&mut self, key: String, value: f32) {
        // Evict least recently used if cache is full
        if self.cache.len() >= self.max_size {
            if let Some(lru_key) = self
                .access_count
                .iter()
                .min_by_key(|(_, &count)| count)
                .map(|(k, _)| k.clone())
            {
                self.cache.remove(&lru_key);
                self.access_count.remove(&lru_key);
            }
        }

        self.cache.insert(key.clone(), value);
        *self.access_count.entry(key).or_insert(0) += 1;
    }

    fn clear(&mut self) {
        self.cache.clear();
        self.access_count.clear();
    }
}

/// Query statistics for monitoring
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QueryStatistics {
    pub similarity_computations: u64,
    pub nearest_neighbor_queries: u64,
    pub query_expansions: u64,
    pub fuzzy_matches: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
}

/// Expanded SPARQL query with semantic enhancements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpandedQuery {
    pub original_query: String,
    pub expanded_query: String,
    pub entity_expansions: HashMap<String, Vec<Expansion>>,
    pub relation_expansions: HashMap<String, Vec<Expansion>>,
    pub expansion_count: usize,
}

/// Query expansion suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Expansion {
    pub original: String,
    pub expanded: String,
    pub confidence: f32,
    pub expansion_type: ExpansionType,
}

/// Type of expansion
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExpansionType {
    Entity,
    Relation,
    Pattern,
}

/// Parsed SPARQL query elements
#[derive(Debug, Clone)]
struct ParsedQuery {
    entities: Vec<String>,
    relations: Vec<String>,
    variables: HashSet<String>,
}

/// Parse SPARQL query (simplified)
fn parse_sparql_query(query: &str) -> Result<ParsedQuery> {
    let mut entities = Vec::new();
    let mut relations = Vec::new();
    let mut variables = HashSet::new();

    // Compile regex patterns once outside the loop
    let uri_pattern =
        regex::Regex::new(r"<(https?://[^>]+)>").expect("regex should compile for valid pattern");
    let var_pattern =
        regex::Regex::new(r"\?(\w+)").expect("regex should compile for valid pattern");

    for line in query.lines() {
        // Extract URIs (entities and relations)
        if line.contains("http://") || line.contains("https://") {
            // Extract full URIs
            for cap in uri_pattern.captures_iter(line) {
                let uri = cap[1].to_string();
                // Heuristic: if it appears in predicate position, it's a relation
                if line.contains(&format!(" <{uri}> ")) {
                    relations.push(uri.clone());
                } else {
                    entities.push(uri);
                }
            }
        }

        // Extract variables
        for cap in var_pattern.captures_iter(line) {
            variables.insert(cap[1].to_string());
        }
    }

    Ok(ParsedQuery {
        entities,
        relations,
        variables,
    })
}

/// Compute cosine similarity between two vectors
/// Returns standard cosine similarity in [-1.0, 1.0] range
fn cosine_similarity(v1: &Vector, v2: &Vector) -> Result<f32> {
    if v1.dimensions != v2.dimensions {
        return Err(anyhow!(
            "Vector dimensions must match: {} vs {}",
            v1.dimensions,
            v2.dimensions
        ));
    }

    let dot_product: f32 = v1
        .values
        .iter()
        .zip(v2.values.iter())
        .map(|(a, b)| a * b)
        .sum();

    let norm1: f32 = v1.values.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm2: f32 = v2.values.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm1 == 0.0 || norm2 == 0.0 {
        return Ok(0.0);
    }

    // Standard cosine similarity in [-1.0, 1.0]
    let cosine_sim = dot_product / (norm1 * norm2);

    Ok(cosine_sim)
}

/// Compute normalized cosine similarity between two vectors
/// Returns normalized similarity in [0.0, 1.0] range
/// This is useful for SPARQL similarity queries where positive-only scores are expected
fn normalized_cosine_similarity(v1: &Vector, v2: &Vector) -> Result<f32> {
    let cosine_sim = cosine_similarity(v1, v2)?;
    // Normalize from [-1.0, 1.0] to [0.0, 1.0]
    Ok((cosine_sim + 1.0) / 2.0)
}

/// Compute fuzzy match score using Levenshtein-like distance
fn fuzzy_match_score(s1: &str, s2: &str) -> f32 {
    let s1_lower = s1.to_lowercase();
    let s2_lower = s2.to_lowercase();

    // Exact match
    if s1_lower == s2_lower {
        return 1.0;
    }

    // Substring match
    if s1_lower.contains(&s2_lower) || s2_lower.contains(&s1_lower) {
        let max_len = s1.len().max(s2.len()) as f32;
        let min_len = s1.len().min(s2.len()) as f32;
        return min_len / max_len;
    }

    // Simplified Levenshtein distance
    let distance = levenshtein_distance(&s1_lower, &s2_lower);
    let max_len = s1.len().max(s2.len()) as f32;

    if max_len == 0.0 {
        return 1.0;
    }

    1.0 - (distance as f32 / max_len)
}

/// Compute Levenshtein distance
#[allow(clippy::needless_range_loop)]
fn levenshtein_distance(s1: &str, s2: &str) -> usize {
    let len1 = s1.len();
    let len2 = s2.len();

    if len1 == 0 {
        return len2;
    }
    if len2 == 0 {
        return len1;
    }

    let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];

    for i in 0..=len1 {
        matrix[i][0] = i;
    }
    for j in 0..=len2 {
        matrix[0][j] = j;
    }

    let s1_chars: Vec<char> = s1.chars().collect();
    let s2_chars: Vec<char> = s2.chars().collect();

    for i in 1..=len1 {
        for j in 1..=len2 {
            let cost = if s1_chars[i - 1] == s2_chars[j - 1] {
                0
            } else {
                1
            };

            matrix[i][j] = (matrix[i - 1][j] + 1)
                .min(matrix[i][j - 1] + 1)
                .min(matrix[i - 1][j - 1] + cost);
        }
    }

    matrix[len1][len2]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::TransE;
    use crate::{ModelConfig, NamedNode, Triple};

    fn create_test_model() -> TransE {
        let config = ModelConfig::default().with_dimensions(10);
        let mut model = TransE::new(config);

        // Add some test triples
        let triples = vec![
            ("alice", "knows", "bob"),
            ("bob", "knows", "charlie"),
            ("alice", "likes", "music"),
            ("charlie", "likes", "art"),
        ];

        for (s, p, o) in triples {
            let triple = Triple::new(
                NamedNode::new(&format!("http://example.org/{s}")).expect("should succeed"),
                NamedNode::new(&format!("http://example.org/{p}")).expect("should succeed"),
                NamedNode::new(&format!("http://example.org/{o}")).expect("should succeed"),
            );
            model.add_triple(triple).expect("should succeed");
        }

        model
    }

    #[tokio::test]
    async fn test_vec_similarity() -> Result<()> {
        let model = create_test_model();
        let ext = SparqlExtension::new(Box::new(model));

        // Train the model first
        {
            let mut model = ext.model.write().await;
            model.train(Some(10)).await?;
        }

        let sim = ext
            .vec_similarity("http://example.org/alice", "http://example.org/bob")
            .await?;

        assert!((0.0..=1.0).contains(&sim));
        Ok(())
    }

    #[tokio::test]
    async fn test_vec_nearest() -> Result<()> {
        let model = create_test_model();
        let ext = SparqlExtension::new(Box::new(model));

        {
            let mut model = ext.model.write().await;
            model.train(Some(10)).await?;
        }

        // Use a lower threshold since we only trained for 10 epochs
        let neighbors = ext
            .vec_nearest("http://example.org/alice", 2, Some(0.0))
            .await?;

        // After training, there should be at least some entities
        // (might be 0 if similarity is very low after minimal training)
        assert!(neighbors.len() <= 2);

        for (entity, sim) in neighbors {
            assert!(!entity.is_empty());
            assert!((0.0..=1.0).contains(&sim));
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_semantic_query_expansion() -> Result<()> {
        let model = create_test_model();
        let ext = SparqlExtension::new(Box::new(model));

        {
            let mut model = ext.model.write().await;
            model.train(Some(10)).await?;
        }

        let query = r#"
            SELECT ?s ?o WHERE {
                ?s <http://example.org/knows> ?o
            }
        "#;

        let expanded = ext.expand_query_semantically(query).await?;

        assert_eq!(expanded.original_query, query);
        assert!(!expanded.expanded_query.is_empty());

        Ok(())
    }

    #[tokio::test]
    async fn test_fuzzy_match() -> Result<()> {
        let model = create_test_model();
        let ext = SparqlExtension::new(Box::new(model));

        let matches = ext.fuzzy_match_entity("alice", 3).await?;

        // The entities are full URIs, so we should find matches
        // but the fuzzy matching compares entity names to "alice"
        // so we might not get perfect matches with short queries
        // Just verify the function returns results or empty list without errors
        assert!(matches.len() <= 3);
        for (entity, score) in matches {
            assert!(!entity.is_empty());
            assert!((0.0..=1.0).contains(&score));
        }

        Ok(())
    }

    #[test]
    fn test_parse_sparql_query() -> Result<()> {
        let query = r#"
            SELECT ?s ?o WHERE {
                ?s <http://example.org/knows> ?o .
                <http://example.org/alice> <http://example.org/likes> ?o .
            }
        "#;

        let parsed = parse_sparql_query(query)?;

        // The parser extracts URIs and variables
        // Variables should always be found
        assert!(parsed.variables.contains("s"));
        assert!(parsed.variables.contains("o"));

        // URIs should be extracted (entities or relations)
        // The total should be > 0
        assert!(
            !parsed.entities.is_empty() || !parsed.relations.is_empty(),
            "Should extract at least some URIs from the query"
        );

        Ok(())
    }

    #[test]
    fn test_cosine_similarity() -> Result<()> {
        let v1 = Vector::new(vec![1.0, 0.0, 0.0]);
        let v2 = Vector::new(vec![1.0, 0.0, 0.0]);
        let sim = cosine_similarity(&v1, &v2)?;
        assert!((sim - 1.0).abs() < 1e-6);

        let v3 = Vector::new(vec![0.0, 1.0, 0.0]);
        let sim2 = cosine_similarity(&v1, &v3)?;
        assert!((sim2 - 0.0).abs() < 1e-6);

        Ok(())
    }

    #[test]
    fn test_levenshtein_distance() {
        assert_eq!(levenshtein_distance("alice", "alice"), 0);
        assert_eq!(levenshtein_distance("alice", "alise"), 1);
        assert_eq!(levenshtein_distance("alice", "bob"), 5);
        assert_eq!(levenshtein_distance("", "abc"), 3);
        assert_eq!(levenshtein_distance("abc", ""), 3);
    }

    #[test]
    fn test_fuzzy_match_score() {
        assert!((fuzzy_match_score("alice", "alice") - 1.0).abs() < 1e-6);
        assert!(fuzzy_match_score("alice", "alise") > 0.7);
        assert!(fuzzy_match_score("alice", "bob") < 0.5);
    }

    #[tokio::test]
    async fn test_statistics_tracking() -> Result<()> {
        let model = create_test_model();
        let ext = SparqlExtension::new(Box::new(model));

        {
            let mut model = ext.model.write().await;
            model.train(Some(10)).await?;
        }

        // Perform some operations
        let _ = ext
            .vec_similarity("http://example.org/alice", "http://example.org/bob")
            .await;
        let _ = ext.vec_nearest("http://example.org/alice", 2, None).await;

        let stats = ext.get_statistics().await;

        assert!(stats.similarity_computations > 0);
        assert!(stats.nearest_neighbor_queries > 0);

        Ok(())
    }

    #[tokio::test]
    async fn test_semantic_cache() -> Result<()> {
        let model = create_test_model();
        let ext = SparqlExtension::new(Box::new(model));

        {
            let mut model = ext.model.write().await;
            model.train(Some(10)).await?;
        }

        // First call - cache miss
        let sim1 = ext
            .vec_similarity("http://example.org/alice", "http://example.org/bob")
            .await?;

        // Second call - should hit cache
        let sim2 = ext
            .vec_similarity("http://example.org/alice", "http://example.org/bob")
            .await?;

        assert!((sim1 - sim2).abs() < 1e-6);

        // Test cache clearing
        ext.clear_cache().await;

        Ok(())
    }
}