
//
// Mining module for TETDM 
// CommunicationSummary.java Version 0.58
// This is one of the sample modules.
// You should read the README file.
//

package module.MiningModules.CommunicationSummary;		// Replace ALL [MiningStyle] with your [module name]

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

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

import java.io.*;


import java.util.regex.*;

public class CommunicationSummary extends MiningModule implements ActionListener
{
	int subjectList[];	
	int checkType[];

	boolean anchorTable[][];
	

	JToggleButton textType;

	
	public void initializePanel()
	{		
		operationPanel.setBackground(Color.RED);
		
		textType = new JToggleButton("ALL TEXT");
		textType.setBackground(Color.RED);
		textType.addActionListener(this);
		operationPanel.add(textType);
		textType.setSelected(false);
		
		if(isMenuInJapanese())
		{
			textType.setText(inJapanese[0]);
		}		
	}
	
	public void initializeData()
	{
		anchorTable = new boolean[text.segmentNumber][text.segmentNumber];
		for(int i=0;i<text.segmentNumber;i++)
			for(int j=0;j<text.segmentNumber;j++)
				anchorTable[i][j] = false;
		anchorFinder();
	}
	
	public CommunicationSummary()
	{
		setModuleID(29);						// Set your module ID after you have got it
		setCategoryID(4);
		setUseMode(1);
		pairingVisualizationID = new int[]{3,1102};
		setToolType(1);
		focusExecute = true;
		defaultSetPanelModuleID = new int[]{29,0};
		defaultSetPanelVisualizationID = new int[]{3,1102};
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 4500:
                boolean focusKeywords[] = getFocusKeywords();
				int count = 0;
				for(int i=0;i<text.keywordNumber;i++)
					if(focusKeywords[i] == true)
						count++;				
				subjectList = new int[count];
				count = 0;
				for(int i=0;i<text.keywordNumber;i++)
					if(focusKeywords[i] == true)
						subjectList[count++] = i;
								
				createSegmentType();
				
				resetData();
				setDataString(createSummary());
				displayOperations(0);				
				break;				
				
			case 0:
				
				subjectList = getNewDataIntegerArray(11,0);
				
				createSegmentType();
				
				resetData();
				setDataString(createSummary());
				break;
				
			case 1:
				
				subjectList = getNewDataIntegerArray(11,0);				
				
				createSegmentType();
				
				resetData();
				setDataString(createSummary());
				displayOperations(0);				
				break;
		}
	}
	
	void anchorFinder()
	{
		String anchor = "&gt;&gt;([0-9-,]+)";
		String anchorText;
		
		Pattern t = Pattern.compile(anchor);

		for(int i=0;i<text.segmentNumber;i++)
		{
			Matcher x = t.matcher(text.segment[i].segmentText);
			
			while(x.find())
			{
				anchorText = x.group(1);
//				System.out.println((i+1)+" >>"+anchorText);
			
				String anchorList[] = anchorText.split(",");
			
				for(int j=0;j<anchorList.length;j++)
					if(anchorList[j].indexOf("-") != -1)
					{
						String numberList[] = anchorList[j].split("-");
						
						if(numberList.length != 2)
							continue;
						
						int startNumber = Integer.parseInt(numberList[0]);
						int endNumber = Integer.parseInt(numberList[1]);
						
						if(startNumber < 1 || startNumber > text.segmentNumber || endNumber < 1 || endNumber > text.segmentNumber)
							continue;
						
						for(int k=startNumber; k<=endNumber;k++)
						{
							anchorTable[i][k-1] = true;
//							System.out.println((i+1)+" -> "+k);							
						}					
					}
					else
					{
						int anchorNumber = Integer.parseInt(anchorList[j]);
						if(anchorNumber < 1 || anchorNumber > text.segmentNumber)
							continue;
						
						anchorTable[i][anchorNumber-1] = true;
	//					System.out.println((i+1)+" -> "+anchorNumber);
					}
			}
		}		
	}
	
	void createSegmentType()
	{
		
		
		checkType = new int[text.segmentNumber];
		
		for(int i=0;i<checkType.length;i++)
			checkType[i] = 0;
		
		//MAIN						BLUE BIG 1

		
//		System.out.println(subjectList.length);
		
		for(int i=0;i<subjectList.length;i++)
		{
			if(text.keyword[subjectList[i]].segmentFrequency > text.segmentNumber * 0.9)
				continue;
			
//			System.out.println(text.keyword[subjectList[i]].segmentFrequency);
			
			for(int j=0;j<text.keyword[subjectList[i]].segmentFrequency;j++)
			{
//				System.out.println(text.keyword[subjectList[i]].appearingSegment[j]);
				checkType[text.keyword[subjectList[i]].appearingSegment[j]] = 1;
			}
		}
		
		//MAIN -> SUB EXPLICIT		BROWN 2
		for(int i=0;i<text.segmentNumber;i++)
			if(checkType[i] == 1)
				for(int j=0;j<text.segmentNumber;j++)
					if(anchorTable[i][j] == true && checkType[j] == 0)
						checkType[j] = 2;
		
		//SUB -> MAIN EXPLICIT		BROWN 3
		for(int i=0;i<text.segmentNumber;i++)
			if(checkType[i] == 0)
				for(int j=0;j<text.segmentNumber;j++)
					if(anchorTable[i][j] == true && checkType[j] == 1)
						checkType[i] = 3;		
				
		//SUB -> SUB EXPLICIT		BLACK 4
		for(int i=0;i<text.segmentNumber;i++)
			if(checkType[i] == 2 || checkType[i] == 3)
				for(int j=0;j<text.segmentNumber;j++)
					if(anchorTable[i][j] == true && checkType[j] == 0)
						checkType[j] = 4;		
		
		
		
		//MAIN <-> SUB IMPLICIT		GREEN 5	, RED 6: IMPLICIT AND EXPLICIT
		int mainWords[];
		int span = 10;
		
		for(int i=0;i<checkType.length;i++)
			if(checkType[i] == 1) //MAIN
			{
				mainWords = new int[text.segment[i].kindKeywordNumber];
				
				int count = 0;
				for(int j=0;j<text.keywordNumber;j++)
				{
					if(text.keyword[j].appearingSegmentTable[i] == 1)
					{
						if(text.keyword[j].segmentFrequency > text.segmentNumber * 0.9)
							continue;					
						
//						System.out.println("MAIN"+(i+1)+" "+text.keyword[j].word+" : "+text.keyword[j].segmentFrequency+" <= "+(text.segmentNumber * 0.9));
						mainWords[count++] = j;
					}
				}
				
				int start = i-span;
				if(start < 0)
					start = 0;
				
				int end = i+span;
				if(end > text.segmentNumber)
					end = text.segmentNumber;
				
				for(int j=start;j<i;j++)
					if(checkType[j] != 1)
						for(int k=0;k<count;k++)
							if(text.keyword[mainWords[k]].appearingSegmentTable[j] == 1)
							{
								if(checkType[j] == 0 || checkType[j] == 4)
									checkType[j] = 5;
								
								if(checkType[j] == 2 || checkType[j] == 3)
									checkType[j] = 6;
								
//								System.out.println((j+1)+" from "+(i+1)+"A implicit by "+text.keyword[mainWords[k]].word);
								break;
							}

				for(int j=i+1;j<end;j++)
					if(checkType[j] != 1)					
						for(int k=0;k<count;k++)
							if(text.keyword[mainWords[k]].appearingSegmentTable[j] == 1)
							{
								if(checkType[j] == 0 || checkType[j] == 4)
									checkType[j] = 5;
								
								if(checkType[j] == 2 || checkType[j] == 3)
									checkType[j] = 6;								
								
//								System.out.println((j+1)+" from "+(i+1)+"B implicit by "+text.keyword[mainWords[k]].word);								
								break;
							}
			}
		
		//OTHERS					BLACK SMALL
		//NG.. WORD NG, SYMBOL NG
	}
	
	String createSummary()
	{
		String title;
		int count = 0;
		
		title = getMainFocusString();
		
		if(title.equals(""))
			title = "TITLE";
        
        initializeFocusSegments();
		
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);		
		
		try {
			bw.write("<dt>COUNT 0:<br><br>");
			
			String subjects = "";
			for(int i=0;i<subjectList.length;i++)
			{
				if(text.keyword[subjectList[i]].segmentFrequency > text.segmentNumber * 0.9)
					continue;
				
				subjects += (text.keyword[subjectList[i]].word + " ");	
			}
			
			bw.write("<h2>"+subjects+"</h2>");				
			bw.write("<h1><span style=color:red;>"+title+"</span></h1>");			
		
						
			for(int i=0;i<text.segmentNumber;i++)
			{
				switch(checkType[i])
				{
					case 1: //MAIN	BLUE
						bw.write("<span style=font-size:18px;color:blue;>");
                        focusSegments[i] = true;
						break;
						
					case 2: //MAIN -> SUB EXPLICIT		BROWN
						bw.write("<span style=font-size:16px;color:#A52A2A;>");
                        focusSegments[i] = true;
						break;
						
					case 3: //SUB -> MAIN EXPLICIT		BROWN
						bw.write("<span style=font-size:16px;color:#A52A2A;>");
                        focusSegments[i] = true;
						break;
						
					case 4: //SUB -> SUB EXPLICIT		BLACK
						bw.write("<span style=font-size:14px;color:black;>");
                        focusSegments[i] = true;
						break;
						
					case 5:	//MAIN <-> SUB IMPLICIT		GREEN
						bw.write("<span style=font-size:14px;color:green;>");
                        focusSegments[i] = true;
						break;
						
					case 6: //MAIN <-> EXPLICIT AND IMPLICIT RED
						bw.write("<span style=font-size:16px;color:red;>");
                        focusSegments[i] = true;
						break;						
						
					default: //OTHERS		BLACK SMALL
						if(textType.isSelected())
							bw.write("<span style=font-size:10px;>");						
						break;
				}

				if(textType.isSelected() == false && checkType[i] == 0)
					continue;
				
//				bw.write("<dt>"+(i+1)+" : "+"<a href=mailto:sage><b>"+"NAME"+" </b></a> "+"TIME"+" : "+"ID:********"+"<br>";)
				bw.write((i+1)+" : "+"NAME"+" </a> "+"TIME"+" : "+"ID:********"+"<br>");				

				for(int j=0;j<text.segment[i].sentenceNumber;j++)
				{
					bw.write(text.segment[i].sentence[j].sentenceText);
					bw.write("<br>");
				}
				
//				bw.write("<br><br>");
				
				bw.write("</span>");
				count++;
			}
			
			bw.write("Extraction Rate = "+count+"/"+text.segmentNumber);
			
			bw.flush();
            
            setFocusSegments(inJapanese[1], "Communication Summary");
		}
        catch (IOException e) 
		{
            System.err.println(e);
        }
		
		return sw.toString();		
	}

	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		
		writeButtonLog(e);/////NEW LOG
		//		insideOfActionPerformed(e);
		
		if(e.getSource () == textType)
			miningOperations(1);
	}
}
