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
// OCR 后处理
// 词典匹配、N-gram语言模型、拼写纠错、置信度校准

///|
/// 后处理配置
pub(all) struct PostProcessConfig {
  use_dictionary : Bool
  use_ngram : Bool
  ngram_size : Int
  min_confidence : Double
  max_candidates : Int
}

///|
/// 创建默认后处理配置
pub fn postprocess_default_config() -> PostProcessConfig {
  PostProcessConfig::{
    use_dictionary: true,
    use_ngram: true,
    ngram_size: 2,
    min_confidence: 0.3,
    max_candidates: 5,
  }
}

///|
/// 词典结构体
pub(all) struct Dictionary {
  words : Array[String]
  word_set : Array[String] // 用于快速查找(简化版)
}

///|
/// 创建词典
pub fn dictionary_create(
  words : Array[String],
) -> Dictionary raise @types.LoadError {
  // 输入验证
  if words.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "dictionary_create: 单词列表不能为空",
    )
  }

  Dictionary::{ words, word_set: words }
}

///|
/// 创建常用英文单词词典
pub fn dictionary_create_common_english() -> Dictionary {
  let words : Array[String] = [
    "the", "be", "to", "of", "and", "a", "in", "that", "have", "I", "it", "for",
    "not", "on", "with", "he", "as", "you", "do", "at", "this", "but", "his", "by",
    "from", "they", "we", "say", "her", "she", "or", "an", "will", "my", "one", "all",
    "would", "there", "their", "what", "so", "up", "out", "if", "about", "who", "get",
    "which", "go", "me", "when", "make", "can", "like", "time", "no", "just", "him",
    "know", "take", "people", "into", "year", "your", "good", "some", "could", "them",
    "see", "other", "than", "then", "now", "look", "only", "come", "its", "over",
    "think", "also", "back", "after", "use", "two", "how", "our", "work", "first",
    "well", "way", "even", "new", "want", "because", "any", "these", "give", "day",
    "most", "us", "hello", "world", "image", "text", "ocr", "digit", "number", "letter",
    "word", "line", "page", "document", "scan", "read", "write", "print", "font",
    "size", "color", "black", "white", "gray", "bright", "dark", "contrast", "sharp",
    "blur", "noise", "filter", "zero", "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine", "ten", "hundred", "thousand", "million", "billion",
    "yes", "no", "true", "false", "ok", "okay", "error", "warning", "info", "debug",
    "start", "stop", "begin", "end", "open", "close", "save", "load", "delete", "copy",
    "paste", "cut", "undo", "redo", "find", "replace", "search", "result", "output",
    "input", "data", "file", "folder", "directory", "path", "name", "value", "key",
    "code", "program", "software", "hardware", "system", "network", "server", "client",
    "user", "admin", "password", "login", "logout", "register", "account", "profile",
    "setting", "option", "config", "parameter", "argument", "variable", "function",
    "class", "object", "method_type", "property", "attribute", "field", "element",
    "node", "tree", "list", "array", "map", "set", "queue", "stack", "heap", "graph",
    "string", "integer", "float", "double", "boolean", "char", "byte", "bit", "pixel",
    "image", "width", "height", "channel", "format", "encode", "decode", "compress",
    "decompress", "encrypt", "decrypt", "hash", "checksum", "signature", "test",
    "test1", "test2", "abc", "abcd", "xyz", "foo", "bar", "baz",
  ]
  Dictionary::{ words, word_set: words }
}

///|
/// 计算编辑距离(Levenshtein距离)
pub fn edit_distance(s1 : String, s2 : String) -> Int raise @types.LoadError {
  // 输入验证
  if s1.length() == 0 && s2.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "edit_distance: 两个字符串不能同时为空",
    )
  }

  let len1 = s1.length()
  let len2 = s2.length()
  if len1 == 0 {
    return len2
  }
  if len2 == 0 {
    return len1
  }
  // 创建动态规划表
  let dp : Array[Int] = Array::make((len1 + 1) * (len2 + 1), 0)
  // 初始化
  for i in 0..<(len1 + 1) {
    dp[i * (len2 + 1)] = i
  }
  for j in 0..<(len2 + 1) {
    dp[j] = j
  }
  // 填充DP表
  for i in 1..<(len1 + 1) {
    for j in 1..<(len2 + 1) {
      let cost = if s1[i - 1] == s2[j - 1] { 0 } else { 1 }
      let insert = dp[i * (len2 + 1) + (j - 1)] + 1
      let delete = dp[(i - 1) * (len2 + 1) + j] + 1
      let substitute = dp[(i - 1) * (len2 + 1) + (j - 1)] + cost
      let min1 = if insert < delete { insert } else { delete }
      dp[i * (len2 + 1) + j] = if min1 < substitute { min1 } else { substitute }
    }
  }
  dp[len1 * (len2 + 1) + len2]
}

