package de.tudarmstadt.ukp.experiments.pythagoras.featureExtractors.pos;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;

import de.tudarmstadt.ukp.experiments.pythagoras.annotations.TranscribedText;
import de.tudarmstadt.ukp.experiments.pythagoras.featureExtractors.tone.TeacherDictionaryFeatureExtractor;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.dkpro.tc.api.features.DocumentFeatureExtractor;
import de.tudarmstadt.ukp.dkpro.tc.api.features.Feature;
import de.tudarmstadt.ukp.dkpro.tc.api.features.FeatureExtractorResource_ImplBase;
import de.tudarmstadt.ukp.dkpro.tc.exception.TextClassificationException;

/**
 * Feature extractor based on the frequency of top-occurring pronouns from pre-defined word lists
 * 
 * @author Tahir Sousa
 * @version last updated: Apr 03, 2014 [Sousa]
 */
public class PronounRatioExtractor extends FeatureExtractorResource_ImplBase
    implements DocumentFeatureExtractor
{
    public static final String PRONOUN_RATIO = "PronounRatio";

    private static final String GROUP_PREFIX = "CSyn_PRat__";
    
    @Override
    public List<Feature> extract(JCas jcas) throws TextClassificationException
    {

        double nrOfTeacherWords = 0.0;
        double nrOfTeacherPronouns = 0.0;
        
        double adverbialPronounTeacher = 0.0;
        
        double demonstrativePronounTeacher = 0.0;
        double att_demonstrativePronounTeacher =0.0;
        double sub_demonstrativePronounTeacher = 0.0;        
        
        double indefinitePronounTeacher = 0.0;
        double sub_indefinitePronounTeacher = 0.0;
        double att_indefinitePronounTeacher = 0.0;
        double attD_indefinitePronounTeacher = 0.0;
        
        double personalIrreflPronounTeacher = 0.0;
        
        double personalReflPronounTeacher = 0.0;
        
        double possessivePronounTeacher = 0.0;
        double sub_possessivePronounTeacher = 0.0;
        double att_possessivePronounTeacher = 0.0;
        
        double relativePronounTeacher = 0.0;
        double sub_relativePronounTeacher = 0.0;
        double att_relativePronounTeacher = 0.0;
        
        double interrogativePronounTeacher = 0.0;
        double sub_interrogativePronounTeacher = 0.0;
        double att_interrogativePronounTeacher = 0.0;
        double adv_interrogativePronounTeacher = 0.0;
        
        double nrOfStudentWords = 0.0;
        double nrOfStudentPronouns = 0.0;
        double adverbialPronounStudent = 0.0;
        
        double demonstrativePronounStudent = 0.0;
        double att_demonstrativePronounStudent = 0.0;
        double sub_demonstrativePronounStudent = 0.0;        
        
        double indefinitePronounStudent = 0.0;
        double sub_indefinitePronounStudent = 0.0;
        double att_indefinitePronounStudent = 0.0;
        double attD_indefinitePronounStudent = 0.0;
        
        double personalIrreflPronounStudent = 0.0;
        
        double personalReflPronounStudent = 0.0;
        
        double possessivePronounStudent = 0.0;
        double sub_possessivePronounStudent = 0.0;
        double att_possessivePronounStudent = 0.0;
        
        double relativePronounStudent = 0.0;
        double sub_relativePronounStudent = 0.0;
        double att_relativePronounStudent = 0.0;
        
        double interrogativePronounStudent = 0.0;
        double sub_interrogativePronounStudent = 0.0;
        double att_interrogativePronounStudent = 0.0;
        double adv_interrogativePronounStudent = 0.0;
        
        List<String> exclude = Arrays.asList("#", "[", "]", "{", "}", "(", ")", ",", ".", ":", "?", "!", "-", "...", "*", "//", "/", "()");
        TeacherDictionaryFeatureExtractor extDicFeatExt = new TeacherDictionaryFeatureExtractor(); 
        
        for	(TranscribedText tt : JCasUtil.select(jcas, TranscribedText.class))	{
        	if	(tt.getSpeaker().contains("T"))	{
        		List<Token> tokens = JCasUtil.selectCovered(jcas, Token.class, tt);
        		
        		for (Token t : JCasUtil.selectCovered(jcas, Token.class, tt)) {

                    if (! exclude.contains(t.getCoveredText())) {
                        nrOfTeacherWords++;
                        String pos = t.getPos().getPosValue();
                        if (pos.startsWith("P") && !pos.startsWith("PTK")) {
                        	//System.out.println("Pronoun: " + "\t" + pos + "\t" + t.getCoveredText() + "\n");
                            //extDicFeatExt.logFeatureContext("pronounTypes.txt", "Pronoun: " + "\t" + pos + "\t" + t.getCoveredText() + "\n", true);
                        	nrOfTeacherPronouns++;
                        	if	(pos.startsWith("PAV"))
                        		adverbialPronounTeacher++;
                        	else if	(pos.startsWith("PD"))	{
                        		demonstrativePronounTeacher++;
                        		
								if	(pos.startsWith("PDAT"))	
                        			att_demonstrativePronounTeacher++;
                        		
                        		else if	(pos.startsWith("PDS"))	
                        			sub_demonstrativePronounTeacher++;
                        		
                        	}
                        		 
                        	else if	(pos.startsWith("PI"))	{
                        		indefinitePronounTeacher++;
                        		if	(pos.startsWith("PIS"))
                        			sub_indefinitePronounTeacher++;
                        		else if	(pos.startsWith("PIAT"))
                        			att_indefinitePronounTeacher++;
                        		else if	(pos.startsWith("PIDAT"))
                        			attD_indefinitePronounTeacher++;
                        	}
                        		
                        	else if	(pos.startsWith("PPER"))
                        		personalIrreflPronounTeacher++;
                        	else if	(pos.startsWith("PRF"))
                        		personalReflPronounTeacher++;
                        	else if	(pos.startsWith("PPOS"))	{
                        		possessivePronounTeacher++;
                        		if (pos.startsWith("PPOSS"))	
                            		sub_possessivePronounTeacher++;
                        		else if (pos.startsWith("PPOSAT"))	
                            		att_possessivePronounTeacher++;
                        	}
                        		
                        	else if	(pos.startsWith("PREL"))	{
                        		relativePronounTeacher++;
                        		if	(pos.startsWith("PRELS"))
                        			sub_relativePronounTeacher++;
                        		else if	(pos.startsWith("PRELAT"))
                        			att_relativePronounTeacher++;
                        	}
                        		
                        	else if	(pos.startsWith("PW"))	{
                        		interrogativePronounTeacher++;
                        		if	(pos.startsWith("PWS"))
                        			sub_interrogativePronounTeacher++;
                        		else if	(pos.startsWith("PWAT"))
                        			att_interrogativePronounTeacher++;
                        		else if	(pos.startsWith("PWAV"))
                        			adv_interrogativePronounTeacher++;
                        	}
                        		
                        }
                    }

                }	
        	}
        	
        	else if	(tt.getSpeaker().contains("S"))	{
        		List<Token> tokens = JCasUtil.selectCovered(jcas, Token.class, tt);
        		for (Token t : JCasUtil.selectCovered(jcas, Token.class, tt)) {

                    if (! exclude.contains(t.getCoveredText())) {
                        nrOfStudentWords++;
                        String pos = t.getPos().getPosValue();
                        if (pos.startsWith("P") && !pos.startsWith("PTK")) {
                        	//System.out.println("Pronoun: " + "\t" + pos + "\t" + t.getCoveredText() + "\n");
                        	//extDicFeatExt.logFeatureContext("pronounTypes.txt", "Pronoun: " + "\t" + pos + "\t" + t.getCoveredText() + "\n", true);
                        	nrOfStudentPronouns++;
                        	if	(pos.startsWith("PAV"))
                        		adverbialPronounStudent++;
                        	else if	(pos.startsWith("PD"))	{
                        		demonstrativePronounStudent++;
                        		
								if	(pos.startsWith("PDAT"))	
                        			att_demonstrativePronounStudent++;
                        		
                        		else if	(pos.startsWith("PDS"))	
                        			sub_demonstrativePronounStudent++;
                        		
                        	}
                        		 
                        	else if	(pos.startsWith("PI"))	{
                        		indefinitePronounStudent++;
                        		if	(pos.startsWith("PIS"))
                        			sub_indefinitePronounStudent++;
                        		else if	(pos.startsWith("PIAT"))
                        			att_indefinitePronounStudent++;
                        		else if	(pos.startsWith("PIDAT"))
                        			attD_indefinitePronounStudent++;
                        	}
                        		
                        	else if	(pos.startsWith("PPER"))
                        		personalIrreflPronounStudent++;
                        	else if	(pos.startsWith("PRF"))
                        		personalReflPronounStudent++;
                        	else if	(pos.startsWith("PPOS"))	{
                        		possessivePronounStudent++;
                        		if (pos.startsWith("PPOSS"))	
                            		sub_possessivePronounStudent++;
                        		else if (pos.startsWith("PPOSAT"))	
                            		att_possessivePronounStudent++;
                        	}
                        		
                        	else if	(pos.startsWith("PREL"))	{
                        		relativePronounStudent++;
                        		if	(pos.startsWith("PRELS"))
                        			sub_relativePronounStudent++;
                        		else if	(pos.startsWith("PRELAT"))
                        			att_relativePronounStudent++;
                        	}
                        		
                        	else if	(pos.startsWith("PW"))	{
                        		interrogativePronounStudent++;
                        		if	(pos.startsWith("PWS"))
                        			sub_interrogativePronounStudent++;
                        		else if	(pos.startsWith("PWAT"))
                        			att_interrogativePronounStudent++;
                        		else if	(pos.startsWith("PWAV"))
                        			adv_interrogativePronounStudent++;
                        	}
                        }
                    }

                }	
        	}
        }
        
        double teacherPronounRatio = nrOfTeacherPronouns / nrOfTeacherWords;
        double studentPronounRatio = nrOfStudentPronouns / nrOfStudentWords;
        List<Feature> featList = new ArrayList<Feature>();
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ PRONOUN_RATIO+"_T", teacherPronounRatio)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ PRONOUN_RATIO+"_S", studentPronounRatio)));
        
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AdverbialPronounRatio"+"_T", adverbialPronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "DemonstrativePronounRatio"+"_T", demonstrativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubDemonstrativePronounRatio"+"_T", sub_demonstrativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttDemonstrativePronounRatio"+"_T", att_demonstrativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "IndefinitePronounRatio"+"_T", indefinitePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubIndefinitePronounRatio"+"_T", sub_indefinitePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttIndefinitePronounRatio"+"_T", att_indefinitePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttDetIndefinitePronounRatio"+"_T", attD_indefinitePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "PersIrreflPronounRatio"+"_T", personalIrreflPronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "PersonalReflPronounRatio"+"_T", personalReflPronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "PossessivePronounRatio"+"_T", possessivePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubPossessivePronounRatio"+"_T", sub_possessivePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttPossessivePronounRatio"+"_T", att_possessivePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "RelativePronounRatio"+"_T", relativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubRelativePronounRatio"+"_T", sub_relativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttRelativePronounRatio"+"_T", att_relativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "InterrogativePronounRatio"+"_T", interrogativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubInterrogativePronounRatio"+"_T", sub_interrogativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttInterrogativePronounRatio"+"_T", att_interrogativePronounTeacher/nrOfTeacherWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AdvInterrogativePronounRatio"+"_T", adv_interrogativePronounTeacher/nrOfTeacherWords)));
        
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AdverbialPronounRatio"+"_S", adverbialPronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "DemonstrativePronounRatio"+"_S", demonstrativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubDemonstrativePronounRatio"+"_S", sub_demonstrativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttDemonstrativePronounRatio"+"_S", att_demonstrativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "IndefinitePronounRatio"+"_S", indefinitePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubIndefinitePronounRatio"+"_S", sub_indefinitePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttIndefinitePronounRatio"+"_S", att_indefinitePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttDetIndefinitePronounRatio"+"_S", attD_indefinitePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "PersIrreflPronounRatio"+"_S", personalIrreflPronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "PersonalReflPronounRatio"+"_S", personalReflPronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "PossessivePronounRatio"+"_S", possessivePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubPossessivePronounRatio"+"_S", sub_possessivePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttPossessivePronounRatio"+"_S", att_possessivePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "RelativePronounRatio"+"_S", relativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubRelativePronounRatio"+"_S", sub_relativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttRelativePronounRatio"+"_S", att_relativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "InterrogativePronounRatio"+"_S", interrogativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "SubInterrogativePronounRatio"+"_S", sub_interrogativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AttInterrogativePronounRatio"+"_S", att_interrogativePronounStudent/nrOfStudentWords)));
        featList.addAll(Arrays.asList(new Feature(GROUP_PREFIX+ "AdvInterrogativePronounRatio"+"_S", adv_interrogativePronounStudent/nrOfStudentWords)));
        return featList;
    }
    
}