/*
############################################################################
##
## Copyright (C) 2006-2009 University of Utah. All rights reserved.
##
## This file is part of DeepPeep.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following to ensure GNU General Public
## Licensing requirements will be met:
## http://www.opensource.org/licenses/gpl-license.php
##
## If you are unsure which license is appropriate for your use (for
## instance, you are interested in developing a commercial derivative
## of DeepPeep), please contact us at deeppeep@sci.utah.edu.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
############################################################################
*/
package focusedCrawler.link.classifier;

import java.net.MalformedURLException;

import focusedCrawler.link.frontier.LinkRelevance;
import focusedCrawler.util.parser.LinkNeighborhood;
import focusedCrawler.util.parser.PaginaURL;

/**
 *
 * <p> </p>
 *
 * <p>Description: This classifier uses the naive bayes link classifier to
 * set the link priority.</p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p> </p>
 *
 * @author Luciano Barbosa
 * @version 1.0
 */

public class LinkClassifierImpl implements LinkClassifier{

	private int[] weights;
	private int intervalRandom = 100;
	private LNClassifier lnClassifier;

	public LinkClassifierImpl(LNClassifier lnClassifier) {
		this.weights = new int[]{2,1,0};
		this.lnClassifier = lnClassifier;
	}

  /**
   * This method classifies links based on the priority set by the
   * naive bayes link classifier.
   * @param page Page
   * @return LinkRelevance[]
   * @throws LinkClassifierException
   */
  public LinkRelevance[] classify(PaginaURL page) throws LinkClassifierException {
	  LinkRelevance[] linkRelevance = null;
	  try {
		  LinkNeighborhood[] lns = page.getLinkNeighboor();
		  linkRelevance = new LinkRelevance[lns.length];
		  for (int i = 0; i < lns.length; i++) {
			  linkRelevance[i] = classify(lns[i]);
		  }
	  }catch(Exception ex){
		  ex.printStackTrace();
		  throw new LinkClassifierException(ex.getMessage());
	  }
	  return linkRelevance;
  }

  public LinkRelevance classify(LinkNeighborhood ln) throws LinkClassifierException {

	  LinkRelevance linkRel = null;
	  try {
		  double[] prob = lnClassifier.classify(ln);
		  int classificationResult = -1;
		  double maxProb = -1;
		  for (int i = 0; i < prob.length; i++) {
			  if(prob[i] > maxProb){
				  maxProb = prob[i];
				  classificationResult = i;
			  }
		  }
		  double probability = prob[classificationResult]*100;
		  if(probability == 100){
			  probability = 99;
		  }
		  classificationResult = weights[classificationResult];
		  double result = (classificationResult * intervalRandom) + probability ;  	
		  linkRel = new LinkRelevance(ln.getLink(),result);
	  }catch (MalformedURLException ex) {
		  ex.printStackTrace();
		  throw new LinkClassifierException(ex.getMessage());
	  }catch (Exception ex) {
		  ex.printStackTrace();
		  throw new LinkClassifierException(ex.getMessage());
	  }
	  return linkRel;
  }

}

@@ -1039,48 +1039,8 @@
 r;%0A%0A
-import java.net.MalformedURLException;%0A%0A
 impo
@@ -1505,16 +1505,22 @@
 private 
+final 
 int%5B%5D we
@@ -1524,18 +1524,36 @@
  weights
+ = new int%5B%5D%7B2,1,0%7D
 ;
-%0D
 %0A%09privat
@@ -1554,16 +1554,22 @@
 private 
+final 
 int inte
@@ -1595,16 +1595,22 @@
 private 
+final 
 LNClassi
@@ -1627,16 +1627,17 @@
 ssifier;
+%0D
 %0A%0D%0A%09publ
@@ -1690,44 +1690,8 @@
 r) %7B
-%0D%0A%09%09this.weights = new int%5B%5D%7B2,1,0%7D;
 %0A%09%09t
@@ -2056,32 +2056,63 @@
 evance = null;%0D%0A
+%09  LinkNeighborhood ln = null;%0A
 %09  try %7B%0A%09%09  Lin
@@ -2252,19 +2252,51 @@
  i++) %7B%0A
-%09%09%09
+            ln = lns%5Bi%5D;%0A          
   linkRe
@@ -2319,20 +2319,16 @@
 ssify(ln
-s%5Bi%5D
 );%0A%09%09  %7D
@@ -2328,25 +2328,32 @@
 ;%0A%09%09  %7D%0A
-%09
+      
   %7D
+ 
 catch
+ 
 (Excepti
@@ -2362,40 +2362,21 @@
  ex)
-%7B%0D%0A%09%09  ex.printStackTrace();%0D%0A%09%09
+ %7B%0A          
   th
@@ -2407,36 +2407,147 @@
 ception(
-ex.getMessage());%0D%0A%09
+%22Failed to classify link %5B%22 + ln.getLink().toString()%0A                    + %22%5D from page: %22 + page.getURL().toString(), ex);%0A      
   %7D%0D%0A%09  
@@ -3274,27 +3274,16 @@
 %09  %7D
+ 
 catch (
-MalformedURL
 Exce
@@ -3299,37 +3299,13 @@
  %7B%0D%0A
-%09
 %09  
-ex.printStackTrace();%0D%0A%09%09
+  
   th
@@ -3340,135 +3340,63 @@
 ion(
-ex.getMessage());%0D%0A%09  %7Dcatch (Exception ex) %7B%0D%0A%09%09  ex.printStackTrace();%0D%0A%09%09  throw new LinkClassifierException(ex.getMessage()
+%22Failed to classify link: %22+ln.getLink().toString(), ex
 );%0D%0A

Improve log messages and make class fields final in LinkClassifierImpl