Skip to content

Commit

Permalink
更新v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
GarthTB committed Nov 19, 2024
1 parent 9896f70 commit 4781e3c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CodeLord/CodeLord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ApplicationIcon>Icon\favicon.ico</ApplicationIcon>
<Product>Codelord</Product>
<Title>Codelord</Title>
<Version>0.0.2</Version>
<Version>0.0.3</Version>
<Company>Garth</Company>
<Authors>Garth</Authors>
<Copyright>© Garth 2024</Copyright>
Expand Down
4 changes: 2 additions & 2 deletions CodeLord/Components/Concater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ private static string JD(string head, string tail)
var a = "abcdefghijklmnopqrstuvwxyz"; // 所有码元
var oldEnd = head[^1];
var newStart = tail[0];
if (head.Length < 4 && y.Contains(head[^1])) // 不足4码且以音码结尾,后补空格
head = $"{head} ";
if (!a.Contains(newStart) && oldEnd == ' ') // 标点开头且前为空格,直接替换空格
return $"{head[..^1]}{tail}";
if (tail.Length < 4 && y.Contains(tail[^1])) // 不足4码且以音码结尾,后补空格
tail = $"{tail} ";
if (x.Contains(newStart) && a.Contains(oldEnd)) // 形码开头且无标点断开,前加空格
tail = $" {tail}";
return $"{head}{tail}";
Expand Down
20 changes: 12 additions & 8 deletions CodeLord/Components/Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ private static string[] FindShortest(ConcurrentDictionary<string, List<string>>
{
Console.WriteLine($"共需遍历{text.Length}字,正在遍历...");
Dictionary<int, HashSet<string>> tempWays = new(text.Length) { [0] = [""] }; // 键为末尾位置,值为编码集合
var maxLen = dict.Keys.Max(x => x.Length); // 词库中的最大字数
var slices = Enumerable.Range(0, text.Length)
.Select(i => new string(text.Skip(i).Take(maxLen).ToArray()))
.ToArray()
.AsSpan(); // 预先切片以提升性能

for (int i = 0; i < text.Length; i++)
{
var heads = tempWays[i];
var minLen = heads.Min(x => x.Length);
var trimHeads = heads.Where(x => x.Length == minLen).Take(limit);
var tails = FindBranches(dict, text, i);
foreach (var head in trimHeads)
var heads = tempWays[i].OrderBy(x => x.Length)
.Take(limit); // 最短路径
var tails = FindBranches(dict, slices[i]);
foreach (var head in heads)
foreach (var (length, codes) in tails) // code无重复项
foreach (var code in codes)
RecordWays(i + length, Concater.Join(codeID, head, code), tempWays);
Expand All @@ -53,12 +57,12 @@ private static string[] FindShortest(ConcurrentDictionary<string, List<string>>
Console.WriteLine($"\n遍历完成,共得到{ways.Length}种最短编码。");
return ways;

static (int, List<string>)[] FindBranches(ConcurrentDictionary<string, List<string>> dict, string text, int i)
static (int, List<string>)[] FindBranches(ConcurrentDictionary<string, List<string>> dict, string text)
{
var branches = dict.Where(x => text[i..].StartsWith(x.Key))
var branches = dict.Where(x => text.StartsWith(x.Key))
.Select(x => (x.Key.Length, x.Value)) // Value无重复项
.ToArray();
return branches.Length == 0 ? [(1, [text.Substring(i, 1)])] : branches;
return branches.Length == 0 ? [(1, [text[0..1]])] : branches;
}

static void RecordWays(int endIndex, string code, Dictionary<int, HashSet<string>> tempWays)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@

## 更新日志

### [0.0.3] - 20241120

- 改善长文本编码性能
- 完善键道规则

### [0.0.2] - 20241119

- 改为流式计算,以避免卡顿并减少内存消耗
Expand Down

0 comments on commit 4781e3c

Please sign in to comment.