#include<iostream>
#include<sstream>
#include<fstream>
#include<map>
#include<vector>
#include<cstring>

std::istream *ifs;


void
read_file(int gram, int out_prob){

  std::map<int, std::map<int, int> > m;
  std::string line;
  std::string buffer;
  
  while(!ifs->eof()){
    int key, value;
    std::getline(*ifs, line);
    std::istringstream iss(line);
    std::vector <int> str_v;
    while(!iss.eof() && (iss >> buffer)) str_v.push_back(atoi(buffer.c_str));
    
    m[key][value] += 1;
    m[key][-2] += 1;
  }

  for(std::map<int, std::map<int, int> >::iterator itr = m.begin();
      itr != m.end(); ++itr){
    int key = itr->first;
    std::map<int, int>& m2 = itr->second;
    int count = m2[-2];
    if(out_prob == 1) m2.erase(-2);
    std::cout << key;
    if(out_prob == 1){
      for(std::map<int, int>::iterator itr2 = m2.begin();
	  itr2 != m2.end(); ++itr2){
	double probability = (double)(itr2->second) / (double)count;
	std::cout << '\t' << itr2->first << ':' << probability;
      }
    }else{
      for(std::map<int, int>::iterator itr2 = m2.begin();
	  itr2 != m2.end(); ++itr2){
	std::cout << '\t' << itr2->first << ':' << itr2->second;
      }
    }
    std::cout << std::endl;
  }
}



int main(int argc, char* argv[]){

  int gram = 2;
  int out_prob = 0;
  if(argc > 1){
    gram = atoi(argv[1]);
  }
  if(argc > 2){
    out_prob = 1;
  }

  ifs = new std::ifstream(argv[3]);

  if(! *ifs){
    std::cerr << "Can't open " << "morphologic.txt" << std::endl;
    exit(-1);
  }
  read_file(gram, out_prob);

  delete(ifs);

}
