subx_cli/core/sync/dialogue/
segment.rs

1/// 代表一段對話或靜默的資料結構
2#[derive(Debug, Clone)]
3pub struct DialogueSegment {
4    pub start_time: f64,
5    pub end_time: f64,
6    pub is_speech: bool,
7    pub confidence: f32,
8}
9
10impl DialogueSegment {
11    /// 建立新的語音片段
12    pub fn new_speech(start: f64, end: f64) -> Self {
13        Self {
14            start_time: start,
15            end_time: end,
16            is_speech: true,
17            confidence: 1.0,
18        }
19    }
20
21    /// 建立新的靜默片段
22    pub fn new_silence(start: f64, end: f64) -> Self {
23        Self {
24            start_time: start,
25            end_time: end,
26            is_speech: false,
27            confidence: 1.0,
28        }
29    }
30
31    /// 取得片段持續時間(秒)
32    pub fn duration(&self) -> f64 {
33        self.end_time - self.start_time
34    }
35
36    /// 判斷是否與其他片段重疊
37    pub fn overlaps_with(&self, other: &DialogueSegment) -> bool {
38        self.start_time < other.end_time && self.end_time > other.start_time
39    }
40
41    /// 與其他同類型片段合併,更新邊界與信心度
42    pub fn merge_with(&mut self, other: &DialogueSegment) {
43        if self.is_speech == other.is_speech && self.overlaps_with(other) {
44            self.start_time = self.start_time.min(other.start_time);
45            self.end_time = self.end_time.max(other.end_time);
46            self.confidence = (self.confidence + other.confidence) / 2.0;
47        }
48    }
49}
50
51/// 靜默片段資料結構,可用於額外處理或分析
52#[derive(Debug, Clone)]
53pub struct SilenceSegment {
54    pub start_time: f64,
55    pub end_time: f64,
56    pub duration: f64,
57}
58
59impl SilenceSegment {
60    /// 建立靜默片段
61    pub fn new(start: f64, end: f64) -> Self {
62        Self {
63            start_time: start,
64            end_time: end,
65            duration: end - start,
66        }
67    }
68
69    /// 檢查此靜默片段是否超過最小長度
70    pub fn is_significant(&self, min_duration: f64) -> bool {
71        self.duration >= min_duration
72    }
73}