1 | package felix.test; |
2 | |
3 | import static org.junit.Assert.*; |
4 | |
5 | import java.io.BufferedReader; |
6 | import java.io.FileReader; |
7 | import java.util.ArrayList; |
8 | import java.util.HashMap; |
9 | import java.util.HashSet; |
10 | import java.util.regex.Matcher; |
11 | import java.util.regex.Pattern; |
12 | |
13 | import org.junit.Test; |
14 | |
15 | import felix.main.Felix; |
16 | import felix.parser.FelixCommandOptions; |
17 | import felix.util.FelixConfig; |
18 | import felix.util.FelixUIMan; |
19 | |
20 | |
21 | /** |
22 | * Test CRF operator's quality. |
23 | * @author Ce Zhang |
24 | * |
25 | */ |
26 | public class CRFTest{ |
27 | |
28 | /** |
29 | * Compare Marginal inference results with CRF++ on a real world data set. |
30 | */ |
31 | @Test |
32 | public final void testCRFMarginal() { |
33 | try{ |
34 | String[] args = {"-e", "test/felix/crf_test/evidence_small.db", "-i", "test/felix/crf_test/prog.mln", |
35 | "-o", "test/testOutput.txt_label", "-queryFile", "test/felix/crf_test/query.db", |
36 | "-marginal", "-verbose", "2", "-threads", "10"}; |
37 | |
38 | for(int wwww = 0; wwww < 2; wwww++){ |
39 | |
40 | FelixConfig.overrideID(); |
41 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
42 | |
43 | if(wwww == 1){ |
44 | options.useDualDecomposition = true; |
45 | FelixConfig.nDDIT = 2; |
46 | } |
47 | |
48 | |
49 | new Felix().run(options); |
50 | |
51 | HashMap<String, Double> gt = new HashMap<String, Double>(); |
52 | |
53 | BufferedReader br = new BufferedReader(new FileReader("test/felix/crf_test/gt_v1")); |
54 | Pattern p = Pattern.compile("gt\\((.*?), (.*?), \"(.*?)/(.*?)\"\\)"); |
55 | String line; |
56 | while((line = br.readLine()) != null){ |
57 | |
58 | Matcher m = p.matcher(line); |
59 | if(m.find()){ |
60 | gt.put(m.group(1) + "|" + m.group(2) + "|" + m.group(3), Double.valueOf(m.group(4))); |
61 | } |
62 | } |
63 | |
64 | String outfile = "test/testOutput.txt_label"; |
65 | |
66 | if(wwww == 1){ |
67 | outfile = outfile + "_snap_0"; |
68 | } |
69 | |
70 | br = new BufferedReader(new FileReader(outfile)); |
71 | p = Pattern.compile("(.*?)\tlabel\\(\"(.*?)\", \"(.*?)\", \"(.*?)\"\\)"); |
72 | int ct = 0; |
73 | |
74 | while((line = br.readLine()) != null){ |
75 | |
76 | Matcher m = p.matcher(line); |
77 | if(m.find()){ |
78 | String key = m.group(2) + "|" + m.group(3); |
79 | Double ass = Double.valueOf(m.group(1)); |
80 | if(m.group(4).equals("WIN")){ |
81 | String a = key + "|Winner"; |
82 | if(gt.containsKey(a)){ |
83 | |
84 | Double target = gt.get(a); |
85 | if(Math.abs(ass - target) < 0.1){ |
86 | ct++; |
87 | } |
88 | } |
89 | }else if(m.group(4).equals("LOS")){ |
90 | String a = key + "|Loser"; |
91 | if(gt.containsKey(a)){ |
92 | Double target = gt.get(a); |
93 | if(Math.abs(ass - target) < 0.1){ |
94 | ct++; |
95 | } |
96 | } |
97 | }else if(m.group(4).equals("NON")){ |
98 | String a = key + "|--"; |
99 | if(gt.containsKey(a)){ |
100 | Double target = gt.get(a); |
101 | if(Math.abs(ass - target) < 0.1){ |
102 | ct++; |
103 | } |
104 | } |
105 | |
106 | } |
107 | } |
108 | |
109 | } |
110 | System.out.println("\n\n# OF LABELS = " +ct + "\n\n"); |
111 | assertTrue(ct > 9200); |
112 | |
113 | } |
114 | |
115 | }catch(Exception e){ |
116 | e.printStackTrace(); |
117 | assertTrue(false); |
118 | } |
119 | } |
120 | |
121 | /** |
122 | * Compare MAP inference results with CRF++ on a real world data set. |
123 | */ |
124 | @Test |
125 | public final void testCRFMAP() { |
126 | try{ |
127 | String[] args = {"-e", "test/felix/crf_test/evidence_small.db", "-i", "test/felix/crf_test/prog.mln", |
128 | "-o", "test/testOutput.txt_label", "-queryFile", "test/felix/crf_test/query.db", "-verbose", "2" |
129 | }; |
130 | |
131 | for(int wwww = 0; wwww < 2; wwww++){ |
132 | |
133 | FelixConfig.overrideID(); |
134 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
135 | |
136 | if(wwww == 1){ |
137 | options.useDualDecomposition = true; |
138 | FelixConfig.nDDIT = 2; |
139 | } |
140 | |
141 | new Felix().run(options); |
142 | |
143 | String outfile = "test/testOutput.txt_label"; |
144 | |
145 | if(wwww == 1){ |
146 | outfile = outfile + "_snap_0"; |
147 | } |
148 | |
149 | HashMap<String, String> gt = new HashMap<String, String>(); |
150 | |
151 | BufferedReader br = new BufferedReader(new FileReader("test/felix/crf_test/gt")); |
152 | Pattern p = Pattern.compile("gt\\((.*?), (.*?), \"(.*?)\"\\)"); |
153 | String line; |
154 | while((line = br.readLine()) != null){ |
155 | |
156 | Matcher m = p.matcher(line); |
157 | if(m.find()){ |
158 | gt.put(m.group(1) + "|" + m.group(2), m.group(3)); |
159 | } |
160 | } |
161 | |
162 | |
163 | br = new BufferedReader(new FileReader(outfile)); |
164 | p = Pattern.compile("label\\(\"*(.*?)\"*, \"*(.*?)\"*, \"(.*?)\"\\)"); |
165 | int ct = 0; |
166 | |
167 | while((line = br.readLine()) != null){ |
168 | |
169 | Matcher m = p.matcher(line); |
170 | if(m.find()){ |
171 | String key = m.group(1) + "|" + m.group(2); |
172 | if(m.group(3).equals("WIN")){ |
173 | |
174 | if(!gt.get(key).equals("Winner")){ |
175 | System.out.println(line); |
176 | }else{ |
177 | ct++; |
178 | } |
179 | }else if(m.group(3).equals("LOS")){ |
180 | |
181 | if(!gt.get(key).equals("Loser")){ |
182 | System.out.println(line); |
183 | }else{ |
184 | ct++; |
185 | } |
186 | }else if(m.group(3).equals("NON")){ |
187 | if(!gt.get(key).equals("--")){ |
188 | System.out.println(line); |
189 | }else{ |
190 | ct++; |
191 | } |
192 | } |
193 | } |
194 | |
195 | } |
196 | |
197 | assertTrue(ct > 9200); |
198 | System.out.println("\n\n# OF LABELS = " +ct + "\n\n"); |
199 | } |
200 | |
201 | |
202 | }catch(Exception e){ |
203 | e.printStackTrace(); |
204 | assertTrue(false); |
205 | } |
206 | } |
207 | |
208 | |
209 | |
210 | } |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |