//
// Mining module for TETDM 
// WordReplace Version 0.10 2012.2.9
// Copyright(C) 2012.2.9 Yoko Nishihara
// 
// This module replaces words with words written in a dictionary.
// You should read the README file.
//

package module.MiningModules.WordReplace;

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

import java.io.*;

public class WordReplace extends MiningModule
{
    String dictionary[];
    String splitKeyword[];
    String originalKeyword[];
    String replaceKeyword[];
	
	public WordReplace()
	{
		setModuleID(4);						// Set your module ID after you have got it
		pairingVisualizationID = new int[]{1};
	}

    public void initializeData()
    {
		dictionary = fileReadArray(myModulePath+"ReplaceWords.txt");
		splitKeyword = new String[2];
		originalKeyword = new String[dictionary.length];
		replaceKeyword = new String[dictionary.length];
    }
	
    public void miningOperations(int optionNumber)
    {
	switch(optionNumber)
	    {
	    case 0:
		setData(0,MyMethod());
		break;
	    }
    }
	
	String MyMethod()
	{	
		for(int i=0; i<dictionary.length; i++)
		{
			splitKeyword = dictionary[i].split(" ");
			originalKeyword[i] = splitKeyword[0];
			replaceKeyword[i] = splitKeyword[1];
		}
		
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);			
		
		try{
			for(int i=0;i<text.sentenceNumber;i++)
			    for(int j=0,k; j != text.sentence[i].wordNumber; j++)
				{
					for(k=0; k<originalKeyword.length; k++)
					    if(text.sentence[i].word[j].equals(originalKeyword[k]))
					    {
						bw.write(replaceKeyword[k]);
						break;
					    }
					if(k == originalKeyword.length)
					    bw.write(text.sentence[i].word[j]);
				}
			bw.flush();
		}
		catch(Exception e){
			System.out.println("writing ERROR in WordReplace");
		}			
		return sw.toString();	
	}
}
