Skip to content

Commit

Permalink
Add IsValid() and ToNGramKey() to KeyValuePaired. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored Jun 23, 2022
1 parent c4bdba5 commit fd11924
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 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.5</ReleaseVersion>
<ReleaseVersion>1.2.6</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
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.5
version = 1.2.6
EndGlobalSection
EndGlobal
18 changes: 10 additions & 8 deletions Megrez/6_Bigram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,24 @@ public Bigram(KeyValuePaired keyValuePreceded, KeyValuePaired keyValue, double s
/// </summary>
public double Score { get; set; }

public override bool Equals(object obj) {
return obj is Bigram bigram &&
EqualityComparer<KeyValuePaired>.Default.Equals(KeyValuePreceded, bigram.KeyValuePreceded) &&
EqualityComparer<KeyValuePaired>.Default.Equals(KeyValue, bigram.KeyValue) && Score == bigram.Score;
}
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;

public override int GetHashCode() { return HashCode.Combine(KeyValuePreceded, KeyValue, Score); }
public override int GetHashCode() => HashCode.Combine(KeyValuePreceded, KeyValue, Score);

public override string ToString() => $"({KeyValuePreceded}|{KeyValue},{Score})";

public static bool operator ==(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded == rhs.KeyValuePreceded && lhs.KeyValue == rhs.KeyValue && lhs.Score == rhs.Score;
return lhs.KeyValuePreceded == rhs.KeyValuePreceded && lhs.KeyValue == rhs.KeyValue &&
Math.Abs(lhs.Score - rhs.Score) < 0.0000001f;
}

public static bool operator !=(Bigram lhs, Bigram rhs) {
return lhs.KeyValuePreceded != rhs.KeyValuePreceded || lhs.KeyValue != rhs.KeyValue || lhs.Score != rhs.Score;
return lhs.KeyValuePreceded != rhs.KeyValuePreceded || lhs.KeyValue != rhs.KeyValue ||
Math.Abs(lhs.Score - rhs.Score) > 0.0000001f;
}

public static bool operator<(Bigram lhs, Bigram rhs) {
Expand Down
9 changes: 4 additions & 5 deletions Megrez/6_Unigram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,19 @@ public Unigram(KeyValuePaired keyValue, double score) {

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

public override int GetHashCode() { return HashCode.Combine(KeyValue, Score); }

public override string ToString() => $"({KeyValue},{Score})";

public static bool operator ==(Unigram lhs, Unigram rhs) {
return lhs.KeyValue == rhs.KeyValue && lhs.Score == rhs.Score;
return lhs.KeyValue == rhs.KeyValue && Math.Abs(lhs.Score - rhs.Score) < 0.0000001f;
}

public static bool operator !=(Unigram lhs, Unigram rhs) {
return lhs.KeyValue != rhs.KeyValue || lhs.Score != rhs.Score;
}
public static bool operator !=(Unigram lhs, Unigram rhs) => lhs.KeyValue != rhs.KeyValue
|| Math.Abs(lhs.Score - rhs.Score) > 0.0000001f;

public static bool operator<(Unigram lhs, Unigram rhs) {
return lhs.KeyValue < rhs.KeyValue || lhs.KeyValue == rhs.KeyValue && lhs.Score < rhs.Score;
Expand Down
24 changes: 18 additions & 6 deletions Megrez/7_KeyValuePair.cs → Megrez/7_KeyValuePaired.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct KeyValuePaired {
/// </summary>
/// <param name="key">鍵。一般情況下用來放置讀音等可以用來作為索引的內容。</param>
/// <param name="value">資料值。</param>
public KeyValuePaired(string key, string value) {
public KeyValuePaired(string key = "", string value = "") {
Key = key;
Value = value;
}
Expand All @@ -48,14 +48,22 @@ public KeyValuePaired(string key, string value) {
/// </summary>
public string Value { get; }

/// <summary>
/// 該鍵值配對是否不為空。
/// </summary>
/// <returns>只要鍵或者值任一為空,則傳回值為「否」。</returns>
public bool IsValid() => !string.IsNullOrEmpty(Key) && !string.IsNullOrEmpty(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 int GetHashCode() => HashCode.Combine(Key, Value);

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

public string ToNGramKey() => IsValid() ? $"({Key},{Value})" : "()";

public static bool operator ==(KeyValuePaired lhs, KeyValuePaired rhs) {
return lhs.Key.Length == rhs.Key.Length && lhs.Value == rhs.Value;
}
Expand All @@ -65,19 +73,23 @@ public override bool Equals(object obj) {
}

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;
return lhs.Key.Length < rhs.Key.Length ||
lhs.Key.Length == rhs.Key.Length && String.Compare(lhs.Value, rhs.Value, StringComparison.Ordinal) < 0;
}

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;
return lhs.Key.Length > rhs.Key.Length ||
lhs.Key.Length == rhs.Key.Length && String.Compare(lhs.Value, rhs.Value, StringComparison.Ordinal) > 0;
}

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;
return lhs.Key.Length <= rhs.Key.Length ||
lhs.Key.Length == rhs.Key.Length && String.Compare(lhs.Value, rhs.Value, StringComparison.Ordinal) <= 0;
}

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;
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
Expand Up @@ -3,16 +3,16 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.5</ReleaseVersion>
<ReleaseVersion>1.2.6</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.5</AssemblyVersion>
<FileVersion>1.2.5</FileVersion>
<Version>1.2.5</Version>
<AssemblyVersion>1.2.6</AssemblyVersion>
<FileVersion>1.2.6</FileVersion>
<Version>1.2.6</Version>
<Product>Megrez</Product>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down

0 comments on commit fd11924

Please sign in to comment.