//
// Mining module for TETDM 
// PartOfSpeechRate.java Version 0.30
// Copyright(C) 2021 WATARU SUNAYAMA All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//


package module.MiningModules.PartOfSpeechRate;

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

import java.io.*;
import java.text.*;
import java.util.*;


public class PartOfSpeechRate extends MiningModule
{
    int speechCount[];
    double speechRate[];
	
	public PartOfSpeechRate()
	{
		setModuleID(58);
		setCategoryID(2);
//		setUseMode(3);
		pairingVisualizationID = new int[]{1};
	}
	
//	public void initializePanel(){}	

	public void initializeData()
    {
        speechCount = new int[21];
        speechRate = new double[21];
        for(int i=0;i<20;i++)
        {
            speechCount[i] = 0;
            speechRate[i] = 0.0;
        }
    }
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
				calculatePartOfSpeechRate();
				break;
		}
	}

	public void countPartOfSpeech()
    {
        int sum=0;
        
        for(int i=0;i<text.sentenceNumber;i++)
            for(int j=0;j<text.sentence[i].wordNumber;j++)
                if(text.sentence[i].wordIDList[j] >= 0)
                    speechCount[text.keyword[text.sentence[i].wordIDList[j]].partOfSpeech]++;
                else
                {
                    speechCount[text.sentence[i].wordIDList[j]+20]++;
//                    System.out.println(" ["+text.sentence[i].word[j]+"]"+text.sentence[i].wordIDList[j]);
                }
        
        for(int i=0;i<20;i++)
            sum += speechCount[i];
        
        for(int i=0;i<20;i++)
            speechRate[i] = 100*(double)speechCount[i]/sum;

    }
    
	public void calculatePartOfSpeechRate()
	{
        String speechNamesInEnglish[] = {"","noun","verb","adjective","conjunction","adverb","emotional","auxiliary","auxiliary verb","pre-noun adjectival"};
        String speechNamesInEnglish2[] = {"","unknown","bug check","return","stop word","stop for one character","stop for hiragana, katakana, hankaku","others, comma, punctuation, etc...","period",""};
        
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        

        try
        {
            countPartOfSpeech();
        
            if(isMenuInJapanese())
                bw.write(inJapanese[0]);
            else
                bw.write("[Keywords]");
            bw.write(System.getProperty("line.separator"));
            
            for(int i=1;i<=9;i++)
            {
                if(isMenuInJapanese())
                    bw.write(String.format("%d: %4.1f%% %s",speechCount[i], speechRate[i], inJapanese[i]));
                else
                    bw.write(String.format("%d: %4.1f%% %s",speechCount[i], speechRate[i], speechNamesInEnglish[i]));
                bw.write(System.getProperty("line.separator"));
            }

            bw.write(System.getProperty("line.separator"));
            
            if(isMenuInJapanese())
                bw.write(inJapanese[10]);
            else
                bw.write("[Non Keywords]");
            bw.write(System.getProperty("line.separator"));
            
            for(int i=17;i>=12;i--)
            {
                if(isMenuInJapanese())
                    bw.write(String.format("%d: %4.1f%% %s",speechCount[i], speechRate[i], inJapanese[30-i]));   //20-i (1-8 -> 11-18)
                else
                    bw.write(String.format("%d: %4.1f%% %s",speechCount[i], speechRate[i], speechNamesInEnglish2[20-i]));   //i-20
                bw.write(System.getProperty("line.separator"));
            }

			bw.flush();
		}
		catch(Exception e){
			System.out.println("Error in calculatePartOfSpeechRate");
            e.printStackTrace();            
		}			
		setDataString(0, sw.toString());
	}
    

}
