lyrics-search 0.2.0

Lyrics search functionality for various platforms
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
use lyrics_core::helpers::chinese_helper::to_simplified;
use lyrics_core::helpers::string_helper::{compute_text_same, remove_duo_spaces};

/// 曲目匹配等级,用于评估搜索结果与目标曲目的相似程度。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MatchType {
    /// 不匹配
    NoMatch = -1,
    /// 极低匹配度
    VeryLow = 10,
    /// 低匹配度
    Low = 30,
    /// 中等匹配度
    Medium = 70,
    /// 较高匹配度
    PrettyHigh = 90,
    /// 高匹配度
    High = 95,
    /// 极高匹配度
    VeryHigh = 99,
    /// 完全匹配
    Perfect = 100,
}

impl PartialOrd for MatchType {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for MatchType {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (*self as i32).cmp(&(*other as i32))
    }
}

/// 名称匹配等级。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NameMatchType {
    /// 不匹配
    NoMatch = -1,
    /// 低匹配度
    Low = 0,
    /// 中等匹配度
    Medium = 1,
    /// 高匹配度
    High = 2,
    /// 极高匹配度
    VeryHigh = 3,
    /// 完全匹配
    Perfect = 4,
}

impl NameMatchType {
    /// 返回该匹配等级对应的分值。
    pub fn score(self) -> f64 {
        match self {
            NameMatchType::Perfect => 7.0,
            NameMatchType::VeryHigh => 6.0,
            NameMatchType::High => 5.0,
            NameMatchType::Medium => 4.0,
            NameMatchType::Low => 2.0,
            NameMatchType::NoMatch => 0.0,
        }
    }
}

/// 艺术家匹配等级。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArtistMatchType {
    /// 不匹配
    NoMatch = -1,
    /// 低匹配度
    Low = 0,
    /// 中等匹配度
    Medium = 1,
    /// 高匹配度
    High = 2,
    /// 极高匹配度
    VeryHigh = 3,
    /// 完全匹配
    Perfect = 4,
}

impl ArtistMatchType {
    /// 返回该匹配等级对应的分值。
    pub fn score(self) -> f64 {
        match self {
            ArtistMatchType::Perfect => 7.0,
            ArtistMatchType::VeryHigh => 6.0,
            ArtistMatchType::High => 5.0,
            ArtistMatchType::Medium => 4.0,
            ArtistMatchType::Low => 2.0,
            ArtistMatchType::NoMatch => 0.0,
        }
    }
}

/// 时长匹配等级。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DurationMatchType {
    /// 不匹配
    NoMatch = -1,
    /// 低匹配度
    Low = 0,
    /// 中等匹配度
    Medium = 1,
    /// 高匹配度
    High = 2,
    /// 极高匹配度
    VeryHigh = 3,
    /// 完全匹配
    Perfect = 4,
}

impl DurationMatchType {
    /// 返回该匹配等级对应的分值。
    pub fn score(self) -> f64 {
        match self {
            DurationMatchType::Perfect => 7.0,
            DurationMatchType::VeryHigh => 6.0,
            DurationMatchType::High => 5.0,
            DurationMatchType::Medium => 4.0,
            DurationMatchType::Low => 2.0,
            DurationMatchType::NoMatch => 0.0,
        }
    }
}

/// 计算名称匹配类型的分值,`None` 时返回 0。
pub fn name_score(m: Option<NameMatchType>) -> f64 {
    m.map_or(0.0, |m| m.score())
}

/// 计算艺术家匹配类型的分值,`None` 时返回 0。
pub fn artist_score(m: Option<ArtistMatchType>) -> f64 {
    m.map_or(0.0, |m| m.score())
}

/// 计算时长匹配类型的分值,`None` 时返回 0。
pub fn duration_score(m: Option<DurationMatchType>) -> f64 {
    m.map_or(0.0, |m| m.score())
}

/// 比较两个时长值(毫秒)的匹配程度。
pub fn compare_duration(d1: Option<i32>, d2: Option<i32>) -> Option<DurationMatchType> {
    let d1 = d1?;
    let d2 = d2?;
    if d1 == 0 || d2 == 0 {
        return None;
    }
    let diff = (d1 - d2).unsigned_abs();
    Some(match diff {
        0 => DurationMatchType::Perfect,
        1..300 => DurationMatchType::VeryHigh,
        300..700 => DurationMatchType::High,
        700..1500 => DurationMatchType::Medium,
        1500..3500 => DurationMatchType::Low,
        _ => DurationMatchType::NoMatch,
    })
}

