Skip to content

Commit

Permalink
Typo Fix: DumpDot -> DumpDOT. (#6)
Browse files Browse the repository at this point in the history
* Typo Fix: DumpDot -> DumpDOT.

* Compositor // Add default index value to walk().

* Suppress documentation warnings.

* Version: 1.2.6 -> 1.2.7.
ShikiSuen authored Jun 29, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ba28717 commit bf3dec3
Showing 11 changed files with 178 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Megrez.Tests/Megrez.Tests.csproj
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<ReleaseVersion>1.2.6</ReleaseVersion>
<ReleaseVersion>1.2.7</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
4 changes: 2 additions & 2 deletions Megrez.Tests/MegrezTests.cs
Original file line number Diff line number Diff line change
@@ -118,9 +118,9 @@ public void TestInput() {
theCompositor.DeleteReadingAtTheRearOfCursor();
theCompositor.DeleteReadingAtTheRearOfCursor();
theCompositor.DeleteReadingAtTheRearOfCursor();
string expectedDumpDot =
string expectedDumpDOT =
"digraph {\ngraph [ rankdir=LR ];\nBOS;\nBOS -> 高;\n高;\n高 -> 科;\n高 -> 科技;\nBOS -> 高科技;\n高科技;\n高科技 -> 工;\n高科技 -> 公司;\n科;\n科 -> 際;\n科 -> 濟公;\n科技;\n科技 -> 工;\n科技 -> 公司;\n際;\n際 -> 工;\n際 -> 公司;\n濟公;\n濟公 -> 斯;\n工;\n工 -> 斯;\n公司;\n公司 -> 的;\n斯;\n斯 -> 的;\n的;\n的 -> 年;\n的 -> 年終;\n年;\n年 -> 中;\n年終;\n年終 -> 獎;\n年終 -> 獎金;\n中;\n中 -> 獎;\n中 -> 獎金;\n獎;\n獎 -> 金;\n獎金;\n獎金 -> EOS;\n金;\n金 -> EOS;\nEOS;\n}\n";
Assert.AreEqual(expectedDumpDot, theCompositor.Grid.DumpDot());
Assert.AreEqual(expectedDumpDOT, theCompositor.Grid.DumpDOT());
}

[Test]
2 changes: 1 addition & 1 deletion Megrez.sln
Original file line number Diff line number Diff line change
@@ -52,6 +52,6 @@ Global
$0.DotNetNamingPolicy = $4
$4.DirectoryNamespaceAssociation = PrefixedHierarchical
$0.StandardHeader = $5
version = 1.2.6
version = 1.2.7
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion Megrez/1_Compositor.cs
Original file line number Diff line number Diff line change
@@ -157,7 +157,7 @@ public bool RemoveHeadReadings(int count) {
/// <param name="joinedPhrase">用以統計累計長詞的內部參數,請勿主動使用。</param>
/// <param name="longPhrases">用以統計累計長詞的內部參數,請勿主動使用。</param>
/// <returns>均有節點的節錨陣列。</returns>
public List<NodeAnchor> Walk(int location, double accumulatedScore = 0.0, string joinedPhrase = "",
public List<NodeAnchor> Walk(int location = 0, double accumulatedScore = 0.0, string joinedPhrase = "",
List<string>? longPhrases = default(List<string>)) {
int newLocation = Grid.Width - Math.Abs(location);
List<NodeAnchor> result = ReverseWalk(newLocation, accumulatedScore, joinedPhrase, longPhrases);
2 changes: 1 addition & 1 deletion Megrez/2_Grid.cs
Original file line number Diff line number Diff line change
@@ -227,7 +227,7 @@ public void FixNodeSelectedCandidate(int location, string value, double overridi
/// 生成用以交給 GraphViz 診斷的資料。
/// </summary>
/// <returns>GraphViz 檔案內容,純文字。</returns>
public string DumpDot() {
public string DumpDOT() {
string strOutput = "digraph {\ngraph [ rankdir=LR ];\nBOS;\n";
for (int p = 0; p < _spans.Count; p++) {
Span span = _spans[p];
4 changes: 4 additions & 0 deletions Megrez/3_NodeAnchor.cs
Original file line number Diff line number Diff line change
@@ -60,6 +60,10 @@ public NodeAnchor(Node node, int location, int spanningLength) {
/// 獲取用來比較的權重。
/// </summary>
public double ScoreForSort => Node?.Score ?? 0.0;
/// <summary>
/// 將當前節錨的內容輸出為字串。
/// </summary>
/// <returns>當前節錨的內容輸出成的字串。</returns>
public override string ToString() {
string stream = "";
stream += "{@(" + Location + "," + SpanningLength + "),";
13 changes: 13 additions & 0 deletions Megrez/4_Node.cs
Original file line number Diff line number Diff line change
@@ -57,6 +57,10 @@ public class Node {
/// 用來登記要施加給「『被標記為選中狀態』的候選字詞」的複寫權重的數值。
/// </summary>
public const double ConSelectedCandidateScore = 99.0;
/// <summary>
/// 將當前節點的內容輸出為字串。
/// </summary>
/// <returns>當前節點的內容輸出成的字串。</returns>
public override string ToString() =>
$"(node,key:{Key},fixed:{(IsCandidateFixed ? "true" : "false")},selected:{_selectedUnigramIndex},{_unigrams})";
/// <summary>
@@ -169,6 +173,11 @@ public double ScoreFor(string candidate) {
}
return result;
}
/// <summary>
/// 判定兩個節點是否相等。
/// </summary>
/// <param name="obj">用來比較的節點。</param>
/// <returns>若相等,則返回 true。</returns>
public override bool Equals(object obj) {
return obj is Node node && EqualityComparer<List<Unigram>>.Default.Equals(_unigrams, node._unigrams) &&
EqualityComparer<List<KeyValuePaired>>.Default.Equals(Candidates, node.Candidates) &&
@@ -179,6 +188,10 @@ public override bool Equals(object obj) {
IsCandidateFixed == node.IsCandidateFixed && _selectedUnigramIndex == node._selectedUnigramIndex;
}

/// <summary>
/// 將當前節點的內容輸出為雜湊資料。
/// </summary>
/// <returns>當前節錨的內容輸出成的雜湊資料。</returns>
public override int GetHashCode() {
unchecked { return (int)BitConverter.ToInt64(Convert.FromBase64String(ToString()), 0); }
}
52 changes: 49 additions & 3 deletions Megrez/6_Bigram.cs
Original file line number Diff line number Diff line change
@@ -55,42 +55,88 @@ public Bigram(KeyValuePaired keyValuePreceded, KeyValuePaired keyValue, double s
/// 權重。
/// </summary>
public double Score { get; set; }

/// <summary>
/// 判定兩個雙元圖是否相等。
/// </summary>
/// <param name="obj">用來比較的雙元圖。</param>
/// <returns>若相等,則返回 true。</returns>
public override bool Equals(object obj) =>
obj is Bigram bigram
&& EqualityComparer<KeyValuePaired>.Default.Equals(KeyValuePreceded, bigram.KeyValuePreceded) &&
EqualityComparer<KeyValuePaired>.Default.Equals(KeyValue, bigram.KeyValue) &&
Math.Abs(Score - bigram.Score) < 0.0000001f;

/// <summary>
/// 將當前物件的內容輸出為雜湊資料。
/// </summary>
/// <returns>當前物件的內容輸出成的雜湊資料。</returns>
public override int GetHashCode() => HashCode.Combine(KeyValuePreceded, KeyValue, Score);

/// <summary>
/// 將當前物件的內容輸出為字串。
/// </summary>
/// <returns>當前物件的內容輸出成的字串。</returns>
public override string ToString() => $"({KeyValuePreceded}|{KeyValue},{Score})";

/// <summary>
/// 判定兩個物件是否相等。
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator ==(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded == rhs.KeyValuePreceded && lhs.KeyValue == rhs.KeyValue &&
Math.Abs(lhs.Score - rhs.Score) < 0.0000001f;
}

/// <summary>
/// 判定兩個物件是否相異。
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator !=(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded != rhs.KeyValuePreceded || lhs.KeyValue != rhs.KeyValue ||
Math.Abs(lhs.Score - rhs.Score) > 0.0000001f;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator<(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded < rhs.KeyValuePreceded || lhs.KeyValue < rhs.KeyValue ||
lhs.KeyValue == rhs.KeyValue && lhs.Score < rhs.Score;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator>(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded > rhs.KeyValuePreceded || lhs.KeyValue > rhs.KeyValue ||
lhs.KeyValue == rhs.KeyValue && lhs.Score > rhs.Score;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator <=(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded <= rhs.KeyValuePreceded || lhs.KeyValue <= rhs.KeyValue ||
lhs.KeyValue == rhs.KeyValue && lhs.Score <= rhs.Score;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator >=(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded >= rhs.KeyValuePreceded || lhs.KeyValue >= rhs.KeyValue ||
lhs.KeyValue == rhs.KeyValue && lhs.Score >= rhs.Score;
51 changes: 49 additions & 2 deletions Megrez/6_Unigram.cs
Original file line number Diff line number Diff line change
@@ -50,34 +50,81 @@ public Unigram(KeyValuePaired keyValue, double score) {
/// </summary>
public double Score { get; set; }

/// <summary>
/// 判定兩個物件是否相等。
/// </summary>
/// <param name="obj">用來比較的物件。</param>
/// <returns>若相等,則返回 true。</returns>
public override bool Equals(object obj) {
return obj is Unigram unigram && EqualityComparer<KeyValuePaired>.Default.Equals(KeyValue, unigram.KeyValue) &&
Math.Abs(Score - unigram.Score) < 0.0000001f;
}

/// <summary>
/// 將當前物件的內容輸出為雜湊資料。
/// </summary>
/// <returns>當前物件的內容輸出成的雜湊資料。</returns>
public override int GetHashCode() { return HashCode.Combine(KeyValue, Score); }

/// <summary>
/// 將當前物件的內容輸出為字串。
/// </summary>
/// <returns>當前物件的內容輸出成的字串。</returns>
public override string ToString() => $"({KeyValue},{Score})";

/// <summary>
/// 判定兩個物件是否相等。
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator ==(Unigram lhs, Unigram rhs) {
return lhs.KeyValue == rhs.KeyValue && Math.Abs(lhs.Score - rhs.Score) < 0.0000001f;
}

/// <summary>
/// 判定兩個物件是否相異。
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator !=(Unigram lhs, Unigram rhs) => lhs.KeyValue != rhs.KeyValue
|| Math.Abs(lhs.Score - rhs.Score) > 0.0000001f;

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator<(Unigram lhs, Unigram rhs) {
return lhs.KeyValue < rhs.KeyValue || lhs.KeyValue == rhs.KeyValue && lhs.Score < rhs.Score;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator>(Unigram lhs, Unigram rhs) {
return lhs.KeyValue > rhs.KeyValue || lhs.KeyValue == rhs.KeyValue && lhs.Score > rhs.Score;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator <=(Unigram lhs, Unigram rhs) {
return lhs.KeyValue <= rhs.KeyValue || lhs.KeyValue == rhs.KeyValue && lhs.Score <= rhs.Score;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator >=(Unigram lhs, Unigram rhs) {
return lhs.KeyValue >= rhs.KeyValue || lhs.KeyValue == rhs.KeyValue && lhs.Score >= rhs.Score;
}
55 changes: 53 additions & 2 deletions Megrez/7_KeyValuePaired.cs
Original file line number Diff line number Diff line change
@@ -54,39 +54,90 @@ public KeyValuePaired(string key = "", string value = "") {
/// <returns>只要鍵或者值任一為空,則傳回值為「否」。</returns>
public bool IsValid() => !string.IsNullOrEmpty(Key) && !string.IsNullOrEmpty(Value);

/// <summary>
/// 判定兩個物件是否相等。
/// </summary>
/// <param name="obj">用來比較的物件。</param>
/// <returns>若相等,則返回 true。</returns>
public override bool Equals(object obj) {
return obj is KeyValuePaired pair && Key == pair.Key && Value == pair.Value;
}

/// <summary>
/// 將當前物件的內容輸出為雜湊資料。
/// </summary>
/// <returns>當前物件的內容輸出成的雜湊資料。</returns>
public override int GetHashCode() => HashCode.Combine(Key, Value);

/// <summary>
/// 將當前物件的內容輸出為字串。
/// </summary>
/// <returns>當前物件的內容輸出成的字串。</returns>
public override string ToString() => $"({Key},{Value})";

/// <summary>
/// 生成統一索引鍵,以作其它用途。如果該鍵值配對有任一為空,則生成空的統一索引鍵。
/// </summary>
/// <returns>生成的統一索引鍵。</returns>
public string ToNGramKey() => IsValid() ? $"({Key},{Value})" : "()";

/// <summary>
/// 判定兩個物件是否相等。
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator ==(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length == rhs.Key.Length && lhs.Value == rhs.Value;
}

/// <summary>
/// 判定兩個物件是否相異。
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator !=(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length != rhs.Key.Length || lhs.Value != rhs.Value;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator<(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length < rhs.Key.Length ||
lhs.Key.Length == rhs.Key.Length && String.Compare(lhs.Value, rhs.Value, StringComparison.Ordinal) < 0;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator>(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length > rhs.Key.Length ||
lhs.Key.Length == rhs.Key.Length && String.Compare(lhs.Value, rhs.Value, StringComparison.Ordinal) > 0;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator <=(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length <= rhs.Key.Length ||
lhs.Key.Length == rhs.Key.Length && String.Compare(lhs.Value, rhs.Value, StringComparison.Ordinal) <= 0;
}

/// <summary>
///
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool operator >=(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length >= rhs.Key.Length ||
lhs.Key.Length == rhs.Key.Length && String.Compare(lhs.Value, rhs.Value, StringComparison.Ordinal) >= 0;
8 changes: 4 additions & 4 deletions Megrez/Megrez.csproj
Original file line number Diff line number Diff line change
@@ -3,16 +3,16 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.6</ReleaseVersion>
<ReleaseVersion>1.2.7</ReleaseVersion>
<PackageId>vChewing.Megrez</PackageId>
<Authors>Shiki Suen</Authors>
<Company>Atelier Inmu</Company>
<Copyright>(c) 2022 and onwards The vChewing Project (MIT-NTL License).</Copyright>
<RepositoryUrl>https://github.com/ShikiSuen/MegrezNT</RepositoryUrl>
<NeutralLanguage>zh-TW</NeutralLanguage>
<AssemblyVersion>1.2.6</AssemblyVersion>
<FileVersion>1.2.6</FileVersion>
<Version>1.2.6</Version>
<AssemblyVersion>1.2.7</AssemblyVersion>
<FileVersion>1.2.7</FileVersion>
<Version>1.2.7</Version>
<Product>Megrez</Product>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>

0 comments on commit bf3dec3

Please sign in to comment.