//
// Mining module for TETDM 
// TextInfo.java Version 0.30
// 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[];
    String titlesInJapanese[];
	
	int subjectList[];
	String keySentence;
	double scoreDist[];
	int relationIDs[];
	int noiseNumber;
	int veryLongSentenceNumber;
	
	int score;

	public TextInfo()
    {
		setModuleID(19);
		pairingVisualizationID = new int[]{3};
		pairingVisualizationID[0] = 3;
		focusClickExecute = true;
		defaultSetPanelModuleID = new int[]{19,11,12,13,2,16};
		defaultSetPanelVisualizationID = new int[]{3,8,2,12,3,3};		
	}

	public void initializePanel(){}
	
	public void initializeData()
	{
		titlesInJapanese = fileReadArray();
	    String titleLists[] = {"Items","Results","Segments","Sentences","Subjects","Key Sentence","Lighting Rate","Coherence Rate","",""};
		
		if(isMenuInJapanese())
			titles = titlesInJapanese;
		else
			titles = titleLists;	
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 4502:	
			case 0:				
				subjectList = getDataIntegerArray(11,77);
				keySentence = getDataStringNew(11,1);
				scoreDist = getDataDoubleArrayNew(12,2);
				relationIDs = getDataIntegerArrayNew(13,2);
				noiseNumber = getDataInteger(2,1);
				veryLongSentenceNumber = getDataInteger(16,1);
				
				checkedText = check_text();				
				setData(0,checkedText);				
				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>STATUS OF TEXT ["+(new File(text.sourceFilename).getName())+"]</H1>");

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

			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center width=200>"+titles[0]+"</TD>");
			bw1.write("<TD ALIGN=center>"+titles[1]+"</TD>");
			bw1.write("</TR>");

			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[2]+"</TD>");
			bw1.write("<TD ALIGN=center>"+text.segmentNumber+"</TD>");
			bw1.write("</TR>");

			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[3]+"</TD>");
			bw1.write("<TD ALIGN=center>"+text.sentenceNumber+"</TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[4]+"</TD>");
			bw1.write("<TD ALIGN=center>");
			for(int i=0;i<subjectList.length;i++)
				bw1.write(text.keyword[subjectList[i]].word+" ");
			bw1.write("</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>");
			
			int count=0;
			for(int i=0;i<text.sentenceNumber;i++)
				if(scoreDist[i] > 0)
					count++;
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[6]+"</TD>");
			bw1.write("<TD ALIGN=center>"+(count*100/text.sentenceNumber)+"% ("+count+"/"+text.sentenceNumber+")</TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[7]+"</TD>");
			bw1.write("<TD ALIGN=center>"+(relationIDs.length*100/text.keywordNumber)+"% ("+relationIDs.length+"/"+text.keywordNumber+")</TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[8]+"</TD>");
			bw1.write("<TD ALIGN=center>"+noiseNumber+"</TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[9]+"</TD>");
			bw1.write("<TD ALIGN=center>"+veryLongSentenceNumber+"</TD>");
			bw1.write("</TR>");
			
			score = 100;
			int a;
			a = count*100/text.sentenceNumber;
			if(a < 80)
				score -= (80-a);
			a = relationIDs.length*100/text.keywordNumber;
			if(a < 80)
				score -= (80-a);
			score -= noiseNumber;
			score -= veryLongSentenceNumber * 5;
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[10]+"</TD>");
			bw1.write("<TD ALIGN=center>"+score+"</TD>");
			bw1.write("</TR>");				

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

		return sw1.toString();
	}
}