/// 比较两组艺术家列表的匹配程度,支持繁简中文转换。
pub fn compare_artist(artist1: &[String], artist2: &[String]) -> Option<ArtistMatchType> {
    let list1: Vec<String> = artist1
        .iter()
        .filter(|a| !a.trim().is_empty())
        .map(|a| to_simplified(&a.to_lowercase()))
        .collect();
    let list2: Vec<String> = artist2
        .iter()
        .filter(|a| !a.trim().is_empty())
        .map(|a| to_simplified(&a.to_lowercase()))
        .collect();

    if list1.is_empty() || list2.is_empty() {
        return None;
    }

    let count = list2.iter().filter(|a| list1.contains(a)).count();

    if count == list1.len() && list1.len() == list2.len() {
        return Some(ArtistMatchType::Perfect);
    }

    if (count + 1 >= list1.len() && list1.len() >= 2)
        || (list1.len() > 6 && count as f64 / list1.len() as f64 > 0.8)
    {
        return Some(ArtistMatchType::VeryHigh);
    }

    if count == 1 && list1.len() == 1 && list2.len() == 2 {
        return Some(ArtistMatchType::High);
    }

    if list1.len() > 5 && (list2[0].contains("Various") || list2[0].contains("群星")) {
        return Some(ArtistMatchType::VeryHigh);
    }

    if list1.len() > 7 && list2.len() > 7 && count as f64 / list1.len() as f64 > 0.66 {
        return Some(ArtistMatchType::High);
    }

    if list1.len() == 1 && list2.len() > 1 && list1[0].starts_with(list2[0].as_str()) {
        return Some(ArtistMatchType::High);
    }

    if list1.len() == 1
        && list2.len() > 1
        && list2[0].chars().count() > 3
        && list1[0].contains(list2[0].as_str())
    {
        return Some(ArtistMatchType::High);
    }

    if list1.len() == 1
        && list2.len() > 1
        && list2[0].chars().count() > 1
        && list1[0].contains(list2[0].as_str())
    {
        return Some(ArtistMatchType::Medium);
    }

    if count == 1 && list1.len() == 1 && list2.len() >= 3 {
        return Some(ArtistMatchType::Medium);
    }

    if count >= 2 {
        return Some(ArtistMatchType::Low);
    }

    Some(ArtistMatchType::NoMatch)
}

/// 计算两个字符串在相同位置上字符相等的数量。
pub fn chars_eq_at(s1: &str, s2: &str) -> usize {
    s1.chars().zip(s2.chars()).filter(|(a, b)| a == b).count()
}

