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
// 简单分类器
// KNN、模板匹配、距离度量、分类结果

///|
/// 分类结果结构体
pub(all) struct ClassificationResult {
  label : String
  confidence : Double
  top_k : Array[(String, Double)]
}

///|
/// 训练样本结构体
pub(all) struct TrainingSample {
  features : Array[Double]
  label : String
}

///|
/// KNN 分类器
pub(all) struct KnnClassifier {
  samples : Array[TrainingSample]
  k : Int
  distance_metric : String
}

///|
/// 模板匹配分类器
pub(all) struct TemplateMatcher {
  templates : Array[(String, Array[Double])]
  distance_metric : String
}

///|
/// 距离度量类型
pub(all) enum DistanceMetric {
  Euclidean
  Manhattan
  Cosine
  Chebyshev
} derive(Eq)

///|
/// 计算欧氏距离
pub fn euclidean_distance(
  a : Array[Double],
  b : Array[Double],
) -> Double raise @types.LoadError {
  // 输入验证
  if a.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "euclidean_distance: 向量a不能为空",
    )
  }
  if b.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "euclidean_distance: 向量b不能为空",
    )
  }

  let n = if a.length() < b.length() { a.length() } else { b.length() }
  let mut sum_sq = 0.0
  for i in 0..<n {
    let diff = a[i] - b[i]
    sum_sq += diff * diff
  }
  sum_sq.sqrt()
}

///|
/// 计算曼哈顿距离
pub fn manhattan_distance(
  a : Array[Double],
  b : Array[Double],
) -> Double raise @types.LoadError {
  // 输入验证
  if a.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "manhattan_distance: 向量a不能为空",
    )
  }
  if b.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "manhattan_distance: 向量b不能为空",
    )
  }

  let n = if a.length() < b.length() { a.length() } else { b.length() }
  let mut sum = 0.0
  for i in 0..<n {
    sum += (a[i] - b[i]).abs()
  }
  sum
}

///|
/// 计算余弦相似度(返回距离 = 1 - 相似度)
pub fn cosine_distance(
  a : Array[Double],
  b : Array[Double],
) -> Double raise @types.LoadError {
  // 输入验证
  if a.length() == 0 {
    raise @types.LoadError::DecodeFailed("cosine_distance: 向量a不能为空")
  }
  if b.length() == 0 {
    raise @types.LoadError::DecodeFailed("cosine_distance: 向量b不能为空")
  }

  let n = if a.length() < b.length() { a.length() } else { b.length() }
  let mut dot = 0.0
  let mut norm_a = 0.0
  let mut norm_b = 0.0
  for i in 0..<n {
    dot += a[i] * b[i]
    norm_a += a[i] * a[i]
    norm_b += b[i] * b[i]
  }
  let denom = norm_a.sqrt() * norm_b.sqrt()
  if denom > 0.0 {
    1.0 - dot / denom
  } else {
    1.0
  }
}

///|
/// 计算切比雪夫距离
pub fn chebyshev_distance(
  a : Array[Double],
  b : Array[Double],
) -> Double raise @types.LoadError {
  // 输入验证
  if a.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "chebyshev_distance: 向量a不能为空",
    )
  }
  if b.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "chebyshev_distance: 向量b不能为空",
    )
  }

  let n = if a.length() < b.length() { a.length() } else { b.length() }
  let mut max_diff = 0.0
  for i in 0..<n {
    let diff = (a[i] - b[i]).abs()
    if diff > max_diff {
      max_diff = diff
    }
  }
  max_diff
}

///|
/// 计算距离(根据度量类型)
pub fn compute_distance(
  a : Array[Double],
  b : Array[Double],
  metric : DistanceMetric,
) -> Double raise @types.LoadError {
  // 输入验证
  if a.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "compute_distance: 向量a不能为空",
    )
  }
  if b.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "compute_distance: 向量b不能为空",
    )
  }

  match metric {
    DistanceMetric::Euclidean => euclidean_distance(a, b)
    DistanceMetric::Manhattan => manhattan_distance(a, b)
    DistanceMetric::Cosine => cosine_distance(a, b)
    DistanceMetric::Chebyshev => chebyshev_distance(a, b)
  }
}

