//
// Mining module for TETDM 
// TFIDF.java Version 0.30
// Copyright(C) 2017 WATARU SUNAYAMA All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//


package module.MiningModules.TFIDF;

import source.*;
import source.TextData.*;
import source.Utility.*;

import java.io.*;
import java.text.*;
import java.util.*;

import java.lang.Character.UnicodeBlock;


public class TFIDF extends MiningModule
{
    double value[];
    
    int wordHighlightNumber[];
    int wordCount;
	
	public TFIDF()
	{
		setModuleID(49);
		setCategoryID(2);
//		setUseMode(3);
		pairingVisualizationID = new int[]{2,1};
	}
	
//	public void initializePanel(){}	

	public void initializeData()
    {
        value = new double[text.keywordNumber];
        wordHighlightNumber = new int[text.wordNumber];
    }
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case -1:displayOperations(11);
                break;
                
			case 0:
				calculateTFIDF();////
                setDataIntegerArray(1,wordHighlightNumber);
				break;
		}
	}

	
	public void calculateTFIDFsegment(int segmentNumber)
	{
        int segmentTotalFrequency = 0;
        
        for(int i=0;i<text.keywordNumber;i++)
            segmentTotalFrequency += text.keyword[i].frequencyInSegment[segmentNumber];
        
        if(text.segmentNumber == 1) //TF only
            for(int i=0;i<text.keywordNumber;i++)
            {
                if(text.keyword[i].frequency != 0)
                    value[i] = ((double)text.keyword[i].frequencyInSegment[segmentNumber] / segmentTotalFrequency);
            }
        else    //TF*IDF
            for(int i=0;i<text.keywordNumber;i++)
            {
                if(text.keyword[i].frequency != 0)
                    value[i] = ((double)text.keyword[i].frequencyInSegment[segmentNumber] / segmentTotalFrequency) *
                    (Math.log((double)text.segmentNumber/text.keyword[i].segmentFrequency) + 1);
            }
	}
	
	public void calculateTFIDF()
	{
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        
        int sortedIndex[] = new int[text.keywordNumber];
        
        int color[] = {12,13,16,17,18,19,20,21,22};
        
        try
        {
            wordCount = 0;/////////
            for(int i=0;i<text.segmentNumber;i++)
            {
                calculateTFIDFsegment(i);
        
                Qsort.initializeIndex(sortedIndex, text.keywordNumber);
                Qsort.quicksort(value, sortedIndex, text.keywordNumber);
	
                bw.write("TOP5 in Segment "+(i+1));
                bw.write(System.getProperty("line.separator"));
                
                for(int j=0;j<5 && j < text.keywordNumber;j++)
                    if(value[j] > 0)
                    {
                        bw.write(String.format("%d: %s %.1f",j+1,text.keyword[sortedIndex[j]].word,value[j]));
                        //                    bw.write((j+1)+" : "+text.keyword[sortedIndex[j]].word+" score = "+value[j]);
                        bw.write(System.getProperty("line.separator"));
                    }
                ///////////
                for(int j=0;j<text.segment[i].sentenceNumber;j++)
                    for(int k=0;k<text.sentence[text.segment[i].positionOfFirstSentence+j].wordNumber;k++, wordCount++)
                    {
                        wordHighlightNumber[wordCount] = 10;
                    
                        for(int t=0;t<5 && t < text.keywordNumber;t++)
                            if(value[t] != 0)
                                if(text.sentence[text.segment[i].positionOfFirstSentence+j].wordIDList[k] == sortedIndex[t])
                                    wordHighlightNumber[wordCount] = color[i%9];
                    }
                ///////////
            }
			bw.flush();
		}
		catch(Exception e){
			System.out.println("Error in calculateTFIDF");
            e.printStackTrace();            
		}			
		setDataString(0, sw.toString());
	}
    

}
