-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategorical2Numerical.java
More file actions
94 lines (79 loc) · 2.36 KB
/
Copy pathCategorical2Numerical.java
File metadata and controls
94 lines (79 loc) · 2.36 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class Categorical2Numerical {
/**
* @param args
*/
public static void main(String[] args) {
try {
PrintWriter pw = new PrintWriter(System.out, true);
if (args.length != 3) {
System.err.println("wrong arguments - exit");
System.exit(-1);
}
String feature = args[0];
String newFeature = args[1];
String mapString = args[2];
String[] a = mapString.split(":");
Map<String, Integer> valueMap = new HashMap<String, Integer>();
for (String e : a) {
String[] pair = e.split("#");
if (pair.length != 2) {
System.err.println("wrong argument " + mapString);
System.exit(-1);
}
valueMap.put(pair[0], Integer.parseInt(pair[1]));
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
List<String> features = new ArrayList<String>();
line = br.readLine();
pw.println(line + "\t" + newFeature);
String[] strs = line.split("\t", 1000000);
for (String str : strs) {
features.add(str);
}
int pos = features.indexOf(feature);
if (pos < 0) {
System.err.println("feature does not exist in data file - aborting...");
System.exit(-1);
}
System.err.println("Start processing input file");
int cnt = 0;
while ((line = br.readLine()) != null) {
String[] vals = line.split("\t", 1000000);
String val = vals[pos];
if (val != null && !val.isEmpty()) {
if (valueMap.containsKey(val) == false) {
System.err.println("feature value " + val + " does not exist in data file - aborting...");
System.exit(-1);
}
pw.println(line + "\t" + valueMap.get(val));
} else {
pw.println(line + "\t");
}
if (++cnt % 1000 == 0) {
System.err.println(cnt + " lines processed.");
}
}
br.close();
System.err.println("done processing input file.\n values on " + feature + " are:");
for (String key : valueMap.keySet()) {
System.err.println(key + " -> " + valueMap.get(key));
}
pw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}