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


package module.MiningModules.KanjiRate;

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

//japanese site average  = 0.365 = 21302350/58299499
//http://www.mwsoft.jp/programming/munou/unicode_block_java.html

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

import java.lang.Character.UnicodeBlock;


public class KanjiRate extends MiningModule
{
	int hiraganaCount[], katakanaCount[], kanjiCount[];
	double rate[];
	int totalHiragana, totalKatakana, totalKanji;
	double totalRate;
	
	public KanjiRate()
	{
		setModuleID(34);
		setCategoryID(5);
		setUseMode(0);
		pairingVisualizationID = new int[]{1,4};		
	}
	
//	public void initializePanel(){}	

//	public void initializeData(){}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{			
			case 0:
				calculateKanjiRate();
				break;
		}
	}

	
	public void calculateRate(String str, int number)
	{
		int length = str.length();
	
		char code;

		for(int i=0;i<length;i++)
		{
			code = str.charAt(i);
			if(UnicodeBlock.of(code) == UnicodeBlock.HIRAGANA)
				hiraganaCount[number]++;
			if(UnicodeBlock.of(code) == UnicodeBlock.KATAKANA)
				katakanaCount[number]++;
			if(UnicodeBlock.of(code) == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
				kanjiCount[number]++;			
		}
		if(hiraganaCount[number]+katakanaCount[number]+kanjiCount[number] == 0)
			rate[number] = 0.0;
		else
			rate[number] = (double)kanjiCount[number]/(hiraganaCount[number]+katakanaCount[number]+kanjiCount[number]);
	}
	
	public void calculateKanjiRate()
	{
		hiraganaCount = new int[text.segmentNumber];
		katakanaCount = new int[text.segmentNumber];
		kanjiCount = new int[text.segmentNumber];		
		rate = new double[text.segmentNumber];
		
		totalHiragana = totalKatakana = totalKanji = 0;
		
		for(int i=0;i<text.segmentNumber;i++)
		{
			calculateRate(text.segment[i].segmentText, i);
			totalHiragana += hiraganaCount[i];
			totalKatakana += katakanaCount[i];
			totalKanji += kanjiCount[i];			
		}
		
		if(totalHiragana+totalKatakana+totalKanji == 0)
			totalRate = 0.0;
		else
			totalRate = (double)totalKanji/(totalHiragana+totalKatakana+totalKanji);		
		
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		try
		{
			bw.write("TOTAL: KANJI Rate = "+totalRate+" Kanji="+totalKanji+" Hiragana="+totalHiragana+" Katakana="+totalKatakana);
			bw.write(System.getProperty("line.separator"));
			for(int i=0;i<text.segmentNumber;i++)
			{
				bw.write("Segment "+(i+1)+": KANJI Rate = "+rate[i]+" Kanji="+kanjiCount[i]+" Hiragana="+hiraganaCount[i]+" Katakana="+katakanaCount[i]);
				bw.write(System.getProperty("line.separator"));
			}
			bw.flush();
		}
		catch(Exception e){
			System.out.println("Error in KanjiRate");
            e.printStackTrace();            
		}			
		setDataString(0, sw.toString());
		
		setDataDoubleArray(0, rate);
		setDataDouble(0, totalRate);		
	}
}
