|
| 1 | +// CSharpened and further development by (c) 2022 and onwards The vChewing Project (MIT License). |
| 2 | +// Was initially rebranded from (c) Lukhnos Liu's C++ library "Gramambular 2" (MIT License). |
| 3 | +// Walking algorithm (Dijkstra) implemented by (c) 2025 and onwards The vChewing Project (MIT License). |
| 4 | +// ==================== |
| 5 | +// This code is released under the MIT license (SPDX-License-Identifier: MIT) |
| 6 | + |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | + |
| 10 | +using static System.String; |
| 11 | +// ReSharper disable InconsistentNaming |
| 12 | + |
| 13 | +namespace Megrez.Tests { |
| 14 | + public class SimpleLM : LangModelProtocol { |
| 15 | + private Dictionary<string, List<Unigram>> _database = new(); |
| 16 | + public string separator { get; set; } |
| 17 | + public SimpleLM(string input, bool swapKeyValue = false, string separator = "") { |
| 18 | + this.separator = separator; |
| 19 | + this.ReConstruct(input, swapKeyValue, separator); |
| 20 | + } |
| 21 | + |
| 22 | + public void ReConstruct(string input, bool swapKeyValue = false, string? separator = null) { |
| 23 | + this.separator = separator ?? this.separator; |
| 24 | + List<string> sStream = new(input.Split('\n')); |
| 25 | + sStream.ForEach(line => { |
| 26 | + if (IsNullOrEmpty(line) || line.FirstOrDefault().CompareTo('#') == 0) |
| 27 | + return; |
| 28 | + List<string> lineStream = new(line.Split(' ')); |
| 29 | + if (lineStream.Count != 3) |
| 30 | + return; |
| 31 | + string col0 = lineStream[0]; // 假設其不為 nil |
| 32 | + string col1 = lineStream[1]; // 假設其不為 nil |
| 33 | + double col2 = 0; // 防呆 |
| 34 | + if (lineStream.Count >= 3 && double.TryParse(lineStream[2], out double number)) |
| 35 | + col2 = number; |
| 36 | + string key; |
| 37 | + string value; |
| 38 | + if (swapKeyValue) { |
| 39 | + key = col1; |
| 40 | + value = col0; |
| 41 | + } else { |
| 42 | + key = col0; |
| 43 | + value = col1; |
| 44 | + } |
| 45 | + Unigram u = new(value, col2); |
| 46 | + if (!_database.ContainsKey(key)) |
| 47 | + _database.Add(key, new()); |
| 48 | + _database[key].Add(u); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + public bool HasUnigramsFor(List<string> keyArray) => _database.ContainsKey(keyArray.Joined(separator: separator)); |
| 53 | + public List<Unigram> UnigramsFor(List<string> keyArray) => |
| 54 | + _database.ContainsKey(keyArray.Joined(separator: separator)) ? _database[keyArray.Joined(separator: separator)] |
| 55 | + : new(); |
| 56 | + public void Trim(string key, string value) { |
| 57 | + if (!_database.TryGetValue(key, out List<Unigram>? arr)) |
| 58 | + return; |
| 59 | + |
| 60 | + if (arr is not { } theArr) |
| 61 | + return; |
| 62 | + theArr = theArr.Where(x => x.Value != value).ToList(); |
| 63 | + if (theArr.IsEmpty()) { |
| 64 | + _database.Remove(key); |
| 65 | + return; |
| 66 | + } |
| 67 | + _database[key] = theArr; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public class MockLM : LangModelProtocol { |
| 72 | + public bool HasUnigramsFor(List<string> keyArray) => !IsNullOrEmpty(keyArray.Joined()); |
| 73 | + public List<Unigram> UnigramsFor(List<string> keyArray) => new() { new(value: keyArray.Joined(), score: -1) }; |
| 74 | + } |
| 75 | +} |
0 commit comments