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";
	}
}

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.3"));
		
		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") > threshold) {
				item.addTopic("environment", probMap.get("Relevant"));
				if(probMap.get("Relevant") > 0.5) {
					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) { }
		
		return true;
	}

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

Fix environmental classifier