package gr.iti.mklab.sfc.filters;

import java.util.Map;

import gr.iti.mklab.classifiers.ClassificationResult;
import gr.iti.mklab.classifiers.EnvironmentalClassification;
import gr.iti.mklab.framework.common.domain.Item;
import gr.iti.mklab.framework.common.domain.config.Configuration;
import gr.iti.mklab.sfc.filters.ItemFilter;

public class EnvironmentalClassifier extends ItemFilter {

	private EnvironmentalClassification ec;
	private double threshold;
	
	public EnvironmentalClassifier(Configuration configuration) {
		super(configuration);
		
		threshold = Double.parseDouble(configuration.getParameter("threshold", "0.2"));
		
		ec = new EnvironmentalClassification();
	}

	@Override
	public boolean accept(Item item) {
		String lang = item.getLanguage();
		
		String text = "";
		String title = item.getTitle();
		String description = item.getDescription();
		
		if(title != null) {
			text += " " + title;
		}
		if (description != null) {
			text +=  " " + title;
		}

		try {
			ClassificationResult result = ec.classify(text, lang, false);
		
			Map<String, Double> probMap = result.getFirstClassifierProbabilityMap();		
			
			if(probMap.get("Relevant") > 0.5) {
				item.addTopic("environment", probMap.get("Relevant"));
				Map<String, Double> subTopics = result.getSecondClassifierProbabilitityMap();
				for(String subTopic : subTopics.keySet()) {
					if(subTopics.get(subTopic) > threshold) {
						item.addTopic(subTopic, subTopics.get(subTopic));
					}
				}
				
				return true;
			}
			else {
				return false;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public String name() {
		return "EnvironmentalClassifier";
	}
}

@@ -626,9 +626,9 @@
  %220.
-2
+3
 %22));
@@ -1169,11 +1169,17 @@
 ) %3E 
-0.5
+threshold
 ) %7B%0A
@@ -1233,24 +1233,65 @@
 elevant%22));%0A
+%09%09%09%09if(probMap.get(%22Relevant%22) %3E 0.5) %7B%0A%09
 %09%09%09%09Map%3CStri
@@ -1364,16 +1364,17 @@
 ();%0A%09%09%09%09
+%09
 for(Stri
@@ -1405,24 +1405,25 @@
 keySet()) %7B%0A
+%09
 %09%09%09%09%09if(subT
@@ -1459,24 +1459,25 @@
 ld) %7B%0A%09%09%09%09%09%09
+%09
 item.addTopi
@@ -1510,24 +1510,32 @@
 subTopic));%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%7D
@@ -1628,53 +1628,28 @@
 e) %7B
+ %7D
 %0A%09%09
-%09e.printStackTrace();%0A%09%09%09return false;%0A%09%09%7D
+%0A%09%09return true;
 %0A%09%7D%0A

Fix environmental classifier