Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hard copy support. #18

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Megrez.Tests/Megrez.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<ReleaseVersion>2.5.4</ReleaseVersion>
<ReleaseVersion>2.6.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
33 changes: 23 additions & 10 deletions Megrez.Tests/MegrezTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public class MegrezTests : TestDataClass {
public void Test01_SpanUnitInternalAbilities() {
SimpleLM langModel = new(input: StrSampleData);
Compositor.SpanUnit span = new();
Compositor.Node n1 =
new(keyArray: new() { "gao" }, spanLength: 1, unigrams: langModel.UnigramsFor(new() { "gao1" }));
Compositor.Node n3 = new(keyArray: new() { "gao1", "ke1", "ji4" }, spanLength: 3,
unigrams: langModel.UnigramsFor(new() { "gao1ke1ji4" }));
Node n1 = new(keyArray: new() { "gao" }, spanLength: 1, unigrams: langModel.UnigramsFor(new() { "gao1" }));
Node n3 = new(keyArray: new() { "gao1", "ke1", "ji4" }, spanLength: 3,
unigrams: langModel.UnigramsFor(new() { "gao1ke1ji4" }));
Assert.AreEqual(actual: span.MaxLength, expected: 0);
span.Append(node: n1);
Assert.AreEqual(actual: span.MaxLength, expected: 1);
Expand All @@ -43,7 +42,7 @@ public void Test01_SpanUnitInternalAbilities() {
span.DropNodesOfOrBeyond(length: 1);
Assert.AreEqual(actual: span.MaxLength, expected: 0);
Assert.AreEqual(actual: span.NodeOf(length: 1), expected: null);
Compositor.Node n114514 = new(new(), 114_514, new());
Node n114514 = new(new(), 114_514, new());
Assert.IsFalse(span.Append(n114514));
Assert.IsNull(span.NodeOf(length: 0));
Assert.IsNull(span.NodeOf(length: Compositor.MaxSpanLength + 1));
Expand Down Expand Up @@ -364,7 +363,7 @@ public void Test15_Compositor_InputTestAndCursorJump() {
compositor.InsertKey("jiang3");
compositor.Walk();
compositor.InsertKey("jin1");
List<Compositor.Node> result = compositor.Walk().WalkedNodes;
List<Node> result = compositor.Walk().WalkedNodes;
Assert.AreEqual(actual: result.Values(), expected: new List<string> { "高科技", "公司", "的", "年中", "獎金" });
Assert.AreEqual(actual: compositor.Length, expected: 10);
compositor.Cursor = 7;
Expand Down Expand Up @@ -418,7 +417,7 @@ public void Test16_Compositor_InputTest2() {
compositor.InsertKey("gao1");
compositor.InsertKey("ke1");
compositor.InsertKey("ji4");
List<Compositor.Node> result = compositor.Walk().WalkedNodes;
List<Node> result = compositor.Walk().WalkedNodes;
Assert.AreEqual(actual: result.Values(), expected: new List<string> { "高科技" });
compositor.InsertKey("gong1");
compositor.InsertKey("si1");
Expand All @@ -432,7 +431,7 @@ public void Test17_Compositor_OverrideOverlappingNodes() {
compositor.InsertKey("gao1");
compositor.InsertKey("ke1");
compositor.InsertKey("ji4");
List<Compositor.Node> result = compositor.Walk().WalkedNodes;
List<Node> result = compositor.Walk().WalkedNodes;
Assert.AreEqual(actual: result.Values(), expected: new List<string> { "高科技" });
compositor.Cursor = 0;
Assert.IsTrue(compositor.OverrideCandidateLiteral("膏", location: compositor.Cursor));
Expand Down Expand Up @@ -466,7 +465,7 @@ public void Test18_Compositor_OverrideReset() {
compositor.InsertKey("zhong1");
compositor.InsertKey("jiang3");
compositor.InsertKey("jin1");
List<Compositor.Node> result = compositor.Walk().WalkedNodes;
List<Node> result = compositor.Walk().WalkedNodes;
Assert.AreEqual(actual: result.Values(), expected: new List<string> { "年中", "獎金" });

Assert.IsTrue(compositor.OverrideCandidateLiteral("終講", location: 1));
Expand All @@ -491,7 +490,7 @@ public void Test19_Compositor_CandidateDisambiguation() {
compositor.InsertKey("yan4");
compositor.InsertKey("wei2");
compositor.InsertKey("xian3");
List<Compositor.Node>? result = compositor.Walk().WalkedNodes;
List<Node>? result = compositor.Walk().WalkedNodes;
Assert.AreEqual(actual: result.Values(), expected: new List<string> { "高熱", "火焰", "危險" });

Assert.IsTrue(compositor.OverrideCandidate(new(keyArray: new() { "huo3" }, value: "🔥"), location: 2));
Expand Down Expand Up @@ -525,4 +524,18 @@ public void Test20_Compositor_updateUnigramData() {
string newResult2 = compositor.Walk().WalkedNodes.Values().Joined(separator: ",");
Assert.AreEqual(actual: newResult2, expected: "年,中");
}

[Test]
public void Test21_Compositor_hardCopy() {
SimpleLM theLM = new(input: StrSampleData);
string rawReadings = "gao1 ke1 ji4 gong1 si1 de5 nian2 zhong1 jiang3 jin1";
Compositor compositorA = new(langModel: theLM, separator: "");
foreach (string key in rawReadings.Split(separator: " ")) {
compositorA.InsertKey(key);
}
Compositor compositorB = compositorA.HardCopy();
List<Node> resultA = compositorA.Walk().WalkedNodes;
List<Node> resultB = compositorB.Walk().WalkedNodes;
Assert.True(resultA.SequenceEqual(resultB));
}
}
2 changes: 1 addition & 1 deletion Megrez.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ Global
$0.DotNetNamingPolicy = $4
$4.DirectoryNamespaceAssociation = PrefixedHierarchical
$0.StandardHeader = $5
version = 2.5.4
version = 2.6.0
EndGlobalSection
EndGlobal
8 changes: 4 additions & 4 deletions Megrez/Megrez.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ReleaseVersion>2.5.4</ReleaseVersion>
<ReleaseVersion>2.6.0</ReleaseVersion>
<PackageId>vChewing.Megrez</PackageId>
<Authors>Shiki Suen</Authors>
<Company>Atelier Inmu</Company>
<Copyright>(c) 2022 and onwards The vChewing Project for Megrez-specific changes; (c) 2022 and onwards Lukhnos Liu for upstream contents.</Copyright>
<RepositoryUrl>https://github.com/ShikiSuen/MegrezNT</RepositoryUrl>
<NeutralLanguage>zh-TW</NeutralLanguage>
<AssemblyVersion>2.5.4</AssemblyVersion>
<FileVersion>2.5.4</FileVersion>
<Version>2.5.4</Version>
<AssemblyVersion>2.6.0</AssemblyVersion>
<FileVersion>2.6.0</FileVersion>
<Version>2.6.0</Version>
<Product>Megrez</Product>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
31 changes: 31 additions & 0 deletions Megrez/src/1_Compositor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,37 @@ public Compositor(LangModelProtocol langModel, string separator = "-") {
Spans = new();
}

/// <summary>
/// 以指定組字器生成拷貝。
/// </summary>
/// <remarks>
/// 因為 Node 不是 Struct,所以會在 Compositor 被拷貝的時候無法被真實複製。
/// 這樣一來,Compositor 複製品當中的 Node 的變化會被反應到原先的 Compositor 身上。
/// 這在某些情況下會造成意料之外的混亂情況,所以需要引入一個拷貝用的建構子。
/// </remarks>
/// <param name="compositor"></param>
public Compositor(Compositor compositor) {
_theLangModel = compositor.TheLangModel;
_cursor = compositor.Cursor;
_marker = compositor.Marker;
Keys = compositor.Keys;
Spans = new();
Separator = compositor.Separator;
foreach (Node walkedNode in compositor.WalkedNodes) WalkedNodes.Add(walkedNode.Copy());
foreach (SpanUnit span in compositor.Spans) Spans.Add(span.HardCopy());
}

/// <summary>
/// 生成自身的拷貝。
/// </summary>
/// <remarks>
/// 因為 Node 不是 Struct,所以會在 Compositor 被拷貝的時候無法被真實複製。
/// 這樣一來,Compositor 複製品當中的 Node 的變化會被反應到原先的 Compositor 身上。
/// 這在某些情況下會造成意料之外的混亂情況,所以需要引入一個拷貝用的建構子。
/// </remarks>
/// <returns>拷貝。</returns>
public Compositor HardCopy() => new(compositor: this);

/// <summary>
/// 重置包括游標在內的各項參數,且清空各種由組字器生成的內部資料。<para/>
/// 且將已經被插入的索引鍵陣列與幅位單元陣列(包括其內的節點)全部清空。
Expand Down
Loading