///|
/// 词典匹配:查找最相似的单词
pub fn dictionary_match(
  word : String,
  dictionary : Dictionary,
  max_distance? : Int = 2,
) -> Array[(String, Int)] raise @types.LoadError {
  // 输入验证
  if word.length() == 0 {
    raise @types.LoadError::DecodeFailed("dictionary_match: 单词不能为空")
  }
  if dictionary.words.length() == 0 {
    raise @types.LoadError::DecodeFailed("dictionary_match: 词典不能为空")
  }
  if max_distance < 0 {
    raise @types.LoadError::DecodeFailed(
      "dictionary_match: max_distance不能为负数",
    )
  }

  let results : Array[(String, Int)] = []
  for dict_word in dictionary.words {
    let dist = edit_distance(word, dict_word)
    if dist <= max_distance {
      results.push((dict_word, dist))
    }
  }
  // 按距离排序
  for _ in 0..<results.length() {
    for j in 0..<(results.length() - 1) {
      if results[j].1 > results[j + 1].1 {
        let temp = results[j]
        results[j] = results[j + 1]
        results[j + 1] = temp
      }
    }
  }
  results
}

///|
/// N-gram 语言模型
pub(all) struct NgramModel {
  n : Int
  bigram_counts : Array[(String, Int)] // 简化版:存储bigram和计数
  unigram_counts : Array[(String, Int)]
}

///|
/// 创建 N-gram 模型(从文本训练)
pub fn ngram_create(
  texts : Array[String],
  n? : Int = 2,
) -> NgramModel raise @types.LoadError {
  // 输入验证
  if texts.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "ngram_create: 文本列表不能为空",
    )
  }
  if n < 1 {
    raise @types.LoadError::DecodeFailed("ngram_create: n必须大于等于1")
  }

  let bigram_counts : Array[(String, Int)] = []
  let unigram_counts : Array[(String, Int)] = []
  for text in texts {
    // 简化:按字符处理
    for i in 0..<(text.length() - 1) {
      let bigram = text[i].to_string() + text[i + 1].to_string()
      // 查找是否已存在
      let mut found = false
      for j in 0..<bigram_counts.length() {
        if bigram_counts[j].0 == bigram {
          bigram_counts[j] = (bigram, bigram_counts[j].1 + 1)
          found = true
        }
      }
      if !found {
        bigram_counts.push((bigram, 1))
      }
    }
    // unigram
    for i in 0..<text.length() {
      let unigram = text[i].to_string()
      let mut found = false
      for j in 0..<unigram_counts.length() {
        if unigram_counts[j].0 == unigram {
          unigram_counts[j] = (unigram, unigram_counts[j].1 + 1)
          found = true
        }
      }
      if !found {
        unigram_counts.push((unigram, 1))
      }
    }
  }
  NgramModel::{ n, bigram_counts, unigram_counts }
}

///|
/// 计算文本的 N-gram 概率(简化版)
pub fn ngram_score(
  text : String,
  model : NgramModel,
) -> Double raise @types.LoadError {
  // 输入验证
  if text.length() == 0 {
    raise @types.LoadError::DecodeFailed("ngram_score: 文本不能为空")
  }
  if model.bigram_counts.length() == 0 && model.unigram_counts.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "ngram_score: N-gram模型不能为空",
    )
  }

  if text.length() < 2 {
    return 0.0
  }
  let mut total_score = 0.0
  let mut count = 0
  for i in 0..<(text.length() - 1) {
    let bigram = text[i].to_string() + text[i + 1].to_string()
    // 查找bigram概率
    let mut bigram_count = 0
    let mut unigram_count = 0
    for j in 0..<model.bigram_counts.length() {
      if model.bigram_counts[j].0 == bigram {
        bigram_count = model.bigram_counts[j].1
      }
    }
    let first_char = text[i].to_string()
    for j in 0..<model.unigram_counts.length() {
      if model.unigram_counts[j].0 == first_char {
        unigram_count = model.unigram_counts[j].1
      }
    }
    if unigram_count > 0 {
      total_score += bigram_count.to_double() / unigram_count.to_double()
      count += 1
    }
  }
  if count > 0 {
    total_score / count.to_double()
  } else {
    0.0
  }
}

