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

package module.MiningModules.SenSegRelationInformation;

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

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

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


public class SenSegRelationInformation extends MiningModule implements ActionListener
{
    boolean segmentMode;
    boolean sentenceMode;
    int focusObject;
    
    boolean multiLineMode;
    
    double cosValue[];
    int sortedIndex[];
    
    String displayTextList[];
    int attribute[];
    
	public SenSegRelationInformation()
    {
		setModuleID(55);
		setCategoryID(3);
		setUseMode(0);
		pairingVisualizationID = new int[]{1};
		setToolType(3);
        
        focusExecute = true;
	}

	public void initializePanel()
    {
        toggleButton = new JToggleButton[1];
        toggleButton[0] = new JToggleButton();
        toggleButton[0].setBackground(Color.red);
        toggleButton[0].addActionListener(this);
        operationPanel.add(toggleButton[0]);
    }

	public void initializeData()
    {
        segmentMode = false;
        sentenceMode = false;
        focusObject = -1;
        multiLineMode = false;
        
        if(isMenuInJapanese())
            toggleButton[0].setText(inJapanese[0]);
        else
            toggleButton[0].setText("Multi Lines Display");
    }
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case -1:
                if(toggleButton[0].isSelected())
                {
                    displayOperations(199);
                    displayOperations(300);
                }
                else
                {
                    displayOperations(199);
                    displayOperations(200);
                }
                break;
                
            case 4500:

                focusObject = getMainFocusSegment();
                if(focusObject >= 0)
                {
                    segmentMode = true;
                    sentenceMode = false;
                }
                else
                {
                    focusObject = getMainFocusSentence();
                    if(focusObject >= 0)
                    {
                        segmentMode = false;
                        sentenceMode = true;
                    }
                }
                if(segmentMode || sentenceMode)
                    createRelation();
                break;
                
			case 0:
                displayTextList = new String[1];
                attribute = new int[1];
                if(isMenuInJapanese())
                    displayTextList[0] = inJapanese[1];
                else
                    displayTextList[0] = "No Main Focus Selected";
                attribute[0] = 10;
				break;
		}
	}
    
    public void createDisplay()
    {
        multiLineMode = toggleButton[0].isSelected();
        
        String outputText = "";
        
        if(segmentMode)
        {
            displayTextList = new String[text.segmentNumber];
            attribute = new int[text.segmentNumber];
            
            for(int i=0;i<text.segmentNumber;i++)
            {
                if(!multiLineMode)
                    displayTextList[i] = "["+(text.segment[sortedIndex[i]].segmentID+1)+"]"+text.segment[sortedIndex[i]].segmentText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
                else
                    displayTextList[i] = "["+(text.segment[sortedIndex[i]].segmentID+1)+"]"+text.segment[sortedIndex[i]].segmentText;
                
                int score = (int)(cosValue[i]/0.1);
                if(score > 8)
                    score = 8;
                attribute[i] = 1+(8-score);
                System.out.println("SCOre = "+score+" "+attribute[i] );
            }
        }
        
        if(sentenceMode)
        {
            displayTextList = new String[text.sentenceNumber];
            attribute = new int[text.sentenceNumber];

            
            for(int i=0;i<text.sentenceNumber;i++)
            {
                if(!multiLineMode)
                    displayTextList[i] = "("+(text.sentence[sortedIndex[i]].sentenceID+1)+")"+text.sentence[sortedIndex[i]].sentenceText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
                else
                    displayTextList[i] = "("+(text.sentence[sortedIndex[i]].sentenceID+1)+")"+text.sentence[sortedIndex[i]].sentenceText;
                int score = (int)(cosValue[i]/0.1);
                if(score > 8)
                    score = 8;
                attribute[i] = 1+(8-score);
            }
        }
        
        setDataStringArray(1,displayTextList);
        setDataIntegerArray(2,attribute);
    }
    
    public int[] createRelationalOrder(int value[])
    {
        int dataID[];
        dataID = new int[value.length];
        
        Qsort.initializeIndex(dataID, value.length);
        Qsort.quicksort(value, dataID, value.length);
        
        return dataID;
    }
    
    public void createSentenceCosValue(int n)
    {
        
        int coFrequency[] = new int[text.sentenceNumber];
        for(int i=0;i<text.sentenceNumber;i++)
        {
            coFrequency[i] = 0;
            for(int j=0;j<text.sentence[n].keywordNumber;j++)
                 coFrequency[i] += text.keyword[text.sentence[n].keywordIDList[j]].appearingSentenceTable[i];
        }
        
        for(int i=0;i<text.sentenceNumber;i++)
            if(text.sentence[n].kindKeywordNumber == 0 || text.sentence[i].kindKeywordNumber == 0)
            {
                if(n != i)
                    cosValue[i] = 0.0;
                else
                    cosValue[i] = 1.0;
            }
            else
                cosValue[i] =  (double)coFrequency[i] / Math.sqrt(text.sentence[n].kindKeywordNumber * text.sentence[i].kindKeywordNumber);
    }
    
	public void createRelation()
	{
        System.out.println("segmentMode="+segmentMode+"  focusObject="+focusObject);
        
        if(segmentMode)
        {
            cosValue = new double[text.segmentNumber];
            for(int i=0;i<text.segmentNumber;i++)
                cosValue[i] = text.segmentRelation.cos[focusObject][i];
        }
        else
        {
            cosValue = new double[text.sentenceNumber];
            createSentenceCosValue(focusObject);
        }
        
        sortedIndex = Qsort.getSortedIndex(cosValue);
        
        createDisplay();
	}
    
    public void actionPerformed(ActionEvent e)
    {
        writeButtonLog(e);
        
        if(e.getSource() == toggleButton[0])
        {
            createDisplay();
            miningOperations(-1);
        }
    }
}


