//
// Mining module for TETDM 
// TermHighLight.java Version 0.30
// Copyright(C) 2012 YASUFUMI TAKAMA All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//


package module.MiningModules.TermHighLight;

	import source.*;

	@SuppressWarnings("serial")
	public class TermHighLight extends MiningModule
	{
		String highlightedText;

		public TermHighLight()
		{
			setModuleID(45); // Change after finished
			pairingVisualizationID = new int[1];
			pairingVisualizationID[0] = 3;
			focusExecute = true;
            
            setCategoryID(2);            
			
			
//			defaultSetPanelModuleID = new int[]{99998,20003,20006};
//			defaultSetPanelVisualizationID = new int[]{3,20000,20000};			
			
			defaultSetPanelModuleID = new int[]{46,47,45};
			defaultSetPanelVisualizationID = new int[]{25,25,3};

		}
		
//		public void initializePanel(){}
		
//		public void selectedFirstTime(){}
		
		public void miningOperations(int optionNumber)
		{
			switch(optionNumber)
			{
				case 0:
				case 4500:
					highlightedText = highlight_text();				
					setDataString(0,highlightedText);
					break;
			}
		}

	
		public String highlight_text()
		{
			StringBuffer headtext = new StringBuffer();
			StringBuffer bodytext = new StringBuffer();
			int tf = 0; // for term frequency
			int sgf = 0; // for segment frequency
			int sf = 0; // for sentence frequency
			boolean skipflg = false;
			

			if(getMainFocusString().equals("")){
				skipflg = true;
			} else {
				System.err.println(getMainFocusString());
			}

			// body
			bodytext.append("<TABLE BORDER=2 BORDERCOLOR=black><font size = +1>");
			bodytext.append("<TR>");
			bodytext.append("<TH ALIGN=right>No.</TH>");
			bodytext.append("<TH ALIGN=right>Comment</TH>");
			bodytext.append("</TR>");

			for(int i=0;i<text.segmentNumber;i++) // segment
			{
				StringBuffer segStr = new StringBuffer();
				String[] stc = new String[text.segment[i].sentenceNumber];
				boolean flg = false;

				if(!skipflg)
				{
					for(int j=0;j<text.segment[i].sentenceNumber;j++) //sentence
					{
//						stc[j] = new String(text.segment[i].sentence[j].sentenceText);
						stc[j] = text.segment[i].sentence[j].sentenceText;
						String[] array = stc[j].split(getMainFocusString());
						if(array.length>1)
						{
							flg = true;
							tf += array.length-1;
							sf += 1;
							StringBuffer sb = new StringBuffer();
							for(int k=0;k<array.length-1;k++)
							{
								sb.append(array[k]);
								sb.append("<span style=\"background-color:lime;\">");
								sb.append(getMainFocusString());
								sb.append("</span>");
							}
							sb.append(array[array.length-1]);
							stc[j] = sb.toString();
						}
						segStr.append(stc[j]);
					}
				}
				// draw segment
				bodytext.append("<TR>");
				if(flg){
					bodytext.append("<TD ALIGN=right style=\"background:#ffcccc\">"+(i+1)+"</TD>");
					sgf += 1;
				} else {
					bodytext.append("<TD ALIGN=right>"+(i+1)+"</TD>");
				}						
				bodytext.append("<TD>");
				if(skipflg)
				{
					bodytext.append(text.segment[i].segmentText);
				} else {
					bodytext.append(segStr.toString());
				}
				bodytext.append("</TD>");
				bodytext.append("</TR>");
			}
			bodytext.append("</font></TABLE>");
			bodytext.append("</BODY></HTML>");

			//header
			headtext.append("<HTML><HEAD><TITLE>ANALYSIS</TITLE></HEAD><BODY>");
			headtext.append("<H1>STATUS OF TEXT</H1>");
			headtext.append("<p>Term: "+getMainFocusString()+"<br>");
			headtext.append("Segment Freq.: "+sgf);
			headtext.append(", Sentence Freq.: "+sf);
			headtext.append(", Freq.: "+tf);
			headtext.append("</p>");
			
			return headtext.toString()+bodytext.toString();
		}
	}
