package com.gs.Classifier;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.Map;

/**
* ÑµÁ·¼¯¹ÜÀíÆ÷
*/

public class TrainingDataManager 
{
	private String[] traningFileClassifications;//ÑµÁ·ÓïÁÏ·ÖÀà¼¯ºÏ
	private File traningTextDir;//ÑµÁ·ÓïÁÏ´æ·ÅÄ¿Â¼
	//private static String defaultPath = "D:\\Lucene\\docs\\ÑµÁ··ÖÀàÓÃÎÄ±¾\\";
	//private static String defaultPath = "D:\\Lucene\\ClassFile\\";
	//private static String defaultPath = "D:\\Lucene\\·ÖÀàÑµÁ·Óë²âÊÔÓïÁÏ¿â\\DRAPÌá¹©µÄ²âÊÔÑµÁ·ÓïÁÏ\\TanCorpMinTrain\\";
	//private static String defaultPath = "D:\\Lucene\\·ÖÀàÑµÁ·Óë²âÊÔÓïÁÏ¿â\\¸´µ©ÀîÈÙÂ½Ìá¹©µÄÓïÁÏ¿â\\";
	private static String defaultPath = "D:\\Lucene\\Corpus\\";
	//private static String defaultPath = "/opt/Test/Corpus";
	//private static String defaultPath = "D:\\Lucene\\·ÖÀàÑµÁ·Óë²âÊÔÓïÁÏ¿â\\DRAPÌá¹©µÄ²âÊÔÑµÁ·ÓïÁÏ\\Corpus\\";
	private Map<String,Map<String,Double>> classMap = new HashMap<String,Map<String,Double>>();
	private static TrainingDataManager ini = new TrainingDataManager();
	@SuppressWarnings("unchecked")
	private TrainingDataManager() 
	{
		traningTextDir = new File(defaultPath);
		if (!traningTextDir.isDirectory()) 
		{
			throw new IllegalArgumentException("ÑµÁ·ÓïÁÏ¿âËÑË÷Ê§°Ü£¡ [" +defaultPath + "]");
		}
		this.traningFileClassifications = traningTextDir.list();
		//¼ÓÔØËùÓÐMap
		String ss[] = traningTextDir.list();
		for(int i= 0;i<ss.length;i++){
			Map<String, Double> map = new HashMap<String, Double>();
			ObjectInputStream ois = null;
			try {
				FileInputStream is = new FileInputStream(defaultPath+ss[i]+"\\map");
				ois = new ObjectInputStream(is);
				map = (Map<String, Double>) ois.readObject();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			classMap.put(ss[i], map);
			map = null;
		}
	}
	
	public static TrainingDataManager getInstance(){
		return ini;
	}
	/**
	* ·µ»ØÑµÁ·ÎÄ±¾Àà±ð£¬Õâ¸öÀà±ð¾ÍÊÇÄ¿Â¼Ãû
	* @return ÑµÁ·ÎÄ±¾Àà±ð
	*/
	public String[] getTraningClassifications() 
	{
		return this.traningFileClassifications;
	}
	/**
	* ¸ù¾ÝÑµÁ·ÎÄ±¾Àà±ð·µ»ØÕâ¸öÀà±ðÏÂµÄËùÓÐÑµÁ·ÎÄ±¾Â·¾¶£¨full path£©
	* @param classification ¸ø¶¨µÄ·ÖÀà
	* @return ¸ø¶¨·ÖÀàÏÂËùÓÐÎÄ¼þµÄÂ·¾¶£¨full path£©
	*/
	public String[] getFilesPath(String classification) 
	{
		File classDir = new File(traningTextDir.getPath() +File.separator +classification);
		String[] ret = classDir.list();
		for (int i = 0; i < ret.length; i++) 
		{
			ret[i] = traningTextDir.getPath() +File.separator +classification +File.separator +ret[i];
		}
		return ret;
	}

	/**
	* ·µ»Ø¸ø¶¨Â·¾¶µÄÎÄ±¾ÎÄ¼þÄÚÈÝ
	* @param filePath ¸ø¶¨µÄÎÄ±¾ÎÄ¼þÂ·¾¶
	* @return ÎÄ±¾ÄÚÈÝ
	* @throws java.io.FileNotFoundException
	* @throws java.io.IOException
	*/
	public static String getText(String filePath) throws FileNotFoundException,IOException 
	{
	
		InputStreamReader isReader =new InputStreamReader(new FileInputStream(filePath),"GBK");
		BufferedReader reader = new BufferedReader(isReader);
		String aline;
		StringBuilder sb = new StringBuilder();
	
		while ((aline = reader.readLine()) != null)
		{
			sb.append(aline + " ");
		}
		isReader.close();
		reader.close();
		return sb.toString();
	}

	/**
	* ·µ»ØÑµÁ·ÎÄ±¾¼¯ÖÐËùÓÐµÄÎÄ±¾ÊýÄ¿
	* @return ÑµÁ·ÎÄ±¾¼¯ÖÐËùÓÐµÄÎÄ±¾ÊýÄ¿
	*/
	public int getTrainingFileCount()
	{
		int ret = 0;
		for (int i = 0; i < traningFileClassifications.length; i++)
		{
			ret +=getTrainingFileCountOfClassification(traningFileClassifications[i]);
		}
		return ret;
	}

	/**
	* ·µ»ØÑµÁ·ÎÄ±¾¼¯ÖÐÔÚ¸ø¶¨·ÖÀàÏÂµÄÑµÁ·ÎÄ±¾ÊýÄ¿
	* @param classification ¸ø¶¨µÄ·ÖÀà
	* @return ÑµÁ·ÎÄ±¾¼¯ÖÐÔÚ¸ø¶¨·ÖÀàÏÂµÄÑµÁ·ÎÄ±¾ÊýÄ¿
	*/
	public int getTrainingFileCountOfClassification(String classification)
	{
		File classDir = new File(traningTextDir.getPath() +File.separator +classification);
		return classDir.list().length;
	}

	/**
	* ·µ»Ø¸ø¶¨·ÖÀàÖÐ°üº¬¹Ø¼ü×Ö£¯´ÊµÄÑµÁ·ÎÄ±¾µÄÊýÄ¿
	* @param classification ¸ø¶¨µÄ·ÖÀà
	* @param key ¸ø¶¨µÄ¹Ø¼ü×Ö£¯´Ê
	* @return ¸ø¶¨·ÖÀàÖÐ°üº¬¹Ø¼ü×Ö£¯´ÊµÄÑµÁ·ÎÄ±¾µÄÊýÄ¿
	*/
	public int getCountContainKeyOfClassification(String classification,String key) 
	{
		int ret = 0;
		ret = (int) (classMap.get(classification).containsKey(key) ? classMap.get(classification).get(key):0);
		return ret;
	}
}