/
DictionaryRecord.cs
110 lines (92 loc) · 3.23 KB
/
DictionaryRecord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
namespace CC_CEDICT.WindowsPhone
{
public class DictionaryRecord : ILine, IComparable, IEquatable<DictionaryRecord>
{
public Chinese Chinese = null;
public List<string> English = new List<string>();
public int Relevance = 100;
#region ILine initialization
public override void Initialize(ref byte[] data)
{
string line = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
int i = 0;
int j = line.IndexOf(" ", i);
if (j == -1)
return;
string traditional = line.Substring(i, j - i);
i = j + 1;
j = line.IndexOf(" [", i);
if (j == -1)
return;
string simplified = line.Substring(i, j - i);
i = j + 2;
j = line.IndexOf("] /", i);
if (j == -1)
return;
string pinyin = line.Substring(i, j - i);
try
{
Chinese = new Chinese(traditional, simplified, pinyin);
}
catch (Exception)
{
return;
}
i = j + 3;
j = line.IndexOf("/", i);
if (j == -1)
return;
English.Add(line.Substring(i, j - i));
while (line.Length > j + 1)
{
i = j + 1;
j = line.IndexOf("/", i);
if (j == -1)
break;
English.Add(line.Substring(i, j - i));
}
}
#endregion
public override string ToString()
{
return String.Format("{0} {1} [{2}] /{3}/",
Chinese.Traditional,
Chinese.Simplified,
Chinese.PinyinNoMarkup,
String.Join("/", English));
}
#region IComparable interface
// TODO-NTH: sort chinese by stroke count and popularity and in-order chinese (and deprioritise "variant of...")
int IComparable.CompareTo(object obj)
{
DictionaryRecord other = (DictionaryRecord)obj;
// compare first by relevance
if (this.Relevance > other.Relevance)
return -1;
if (this.Relevance < other.Relevance)
return 1;
// then by Pinyin
for (int i = 0; i < this.Chinese.Characters.Count; i++)
{
if (i >= other.Chinese.Characters.Count) // other is shorter than this
return 1;
int cmp = this.Chinese.Characters[i].Pinyin.CompareTo(other.Chinese.Characters[i].Pinyin);
if (cmp != 0)
return cmp;
}
if (this.Chinese.Characters.Count < other.Chinese.Characters.Count) // this is shorter than other
return -1;
// finally by English
return String.Join(";", this.English).CompareTo(String.Join(";", other.English));
}
#endregion
#region IEquatable interface
public bool Equals(DictionaryRecord other)
{
return this.ToString().Equals(other.ToString());
}
#endregion
}
}