#!/usr/bin/python
# This Python file uses the following encoding: utf-8
from __future__ import print_function
from itertools import groupby
from operator import itemgetter
import collections

#f= open('Hi_Blogs.txt', 'r')
#f= open('htest.txt', 'r')
#my_text = f.read()
#f.close()




def bigrams(string):
	f1= open('bigrams.txt', 'w')
	temp = string.split()
	bigram_list = []
	#freq_list = []
	for i in range(0, len(temp)-1):
		bgram = temp[i] + ' ' + temp[i+1]
		'''
		if(bgram in bigram_list):
			index = bigram_list.index(bgram)
			freq_list[index] += 1
		else:
			bigram_list.append(bgram)
			freq_list.append(1)
		'''
		bigram_list.append(bgram)
		#freq_list.append(1)
	#print (bigram_list)
	#print (freq_list)
	for i in range(0, len(bigram_list)):
		#f1.write(bigram_list[i] + ' ' + str(freq_list[i]) + '\n')
		f1.write(bigram_list[i] + '\n')
	f1.close()
	

def trigrams(string):
	f2= open('trigrams.txt', 'w')
	temp = string.split()
	trigram_list = []
	#freq_list = []
	for i in range(0, len(temp)-2):
		tgram = temp[i] + ' ' + temp[i+1] + ' ' + temp[i+2]
		'''
		if(tgram in trigram_list):
			index = trigram_list.index(tgram)
			freq_list[index] += 1
		else:
			trigram_list.append(tgram)
			freq_list.append(1)
		'''
		trigram_list.append(tgram)
		#freq_list.append(1)
	#print (trigram_list)
	#print (freq_list)
	for i in range(0, len(trigram_list)):
		#f2.write(trigram_list[i] + ' ' + str(freq_list[i]) + '\n')
		f2.write(trigram_list[i] + '\n')
	f2.close()


def dupli(filename, filename2):
	#f =open(filename, 'r')
	f1 = open(filename2, 'w')
	with open(filename) as infile:
    		counts = collections.Counter(l.strip() for l in infile)
	for line, count in counts.most_common():
    		f1.write(line + ' ' + str(count) + '\n')
	


#bigrams(my_text)
#trigrams(my_text)


#dupli('trigrams.txt', '1trigrams.txt' )
#dupli('bigrams.txt', '1bigrams.txt' )	




'''
with open('1trigrams.txt') as fin:
    lines = [line.split() for line in fin]

lines.sort(key=itemgetter(3), reverse = True)

with open('3_trigrams.txt', 'w') as fout:
    for el in lines:
        fout.write('{0}\n'.format(' '.join(el)))
'''