///|
/// 完整后处理流水线
pub fn ocr_postprocess(
  result : OcrResult,
  config? : PostProcessConfig,
  dictionary? : Dictionary,
  ngram_model? : NgramModel,
) -> OcrResult raise @types.LoadError {
  ignore(ngram_model)
  // 输入验证
  if result.characters.length() == 0 && result.text.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "ocr_postprocess: OCR结果不能为空",
    )
  }

  let cfg = match config {
    Some(c) => c
    None => postprocess_default_config()
  }
  let dict = match dictionary {
    Some(d) => d
    None => dictionary_create_common_english()
  }
  // 1. 低置信度字符标记
  let mut corrected_text = ""
  let mut total_confidence = 0.0
  let corrected_chars : Array[OcrCharResult] = []
  for char_result in result.characters {
    let mut corrected_char = char_result.char
    let mut confidence = char_result.confidence
    // 2. 词典匹配(对低置信度字符)
    if cfg.use_dictionary && confidence < cfg.min_confidence {
      // 简化:只对单个字符进行常见纠错
      let corrections = common_char_corrections(char_result.char)
      if corrections.length() > 0 {
        corrected_char = corrections[0]
        confidence = confidence + 0.1
      }
    }
    corrected_text += corrected_char
    total_confidence += confidence
    corrected_chars.push(OcrCharResult::{
      char: corrected_char,
      confidence,
      x: char_result.x,
      y: char_result.y,
      width: char_result.width,
      height: char_result.height,
    })
  }
  // 3. 词典匹配(对整个文本中的单词)
  if cfg.use_dictionary {
    let words = split_into_words(corrected_text)
    let mut corrected_words = ""
    for word in words {
      if word.length() >= 2 {
        let matches = dictionary_match(word, dict, max_distance=1)
        if matches.length() > 0 && matches[0].1 > 0 {
          corrected_words += matches[0].0
        } else {
          corrected_words += word
        }
      } else {
        corrected_words += word
      }
    }
    corrected_text = corrected_words
  }
  let avg_confidence = if corrected_chars.length() > 0 {
    total_confidence / corrected_chars.length().to_double()
  } else {
    0.0
  }
  OcrResult::{
    text: corrected_text,
    confidence: avg_confidence,
    characters: corrected_chars,
  }
}

///|
/// 常见字符纠错映射
fn common_char_corrections(char : String) -> Array[String] {
  let corrections : Array[String] = []
  // OCR常见混淆字符对
  match char {
    "0" => corrections.push("O")
    "O" => corrections.push("0")
    "1" => corrections.push("I")
    "I" => corrections.push("1")
    "l" => corrections.push("1")
    "2" => corrections.push("Z")
    "Z" => corrections.push("2")
    "5" => corrections.push("S")
    "S" => corrections.push("5")
    "8" => corrections.push("B")
    "B" => corrections.push("8")
    "6" => corrections.push("G")
    "G" => corrections.push("6")
    "9" => corrections.push("g")
    "g" => corrections.push("9")
    _ => ignore(())
  }
  corrections
}

///|
/// 将文本分割成单词(简化版)
fn split_into_words(text : String) -> Array[String] {
  let words : Array[String] = []
  let mut current_word = ""
  for i in 0..<text.length() {
    let c = text[i]
    if c == ' ' || c == '\n' || c == '\t' {
      if current_word.length() > 0 {
        words.push(current_word)
        current_word = ""
      }
      words.push(c.to_string())
    } else {
      current_word += c.to_string()
    }
  }
  if current_word.length() > 0 {
    words.push(current_word)
  }
  words
}

///|
/// 置信度校准
pub fn calibrate_confidence(
  result : OcrResult,
  factor? : Double = 1.0,
) -> OcrResult raise @types.LoadError {
  // 输入验证
  if result.characters.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "calibrate_confidence: OCR结果字符列表不能为空",
    )
  }
  if factor <= 0.0 {
    raise @types.LoadError::DecodeFailed(
      "calibrate_confidence: factor必须大于0",
    )
  }

  let calibrated_chars : Array[OcrCharResult] = []
  let mut total = 0.0
  for char_result in result.characters {
    let new_confidence = if char_result.confidence * factor > 1.0 {
      1.0
    } else {
      char_result.confidence * factor
    }
    total += new_confidence
    calibrated_chars.push(OcrCharResult::{
      char: char_result.char,
      confidence: new_confidence,
      x: char_result.x,
      y: char_result.y,
      width: char_result.width,
      height: char_result.height,
    })
  }
  let avg = if calibrated_chars.length() > 0 {
    total / calibrated_chars.length().to_double()
  } else {
    0.0
  }
  OcrResult::{
    text: result.text,
    confidence: avg,
    characters: calibrated_chars,
  }
}