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

package module.MiningModules.WordRelationInformation;

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

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

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class WordRelationInformation extends MiningModule implements ActionListener
{
    double data[][];
	String header[];
    int dataNumber;

    String dataNames[];
    String speechNames[];

	JToggleButton tButton[];
    int buttonNumber;
	ButtonGroup buttonGroup;	
    
    ShrinkPanel shrinkPanel;
    OsusumePanel osusumePanel;
    JPanel lowerPanel;

    
	public WordRelationInformation()
    {
		setModuleID(53);
		setCategoryID(2);
		setUseMode(0);
		pairingVisualizationID = new int[]{24};
		setToolType(3);
        
        focusExecute = true;
	}

	public void initializePanel()
	{
//        String names[] = {"Word","Speech","Freq","SenFreq","SegFreq","SubFreq"};
        String names[] = {"Word","Freq"};
		
        header = new String[2];
		if(isMenuInJapanese())
            for(int i=0;i<2;i++)
                header[i] = inJapanese[i];
		else
            for(int i=0;i<2;i++)
                header[i]= names[i];
		
		operationPanel.setBackground(Color.RED);		
        buttonNumber = header.length-1;
	    tButton = new JToggleButton[buttonNumber];		
        operationPanel.setLayout(new GridLayout(1,buttonNumber));
        
		buttonGroup = new ButtonGroup();
		
	    for(int i=0;i<buttonNumber;i++)
		{
		    tButton[i] = new JToggleButton(header[i+1]);
			tButton[i].setBackground(Color.red);
		    tButton[i].addActionListener(this);			
		    operationPanel.add(tButton[i]);
			buttonGroup.add(tButton[i]);
		}
        
        ///////DATA SHRINK///////
        /*
        lowerPanel = new JPanel();
        lowerPanel.setLayout(new GridLayout(2,1));
        
        shrinkPanel = new ShrinkPanel(header,1,buttonNumber+1, this, isMenuInJapanese(), text.control.absolutePath);
        lowerPanel.add(shrinkPanel);
        osusumePanel = new OsusumePanel(header,1,buttonNumber+1, this, isMenuInJapanese(), text.control.absolutePath);
        lowerPanel.add(osusumePanel);

        add(lowerPanel, BorderLayout.SOUTH);
         */
	}	

	public void initializeData()
    {
        dataNumber = text.keywordNumber;
    }
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case -1:
                displayOperations(32);  //Frequency Sort for TableDisplay
                break;
                
            case 4500:
			case 0:
				createWordData();
//                shrinkPanel.initializeData(data,0);
  //              osusumePanel.initializeData(data,0);
				break;
		}
	}
	
	public void createWordData()
	{
		dataNames = new String[text.keywordNumber];
		for(int i=0;i<text.keywordNumber;i++)
			dataNames[i] = text.keyword[i].word;
		
        /*
        speechNames = new String[8];
        for(int i=0;i<8;i++)
            speechNames[i] = inJapanese[6+i];
         */
        
        int keywordFrequency[] = new int[text.keywordNumber];
        for(int i=0;i<text.keywordNumber;i++)
            keywordFrequency[i] = text.keyword[i].frequency;
        
        focusSentences = getFocusSentences();
        
        for(int i=0;i<text.sentenceNumber;i++)
            if(focusSentences[i] == false)
                for(int j=0;j<text.sentence[i].keywordNumber;j++)
                    keywordFrequency[text.sentence[i].keywordIDList[j]]--;

        
		data = new double[header.length][dataNames.length];
		
		for(int i=0;i<dataNames.length;i++)
		{
//			data[0][i] = text.keyword[i].partOfSpeech;
            data[0][i] = keywordFrequency[i];
//			data[2][i] = text.keyword[i].sentenceFrequency;
	//		data[3][i] = text.keyword[i].segmentFrequency;
		//	data[4][i] = text.keyword[i].frequencyAsSubject;
		}

		setDataStringArray(0, header);
		setDataStringArray(1, dataNames);
		setDataDoubleArray2(0, data);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		writeButtonLog(e);/////NEW LOG
		
		for(int i=0;i<buttonNumber;i++)
			if(e.getSource() == tButton[i])
			{
				setDataDoubleArray(0, data[i]);
				displayOperations(0);	//save text and re-calculation in all modules
			}

	}
  
}


