-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathProgram.cs
269 lines (238 loc) · 10.2 KB
/
Program.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using AdvUtils;
using RNNSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
/// <summary>
/// RNNSharp written by Zhongkai Fu (fuzhongkai@gmail.com)
/// </summary>
namespace TFeatureBin
{
internal class Program
{
private static string templateFilePath = "";
private static string inputFilePath = "";
private static string featureFilePath = "";
private static string strMode = "";
private static int minfreq = 1;
private static TemplateFeaturizer templateFeaturizer;
private static int ArgPos(string str, string[] args)
{
int a;
for (a = 0; a < args.Length; a++)
{
if (str == args[a])
{
if (a == args.Length - 1)
{
Logger.WriteLine("Argument missing for {0}", str);
return -1;
}
return a;
}
}
return -1;
}
private static void UsageTitle()
{
Console.WriteLine("Template Feature Builder written by Zhongkai Fu(fuzhongkai@gmail.com)");
}
private static void Usage()
{
UsageTitle();
Console.WriteLine("TFeatureBin.exe <parameters>");
Console.WriteLine("The tool is to generate template feature from corpus and index them into file");
Console.WriteLine("-mode <string> : support extract,index and build modes, default is build mode.");
Console.WriteLine(" extract : extract features from corpus and save them as raw text feature list");
Console.WriteLine(" index : build indexed feature set from raw text feature list");
Console.WriteLine(" build : extract features from corpus and generate indexed feature set");
}
private static void UsageBuild()
{
UsageTitle();
Console.WriteLine("TFeatureBin.exe -mode build <parameters>");
Console.WriteLine("This mode is to extract feature from corpus and generate indexed feature set");
Console.WriteLine("-template <string> : feature template file");
Console.WriteLine("-inputfile <string> : file used to generate features");
Console.WriteLine("-ftrfile <string> : generated indexed feature file");
Console.WriteLine("-minfreq <int> : min-frequency of feature");
Console.WriteLine();
Console.WriteLine(
"Example: TFeatureBin.exe -mode build -template template.txt -inputfile train.txt -ftrfile tfeatures -minfreq 3");
}
private static void UsageExtract()
{
UsageTitle();
Console.WriteLine("TFeatureBin.exe -mode extract <parameters>");
Console.WriteLine("This mode is to extract features from corpus and save them as text feature list");
Console.WriteLine("-template <string> : feature template file");
Console.WriteLine("-inputfile <string> : file used to generate features");
Console.WriteLine("-ftrfile <string> : generated feature list file in raw text format");
Console.WriteLine("-minfreq <int> : min-frequency of feature");
Console.WriteLine();
Console.WriteLine(
"Example: TFeatureBin.exe -mode extract -template template.txt -inputfile train.txt -ftrfile features_raw.txt -minfreq 3");
}
private static void UsageIndex()
{
UsageTitle();
Console.WriteLine("TFeatureBin.exe -mode index <parameters>");
Console.WriteLine("This mode is to build indexed feature set from raw text feature list");
Console.WriteLine("-template <string> : feature template file");
Console.WriteLine("-inputfile <string> : feature list in raw text format");
Console.WriteLine("-ftrfile <string> : indexed feature set");
Console.WriteLine();
Console.WriteLine(
"Example: TFeatureBin.exe -mode index -template template.txt -inputfile features.txt -ftrfile tfeatures");
}
private static void ExtractMode()
{
if (File.Exists(inputFilePath) == false ||
File.Exists(templateFilePath) == false)
{
UsageExtract();
return;
}
//Extract feature set from given corpus
var feature2freq = ExtractFeatureSetFromFile();
//Save feature set into raw text file
var sw = new StreamWriter(featureFilePath, false, Encoding.UTF8);
foreach (var pair in feature2freq)
{
sw.WriteLine("{0}\t{1}", pair.Key, pair.Value);
}
sw.Close();
}
private static void IndexMode()
{
if (File.Exists(inputFilePath) == false ||
File.Exists(templateFilePath) == false)
{
UsageIndex();
return;
}
//Load feature set from given file
var features = new List<string>();
var sr = new StreamReader(inputFilePath);
string strLine;
while ((strLine = sr.ReadLine()) != null)
{
var items = strLine.Split('\t');
features.Add(items[0]);
}
sr.Close();
//Build indexed feature set
templateFeaturizer = new TemplateFeaturizer();
templateFeaturizer.LoadTemplateFromFile(templateFilePath);
templateFeaturizer.BuildIndexedFeatureIntoFile(featureFilePath, features);
}
private static void BuildMode()
{
if (File.Exists(inputFilePath) == false ||
File.Exists(templateFilePath) == false)
{
UsageBuild();
return;
}
//Extract feature set from given corpus
var feature2freq = ExtractFeatureSetFromFile();
var features = feature2freq.Select(pair => pair.Key).ToList();
//Build indexed feature set
templateFeaturizer.BuildIndexedFeatureIntoFile(featureFilePath, features);
}
private static IDictionary<string, int> ExtractFeatureSetFromFile()
{
//Load templates from given file
Logger.WriteLine("Loading feature template from {0}...", templateFilePath);
templateFeaturizer = new TemplateFeaturizer();
templateFeaturizer.LoadTemplateFromFile(templateFilePath);
Logger.WriteLine("Generate feature set...");
var feature2freq = new BigDictionary<string, int>();
var tokenList = new List<string[]>();
using (var srCorpus = new StreamReader(inputFilePath, Encoding.UTF8))
{
string strLine;
Sentence sentence;
while ((strLine = srCorpus.ReadLine()) != null)
{
strLine = strLine.Trim();
if (strLine.Length == 0)
{
//The end of current record
sentence = new Sentence(tokenList);
for (var i = 0; i < sentence.TokensList.Count; i++)
{
//Get feature of i-th token
var featureList = templateFeaturizer.GenerateFeature(sentence.TokensList, i);
foreach (var strFeature in featureList)
{
if (feature2freq.ContainsKey(strFeature) == false)
{
feature2freq.Add(strFeature, 0);
}
feature2freq[strFeature]++;
}
}
tokenList.Clear();
}
else
{
tokenList.Add(strLine.Split('\t'));
}
}
//The end of current record
sentence = new Sentence(tokenList);
for (var i = 0; i < sentence.TokensList.Count; i++)
{
//Get feature of i-th token
var featureList = templateFeaturizer.GenerateFeature(sentence.TokensList, i);
foreach (var strFeature in featureList)
{
if (feature2freq.ContainsKey(strFeature) == false)
{
feature2freq.Add(strFeature, 0);
}
feature2freq[strFeature]++;
}
}
}
//Only save the feature whose frequency is not less than minfreq
Logger.WriteLine("Filter out features whose frequency is less than {0}", minfreq);
var features = new SortedDictionary<string, int>(StringComparer.Ordinal);
foreach (KeyValuePair<string, int> pair in feature2freq)
{
if (pair.Value >= minfreq)
{
features.Add(pair.Key, pair.Value);
}
}
return features;
}
private static void Main(string[] args)
{
int i;
if ((i = ArgPos("-template", args)) >= 0) templateFilePath = args[i + 1];
if ((i = ArgPos("-inputfile", args)) >= 0) inputFilePath = args[i + 1];
if ((i = ArgPos("-ftrfile", args)) >= 0) featureFilePath = args[i + 1];
if ((i = ArgPos("-minfreq", args)) >= 0) minfreq = int.Parse(args[i + 1]);
if ((i = ArgPos("-mode", args)) >= 0) strMode = args[i + 1];
switch (strMode)
{
case "build":
BuildMode();
break;
case "extract":
ExtractMode();
break;
case "index":
IndexMode();
break;
default:
Usage();
break;
}
}
}
}