///|
/// 创建 KNN 分类器
pub fn knn_create(
  k? : Int = 3,
  distance_metric? : String = "euclidean",
) -> KnnClassifier raise @types.LoadError {
  // 输入验证
  if k <= 0 {
    raise @types.LoadError::DecodeFailed("knn_create: k必须大于0")
  }

  KnnClassifier::{ samples: [], k, distance_metric }
}

///|
/// 添加训练样本
pub fn knn_add_sample(
  classifier : KnnClassifier,
  features : Array[Double],
  label : String,
) -> KnnClassifier raise @types.LoadError {
  // 输入验证
  if features.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "knn_add_sample: 特征向量不能为空",
    )
  }
  if label.length() == 0 {
    raise @types.LoadError::DecodeFailed("knn_add_sample: 标签不能为空")
  }

  let new_samples = classifier.samples
  new_samples.push(TrainingSample::{ features, label })
  KnnClassifier::{
    samples: new_samples,
    k: classifier.k,
    distance_metric: classifier.distance_metric,
  }
}

///|
/// 批量添加训练样本
pub fn knn_add_samples(
  classifier : KnnClassifier,
  samples : Array[(Array[Double], String)],
) -> KnnClassifier raise @types.LoadError {
  // 输入验证
  if samples.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "knn_add_samples: 样本列表不能为空",
    )
  }

  let new_samples = classifier.samples
  for _tup0 in samples {
    let (features, label) = _tup0
    new_samples.push(TrainingSample::{ features, label })
  }
  KnnClassifier::{
    samples: new_samples,
    k: classifier.k,
    distance_metric: classifier.distance_metric,
  }
}

///|
/// KNN 预测
pub fn knn_predict(
  classifier : KnnClassifier,
  features : Array[Double],
) -> ClassificationResult raise @types.LoadError {
  // 输入验证
  if features.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "knn_predict: 特征向量不能为空",
    )
  }
  if classifier.samples.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "knn_predict: 分类器没有训练样本",
    )
  }

  let metric = match classifier.distance_metric {
    "manhattan" => DistanceMetric::Manhattan
    "cosine" => DistanceMetric::Cosine
    "chebyshev" => DistanceMetric::Chebyshev
    _ => DistanceMetric::Euclidean
  }
  // 计算所有样本的距离
  let distances : Array[(Double, String)] = []
  for sample in classifier.samples {
    let dist = compute_distance(features, sample.features, metric)
    distances.push((dist, sample.label))
  }
  // 按距离排序(冒泡排序,简单实现)
  for _ in 0..<distances.length() {
    for j in 0..<(distances.length() - 1) {
      if distances[j].0 > distances[j + 1].0 {
        let temp = distances[j]
        distances[j] = distances[j + 1]
        distances[j + 1] = temp
      }
    }
  }
  // 取前 k 个最近邻
  let k = if classifier.k < distances.length() {
    classifier.k
  } else {
    distances.length()
  }
  let top_k : Array[(String, Double)] = []
  let label_votes : Array[(String, Int)] = []
  for i in 0..<k {
    let (dist, label) = distances[i]
    let confidence = if dist > 0.0 { 1.0 / (1.0 + dist) } else { 1.0 }
    top_k.push((label, confidence))
    // 投票
    let mut found = false
    for j in 0..<label_votes.length() {
      if label_votes[j].0 == label {
        label_votes[j] = (label, label_votes[j].1 + 1)
        found = true
      }
    }
    if !found {
      label_votes.push((label, 1))
    }
  }
  // 找得票最多的标签
  let mut best_label = ""
  let mut best_votes = 0
  for _tup1 in label_votes {
    let (label, votes) = _tup1
    if votes > best_votes {
      best_votes = votes
      best_label = label
    }
  }
  let confidence = if k > 0 {
    best_votes.to_double() / k.to_double()
  } else {
    0.0
  }
  ClassificationResult::{ label: best_label, confidence, top_k }
}

///|
/// 创建模板匹配分类器
pub fn template_matcher_create(
  distance_metric? : String = "euclidean",
) -> TemplateMatcher {
  // 输入验证:distance_metric 不做强制验证,使用默认值兜底
  TemplateMatcher::{ templates: [], distance_metric }
}