/// 比较两个名称(标题或专辑)的匹配程度,支持繁简中文和特殊标记容错。
pub fn compare_name(name1: Option<&str>, name2: Option<&str>) -> Option<NameMatchType> {
    let name1 = name1.filter(|s| !s.trim().is_empty())?;
    let name2 = name2.filter(|s| !s.trim().is_empty())?;

    fn normalize_name(name: &str) -> String {
        let normalized = to_simplified(name)
            .to_lowercase()
            .trim()
            .replace('\u{2019}', "'")
            .replace('\u{ff0c}', ",")
            .replace('\u{ff08}', "(")
            .replace('\u{ff09}', ")")
            .replace('[', "(")
            .replace(']', ")");
        remove_duo_spaces(&normalized)
            .replace(" (", "(")
            .replace("( ", "(")
            .replace(" )", ")")
    }

    let mut n1 = normalize_name(name1);
    let mut n2 = normalize_name(name2);

    if n1 == n2 {
        return Some(NameMatchType::Perfect);
    }

    n1 = n1.replace("acoustic version", "acoustic");
    n2 = n2.replace("acoustic version", "acoustic");

    {
        let s1 = format!("{})", n1.replace(" - ", " (").trim());
        let s2 = format!("{})", n2.replace(" - ", " (").trim());
        if s1.replace(' ', "") == s2.replace(' ', "") {
            return Some(NameMatchType::VeryHigh);
        }
    }

    fn special_compare(str1: &str, str2: &str, special: &str) -> bool {
        let special = format!("({special}");
        let c1 = str1.contains(&special);
        let c2 = str2.contains(&special);
        if c1 && !c2 {
            let idx = str1.find(&special).unwrap();
            if str1[..idx].trim() == str2 {
                return true;
            }
        }
        if c2 && !c1 {
            let idx = str2.find(&special).unwrap();
            if str2[..idx].trim() == str1 {
                return true;
            }
        }
        false
    }

    fn single_special_compare(str1: &str, str2: &str, special: &str) -> bool {
        let special = format!("({special}");
        if str1.contains(&special) && str2.contains(&special) {
            let i1 = str1.find(&special).unwrap();
            let i2 = str2.find(&special).unwrap();
            if str1[..i1].trim() == str2[..i2].trim() {
                return true;
            }
        }
        false
    }

    fn duo_special_compare(str1: &str, str2: &str, special1: &str, special2: &str) -> bool {
        let s1 = format!("({special1}");
        let s2 = format!("({special2}");
        if str1.contains(&s1) && str2.contains(&s2) {
            let i1 = str1.find(&s1).unwrap();
            let i2 = str2.find(&s2).unwrap();
            if str1[..i1].trim() == str2[..i2].trim() {
                return true;
            }
        }
        if str1.contains(&s2) && str2.contains(&s1) {
            let i1 = str1.find(&s2).unwrap();
            let i2 = str2.find(&s1).unwrap();
            if str1[..i1].trim() == str2[..i2].trim() {
                return true;
            }
        }
        false
    }

    fn brackets_compare(str1: &str, str2: &str) -> bool {
        if str1.contains('(') && !str2.contains('(') {
            let idx = str1.find('(').unwrap();
            if str1[..idx].trim() == str2 {
                return true;
            }
        }
        if str2.contains('(') && !str1.contains('(') {
            let idx = str2.find('(').unwrap();
            if str2[..idx].trim() == str1 {
                return true;
            }
        }
        false
    }

    if special_compare(&n1, &n2, "deluxe") {
        return Some(NameMatchType::VeryHigh);
    }
    if special_compare(&n1, &n2, "explicit") {
        return Some(NameMatchType::VeryHigh);
    }
    if special_compare(&n1, &n2, "special edition") {
        return Some(NameMatchType::VeryHigh);
    }
    if special_compare(&n1, &n2, "bonus track") {
        return Some(NameMatchType::VeryHigh);
    }
    if special_compare(&n1, &n2, "feat") {
        return Some(NameMatchType::VeryHigh);
    }
    if special_compare(&n1, &n2, "with") {
        return Some(NameMatchType::VeryHigh);
    }

    if duo_special_compare(&n1, &n2, "feat", "explicit") {
        return Some(NameMatchType::High);
    }
    if duo_special_compare(&n1, &n2, "with", "explicit") {
        return Some(NameMatchType::High);
    }
    if single_special_compare(&n1, &n2, "feat") {
        return Some(NameMatchType::High);
    }
    if single_special_compare(&n1, &n2, "with") {
        return Some(NameMatchType::High);
    }

    if brackets_compare(&n1, &n2) {
        return Some(NameMatchType::Medium);
    }

    let n1_len = n1.chars().count();
    let n2_len = n2.chars().count();
    if n1_len == n2_len && n1_len > 0 {
        let count = chars_eq_at(&n1, &n2);
        if (count as f64 / n1_len as f64 >= 0.8 && n1_len >= 4)
            || (count as f64 / n1_len as f64 >= 0.5 && (2..=3).contains(&n1_len))
        {
            return Some(NameMatchType::High);
        }
    }

    let text_same = compute_text_same(&n1, &n2, true);
    if text_same > 90.0 {
        return Some(NameMatchType::VeryHigh);
    }
    if text_same > 80.0 {
        return Some(NameMatchType::High);
    }
    if text_same > 68.0 {
        return Some(NameMatchType::Medium);
    }
    if text_same > 55.0 {
        return Some(NameMatchType::Low);
    }

    Some(NameMatchType::NoMatch)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compare_duration_exact() {
        assert_eq!(
            compare_duration(Some(180000), Some(180000)),
            Some(DurationMatchType::Perfect)
        );
    }

    #[test]
    fn test_compare_duration_close() {
        assert_eq!(
            compare_duration(Some(180000), Some(180200)),
            Some(DurationMatchType::VeryHigh)
        );
    }

    #[test]
    fn test_compare_duration_none() {
        assert_eq!(compare_duration(None, Some(180000)), None);
    }

    #[test]
    fn test_compare_duration_zero() {
        assert_eq!(compare_duration(Some(0), Some(180000)), None);
    }

    #[test]
    fn test_compare_name_exact() {
        assert_eq!(
            compare_name(Some("Hello"), Some("Hello")),
            Some(NameMatchType::Perfect)
        );
    }

    #[test]
    fn test_compare_name_case_insensitive() {
        assert_eq!(
            compare_name(Some("Hello"), Some("hello")),
            Some(NameMatchType::Perfect)
        );
    }

    #[test]
    fn test_compare_name_different() {
        let result = compare_name(Some("Hello World"), Some("Something Completely Different"));
        assert_eq!(result, Some(NameMatchType::NoMatch));
    }

    #[test]
    fn test_compare_name_none() {
        assert_eq!(compare_name(None, Some("Hello")), None);
    }

    #[test]
    fn test_compare_name_blank() {
        assert_eq!(compare_name(Some("Hello"), Some(" \t ")), None);
    }

    #[test]
    fn test_compare_name_fullwidth_brackets() {
        assert_eq!(
            compare_name(Some("Song(Deluxe)"), Some("Song (Deluxe)")),
            Some(NameMatchType::Perfect)
        );
    }

    #[test]
    fn test_compare_artist_exact() {
        let a = vec!["Taylor Swift".to_string()];
        let b = vec!["Taylor Swift".to_string()];
        assert_eq!(compare_artist(&a, &b), Some(ArtistMatchType::Perfect));
    }

    #[test]
    fn test_compare_artist_empty() {
        let a: Vec<String> = vec![];
        let b = vec!["Taylor Swift".to_string()];
        assert_eq!(compare_artist(&a, &b), None);
    }

    #[test]
    fn test_compare_artist_subset() {
        let a = vec!["Taylor Swift".to_string(), "Ed Sheeran".to_string()];
        let b = vec!["Taylor Swift".to_string()];
        let result = compare_artist(&a, &b);
        // 1 out of 2 match
        assert!(result.is_some());
    }
}