//
// Mining module for TETDM 
// Morpheme.java Version 0.30
// This program is provided under the license.
// You Should read the README file.
//

package module.MiningModules.MorphemeAnalysis;

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

import java.io.*;

import net.reduls.igo.Tagger;
import net.reduls.igo.Morpheme;


public class MorphemeAnalysis extends MiningModule
{
	public MorphemeAnalysis()
	{
		setModuleID(9000);						// Set your module ID after you have got it
		setCategoryID(8);
		pairingVisualizationID = new int[]{1};
		setToolType(2);
	}

//	public void initializePanel(){}
	
//	public void initializeData(){}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
                resetData();
				setDataString(MyMethod());
				break;
		}
	}

	String MyMethod()
	{
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);			
		
		
		
		try{		
		
			final String dicDir = text.control.sp1.dictionaryPath+"/ipadic";
			final Tagger tagger = new Tagger(dicDir);
			
			int lastLength, lastStart, length;
			lastLength = 0;
			lastStart = 0;			
			
			System.out.println("dicDir="+dicDir);
			
			for(Morpheme m : tagger.parse(text.originalText))
			{

				for(int i=0;i<(m.start - lastStart) - lastLength;i++)	//If invisible words are included
				{
                    String invisibleWord = text.originalText.substring(lastStart+lastLength+i, lastStart+lastLength+i+1);
                    
                    if(invisibleWord.equals(System.getProperty("line.separator")) || invisibleWord.charAt(0) == 10)   //LF
                        bw.write("EOS");
                    else
                        bw.write(inJapanese[0]+"\t"+"SPACE/TAB,*,*,*,*,*,X,*,*");

					bw.write(System.getProperty("line.separator"));								
				}
				lastLength = m.surface.length();
				lastStart = m.start;				
				
				
//				System.out.println(m.surface+"	"+m.feature); //TAB
                
             
                if(m.surface.charAt(0) != 13)   // CR for windows
                {
     //                System.out.println("CODE = "+m.surface.charAt(0)+" ["+(int)m.surface.charAt(0)+"]");
                     bw.write(m.surface+"    "+m.feature);    //TAB
                     bw.write(System.getProperty("line.separator"));
                }

	
			}
					bw.write("EOS");						
					bw.write(System.getProperty("line.separator"));	//need
			bw.flush();
			bw.close();
			
		}
		catch(Exception e){
			System.out.println("writing ERROR in MorphemeAnalysis.java");
		}		

		
		return sw.toString();	
	}	
}
