Repository navigation
Expand file tree
/
Copy pathBaseModel.cs
More file actions
154 lines (136 loc) · 3.95 KB
/
Copy pathBaseModel.cs
File metadata and controls
154 lines (136 loc) · 3.95 KB
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
using System.Collections.Generic;
using System.Text;
namespace Bigtree.Algorithm.CRFLite
{
public class BaseModel
{
public long maxid_;
public double cost_factor_;
public List<string> unigram_templs_;
public List<string> bigram_templs_;
//Labeling tag list
public List<string> y_;
public uint ysize() { return (uint)y_.Count; }
//The dimension training corpus
public uint xsize_;
//Feature set value array
public double[] alpha_;
public BaseModel()
{
cost_factor_ = 1.0;
}
//获取类别i的字符表示
public string y(int i) { return y_[i]; }
public long feature_size() { return maxid_; }
public StringBuilder apply_rule(string p, int pos, StringBuilder resultContainer, Tagger tagger)
{
resultContainer.Clear();
for (var i = 0; i < p.Length; i++)
{
if (p[i] == '%')
{
i++;
if (p[i] == 'x')
{
i++;
var res = get_index(p, pos, i, tagger);
i = res.idx;
if (res.value == null)
{
return null;
}
resultContainer.Append(res.value);
}
else
{
return null;
}
}
else
{
resultContainer.Append(p[i]);
}
}
return resultContainer;
}
Index get_index(string p, int pos, int i, Tagger tagger)
{
if (p[i] != '[')
{
return new Index(null, i);
}
i++;
var isInRow = true;
var col = 0;
var row = 0;
var neg = 1;
if (p[i] == '-')
{
neg = -1;
i++;
}
for (; i < p.Length; i++)
{
var c = p[i];
if (isInRow)
{
if (c >= '0' && c <= '9')
{
row = 10 * row + (c - '0');
}
else if (c == ',')
{
isInRow = false;
}
else
{
return new Index(null, i);
}
}
else
{
if (c >= '0' && c <= '9')
{
col = 10 * col + (c - '0');
}
else if (c == ']')
{
break;
}
else
{
return new Index(null, i);
}
}
}
row *= neg;
if (col < 0 || col >= xsize_)
{
return new Index(null, i);
}
var idx = pos + row;
if (idx < 0)
{
return new Index("_B-" + (-idx).ToString(), i); ;
}
if (idx >= tagger.word_num)
{
return new Index("_B+" + (idx - tagger.word_num + 1).ToString(), i);
}
return new Index(tagger.x_[idx][col], i);
}
private struct Index
{
public int idx;
public string value;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Object"/> class.
/// </summary>
public Index(string value, int idx)
{
this.idx = idx;
this.value = value;
}
}
}
}