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

package module.MiningModules.TextInfo;

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 TextInfo extends MiningModule
{
	String checkedText;

	String titles[];
	
	int mainCharacterID;
	
	int subjectList[];
	String keySentence;
	double scoreDist[];
	int relationIDs[];
	int noiseNumber;
	int veryLongSentenceNumber;
	int similarNumber, similarRate;
    int twiceWordsSentenceNumber;
//	int impoliteAttr[];
	double kanjiRate;
	
	int score;

	public TextInfo()
    {
		setModuleID(19);
		setCategoryID(5);
		setUseMode(0);
		pairingVisualizationID = new int[]{3};
		setToolType(2);
		focusExecute = true;
//		defaultSetPanelModuleID = new int[]{19,11,12,5,13,2,16,26};
//		defaultSetPanelVisualizationID = new int[]{3,8,2,2,12,3,3,3};
	}

	public void initializePanel(){}
	
	public void initializeData()
	{
	    String titleLists[] = {"Items","Results","Segments","Sentences","Subjects","Key Sentence","Lighting Rate","Coherence Rate",
							"Word Expression","Long Sentences","Total Score","Text","pts.","Keywords","Main Character","Subject Rate","Overlap Rate",
							"Structure","Expression","Wordy Sentences","Impolite Rate","Kanji Rate"};
		
		if(isMenuInJapanese())
			titles = inJapanese;
		else
			titles = titleLists;	
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case 2:
			case 4500:/*
				subjectList = getDataIntegerArray(11,0);
				keySentence = getDataString(11,1);
				scoreDist = getDataDoubleArray(12,0);
				relationIDs = getDataIntegerArray(13,2);
				noiseNumber = getDataInteger(2,1);
				veryLongSentenceNumber = getDataInteger(16,1);
                
				checkedText = check_text();
				resetData();
				setDataString(checkedText);
				break;*/
                
			case 0:				
				subjectList = getNewDataIntegerArray(11,0);
				keySentence = getNewDataString(11,1);
				scoreDist = getNewDataDoubleArray(12,0);
				relationIDs = getNewDataIntegerArray(13,2);
				noiseNumber = getNewDataInteger(2,1);
				veryLongSentenceNumber = getNewDataInteger(16,1);
				similarNumber = getNewDataInteger(30,1);
                twiceWordsSentenceNumber = getNewDataInteger(31,1);
				
//				impoliteAttr = getNewDataIntegerArray(26,0);
				kanjiRate = getNewDataDouble(34,0);
				
				checkedText = check_text();				
				resetData();
				setDataString(checkedText);
				break;
				
			case 1:
				displayOperations(0);
				break;
		}
	}
    public String check_text()
	{
		StringWriter sw1 = new StringWriter();
		BufferedWriter bw1 = new BufferedWriter(sw1);
		
		try
		{
			//header
			bw1.write("<HTML><HEAD><TITLE>ANALYSIS</TITLE></HEAD><BODY>");
			bw1.write("<H1>"+titles[11]+" ["+(new File(text.sourceFilename).getName())+"] </H1>");

			// each segment evaluation
			bw1.write("<TABLE BORDER=2 BORDERCOLOR=black>");

			mainCharacterID = 0;
			int max = text.keyword[0].frequencyAsSubject;
			for(int i=0;i<text.keywordNumber;i++)
				if(text.keyword[i].frequencyAsSubject > max)
				{
					max = text.keyword[i].frequencyAsSubject;
					mainCharacterID = i;
				}			

			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[14]+"</TD>");
			bw1.write("<TD ALIGN=center><span style=font-size:20px;>");
			if(text.keywordNumber == 0)
				bw1.write("");
			else
            {
				bw1.write(text.keyword[mainCharacterID].word);

            }
			bw1.write("</span>("+max+")</TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[4]+"</TD>");
			bw1.write("<TD ALIGN=center><span style=font-size:20px;>");
			for(int i=0;i<subjectList.length;i++)
				bw1.write(text.keyword[subjectList[i]].word+" ");
			bw1.write("</span></TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[5]+"</TD>");
			bw1.write("<TD ALIGN=center>"+keySentence+"</TD>");
			bw1.write("</TR>");
			
			bw1.write("</TABLE>");
			
			
			//EVALUATION TABLE
			bw1.write("<H1>"+titles[1]+"</H1>");			
			score = 100;			
			
			//CONSTITUTION
			bw1.write("<H2>"+titles[17]+"</H2>");			
			bw1.write("<TABLE BORDER=2 BORDERCOLOR=black>");
			
			
			int point;
			
			int count=0;
			for(int i=0;i<text.sentenceNumber;i++)
				if(scoreDist[i] > 0)
					count++;
			
			int a = 0;
			point = 0;
			if(text.sentenceNumber > 0)
				a = count*100/text.sentenceNumber;
			if(a < 80)
			{
				point = 80-a;
				score -= point;
			}
			
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[6]+"</TD>");
			if(text.sentenceNumber > 0)
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+(count*100/text.sentenceNumber)+"%</span> ("+count+"/"+text.sentenceNumber+")</TD>");
			else
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;> 0% </span>("+count+"/"+text.sentenceNumber+")</TD>");
			bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");			
			bw1.write("</TR>");
			
			a=0;
			point = 0;
			if(text.keywordNumber > 0)
				a = relationIDs.length*100/text.keywordNumber;
			if(a < 80)
			{
				point = 80-a;
				score -= point;				
			}
			
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[7]+"</TD>");
			if(text.keywordNumber > 0)
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+(relationIDs.length*100/text.keywordNumber)+"%</span> ("+relationIDs.length+"/"+text.keywordNumber+")</TD>");
			else
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;> 0% </span>("+relationIDs.length+"/"+text.keywordNumber+")</TD>");
			bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");			
			bw1.write("</TR>");			
			
			bw1.write("</TABLE>");			
			
			
			//EXPRESSION			
			bw1.write("<H2>"+titles[18]+"</H2>");			
			bw1.write("<TABLE BORDER=2 BORDERCOLOR=black>");			

            //subject
            
			int subjectCount = 0;
			for(int i=0;i<text.sentenceNumber;i++)
				if(text.sentence[i].hasSubject)
					subjectCount++;
			
			point = text.sentenceNumber - subjectCount;
			score -= point;
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[15]+"</TD>");
			if(text.sentenceNumber > 0)
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+(subjectCount*100/text.sentenceNumber)+"%</span> ("+subjectCount+"/"+text.sentenceNumber+")</TD>");
			else
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;> 0% </span>("+subjectCount+"/"+text.sentenceNumber+")</TD>");
		bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");			
			bw1.write("</TR>");			
			
            //LongSentence
            
            point = veryLongSentenceNumber * 5;
            score -= point;
            
            bw1.write("<TR>");
            bw1.write("<TD ALIGN=center>"+titles[9]+"</TD>");
            bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+veryLongSentenceNumber+"</span></TD>");
            bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");
            bw1.write("</TR>");
            

			

			
			if(text.sentenceNumber <= text.sentenceRelationLimit)// sentenceRelatioinLimit
			{
			
				similarRate = (int)((double)similarNumber*100/text.sentenceNumber);
			
				point = 0;
				if(similarRate > 20)
					point = similarRate - 20;
				score -= point;
			
				bw1.write("<TR>");
				bw1.write("<TD ALIGN=center>"+titles[16]+"</TD>");
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+similarRate+"%</span> ("+similarNumber+"/"+text.sentenceNumber+")</TD>");
				bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");			
				bw1.write("</TR>");			
			}

            //jouchoubun
            
            point = 0;
            if(twiceWordsSentenceNumber > 5)
                point = twiceWordsSentenceNumber-5;
            score -= point;
            
            bw1.write("<TR>");
            bw1.write("<TD ALIGN=center>"+titles[19]+"</TD>");
            bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+twiceWordsSentenceNumber+"</span></TD>");
            bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");
            bw1.write("</TR>");

            
            //Fuzzy Word
            
            point = noiseNumber;
            score -= point;
            
            bw1.write("<TR>");
            bw1.write("<TD ALIGN=center>"+titles[8]+"</TD>");
            bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+noiseNumber+"</span></TD>");
            bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");
            bw1.write("</TR>");
			
			
			////////////////////////SHITSUREI
			/*
			count = 0;
			for(int i=0;i<text.sentenceNumber;i++)
				if(impoliteAttr[i] == 1)
					count++;
			a = 0;
			if(text.sentenceNumber > 0)
				a = count*100/text.sentenceNumber;
			
			point = 0;
			if(a > 20)
			{
				point = a-20;
				score -= point;
			}
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[20]+"</TD>");
			if(text.sentenceNumber > 0)
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+a+"%</span> ("+count+"/"+text.sentenceNumber+")</TD>");
			else
				bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;> 0% </span> ("+count+"/"+text.sentenceNumber+")</TD>");				
			bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");			
			bw1.write("</TR>");		
			*/
			
		
			////////////////////////SHITSUREI END
			
			////////////////////////KANJI			
			
			a = (int)(kanjiRate*100);
			point = 0;
			if(a > 35)
				point = (a-35)*2;
            score -= point;			
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[21]+"</TD>");
			bw1.write("<TD ALIGN=center><span style=font-size:20px;color:blue;>"+a+"%</span> </TD>");				
			bw1.write("<TD ALIGN=center><span style=color:red;>"+(-point)+"</span></TD>");			
			bw1.write("</TR>");			
			
			
			////////////////////////KANJI END			
			

			
			//Total
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[10]+"</TD>");
			bw1.write("<TD ALIGN=center></TD>");			
			bw1.write("<TD ALIGN=center><span style=font-size:20px;color:red;>"+score+" "+titles[12]+"</span></TD>");			
			bw1.write("</TR>");				

			bw1.write("</TABLE>");	
			
			bw1.write("</BODY></HTML>");
			bw1.flush();
		}
		catch(Exception e){
			System.out.println("Error in TextInfo");
            e.printStackTrace();            
		}

		return sw1.toString();
	}
}


