// Path: src/main/java/vn/vitk/util/MapUtil.java
// public class MapUtil {
// 	
// 	/**
// 	 * Sorts a map by value.
// 	 * @param map
// 	 * @return a map whose values are sorted.
// 	 */
// 	public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
// 		List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
// 		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
// 			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
// 				return -(o1.getValue()).compareTo(o2.getValue());
// 			}
// 		});
// 
// 		Map<K, V> result = new LinkedHashMap<K, V>();
// 		for (Map.Entry<K, V> entry : list) {
// 			result.put(entry.getKey(), entry.getValue());
// 		}
// 		return result;
// 	}
// }


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import scala.Tuple2;
import vn.vitk.util.MapUtil;
	
	public static void main(String[] args) {
//		String corpusFileName = "/export/dat/udt/en/en-ud-train.conllu";
//		String corpusFileName = "/export/dat/udt/en/en-ud-test.conllu";
//		List<DependencyGraph> graphs = DependencyGraphReader.readCoNLLU(corpusFileName);
		
		String corpusFileName = "/export/dat/udt/vi/dataAll.conll";
		List<DependencyGraph> graphs = DependencyGraphReader.read(corpusFileName, 'x');
		
		Map<Tuple2<String, String>, Integer> map = headDependentMap(graphs);
		System.out.println("#(tuples) = " + map.size());
		for (Tuple2<String, String> tuple : map.keySet()) {
			System.out.println(tuple + ":" + map.get(tuple));
		}
		System.out.println();
		
		Map<String, Map<String, Integer>> heads = headMap(graphs);
		System.out.println("#(head) = " + heads.size());
		for (String head : heads.keySet()) {
			System.out.println(head);
			Map<String, Integer> dependents = heads.get(head);
			System.out.println("\t" + dependents.size() + " dependents.");
			System.out.println("\t" + heads.get(head));
		}
		
		// get possible root PoS tags, ordered by frequency
		// 
		System.out.println();
		System.out.println("root PoS: ");
		Map<String, Integer> dependentsOfRoot = heads.get("ROOT");
// Path: src/main/java/vn/vitk/util/MapUtil.java
// public class MapUtil {
// 	
// 	/**
// 	 * Sorts a map by value.
// 	 * @param map
// 	 * @return a map whose values are sorted.
// 	 */
// 	public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
// 		List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
// 		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
// 			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
// 				return -(o1.getValue()).compareTo(o2.getValue());
// 			}
// 		});
// 
// 		Map<K, V> result = new LinkedHashMap<K, V>();
// 		for (Map.Entry<K, V> entry : list) {
// 			result.put(entry.getKey(), entry.getValue());
// 		}
// 		return result;
// 	}
// }


// Path: src/main/java/vn/vitk/dep/DependencyGraphStatistics.java
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import scala.Tuple2;
import vn.vitk.util.MapUtil;

	
	public static void main(String[] args) {
//		String corpusFileName = "/export/dat/udt/en/en-ud-train.conllu";
//		String corpusFileName = "/export/dat/udt/en/en-ud-test.conllu";
//		List<DependencyGraph> graphs = DependencyGraphReader.readCoNLLU(corpusFileName);
		
		String corpusFileName = "/export/dat/udt/vi/dataAll.conll";
		List<DependencyGraph> graphs = DependencyGraphReader.read(corpusFileName, 'x');
		
		Map<Tuple2<String, String>, Integer> map = headDependentMap(graphs);
		System.out.println("#(tuples) = " + map.size());
		for (Tuple2<String, String> tuple : map.keySet()) {
			System.out.println(tuple + ":" + map.get(tuple));
		}
		System.out.println();
		
		Map<String, Map<String, Integer>> heads = headMap(graphs);
		System.out.println("#(head) = " + heads.size());
		for (String head : heads.keySet()) {
			System.out.println(head);
			Map<String, Integer> dependents = heads.get(head);
			System.out.println("\t" + dependents.size() + " dependents.");
			System.out.println("\t" + heads.get(head));
		}
		
		// get possible root PoS tags, ordered by frequency
		// 
		System.out.println();
		System.out.println("root PoS: ");
		Map<String, Integer> dependentsOfRoot = heads.get("ROOT");
		Map<String, Integer> sortedDoR = MapUtil.sortByValue(dependentsOfRoot);