-
Notifications
You must be signed in to change notification settings - Fork 11
/
MeCabNodeBase.cs
236 lines (190 loc) · 6.44 KB
/
MeCabNodeBase.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
//
// Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
// Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
using NMeCab.Core;
using System.Text;
namespace NMeCab
{
/// <summary>
/// 形態素ノードを表す抽象基底クラスです。
/// </summary>
/// <typeparam name="TNode">連結する形態素ノードの具象型</typeparam>
public abstract class MeCabNodeBase<TNode>
where TNode : MeCabNodeBase<TNode>
{
/// <summary>
/// 解析の単位で形態素に付与するユニークID
/// </summary>
public uint Id { get; set; }
#region link
/// <summary>
/// 一つ前の形態素
/// </summary>
public TNode Prev { get; set; }
/// <summary>
/// 一つ後の形態素
/// </summary>
public TNode Next { get; set; }
/// <summary>
/// 同じ開始位置で始まる形態素
/// </summary>
public TNode BNext { get; set; }
/// <summary>
/// 同じ位置で終わる形態素
/// </summary>
public TNode ENext { get; set; }
/// <summary>
/// 前の形態素の候補へのパス
/// </summary>
public MeCabPath<TNode> LPath { get; set; }
/// <summary>
/// 次の形態素の候補へのパス
/// </summary>
public MeCabPath<TNode> RPath { get; set; }
#endregion
#region surface info
/// <summary>
/// 形態素の表層形
/// </summary>
public string Surface { get; set; }
/// <summary>
/// 形態素の長さ
/// </summary>
public int Length { get; set; }
/// <summary>
/// 形態素の長さ(先頭のスペースを含む)
/// </summary>
public int RLength { get; set; }
/// <summary>
/// 開始位置
/// </summary>
public int BPos { get; set; }
/// <summary>
/// 終了位置
/// </summary>
public int EPos { get; set; }
#endregion
#region token info
/// <summary>
/// 左文脈ID
/// </summary>
public ushort LCAttr { get; set; }
/// <summary>
/// 右文脈ID
/// </summary>
public ushort RCAttr { get; set; }
/// <summary>
/// 形態素ID
/// </summary>
public ushort PosId { get; set; }
/// <summary>
/// 単語生起コスト
/// </summary>
public short WCost { get; set; }
#endregion
#region vitebi info
/// <summary>
/// 文字種情報
/// </summary>
public uint CharType { get; set; }
/// <summary>
/// 形態素の種類
/// </summary>
public MeCabNodeStat Stat { get; set; }
/// <summary>
/// 累積コスト
/// </summary>
public long Cost { get; set; }
/// <summary>
/// ベスト解か
/// </summary>
public bool IsBest { get; set; }
#endregion
#region prob
/// <summary>
/// forward backward の foward log 確率
/// </summary>
public float Alpha { get; set; }
/// <summary>
/// forward backward の backward log 確率
/// </summary>
public float Beta { get; set; }
/// <summary>
/// 周辺確率
/// </summary>
public float Prob { get; set; }
#endregion
#region get feature
internal unsafe byte* PFeature { get; set; }
internal Encoding Encoding { get; set; }
private string feature;
private string[] features;
/// <summary>
/// CSVで表記された素性情報
/// </summary>
public unsafe string Feature
{
get
{
if (this.feature == null && this.Encoding != null && this.PFeature != null)
this.feature = StrUtils.GetString(this.PFeature, this.Encoding);
return this.feature;
}
set
{
this.feature = value;
}
}
/// <summary>
/// 素性情報から指定したインデックスの要素を取得
/// </summary>
/// <param name="index">要素のインデックス</param>
/// <returns>素性情報の要素</returns>
public string GetFeatureAt(int index)
{
if (this.features == null)
{
var featureCsv = this.Feature;
if (featureCsv == null)
this.features = new string[0];
else
this.features = StrUtils.SplitCsvRow(featureCsv, 32, 16);
}
if (index >= this.features.Length) return string.Empty;
return this.features[index];
}
#endregion
/// <summary>
/// インスタンスの文字列表現を返します。
/// </summary>
/// <returns>文字列表現</returns>
public override string ToString()
{
var os = new StringBuilder();
os.Append(this.Id).Append(" ");
os.Append("[Surface:");
if (this.Stat == MeCabNodeStat.Bos)
os.Append("BOS");
else if (this.Stat == MeCabNodeStat.Eos)
os.Append("EOS");
else
os.Append(this.Surface);
os.Append("]");
os.Append("[Feature:").Append(this.Feature).Append("]");
os.Append("[BPos:").Append(this.BPos).Append("]");
os.Append("[EPos:").Append(this.EPos).Append("]");
os.Append("[RCAttr:").Append(this.RCAttr).Append("]");
os.Append("[LCAttr:").Append(this.LCAttr).Append("]");
os.Append("[PosId:").Append(this.PosId).Append("]");
os.Append("[CharType:").Append(this.CharType).Append("]");
os.Append("[Stat:").Append((int)this.Stat).Append("]");
os.Append("[IsBest:").Append(this.IsBest).Append("]");
os.Append("[Alpha:").Append(this.Alpha).Append("]");
os.Append("[Beta:").Append(this.Beta).Append("]");
os.Append("[Prob:").Append(this.Prob).Append("]");
os.Append("[Cost:").Append(this.Cost).Append("]");
return os.ToString();
}
}
}