overall[tag] += alpha[metapathnum]*(prob)
							if rank_num >= 10:
								break
						metapathnum += 1		
					sorted_x = sorted(overall.items(), key=lambda x:x[1], reverse= True)
					sorted_x = sorted_x[:20]
					sum_ = 0.0
					for item in sorted_x:
						sum_ += item[1]
					matchedTags = []
					for item in sorted_x:
						matchedTags.append((item[0],item[1]/sum_))
					
					predictedTags = [item[0] for item in matchedTags]
					prec = calcPrecision_2(expectedTags, predictedTags)
					rec = calcRecall_2(expectedTags, predictedTags)
					
					cumPre[0] += prec[0]
					cumPre[1] += prec[1]
					cumPre[2] += prec[2]
					cumRec[0] += rec[0]
					cumRec[1] += rec[1]
					cumRec[2] += rec[2]	

				out = str(alpha[0]) + ',' + str(alpha[1]) + ',' + str(alpha[2]) + ',' 
				out += str(cumPre[0]/totcnt) + ',' + str(cumPre[1]/totcnt) + ',' + str(cumPre[2]/totcnt) + ',' + str(cumRec[0]/totcnt) + ',' + str(cumRec[1]/totcnt) + ',' + str(cumRec[2]/totcnt) + ','			
				out += '\n'
				wfile.write(out)
				wfile.flush()

	wfile.close()
				# predictedTags = [item[0] for item in post['predicted_by_Doc2Vec_Matching']]
				# predictedTags = [item[0] for item in post['predicted_by_StringAndFIC_Matching']]
				# predictedTags = [item[0] for item in post['predicted_by_StringAndWVTool_Matching']]
				# predictedTags = [item[0] for item in post['final_Recommended_Tags']]
				# predictedTags = [item[0] for item in post['spread_final_Recommended_Tags']]
				
				#for baseline checking
				# predictedTags = [item[0] for item in post['predicted']]
				# predictedTags = [item[0] for item in post['predicted_By_EnTagRec']]
				if qcount%1000 == 0:
					print qcount
					sys.stdout.flush()				
				if len(predictedTags) <= 0:
					continue
				post['precision'] = calcPrecision_2(expectedTags, predictedTags)
				post['recall'] = calcRecall_2(expectedTags, predictedTags)

				cumPre[0] += post['precision'][0]
				cumPre[1] += post['precision'][1]
				cumPre[2] += post['precision'][2]
				cumRec[0] += post['recall'][0]
				cumRec[1] += post['recall'][1]
				cumRec[2] += post['recall'][2]

			# wfile.write(str(post) + '\n')
			# wfile.flush()

	# wfile.close()
		print 'Num Docs', qcount
		print 'Precision: ',
		print cumPre[0]/qcount,
					expectedTags = FIC_tags[i]['expected']
					tags = {}
					
					for tag,tag_prob in LDA_tags[i]['predicted']:
						if tag not in tags:
							tags[tag] = 0.0
						tags[tag] += alpha*tag_prob
					
					for tag,tag_prob in FIC_tags[i]['predicted']:
						if tag not in tags:
							tags[tag] = 0.0
						tags[tag] += beta*tag_prob
					
					sorted_x = sorted(tags.items(), key=lambda x:x[1], reverse= True)
					predicted = [i[0] for i in sorted_x]
			
					recall += calcRecall_2(expectedTags, predicted)[-1]
				
				if 	recall > recall_max:
					recall_max = recall
					alpha_max = alpha
					beta_max = beta

		print alpha_max, beta_max, recall_max/((end-start))
		# wfile = open('../data/EnTagRecWeights_'+str(part)+'_.txt','w')
		# wfile.write(str(alpha_max) + ' ' + str(beta_max) + ' ' + str(recall_max/(num-(end-start))) + '\n' )
		# wfile.flush()
		# wfile.close()

			temp = {}
			for tag,prob in predicted_by_String_Matching:
				temp[tag] = alpha*prob
			# for tag,prob in predicted_By_FIC:
			for tag,prob in predicted_by_Doc2Vec_Matching:
				if tag not in temp.keys():
					temp[tag] = (1.0-alpha)*prob
				else:
					temp[tag] += (1.0-alpha)*prob

			sorted_tags = sorted(temp.items(), key=operator.itemgetter(1), reverse=True)
			sorted_tags = sorted_tags[:topK]
			predicted = [it[0] for it in sorted_tags]
			expected = post['expected']
			precision = calcPrecision_2(expected, predicted)
			recall = calcRecall_2(expected, predicted)

			cum_pre[0] += precision[0]
			cum_pre[1] += precision[1]
			cum_pre[2] += precision[2]

			cum_rec[0] += recall[0]
			cum_rec[1] += recall[1]
			cum_rec[2] += recall[2]
			cnt += 1.0
			# matchedTags = []
			# sum_ = 0.0
			# for sorted_tag in sorted_tags:
			# 	sum_ += sorted_tag[1]
			# for sorted_tag in sorted_tags:
			# 	matchedTags.append((sorted_tag[0],sorted_tag[1]/sum_))