<commit_before>package qa.qcri.aidr.trainer.api.template;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import qa.qcri.aidr.dbmanager.dto.CollectionDTO;
import qa.qcri.aidr.dbmanager.dto.ModelFamilyDTO;
import qa.qcri.aidr.dbmanager.dto.NominalLabelDTO;
import qa.qcri.aidr.dbmanager.entities.model.NominalLabel;
import qa.qcri.aidr.dbmanager.entities.task.Document;
import qa.qcri.aidr.dbmanager.entities.task.DocumentNominalLabel;
import qa.qcri.aidr.dbmanager.entities.task.DocumentNominalLabelId;
import qa.qcri.aidr.dbmanager.entities.task.TaskAnswer;
import qa.qcri.aidr.trainer.api.service.CrisisService;

/**
 * Created with IntelliJ IDEA.
 * User: jilucas
 * Date: 9/16/13
 * Time: 4:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class PybossaTemplate {
	
	private static Logger logger=Logger.getLogger(PybossaTemplate.class);

    public TaskAnswerResponse getPybossaTaskAnswer(String data, CrisisService crisisService){

        TaskAnswer taskAnswer = null;
        JSONArray  attributeIDJson = new JSONArray();
        TaskAnswerResponse taskAnswerResponse = new TaskAnswerResponse();

        try {
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(data);

            JSONArray jsonObject = (JSONArray) obj;

            Iterator itr= jsonObject.iterator();

            while(itr.hasNext()){
                JSONArray taskAnswerJson = new JSONArray();

                JSONObject featureJsonObj = (JSONObject)itr.next();
                JSONObject info = (JSONObject)featureJsonObj.get("info");
                String category = (String)info.get("category");
                String[] categorySet = category.split("\\,");
                Long crisisID = (Long)info.get("crisisID");
                Long documentID = (Long)info.get("documentID");
                Long userID = (Long)info.get("aidrID");
                Long attributeID = (Long)info.get("attributeID");

                CollectionDTO crisis = crisisService.findByCrisisID(crisisID) ;
                List<ModelFamilyDTO> modelFamilySet= crisis.getModelFamiliesDTO();

                DocumentNominalLabel documentNominalLabel = null;
                // String category ="Food and water";

                for (ModelFamilyDTO modelFamily : modelFamilySet){
                    if(modelFamily.getNominalAttributeDTO().getNominalAttributeId().equals(attributeID)){
                        ModelFamilyDTO currentModelFamily = modelFamily;

                        List<NominalLabelDTO> nominalLabelSet = modelFamily.getNominalAttributeDTO().getNominalLabelsDTO();
                        logger.info("attribute name : "   + modelFamily.getNominalAttributeDTO().getName() + "\n");
                        for (NominalLabelDTO nominalLabel : nominalLabelSet){
                            JSONObject taskAnswerElement = new JSONObject();
                            if(nominalLabel.getNominalAttributeDTO().equals(attributeID)) {
                                if(findMatchingLabel(categorySet, nominalLabel.getNominalLabelCode())) {
                                    Long labelID = nominalLabel.getNominalLabelId().longValue();
                                    if(!attributeIDJson.contains(attributeID) ){
                                        attributeIDJson.add(attributeID ) ;
                                    }
<commit_msg>Fix bug in aidr-trainer ejb migration<commit_after>package qa.qcri.aidr.trainer.api.template;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import qa.qcri.aidr.dbmanager.dto.CollectionDTO;
import qa.qcri.aidr.dbmanager.dto.ModelFamilyDTO;
import qa.qcri.aidr.dbmanager.dto.NominalLabelDTO;
import qa.qcri.aidr.dbmanager.entities.model.NominalLabel;
import qa.qcri.aidr.dbmanager.entities.task.Document;
import qa.qcri.aidr.dbmanager.entities.task.DocumentNominalLabel;
import qa.qcri.aidr.dbmanager.entities.task.DocumentNominalLabelId;
import qa.qcri.aidr.dbmanager.entities.task.TaskAnswer;
import qa.qcri.aidr.trainer.api.service.CrisisService;

/**
 * Created with IntelliJ IDEA.
 * User: jilucas
 * Date: 9/16/13
 * Time: 4:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class PybossaTemplate {
	
	private static Logger logger=Logger.getLogger(PybossaTemplate.class);

    public TaskAnswerResponse getPybossaTaskAnswer(String data, CrisisService crisisService){

        TaskAnswer taskAnswer = null;
        JSONArray  attributeIDJson = new JSONArray();
        TaskAnswerResponse taskAnswerResponse = new TaskAnswerResponse();

        try {
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(data);

            JSONArray jsonObject = (JSONArray) obj;

            Iterator itr= jsonObject.iterator();

            while(itr.hasNext()){
                JSONArray taskAnswerJson = new JSONArray();

                JSONObject featureJsonObj = (JSONObject)itr.next();
                JSONObject info = (JSONObject)featureJsonObj.get("info");
                String category = (String)info.get("category");
                String[] categorySet = category.split("\\,");
                Long crisisID = (Long)info.get("crisisID");
                Long documentID = (Long)info.get("documentID");
                Long userID = (Long)info.get("aidrID");
                Long attributeID = (Long)info.get("attributeID");

                CollectionDTO crisis = crisisService.findByCrisisID(crisisID) ;
                List<ModelFamilyDTO> modelFamilySet= crisis.getModelFamiliesDTO();

                DocumentNominalLabel documentNominalLabel = null;
                // String category ="Food and water";

                for (ModelFamilyDTO modelFamily : modelFamilySet){
                    if(modelFamily.getNominalAttributeDTO().getNominalAttributeId().equals(attributeID)){
                        ModelFamilyDTO currentModelFamily = modelFamily;

                        List<NominalLabelDTO> nominalLabelSet = modelFamily.getNominalAttributeDTO().getNominalLabelsDTO();
                        logger.info("attribute name : "   + modelFamily.getNominalAttributeDTO().getName() + "\n");
                        for (NominalLabelDTO nominalLabel : nominalLabelSet){
                            JSONObject taskAnswerElement = new JSONObject();
                            if(nominalLabel.getNominalAttributeDTO().getNominalAttributeId().equals(attributeID)) {
                                if(findMatchingLabel(categorySet, nominalLabel.getNominalLabelCode())) {
                                    Long labelID = nominalLabel.getNominalLabelId().longValue();
                                    if(!attributeIDJson.contains(attributeID) ){
                                        attributeIDJson.add(attributeID ) ;
                                    }

{'fraction_non_alphanumeric': 0.09816971713810316, 'fraction_numerical': 0.0024958402662229617, 'mean_word_length': 2.487911025145068, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6038512', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1924, 'n_words': 339}