//
// Mining module for TETDM 
// PosiNega.java Version 0.58
// Copyright(C) 2022 System Interface Laboratory All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//

package module.MiningModules.PosiNega;

import source.*;
import source.TextData.*;
import java.math.*;
import java.io.*;

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

import java.util.*;

public class PosiNega extends MiningModule {

    String[] exceptionDic; // Exception for Evaluation

//    String[] addPojiDic; // Additional Positive Words
  //  String[] addNegaDic; // Additional Negative Words

    double documentPValue;
    double documentNValue;
    double segmentPValue[];
    double segmentNValue[];
    double sentencePValue[];
    double sentenceNValue[];

    //hi-light for textDisplay Color
    int wordAttr[];
    int wordCount;

    public PosiNega()
    {
        setModuleID(59);
        setCategoryID(3);
        setUseMode(0);
//        pairingVisualizationID = new int[]{3, 2};
        pairingVisualizationID = new int[]{2};
        setToolType(3);
    }

    public void initializePanel() {}

    public void initializeData()
    {
        segmentPValue = new double[text.segmentNumber];
        segmentNValue = new double[text.segmentNumber];
        sentencePValue = new double[text.sentenceNumber];
        sentenceNValue = new double[text.sentenceNumber];

 //       addPojiDic = fileReadArray(myModulePath + "addPojiDic.txt");
   //     addNegaDic = fileReadArray(myModulePath + "addNegaDic.txt");
        
        exceptionDic = fileReadArray(myModulePath + "exceptionDic.txt");
    }

    
    ///////////////////

    public void miningOperations(int optionNumber)
    {
        switch (optionNumber)
        {
            case -1:displayOperations(12);      //For TextDisplayColor using text2
                    displayOperations(300);     //For TextDisplayColor using text2
                    break;
                
                
            case 0:
                setDataString(0, colorByImpoliteWord());// for TextDisplayHtml
                setDataIntegerArray(1, wordAttr);// for TextDisplayColor

                break;
        }
    }

    String colorByImpoliteWord()
    {
        int positiveSentences = 0, negativeSentences = 0;
        
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);

                
        wordAttr = new int[text.wordNumber];
        for (int i = 0; i < text.sentenceNumber; i++)
            wordAttr[i] = 10;
        

        try {
            bw.write("<HTML><BODY>");
            bw.write("<TABLE BORDER=0 BORDERCOLOR=white>");

            bw.write("</TD>");
            bw.write("</TR>");

            bw.write("<TD COLSPAN=5>");

            createProcessingPop();

            documentPValue = documentNValue = 0.0;
            wordCount = 0;

            for(int i=0;i<text.segmentNumber;i++)
            {
                setProcessingPercent((int) (i * 100 / text.segmentNumber));
                
                segmentPValue[i] = 0.0;
                segmentNValue[i] = 0.0;
                for(int j=0;j<text.segment[i].sentenceNumber;j++)
                {
                    
                    sentencePValue[j] = 0.0;    /////  j is a number in segment
                    sentenceNValue[j] = 0.0;    /////
                    for(int k=0;k<text.segment[i].sentence[j].wordNumber;k++)
                    {
                        if(exceptionCheck(text.segment[i].sentence[j].wordEndForm[k]))
                            text.segment[i].sentence[j].wordPosiNega[k] = 0;

                        if(text.segment[i].sentence[j].wordPosiNega[k] > 0)
                            sentencePValue[j] += text.segment[i].sentence[j].wordPosiNega[k];
                        else
                            sentenceNValue[j] += text.segment[i].sentence[j].wordPosiNega[k];

                    }//Word Loop

                    int sentenceAttr = 10;
                    
                    String sentenceColor = "#ffffff"; //Normal
                    if(sentencePValue[j] + sentenceNValue[j] > 0)
                    {
                        sentenceColor = "#ffcce5";      //positive color
                        sentenceAttr = 18;//pink
                        positiveSentences++;
                    }
                    else if(sentencePValue[j] + sentenceNValue[j] < 0)
                    {
                        sentenceColor = "#cce5ff";      //negative color
                        sentenceAttr = 17;//lightsteelblue
                        negativeSentences++;
                    }
                    

                    //TO HTML
                    bw.write("<span style=background-color:" + sentenceColor + ";>");
                    for(int k=0;k<text.segment[i].sentence[j].wordNumber;k++,wordCount++)
                    {
                        if(text.segment[i].sentence[j].wordPosiNega[k] > 0) //positive
                        {
                            wordAttr[wordCount] = 13;
                            bw.write("</span><span style=background-color:#F08080;>" + text.segment[i].sentence[j].word[k] + "</span>");
                        }
                        else if(text.segment[i].sentence[j].wordPosiNega[k] < 0) //negative
                        {
                            wordAttr[wordCount] = 19;
                            bw.write("</span><span style=background-color:#00afcc;>" + text.segment[i].sentence[j].word[k] + "</span>");
                        }
                        else
                        {
                            wordAttr[wordCount] = sentenceAttr;
                            bw.write(text.segment[i].sentence[j].word[k]);
                        }
                        bw.write("<span style=background-color:" + sentenceColor + ";>");
                    }
                    bw.write("</span>");
                                                            
                    if(sentencePValue[j] > 0 || sentenceNValue[j] < 0)
                        bw.write("<span style=background-color:#98fb98;>" + "(" +inJapanese[9] + String.format("%.0f",sentencePValue[j] + sentenceNValue[j]) + ": " +
                                 inJapanese[12] + "(" + String.format("%.0f",sentencePValue[j]) + "), " + inJapanese[13] + "(" + String.format("%.0f",sentenceNValue[j]) + "))" + "</span>");
                                        
                    bw.write("<br>");
                    
                    segmentPValue[i] += sentencePValue[j];
                    segmentNValue[i] += sentenceNValue[j];
                    
                }//Sentence Loop

                if(segmentPValue[i] > 0 || segmentNValue[i] < 0)
                    bw.write("<span style=background-color:#ffd700;>" + "(" +inJapanese[10] + String.format("%.0f",segmentPValue[i] + segmentNValue[i]) + ": "
                             + inJapanese[12] + "(" + String.format("%.0f",segmentPValue[i]) + "), " + inJapanese[13] + "(" + String.format("%.0f",segmentNValue[i]) + "))" + "</span>" + "<br><br>");

                documentPValue += segmentPValue[i];
                documentNValue += segmentNValue[i];
                
            }//Segment Loop
            
            bw.write("<span style=background-color:#ffb6c1;>" + "(" +inJapanese[11] + String.format("%.0f",documentPValue + documentNValue) + ": "
                     + inJapanese[12] + "(" + String.format("%.0f",documentPValue) + ")," + inJapanese[13] + "(" + String.format("%.0f",documentNValue) + "))" + "<br>"
                     + inJapanese[14] + ": " + inJapanese[12] + ":" + positiveSentences + ", " + inJapanese[13] + negativeSentences + "</span>");
            
            bw.write("</TD>");
            bw.write("</TR>");
            bw.write("</TABLE>");
            bw.write("</BODY></HTML>");

            bw.flush();


            disposeProcessingPop();
            
        } catch (Exception e) {
            System.out.println("Error in ImpoliteExpression");
        }

        return sw.toString();
    }

    public boolean exceptionCheck(String word)// check exception : true if matched
    {
        for (int i=0; i<exceptionDic.length;i++)
            if(word.equals(exceptionDic[i]))
                return true;

        return false;
    }
    

}