///|
/// 添加模板
pub fn template_matcher_add(
  matcher : TemplateMatcher,
  label : String,
  template : Array[Double],
) -> TemplateMatcher raise @types.LoadError {
  // 输入验证
  if template.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "template_matcher_add: 模板特征向量不能为空",
    )
  }
  if label.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "template_matcher_add: 标签不能为空",
    )
  }

  let new_templates = matcher.templates
  new_templates.push((label, template))
  TemplateMatcher::{
    templates: new_templates,
    distance_metric: matcher.distance_metric,
  }
}

///|
/// 模板匹配预测
pub fn template_matcher_predict(
  matcher : TemplateMatcher,
  features : Array[Double],
) -> ClassificationResult raise @types.LoadError {
  // 输入验证
  if features.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "template_matcher_predict: 特征向量不能为空",
    )
  }
  if matcher.templates.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "template_matcher_predict: 模板匹配器没有模板",
    )
  }

  let metric = match matcher.distance_metric {
    "manhattan" => DistanceMetric::Manhattan
    "cosine" => DistanceMetric::Cosine
    "chebyshev" => DistanceMetric::Chebyshev
    _ => DistanceMetric::Euclidean
  }
  // 计算与所有模板的距离
  let distances : Array[(Double, String)] = []
  for _tup2 in matcher.templates {
    let (label, template) = _tup2
    let dist = compute_distance(features, template, metric)
    distances.push((dist, label))
  }
  // 按距离排序
  for _ in 0..<distances.length() {
    for j in 0..<(distances.length() - 1) {
      if distances[j].0 > distances[j + 1].0 {
        let temp = distances[j]
        distances[j] = distances[j + 1]
        distances[j + 1] = temp
      }
    }
  }
  // 返回最佳匹配
  let top_k : Array[(String, Double)] = []
  let n = if 5 < distances.length() { 5 } else { distances.length() }
  for i in 0..<n {
    let (dist, label) = distances[i]
    let confidence = if dist > 0.0 { 1.0 / (1.0 + dist) } else { 1.0 }
    top_k.push((label, confidence))
  }
  if distances.length() > 0 {
    let (best_dist, best_label) = distances[0]
    let confidence = if best_dist > 0.0 { 1.0 / (1.0 + best_dist) } else { 1.0 }
    ClassificationResult::{ label: best_label, confidence, top_k }
  } else {
    ClassificationResult::{ label: "", confidence: 0.0, top_k: [] }
  }
}

///|
/// 特征归一化(Min-Max 归一化到 [0, 1])
pub fn normalize_features(
  features : Array[Double],
) -> Array[Double] raise @types.LoadError {
  // 输入验证
  if features.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "normalize_features: 特征向量不能为空",
    )
  }
  let mut min_val = features[0]
  let mut max_val = features[0]
  for v in features {
    if v < min_val {
      min_val = v
    }
    if v > max_val {
      max_val = v
    }
  }
  let range = max_val - min_val
  let normalized : Array[Double] = []
  if range > 0.0 {
    for v in features {
      normalized.push((v - min_val) / range)
    }
  } else {
    for _ in features {
      normalized.push(0.0)
    }
  }
  normalized
}

///|
/// 特征标准化(Z-score 标准化,均值0,标准差1)
pub fn standardize_features(
  features : Array[Double],
) -> Array[Double] raise @types.LoadError {
  // 输入验证
  if features.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "standardize_features: 特征向量不能为空",
    )
  }
  // 计算均值
  let mut sum = 0.0
  for v in features {
    sum += v
  }
  let mean = sum / features.length().to_double()
  // 计算标准差
  let mut sum_sq = 0.0
  for v in features {
    let diff = v - mean
    sum_sq += diff * diff
  }
  let std = if sum_sq > 0.0 {
    (sum_sq / features.length().to_double()).sqrt()
  } else {
    1.0
  }
  // 标准化
  let standardized : Array[Double] = []
  for v in features {
    standardized.push((v - mean) / std)
  }
  standardized
}