Skip to content

Commit

Permalink
KeyValuePair -> KeyValuePaired. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored Jun 20, 2022
1 parent da53af9 commit 8380a6f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
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>1.2.3</ReleaseVersion>
<ReleaseVersion>1.2.4</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Megrez.Tests/MegrezTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public SimpleLM(string input, bool swapKeyValue = false) {
string col1 = lineStream[1]; // 假設其不為 nil
double col2 = 0; // 防呆
if (lineStream.Count >= 3 && Double.TryParse(lineStream[2], out double number)) col2 = number;
Unigram u = new(new KeyValuePair(), 0);
Unigram u = new(new KeyValuePaired(), 0);
if (swapKeyValue)
u.KeyValue = new(col1, col0);
else
Expand Down
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 = 1.2.3
version = 1.2.4
EndGlobalSection
EndGlobal
8 changes: 4 additions & 4 deletions Megrez/2_Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ public NodeAnchor FixNodeSelectedCandidate(int location, string value) {
foreach (NodeAnchor nodeAnchor in NodesCrossingOrEndingAt(theLocation)) {
Node? theNode = nodeAnchor.Node;
if (theNode == null) continue;
List<KeyValuePair> candidates = theNode.Candidates;
List<KeyValuePaired> candidates = theNode.Candidates;
// 將該位置的所有節點的候選字詞鎖定狀態全部重設。
theNode.ResetCandidate();
int I = 0;
foreach (KeyValuePair candidate in candidates) {
foreach (KeyValuePaired candidate in candidates) {
if (candidate.Value == value) {
theNode.SelectCandidateAt(I);
node = nodeAnchor;
Expand All @@ -210,11 +210,11 @@ public void FixNodeSelectedCandidate(int location, string value, double overridi
foreach (NodeAnchor nodeAnchor in NodesCrossingOrEndingAt(theLocation)) {
Node? theNode = nodeAnchor.Node;
if (theNode == null) continue;
List<KeyValuePair> candidates = theNode.Candidates;
List<KeyValuePaired> candidates = theNode.Candidates;
// 將該位置的所有節點的候選字詞鎖定狀態全部重設。
theNode.ResetCandidate();
int I = 0;
foreach (KeyValuePair candidate in candidates) {
foreach (KeyValuePaired candidate in candidates) {
if (candidate.Value == value) {
theNode.SelectFloatingCandidateAt(I, overridingScore);
break;
Expand Down
16 changes: 8 additions & 8 deletions Megrez/4_Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Node {
/// <summary>
/// 專門「用給定鍵值來取對應的雙元圖陣列」的辭典。
/// </summary>
private Dictionary<KeyValuePair, List<Bigram>> _precedingBigramMap = new();
private Dictionary<KeyValuePaired, List<Bigram>> _precedingBigramMap = new();
/// <summary>
/// 用來登記「當前選中的單元圖」的索引值的變數。
/// </summary>
Expand All @@ -62,7 +62,7 @@ public override string ToString() =>
/// <summary>
/// 公開:候選字詞陣列(唯讀),以鍵值陣列的形式存在。
/// </summary>
public List<KeyValuePair> Candidates { get; } = new();
public List<KeyValuePaired> Candidates { get; } = new();
/// <summary>
/// 公開:用來登記「當前選中的單元圖」的索引值的變數(唯讀)。
/// </summary>
Expand All @@ -78,7 +78,7 @@ public override string ToString() =>
/// <summary>
/// 公開:當前被選中的候選字詞的鍵值配對。
/// </summary>
public KeyValuePair CurrentKeyValue =>
public KeyValuePaired CurrentKeyValue =>
_selectedUnigramIndex >= _unigrams.Count ? new() : Candidates[_selectedUnigramIndex];
/// <summary>
/// 公開:給出當前單元圖陣列內最高的權重數值。
Expand Down Expand Up @@ -111,11 +111,11 @@ public Node(string key, List<Unigram> unigrams, List<Bigram>? bigrams = null) {
/// 對擁有「給定的前述鍵值陣列」的節點提權。
/// </summary>
/// <param name="precedingKeyValues">前述鍵值陣列。</param>
public void PrimeNodeWith(List<KeyValuePair> precedingKeyValues) {
public void PrimeNodeWith(List<KeyValuePaired> precedingKeyValues) {
int newIndex = _selectedUnigramIndex;
double maxScore = Score;
if (!IsCandidateFixed) {
foreach (KeyValuePair neta in precedingKeyValues) {
foreach (KeyValuePaired neta in precedingKeyValues) {
List<Bigram> bigrams = new();
if (_precedingBigramMap.ContainsKey(neta)) bigrams = _precedingBigramMap[neta];
foreach (Bigram bigram in bigrams.Where(bigram => bigram.Score > maxScore)
Expand Down Expand Up @@ -171,11 +171,11 @@ public double ScoreFor(string candidate) {
}
public override bool Equals(object obj) {
return obj is Node node && EqualityComparer<List<Unigram>>.Default.Equals(_unigrams, node._unigrams) &&
EqualityComparer<List<KeyValuePair>>.Default.Equals(Candidates, node.Candidates) &&
EqualityComparer<List<KeyValuePaired>>.Default.Equals(Candidates, node.Candidates) &&
EqualityComparer<Dictionary<string, int>>.Default.Equals(_valueUnigramIndexMap,
node._valueUnigramIndexMap) &&
EqualityComparer<Dictionary<KeyValuePair, List<Bigram>>>.Default.Equals(_precedingBigramMap,
node._precedingBigramMap) &&
EqualityComparer<Dictionary<KeyValuePaired, List<Bigram>>>.Default.Equals(_precedingBigramMap,
node._precedingBigramMap) &&
IsCandidateFixed == node.IsCandidateFixed && _selectedUnigramIndex == node._selectedUnigramIndex;
}

Expand Down
10 changes: 5 additions & 5 deletions Megrez/6_Bigram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct Bigram {
/// <param name="keyValuePreceded">前述鍵值。</param>
/// <param name="keyValue">當前鍵值。</param>
/// <param name="score">權重(雙精度小數)。</param>
public Bigram(KeyValuePair keyValuePreceded, KeyValuePair keyValue, double score) {
public Bigram(KeyValuePaired keyValuePreceded, KeyValuePaired keyValue, double score) {
KeyValuePreceded = keyValuePreceded;
KeyValue = keyValue;
Score = score;
Expand All @@ -46,20 +46,20 @@ public Bigram(KeyValuePair keyValuePreceded, KeyValuePair keyValue, double score
/// <summary>
/// 前述鍵值。
/// </summary>
public KeyValuePair KeyValuePreceded { get; set; }
public KeyValuePaired KeyValuePreceded { get; set; }
/// <summary>
/// 當前鍵值。
/// </summary>
public KeyValuePair KeyValue { get; set; }
public KeyValuePaired KeyValue { get; set; }
/// <summary>
/// 權重。
/// </summary>
public double Score { get; set; }

public override bool Equals(object obj) {
return obj is Bigram bigram &&
EqualityComparer<KeyValuePair>.Default.Equals(KeyValuePreceded, bigram.KeyValuePreceded) &&
EqualityComparer<KeyValuePair>.Default.Equals(KeyValue, bigram.KeyValue) && Score == bigram.Score;
EqualityComparer<KeyValuePaired>.Default.Equals(KeyValuePreceded, bigram.KeyValuePreceded) &&
EqualityComparer<KeyValuePaired>.Default.Equals(KeyValue, bigram.KeyValue) && Score == bigram.Score;
}

public override int GetHashCode() { return HashCode.Combine(KeyValuePreceded, KeyValue, Score); }
Expand Down
6 changes: 3 additions & 3 deletions Megrez/6_Unigram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ public struct Unigram {
/// </summary>
/// <param name="keyValue">鍵值。</param>
/// <param name="score">權重(雙精度小數)。</param>
public Unigram(KeyValuePair keyValue, double score) {
public Unigram(KeyValuePaired keyValue, double score) {
KeyValue = keyValue;
Score = score;
}

/// <summary>
/// 鍵值。
/// </summary>
public KeyValuePair KeyValue { get; set; }
public KeyValuePaired KeyValue { get; set; }
/// <summary>
/// 權重。
/// </summary>
public double Score { get; set; }

public override bool Equals(object obj) {
return obj is Unigram unigram && EqualityComparer<KeyValuePair>.Default.Equals(KeyValue, unigram.KeyValue) &&
return obj is Unigram unigram && EqualityComparer<KeyValuePaired>.Default.Equals(KeyValue, unigram.KeyValue) &&
Score == unigram.Score;
}

Expand Down
20 changes: 11 additions & 9 deletions Megrez/7_KeyValuePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ namespace Megrez {
/// <summary>
/// 鍵值配對。
/// </summary>
public struct KeyValuePair {
public struct KeyValuePaired {
/// <summary>
/// 初期化一組鍵值配對。
/// </summary>
/// <param name="key">鍵。一般情況下用來放置讀音等可以用來作為索引的內容。</param>
/// <param name="value">資料值。</param>
public KeyValuePair(string key, string value) {
public KeyValuePaired(string key, string value) {
Key = key;
Value = value;
}
Expand All @@ -48,33 +48,35 @@ public KeyValuePair(string key, string value) {
/// </summary>
public string Value { get; }

public override bool Equals(object obj) { return obj is KeyValuePair pair && Key == pair.Key && Value == pair.Value; }
public override bool Equals(object obj) {
return obj is KeyValuePaired pair && Key == pair.Key && Value == pair.Value;
}

public override int GetHashCode() { return HashCode.Combine(Key, Value); }

public override string ToString() => $"({Key},{Value})";

public static bool operator ==(KeyValuePair lhs, KeyValuePair rhs) {
public static bool operator ==(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length == rhs.Key.Length && lhs.Value == rhs.Value;
}

public static bool operator !=(KeyValuePair lhs, KeyValuePair rhs) {
public static bool operator !=(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length != rhs.Key.Length || lhs.Value != rhs.Value;
}

public static bool operator<(KeyValuePair lhs, KeyValuePair rhs) {
public static bool operator<(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length < rhs.Key.Length || lhs.Key.Length == rhs.Key.Length && lhs.Value.CompareTo(rhs.Value) < 0;
}

public static bool operator>(KeyValuePair lhs, KeyValuePair rhs) {
public static bool operator>(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length > rhs.Key.Length || lhs.Key.Length == rhs.Key.Length && lhs.Value.CompareTo(rhs.Value) > 0;
}

public static bool operator <=(KeyValuePair lhs, KeyValuePair rhs) {
public static bool operator <=(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length <= rhs.Key.Length || lhs.Key.Length == rhs.Key.Length && lhs.Value.CompareTo(rhs.Value) <= 0;
}

public static bool operator >=(KeyValuePair lhs, KeyValuePair rhs) {
public static bool operator >=(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length >= rhs.Key.Length || lhs.Key.Length == rhs.Key.Length && lhs.Value.CompareTo(rhs.Value) >= 0;
}
}
Expand Down
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>1.2.3</ReleaseVersion>
<ReleaseVersion>1.2.4</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.3</AssemblyVersion>
<FileVersion>1.2.3</FileVersion>
<Version>1.2.3</Version>
<AssemblyVersion>1.2.4</AssemblyVersion>
<FileVersion>1.2.4</FileVersion>
<Version>1.2.4</Version>
<Product>Megrez</Product>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down

0 comments on commit 8380a6f

Please sign in to comment.