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

package module.MiningModules.ImpoliteWord;

import source.*;
import source.TextData.*;

import java.io.*;

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

import java.util.*;

public class ImpoliteWord extends MiningModule
{	
	//dictionary	
    String dic[];
	String word[];
    String yomi[];
    String katakana[];

    int dictionaryWordNumber;

	//hi-light for textDisplay Color
	int attr[];
	
	public ImpoliteWord()
	{
		setModuleID(26);
		setCategoryID(2);		
		pairingVisualizationID = new int[]{3,2};
		setToolType(3);		
	}
    
	public void initializePanel(){}

    public void initializeData()
    {	
		String wordAndYomi[] = new String[4];	
		
        dic = fileReadArray(myModulePath+"dic.txt");
		
        word = new String[dic.length];
        yomi = new String[dic.length];
        katakana = new String[dic.length];
		
		dictionaryWordNumber = 0;
 
        for(int i=0;i<dic.length;i++)
        {
            wordAndYomi = dic[i].split(":");
			
            word[i] = wordAndYomi[0];
			yomi[i] = wordAndYomi[1];
			katakana[i] = wordAndYomi[2];
			
            dictionaryWordNumber++;   
        }
    }
	
    public void miningOperations(int optionNumber)
	{
	    switch(optionNumber)
		{
			case 0:
				setDataString(0,colorByImpoliteWord());
				setDataIntegerArray(0, attr);				
				displayOperations(0);
				break;
		}
	}
    
	String colorByImpoliteWord()
	{
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		
		attr = new int[text.sentenceNumber];
        for(int i=0;i<text.sentenceNumber;i++)
			attr[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>");
			
			int x=0;
			
			createProcessingPop();

			
            for(int i=0;i<text.sentenceNumber;i++)
            {
				setProcessingPercent((int)(i*100/text.sentenceNumber));				
				
                for(int j=0;j<text.sentence[i].wordNumber;j++)
				{					
					for(x=0;x<dictionaryWordNumber;x++)
						if(text.sentence[i].word[j].equals(word[x]) || text.sentence[i].word[j].equals(yomi[x]) || text.sentence[i].word[j].equals(katakana[x]))
							break;
					
					if(x < dictionaryWordNumber)
						break;
				}				
				
				if(x < dictionaryWordNumber)
				{
					bw.write("<span style=background-color:#FFDEAD;>");					
					
					for(int j=0;j<text.sentence[i].wordNumber;j++)
					{
						for(x=0;x<dictionaryWordNumber;x++)
							if(text.sentence[i].word[j].equals(word[x]) || text.sentence[i].word[j].equals(yomi[x]) || text.sentence[i].word[j].equals(katakana[x]))
								break;
						
						if(x < dictionaryWordNumber)
						{
							bw.write("</span>");
							bw.write("<span style=background-color:#F08080;>"+text.sentence[i].word[j]+"</span>");
							bw.write("<span style=background-color:#FFDEAD;>");
						}
						else 
							bw.write(text.sentence[i].word[j]);							
					}

					bw.write("</span>");
					
					bw.write("<br>");
					attr[i] = 1;					
				}
				else
					bw.write(text.sentence[i].sentenceText+"<br>");						

            }
			
        	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();
	} 
}