001 /** 002 * SpamFilterToy.java 003 * jCOLIBRI2 framework. 004 * @author Juan A. Recio-García. 005 * GAIA - Group for Artificial Intelligence Applications 006 * http://gaia.fdi.ucm.es 007 * 02/08/2007 008 */ 009 package jcolibri.test.test16.gui; 010 011 import java.awt.BorderLayout; 012 import java.awt.Component; 013 import java.awt.Dimension; 014 import java.awt.GridLayout; 015 import java.awt.event.ActionEvent; 016 import java.awt.event.ActionListener; 017 import java.awt.event.WindowAdapter; 018 import java.awt.event.WindowEvent; 019 020 import javax.swing.BorderFactory; 021 import javax.swing.BoxLayout; 022 import javax.swing.ButtonGroup; 023 import javax.swing.JButton; 024 import javax.swing.JComponent; 025 import javax.swing.JFileChooser; 026 import javax.swing.JFrame; 027 import javax.swing.JLabel; 028 import javax.swing.JPanel; 029 import javax.swing.JRadioButton; 030 import javax.swing.JSpinner; 031 import javax.swing.JTextField; 032 import javax.swing.SpinnerNumberModel; 033 import javax.swing.UIManager; 034 035 import jcolibri.cbrcore.CBRCaseBase; 036 import jcolibri.evaluation.EvaluationReport; 037 import jcolibri.evaluation.Evaluator; 038 import jcolibri.evaluation.evaluators.HoldOutEvaluator; 039 import jcolibri.evaluation.evaluators.LeaveOneOutEvaluator; 040 import jcolibri.evaluation.evaluators.NFoldEvaluator; 041 import jcolibri.evaluation.evaluators.SameSplitEvaluator; 042 import jcolibri.exception.ExecutionException; 043 import jcolibri.extensions.textual.IE.common.StopWordsDetector; 044 import jcolibri.extensions.textual.IE.common.TextStemmer; 045 import jcolibri.extensions.textual.IE.opennlp.OpennlpSplitter; 046 import jcolibri.extensions.visualization.CasesVisualization; 047 import jcolibri.method.retrieve.KNNretrieval.similarity.LocalSimilarityFunction; 048 import jcolibri.method.retrieve.KNNretrieval.similarity.local.compressionbased.CompressionBased; 049 import jcolibri.method.retrieve.KNNretrieval.similarity.local.compressionbased.GZipCompressor; 050 import jcolibri.method.retrieve.KNNretrieval.similarity.local.compressionbased.NormalisedCompression; 051 import jcolibri.method.retrieve.KNNretrieval.similarity.local.textual.CosineCoefficient; 052 import jcolibri.method.retrieve.KNNretrieval.similarity.local.textual.DiceCoefficient; 053 import jcolibri.method.retrieve.KNNretrieval.similarity.local.textual.JaccardCoefficient; 054 import jcolibri.method.retrieve.KNNretrieval.similarity.local.textual.OverlapCoefficient; 055 import jcolibri.method.reuse.classification.KNNClassificationMethod; 056 import jcolibri.method.reuse.classification.MajorityVotingMethod; 057 import jcolibri.method.reuse.classification.SimilarityWeightedVotingMethod; 058 import jcolibri.test.test16.SpamFilterApp; 059 import jcolibri.test.test8.SwingProgressBar; 060 import jcolibri.util.ProgressController; 061 import jcolibri.util.ProgressListener; 062 063 /** 064 * @author Juan A. Recio-Garcia 065 * @version 1.0 066 * 067 */ 068 public class SpamFilterToy extends JFrame 069 { 070 private static final long serialVersionUID = 1L; 071 072 JRadioButton corpus300; 073 074 JRadioButton corpus600; 075 076 JRadioButton leaveOneOut; 077 078 JRadioButton holdOutFixed10; 079 080 JRadioButton holdOutFixed20; 081 082 JRadioButton holdOutFixed30; 083 084 JRadioButton sameSplit; 085 086 JRadioButton sameSplitGenerate; 087 088 JButton saveSplit; 089 090 JTextField sameSplitGenerateFile; 091 092 JRadioButton sameSplitReuse; 093 094 JButton loadSplit; 095 096 JTextField sameSplitReuseFile; 097 098 JLabel sameSplitGeneratePercent; 099 100 SpinnerNumberModel sameSplitPercent; 101 102 JSpinner sameSplitGeneratePercentSpinner; 103 104 JRadioButton holdOut; 105 106 JLabel holdOutLabel1; 107 108 SpinnerNumberModel holdOutPercent; 109 110 JSpinner holdOutPercentSpinner; 111 112 JLabel holdOutLabel2; 113 114 SpinnerNumberModel holdOutRepetitions; 115 116 JSpinner holdOutRepetitionsSpinner; 117 118 JRadioButton nFold; 119 120 JLabel nFoldLabel1; 121 122 SpinnerNumberModel nFoldFolds; 123 124 JSpinner nFoldFoldsSpinner; 125 126 JLabel nFoldLabel2; 127 128 SpinnerNumberModel nFoldRepetitions; 129 130 JSpinner nFoldRepetitionsSpinner; 131 132 JRadioButton compressionBased; 133 134 JRadioButton normalizedCompression; 135 136 JRadioButton cosine; 137 138 JRadioButton dice; 139 140 JRadioButton jaccard; 141 142 JRadioButton overlap; 143 144 JRadioButton majorityVoting; 145 146 JRadioButton weightedVoting; 147 148 SpinnerNumberModel kValue; 149 150 JSpinner kValueSpinner; 151 152 JButton evaluate; 153 154 JButton visualize; 155 156 157 158 159 public SpamFilterToy() 160 { 161 configure(); 162 } 163 164 public void configure() 165 { 166 try 167 { 168 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 169 } catch (Exception e) 170 { 171 } 172 173 /** ****** Corpus Panel ************ */ 174 175 JPanel corpusPanel = new JPanel(); 176 corpusPanel.setLayout(new GridLayout(2, 1)); 177 corpusPanel.setBorder(BorderFactory.createTitledBorder("Corpus")); 178 179 corpus300 = new JRadioButton("Corpus - 300 emails"); 180 corpus600 = new JRadioButton("Corpus - 600 emails"); 181 182 ButtonGroup corpusGroup = new ButtonGroup(); 183 corpusGroup.add(corpus300); 184 corpusGroup.add(corpus600); 185 186 corpusPanel.add(corpus300); 187 corpusPanel.add(corpus600); 188 189 corpus300.setSelected(true); 190 191 /** ****** Evaluator Panel ************ */ 192 193 JPanel evaluatorPanel = new JPanel(); 194 evaluatorPanel.setBorder(BorderFactory.createTitledBorder("Evaluator")); 195 evaluatorPanel.setLayout(new BoxLayout(evaluatorPanel, BoxLayout.Y_AXIS)); 196 197 leaveOneOut = new JRadioButton("Leave One Out"); 198 leaveOneOut.addActionListener(new ActionListener() { 199 public void actionPerformed(ActionEvent e) 200 { 201 enableGroup("none"); 202 } 203 }); 204 205 holdOutFixed10 = new JRadioButton("Hold Out - Query Set Fixed 10%"); 206 holdOutFixed10.addActionListener(new ActionListener() { 207 public void actionPerformed(ActionEvent e) 208 { 209 enableGroup("none"); 210 } 211 }); 212 holdOutFixed20 = new JRadioButton("Hold Out - Query Set Fixed 20%"); 213 holdOutFixed20.addActionListener(new ActionListener() { 214 public void actionPerformed(ActionEvent e) 215 { 216 enableGroup("none"); 217 } 218 }); 219 holdOutFixed30 = new JRadioButton("Hold Out - Query Set Fixed 30%"); 220 holdOutFixed30.addActionListener(new ActionListener() { 221 public void actionPerformed(ActionEvent e) 222 { 223 enableGroup("none"); 224 } 225 }); 226 227 228 sameSplit = new JRadioButton("Hold Out - Query Set Custom"); 229 sameSplit.addActionListener(new ActionListener() { 230 public void actionPerformed(ActionEvent e) 231 { 232 enableGroup("samesplit"); 233 } 234 }); 235 236 JPanel sameSplitPanel = new JPanel(); 237 sameSplitPanel.setLayout(new BoxLayout(sameSplitPanel, BoxLayout.Y_AXIS)); 238 sameSplitPanel.setBorder(BorderFactory.createEmptyBorder(2,15,2,2)); 239 240 JPanel sameSplitPanel1 = new JPanel(); 241 sameSplitGenerate = new JRadioButton("Generate New Query Set & Evaluate"); 242 sameSplitGenerate.addActionListener(new ActionListener() { 243 public void actionPerformed(ActionEvent e) 244 { 245 loadSplit.setEnabled(false); 246 sameSplitReuseFile.setEnabled(false); 247 saveSplit.setEnabled(true); 248 sameSplitGenerateFile.setEnabled(true); 249 sameSplitGeneratePercentSpinner.setEnabled(true); 250 sameSplitGeneratePercent.setEnabled(true); 251 } 252 }); 253 saveSplit = new JButton("Save Query Set"); 254 sameSplitGenerateFile = new JTextField(20); 255 saveSplit.addActionListener(new ActionListener() { 256 public void actionPerformed(ActionEvent e) 257 { 258 JFileChooser jfc = new JFileChooser("."); 259 int returnVal = jfc.showSaveDialog(null); 260 if(returnVal == JFileChooser.APPROVE_OPTION) 261 sameSplitGenerateFile.setText(jfc.getSelectedFile().toString()); 262 } 263 }); 264 265 JPanel sameSplitPanel1b = new JPanel(); 266 sameSplitGeneratePercent = new JLabel("Query Set percent:"); 267 sameSplitPercent = new SpinnerNumberModel(10, 5, 95, 5); 268 sameSplitGeneratePercentSpinner = new JSpinner(sameSplitPercent); 269 270 271 JPanel sameSplitPanel2 = new JPanel(); 272 sameSplitReuse = new JRadioButton("Evaluate Using Existing Query Set"); 273 sameSplitReuse.addActionListener(new ActionListener() { 274 public void actionPerformed(ActionEvent e) 275 { 276 loadSplit.setEnabled(true); 277 sameSplitReuseFile.setEnabled(true); 278 saveSplit.setEnabled(false); 279 sameSplitGenerateFile.setEnabled(false); 280 sameSplitGeneratePercentSpinner.setEnabled(false); 281 sameSplitGeneratePercent.setEnabled(false); 282 } 283 }); 284 loadSplit = new JButton("Open Query Set"); 285 sameSplitReuseFile = new JTextField(20); 286 loadSplit.addActionListener(new ActionListener() { 287 public void actionPerformed(ActionEvent e) 288 { 289 JFileChooser jfc = new JFileChooser("."); 290 int returnVal = jfc.showOpenDialog(null); 291 if(returnVal == JFileChooser.APPROVE_OPTION) 292 sameSplitReuseFile.setText(jfc.getSelectedFile().toString()); 293 } 294 }); 295 296 297 sameSplitPanel1.add(saveSplit); 298 sameSplitPanel1.add(sameSplitGenerateFile); 299 sameSplitPanel1b.add(sameSplitGeneratePercent); 300 sameSplitPanel1b.add(sameSplitGeneratePercentSpinner); 301 sameSplitPanel2.add(loadSplit); 302 sameSplitPanel2.add(sameSplitReuseFile); 303 304 sameSplitPanel.add(sameSplitGenerate); 305 sameSplitPanel.add(sameSplitPanel1); 306 sameSplitPanel.add(sameSplitPanel1b); 307 sameSplitPanel.add(sameSplitReuse); 308 sameSplitPanel.add(sameSplitPanel2); 309 sameSplitPanel1.setAlignmentX(JComponent.LEFT_ALIGNMENT); 310 sameSplitPanel2.setAlignmentX(JComponent.LEFT_ALIGNMENT); 311 sameSplitPanel1b.setAlignmentX(JComponent.LEFT_ALIGNMENT); 312 313 ButtonGroup sameSplitGroup = new ButtonGroup(); 314 sameSplitGroup.add(sameSplitGenerate); 315 sameSplitGroup.add(sameSplitReuse); 316 317 sameSplitGenerate.setSelected(true); 318 loadSplit.setEnabled(false); 319 sameSplitReuseFile.setEnabled(false); 320 321 holdOut = new JRadioButton("Hold Out - Query Set Random"); 322 holdOut.addActionListener(new ActionListener() { 323 public void actionPerformed(ActionEvent e) 324 { 325 enableGroup("holdout"); 326 } 327 }); 328 329 JPanel holdOutOptions = new JPanel(); 330 holdOutLabel1 = new JLabel("Query Set Percent"); 331 holdOutOptions.add(holdOutLabel1); 332 holdOutPercent = new SpinnerNumberModel(10, 5, 95, 5); 333 holdOutPercentSpinner = new JSpinner(holdOutPercent); 334 holdOutOptions.add(holdOutPercentSpinner); 335 holdOutLabel2 = new JLabel("Repetitions"); 336 holdOutOptions.add(holdOutLabel2); 337 holdOutRepetitions = new SpinnerNumberModel(1, 1, 10, 1); 338 holdOutRepetitionsSpinner = new JSpinner(holdOutRepetitions); 339 holdOutOptions.add(holdOutRepetitionsSpinner); 340 341 nFold = new JRadioButton("N-Fold - Random"); 342 nFold.addActionListener(new ActionListener() { 343 public void actionPerformed(ActionEvent e) 344 { 345 enableGroup("nfold"); 346 } 347 }); 348 349 JPanel nFoldOptions = new JPanel(); 350 nFoldLabel1 = new JLabel("Folds"); 351 nFoldOptions.add(nFoldLabel1); 352 nFoldFolds = new SpinnerNumberModel(4, 2, 10, 1); 353 nFoldFoldsSpinner = new JSpinner(nFoldFolds); 354 nFoldOptions.add(nFoldFoldsSpinner); 355 nFoldLabel2 = new JLabel("Repetitions"); 356 nFoldOptions.add(nFoldLabel2); 357 nFoldRepetitions = new SpinnerNumberModel(1, 1, 10, 1); 358 nFoldRepetitionsSpinner = new JSpinner(nFoldRepetitions); 359 nFoldOptions.add(nFoldRepetitionsSpinner); 360 361 ButtonGroup evaluatorGroup = new ButtonGroup(); 362 evaluatorGroup.add(leaveOneOut); 363 evaluatorGroup.add(holdOutFixed10); 364 evaluatorGroup.add(holdOutFixed20); 365 evaluatorGroup.add(holdOutFixed30); 366 evaluatorGroup.add(sameSplit); 367 evaluatorGroup.add(holdOut); 368 evaluatorGroup.add(nFold); 369 370 evaluatorPanel.add(leaveOneOut); 371 evaluatorPanel.add(holdOutFixed10); 372 evaluatorPanel.add(holdOutFixed20); 373 evaluatorPanel.add(holdOutFixed30); 374 evaluatorPanel.add(holdOut); 375 evaluatorPanel.add(holdOutOptions); 376 evaluatorPanel.add(nFold); 377 evaluatorPanel.add(nFoldOptions); 378 evaluatorPanel.add(sameSplit); 379 evaluatorPanel.add(sameSplitPanel); 380 381 leaveOneOut.setAlignmentX(Component.LEFT_ALIGNMENT); 382 holdOutFixed10.setAlignmentX(Component.LEFT_ALIGNMENT); 383 holdOutFixed20.setAlignmentX(Component.LEFT_ALIGNMENT); 384 holdOutFixed30.setAlignmentX(Component.LEFT_ALIGNMENT); 385 sameSplit.setAlignmentX(Component.LEFT_ALIGNMENT); 386 sameSplitPanel.setAlignmentX(Component.LEFT_ALIGNMENT); 387 holdOut.setAlignmentX(Component.LEFT_ALIGNMENT); 388 holdOutOptions.setAlignmentX(Component.LEFT_ALIGNMENT); 389 nFold.setAlignmentX(Component.LEFT_ALIGNMENT); 390 nFoldOptions.setAlignmentX(Component.LEFT_ALIGNMENT); 391 392 393 394 leaveOneOut.setSelected(true); 395 396 this.setEnabledHoldOut(false); 397 this.setEnabledNFold(false); 398 this.setEnabledSameSplit(false); 399 400 /** ****** Similarity Panel ************ */ 401 402 JPanel similPanel = new JPanel(); 403 similPanel.setBorder(BorderFactory.createTitledBorder("Similarity Function")); 404 similPanel.setLayout(new GridLayout(6, 1)); 405 406 compressionBased = new JRadioButton("Compression"); 407 normalizedCompression = new JRadioButton("Normalized Compression"); 408 cosine = new JRadioButton("Cosine Coefficient"); 409 dice = new JRadioButton("Dice Coefficient"); 410 jaccard = new JRadioButton("Jaccard Coefficient"); 411 overlap = new JRadioButton("Overlap Coefficient"); 412 413 ButtonGroup similGroup = new ButtonGroup(); 414 similGroup.add(compressionBased); 415 similGroup.add(normalizedCompression); 416 similGroup.add(cosine); 417 similGroup.add(dice); 418 similGroup.add(jaccard); 419 similGroup.add(overlap); 420 421 similPanel.add(compressionBased); 422 similPanel.add(normalizedCompression); 423 similPanel.add(cosine); 424 similPanel.add(dice); 425 similPanel.add(jaccard); 426 similPanel.add(overlap); 427 428 compressionBased.setSelected(true); 429 430 /** ****** Corpus Panel ************ */ 431 432 JPanel clasifPanel = new JPanel(); 433 clasifPanel.setBorder(BorderFactory.createTitledBorder("KNN Classification Method")); 434 clasifPanel.setLayout(new GridLayout(3, 1)); 435 436 majorityVoting = new JRadioButton("Majority Voting Method"); 437 weightedVoting = new JRadioButton("Weighted Voting Method"); 438 439 JPanel kPanel = new JPanel(); 440 kPanel.add(new JLabel("K value")); 441 kValue = new SpinnerNumberModel(3, 1, 10, 1); 442 kValueSpinner = new JSpinner(kValue); 443 kPanel.add(kValueSpinner); 444 445 ButtonGroup clasifGroup = new ButtonGroup(); 446 clasifGroup.add(majorityVoting); 447 clasifGroup.add(weightedVoting); 448 449 clasifPanel.add(weightedVoting); 450 clasifPanel.add(majorityVoting); 451 clasifPanel.add(kPanel); 452 453 weightedVoting.setSelected(true); 454 455 /** ********* Buttons ********** */ 456 457 JPanel buttons = new JPanel(); 458 459 evaluate = new JButton("Evaluate Application"); 460 visualize = new JButton("Visualize Case Base using Similarity Function"); 461 462 buttons.add(evaluate); 463 buttons.add(visualize); 464 465 evaluate.addActionListener(new ActionListener() { 466 public void actionPerformed(ActionEvent e) 467 { 468 evaluate(); 469 } 470 }); 471 472 visualize.addActionListener(new ActionListener() { 473 public void actionPerformed(ActionEvent e) 474 { 475 visualize(); 476 } 477 }); 478 479 /************ Progress Listener ****************/ 480 //SwingProgressBar shows the progress 481 ProgressListener listener = new SwingProgressBar(); 482 ProgressController.register(listener, CasesVisualization.class); 483 ProgressController.register(listener, LeaveOneOutEvaluator.class); 484 ProgressController.register(listener, NFoldEvaluator.class); 485 ProgressController.register(listener, SameSplitEvaluator.class); 486 ProgressController.register(listener, HoldOutEvaluator.class); 487 ProgressController.register(listener, OpennlpSplitter.class); 488 ProgressController.register(listener, StopWordsDetector.class); 489 ProgressController.register(listener, TextStemmer.class); 490 491 492 /** ********* Main Panel ********** */ 493 494 JPanel central = new JPanel(); 495 central.setLayout(new BoxLayout(central, BoxLayout.X_AXIS)); 496 central.add(corpusPanel); 497 central.add(similPanel); 498 central.add(evaluatorPanel); 499 central.add(clasifPanel); 500 501 JPanel main = new JPanel(); 502 main.setLayout(new BorderLayout()); 503 504 main.add(central, BorderLayout.CENTER); 505 main.add(buttons, BorderLayout.SOUTH); 506 507 this.getContentPane().add(main); 508 this.pack(); 509 510 this.setTitle("Spam Filter Toy"); 511 Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 512 setBounds((screenSize.width - this.getWidth()) / 2, (screenSize.height - this.getHeight()) / 2, getWidth(), 513 getHeight()); 514 515 this.addWindowListener(new WindowAdapter() 516 { public void windowClosing(WindowEvent arg0) 517 { 518 System.exit(0); 519 } 520 }); 521 } 522 523 void enableGroup(String group) 524 { 525 if (group.equals("nfold")) 526 { 527 setEnabledNFold(true); 528 setEnabledHoldOut(false); 529 setEnabledSameSplit(false); 530 } else if (group.equals("holdout")) 531 { 532 setEnabledNFold(false); 533 setEnabledSameSplit(false); 534 setEnabledHoldOut(true); 535 } 536 else if (group.equals("samesplit")) 537 { 538 setEnabledNFold(false); 539 setEnabledSameSplit(true); 540 setEnabledHoldOut(false); 541 } 542 else if(group.equals("none")) 543 { 544 setEnabledNFold(false); 545 setEnabledSameSplit(false); 546 setEnabledHoldOut(false); 547 } 548 } 549 550 551 private void setEnabledSameSplit(boolean enabled) 552 { 553 this.sameSplitGenerate.setEnabled(enabled); 554 this.sameSplitReuse.setEnabled(enabled); 555 if(!enabled) 556 { 557 this.saveSplit.setEnabled(enabled); 558 this.loadSplit.setEnabled(enabled); 559 this.sameSplitGenerateFile.setEnabled(enabled); 560 this.sameSplitReuseFile.setEnabled(enabled); 561 this.sameSplitGeneratePercentSpinner.setEnabled(enabled); 562 this.sameSplitGeneratePercent.setEnabled(enabled); 563 } 564 else 565 { 566 567 if(sameSplitGenerate.isSelected()) 568 { 569 saveSplit.setEnabled(true); 570 sameSplitGenerateFile.setEnabled(true); 571 sameSplitGeneratePercentSpinner.setEnabled(true); 572 sameSplitGeneratePercent.setEnabled(true); 573 } 574 else 575 { 576 loadSplit.setEnabled(true); 577 sameSplitReuseFile.setEnabled(true); 578 } 579 } 580 } 581 582 private void setEnabledNFold(boolean enabled) 583 { 584 nFoldLabel1.setEnabled(enabled); 585 nFoldLabel2.setEnabled(enabled); 586 nFoldFoldsSpinner.setEnabled(enabled); 587 nFoldRepetitionsSpinner.setEnabled(enabled); 588 } 589 590 private void setEnabledHoldOut(boolean enabled) 591 { 592 holdOutLabel1.setEnabled(enabled); 593 holdOutLabel2.setEnabled(enabled); 594 holdOutPercentSpinner.setEnabled(enabled); 595 holdOutRepetitionsSpinner.setEnabled(enabled); 596 } 597 598 void setButtonsEnabled(boolean enabled) 599 { 600 visualize.setEnabled(enabled); 601 evaluate.setEnabled(enabled); 602 } 603 604 void evaluate() 605 { 606 SpamFilterApp app = configureApp(); 607 Thread thread = new Thread(new Evaluation(app)); 608 thread.start(); 609 } 610 611 /** 612 * Thread for running the Visualization 613 * 614 * @author Juan A. Recio-Garcia 615 * @version 1.0 616 */ 617 class Evaluation implements Runnable 618 { 619 SpamFilterApp app; 620 621 Evaluation(SpamFilterApp app) 622 { 623 this.app = app; 624 } 625 626 public void run() 627 { 628 setButtonsEnabled(false); 629 try 630 { 631 if (leaveOneOut.isSelected()) 632 { 633 LeaveOneOutEvaluator eval = new LeaveOneOutEvaluator(); 634 eval.init(app); 635 eval.LeaveOneOut(); 636 } 637 else if(sameSplit.isSelected()) 638 { 639 SameSplitEvaluator eval = new SameSplitEvaluator(); 640 eval.init(app); 641 if(sameSplitGenerate.isSelected()) 642 { 643 String file = sameSplitGenerateFile.getText(); 644 int percent = sameSplitPercent.getNumber().intValue(); 645 eval.generateSplit(percent, file); 646 eval.HoldOutfromFile(file); 647 } 648 else if(sameSplitReuse.isSelected()) 649 { 650 eval.HoldOutfromFile(sameSplitReuseFile.getText()); 651 } 652 } 653 else if (holdOut.isSelected()) 654 { 655 HoldOutEvaluator eval = new HoldOutEvaluator(); 656 eval.init(app); 657 int testPercent = holdOutPercent.getNumber().intValue(); 658 int repetitions = holdOutRepetitions.getNumber().intValue(); 659 eval.HoldOut(testPercent, repetitions); 660 } 661 else if (nFold.isSelected()) 662 { 663 NFoldEvaluator eval = new NFoldEvaluator(); 664 eval.init(app); 665 int folds = nFoldFolds.getNumber().intValue(); 666 int repetitions = nFoldRepetitions.getNumber().intValue(); 667 eval.NFoldEvaluation(folds, repetitions); 668 } 669 else 670 { 671 String filename = "jcolibri/test/test16/splits/corpus"; 672 if(corpus300.isSelected()) 673 filename+="300-"; 674 else 675 filename+="600-"; 676 677 if(holdOutFixed10.isSelected()) 678 filename+="10.split"; 679 else if(holdOutFixed20.isSelected()) 680 filename+="20.split"; 681 else if(holdOutFixed30.isSelected()) 682 filename+="30.split"; 683 684 SameSplitEvaluator eval = new SameSplitEvaluator(); 685 eval.init(app); 686 eval.HoldOutfromFile(filename); 687 688 } 689 690 double tp = app.getTruePositives(); 691 double fp = app.getFalsePositives(); 692 double fn = app.getFalseNegatives(); 693 double tn = app.getTrueNegatives(); 694 double precision = tp / (tp + fp); 695 double recall = tp / (tp + fn); 696 697 EvaluationReport report = Evaluator.getEvaluationReport(); 698 report.putOtherData("Precision", String.valueOf(precision)); 699 report.putOtherData("Recall", String.valueOf(recall)); 700 report.putOtherData("True Spam", String.valueOf(tp)); 701 report.putOtherData("True Ham", String.valueOf(tn)); 702 report.putOtherData("False Spam", String.valueOf(fp)); 703 report.putOtherData("False Ham", String.valueOf(fn)); 704 705 System.out.println(report); 706 jcolibri.evaluation.tools.EvaluationResultGUI.show(report, "Spam Filter Toy - Evaluation", false); 707 708 } catch (Exception e) 709 { 710 org.apache.commons.logging.LogFactory.getLog(SpamFilterApp.class).error(e); 711 712 } 713 setButtonsEnabled(true); 714 } 715 } 716 717 void visualize() 718 { 719 SpamFilterApp app = configureApp(); 720 Thread thread = new Thread(new Visualization(app)); 721 thread.start(); 722 } 723 724 /** 725 * Thread for running the Visualization 726 * 727 * @author Juan A. Recio-Garcia 728 * @version 1.0 729 */ 730 class Visualization implements Runnable 731 { 732 SpamFilterApp app; 733 Visualization(SpamFilterApp app) 734 { 735 this.app = app; 736 } 737 738 public void run() 739 { 740 setButtonsEnabled(false); 741 try 742 { 743 app.configure(); 744 CBRCaseBase _caseBase = app.preCycle(); 745 jcolibri.extensions.visualization.CasesVisualization.visualize(_caseBase.getCases(), app.getKNNConfig()); 746 } catch (ExecutionException e) 747 { 748 org.apache.commons.logging.LogFactory.getLog(SpamFilterApp.class).error(e); 749 750 } 751 setButtonsEnabled(true); 752 753 } 754 } 755 756 private SpamFilterApp configureApp() 757 { 758 // Connector 759 String corpusFile = null; 760 if(this.corpus300.isSelected()) 761 corpusFile = "jcolibri/test/test16/ling-corpus-300.zip"; 762 else if(this.corpus600.isSelected()) 763 corpusFile = "jcolibri/test/test16/ling-corpus-600.zip"; 764 765 // Simil function 766 LocalSimilarityFunction similFunc = null; 767 if(this.compressionBased.isSelected()) 768 similFunc = new CompressionBased(new GZipCompressor()); 769 else if(this.normalizedCompression.isSelected()) 770 similFunc = new NormalisedCompression(new GZipCompressor()); 771 else if(this.cosine.isSelected()) 772 similFunc = new CosineCoefficient(); 773 else if(this.dice.isSelected()) 774 similFunc = new DiceCoefficient(); 775 else if(this.jaccard.isSelected()) 776 similFunc = new JaccardCoefficient(); 777 else if(this.overlap.isSelected()) 778 similFunc = new OverlapCoefficient(); 779 780 // Classification method 781 KNNClassificationMethod classifMethod = null; 782 if(this.majorityVoting.isSelected()) 783 classifMethod = new MajorityVotingMethod(); 784 else if(this.weightedVoting.isSelected()) 785 classifMethod = new SimilarityWeightedVotingMethod(); 786 787 //K 788 int k = this.kValue.getNumber().intValue(); 789 790 SpamFilterApp app = new SpamFilterApp(corpusFile); 791 app.setSimilFunc(similFunc); 792 app.setClasifMethod(classifMethod); 793 app.setK(k); 794 795 return app; 796 } 797 }