

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

double *hat_rd_score;
long *DocID;
FILE *patentPropertiesFILE;

long TotalFoundLowFindableDocuments;
long TotalFoundHighFindableDocuments;
int *ComponentLabel;
int PPS;
int Ex = 0;
int ExpansionCount;
long GoodDocuments;
int TotalItems = 0;
int TotalPatents = 0;
char sourcePath[300];
long **m_New32Projection;
long *m_New32Projection_Clean;
long *ExpansionTerm_Clean;
long *QueryClusterN;
long **DocumentSimilarity;
long **fullPatent;
long *TermFreqInRelevantDocuments;
long **fullPatentInterval;
long *fullPatentIntervalLength;
long *fullPatentRowLength;
double *LastPatentConsidered;
long ExpandedTerms[900];
double ExpandedTermsWeight[900];
long *tempRow;
double Minimum;
long MinimumIndex;
int tempRowCount = 0;
long TopExpandedTermsConsider;
long TopExpandedTermsAdded;
int Size = 0;
long ClaimQueryItemFrequency[1000];
long *IDFValue;
double *Patent_QRelevance_Score;
long *Patent_QRelevance_Name;
int Patent_QRelevance_Count;
long **PatentTermsArray;
int QueryItemCount = 0;
int QueryItemArray[100];
int QueryGenerationOn = 0;
float AveragePatentLength = 0;
long *FastProjection;
int FastProjectionCount = 0;
long *PatentHashIndex;
long *fullPatentUniqueItems;
float Parameter_Value;
long *TermFreqInWholeCollection;
long *TermSingleFreqInAllDocuments;
long Projection_32RowCount;
double *PatentRMScore;
long *TermFreqInAllDocuments;

long **DocFindability_High;
long *DocTotalQueries_High;


long HighQ,LowQ,MediumQ;
long ExpansionCountHigh,ExpansionCountMedium,ExpansionCountLow;
long **DocFindability_Medium;
long *DocTotalQueries_Medium;


long **DocFindability_Low;
long *DocTotalQueries_Low;


struct TermsBinaryTree
{
		struct TermsBinaryTree *RightBT;
		struct TermsBinaryTree *LeftBT;
		int Number;
		char WordName[300];
};
struct TermsBinaryTree *TermsBinaryTreeParentNode,*TermsBinaryTreeParentNodeQG,*ProcessQueryUniqueTerms;

int ProcessQueryUniqueTermsCount = 0;

struct TermsStruct
{
		char TermsName[100];
};
struct TermsStruct *TermsQG,*Terms;


char cleanTermName[100];

long FastQueryBank[100000][10];
long FastQueryBankLength[100000];
int FastQueryBankCount = 0;
int SS = 0;
unsigned long *ProjectionRows;




long *HashIndex;





/*--------------------------- Inverted Index Variables ------------------------------------- */

struct InvertedItemsArray
{
		float Score;
		float TFScore;
		int DocumentNumber;
	//	struct InvertedItemsArray *nextPtr;


};
struct InvertedItemsArray **InvertedItemsNumbers,*tempInvertedVariable;
int *InvertedItemsNumbersCount;

long AllTermsOccurance;



long **DocTermsArray_TermNumber;
long **DocTermsArray_TermFrequency;
long *DocTermsArray_TotalTerms;

long *Projection_FoundPatentMappingNumber;


long *fullPatentLength;


float *PostingScore;
float *PostingTFScore;
int *PostingQueryTermsPresence;
long *PostingAge;
int GlobalPostingAge;


int *FoundPatents;
float *FoundPatentsScore;
float *FoundPatentsTFScore;
int FoundPatentsCount;


struct TopNBinaryTree
{
		struct TopNBinaryTree *RightBT;
		struct TopNBinaryTree *LeftBT;
		struct TopNBinaryTree *Parent;

		float Score;
		float TFScore;
		int DocumentNumber;
		
};
struct TopNBinaryTree *TopNParentNode,*TopNMinimumNode;
int TopNCount = 0;
float MinimumTopNScore = 0;
int QPG = 0;
int *DocumentsFindability;
float predicted_score;
FILE *findability_log_High;
FILE *findability_log_Medium;
FILE *findability_log_Low;
int fileNumber;


long **FoundQP;
long **TotalQP;


long TermsArray[190000];
long TermsFreqArray[190000];

int TermsArrayCount = 0;


int EvalDoc = 0;
int MinimumIDF = 0;
int RSType = 0;
float PartitionMinScore[102];
float PartitionMaxScore[102];
int FastPartitionValue;
int *TermCollectionSum;
long CollectionModel;




/*------------------------------------------------------------------------------------------ */








int gotoxy(int x, int y,char printText[300]) {

					char essq[100]; // String variable to hold the escape sequence

					char xstr[100]; // Strings to hold the x and y coordinates

					char ystr[100]; // Escape sequences must be built with characters

					sprintf(xstr, "%d", x);

					sprintf(ystr, "%d", y);

					essq[0] = '\0';

					strcat(essq, "\033[");

					strcat(essq, ystr);

					strcat(essq, "d");


					strcat(essq, "\033[");

					strcat(essq, xstr);

					strcat(essq, "G");

					printf("%s%s",essq,printText);

 


					return 0;

}
void clrscr()
{
		int i , j;

		for ( i = 0; i < 100; i++ )
		{
			for ( j = 0; j < 100; j ++ )
					gotoxy(i,j," ");
		}
}



void InsertItem_InvertedIndex(int DocumentNumber, int ItemNumber, float Score)
{

		InvertedItemsNumbers[ItemNumber][InvertedItemsNumbersCount[ItemNumber]].DocumentNumber = DocumentNumber;
		InvertedItemsNumbers[ItemNumber][InvertedItemsNumbersCount[ItemNumber]].Score = Score;
		InvertedItemsNumbers[ItemNumber][InvertedItemsNumbersCount[ItemNumber]].TFScore = Score;
		InvertedItemsNumbersCount[ItemNumber]++;
	
}





void ReadItemFile(int clusterNumber,int patentsCopyingFrom, int patentsCopyingTo)
{
	FILE *itemfptr,*rowfptr,*fullTextfptr,*wordNumberfptr,*idf_fptr;
	char itemFileName[300];
	char rowFileName[300];
	char fullTextFileName[300];
	char wordNumberFileName[300];
	char IDFFileName[300];

	int i, j;
	int ItemNumber = 0;
	int eof = 0;
	int RowCount = 0;
	int BlockCount = 0;
	int tt = 0;
	int IntLength = 0;
	char word[300];
	int IntervalCount = 0;
	int LastItem = 0;
	int Difference = 0;
	long ANDValue = 65535;
	int idfcount = 0;
	char printText[300];
	float Min,Max;
	 
	



	sprintf(itemFileName,"%sItemsetProcessing%d.txt",sourcePath,clusterNumber);
	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);





	itemfptr = fopen(itemFileName,"r");
	fullTextfptr = fopen(fullTextFileName,"r");



	fscanf(itemfptr,"%d",&TotalPatents);
	fscanf(itemfptr,"%d",&TotalItems);
	TotalItems++;

	if ( patentsCopyingTo > TotalPatents )
		patentsCopyingTo = TotalPatents;


	TotalPatents = (patentsCopyingTo-patentsCopyingFrom);

	TermsBinaryTreeParentNode = NULL;
	TermsBinaryTreeParentNodeQG = NULL;


	RowCount = 0;
	tt = 0;


	






	IDFValue = (long *)malloc((TotalItems+2)*sizeof(long));
	TermCollectionSum = (int *)malloc((TotalItems+2)*sizeof(int));

	for ( j = 0; j < TotalItems+2; j++ )
	{
			IDFValue[j] = 0;
			TermCollectionSum[j] = 0;
	}




	DocTermsArray_TotalTerms = (long *)malloc((TotalPatents)*sizeof(long));
	for ( j = 0; j < TotalPatents; j++ )
			DocTermsArray_TotalTerms[j] = 0;








	RowCount = 0;
	IntervalCount = 0;
	Patent_QRelevance_Count = 0;
	FoundPatentsCount = 0;
	while(1)
	{
			if ( fscanf(fullTextfptr,"%d %d",&ItemNumber,&IntLength) == EOF )
					break;

			if ( ItemNumber != -17 && ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo) )
			{

					 IDFValue[ItemNumber]++;
					 DocTermsArray_TotalTerms[RowCount]++;


			}

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{
							RowCount++;
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);




	InvertedItemsNumbers = (struct InvertedItemsArray *)malloc((TotalItems+2)*sizeof(struct InvertedItemsArray));
	InvertedItemsNumbersCount = (int *)malloc((TotalItems+2)*sizeof(int));
	for ( j = 0; j < TotalItems+2; j++ )
	{
			InvertedItemsNumbers[j] = (struct InvertedItemsArray *)malloc((IDFValue[j])*sizeof(struct InvertedItemsArray));
			InvertedItemsNumbersCount[j] = 0;
	}



	ComponentLabel = (int *)malloc((TotalPatents)*sizeof(int));


	DocID = (long *)malloc((TotalPatents)*sizeof(long));
	hat_rd_score = (double *)malloc((TotalPatents)*sizeof(double));


	fullPatentLength = (long *)malloc((TotalPatents)*sizeof(long));
	DocumentsFindability = (int *)malloc((TotalPatents)*sizeof(int));
	PostingScore = (float *)malloc((TotalPatents)*sizeof(float));
	PostingTFScore = (float *)malloc((TotalPatents)*sizeof(float));
	PostingQueryTermsPresence = (int *)malloc((TotalPatents)*sizeof(int));
	PostingAge = (long *)malloc((TotalPatents)*sizeof(long));
	FoundPatents = (int *)malloc((TotalPatents)*sizeof(int));
	FoundPatentsScore = (float *)malloc((TotalPatents)*sizeof(float));
	FoundPatentsTFScore = (float *)malloc((TotalPatents)*sizeof(float));
	fullPatentUniqueItems = (long *)malloc((TotalPatents)*sizeof(long));

	DocTermsArray_TermNumber = (long *)malloc((TotalPatents)*sizeof(long));
	DocTermsArray_TermFrequency = (long *)malloc((TotalPatents)*sizeof(long));


	



	FoundPatentsCount = 0;
	GlobalPostingAge = 0;


	for ( j = 0; j < TotalPatents; j++ )
	{
			fullPatentLength[j] = 0;
			PostingAge[j] = -1;
			DocumentsFindability[j] = 0;
			fullPatentUniqueItems[j] = 0;


			DocTermsArray_TermNumber[j] = (long *)malloc((DocTermsArray_TotalTerms[j])*sizeof(long));
			DocTermsArray_TermFrequency[j] = (long *)malloc((DocTermsArray_TotalTerms[j])*sizeof(long));

			DocTermsArray_TotalTerms[j] = 0;



	}








	CollectionModel = 0;
	RowCount = 0;
	IntervalCount = 0;
	Patent_QRelevance_Count = 0;
	AveragePatentLength = 0;
	FoundPatentsCount = 0;
	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	fullTextfptr = fopen(fullTextFileName,"r");

	while(1)
	{
			if ( fscanf(fullTextfptr,"%d %d",&ItemNumber,&IntLength) == EOF )
					break;

			if ( ItemNumber != -17 && ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo) )
			{
				//	if ( RowCount > 1157380 )
					{
							InsertItem_InvertedIndex(RowCount,ItemNumber,IntLength);	
							fullPatentLength[RowCount] += IntLength;
							fullPatentUniqueItems[RowCount]++;

							DocTermsArray_TermNumber[RowCount][DocTermsArray_TotalTerms[RowCount]] = ItemNumber; 
							DocTermsArray_TermFrequency[RowCount][DocTermsArray_TotalTerms[RowCount]] = IntLength; 
							DocTermsArray_TotalTerms[RowCount]++;


							TermCollectionSum[ItemNumber] += IntLength;
							CollectionModel++;
					}


					
			
			}

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{							
							AveragePatentLength += fullPatentLength[RowCount];

						//	if ( RowCount > 1157380 )
						//			printf("\n%d....%d\n",RowCount,fullPatentLength[RowCount]);

						//	if ( RowCount == 1157386 )
						//			exit(1);

							if ( fullPatentUniqueItems[RowCount] > 190000 )
								exit(1);

							RowCount++;

							if ( RowCount%20000 == 0 )
								printf("\n Indexing Documents...%d",RowCount);
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);

	//exit(1);








}

void quicksort__FindabilityScore ( int lo , int hi )
{
		int i = lo, j = hi;
		long h;
		int x = ComponentLabel[ FoundPatents[(lo+hi)/2] ];
		int iTempNumber;
		double dTempNumber;
		do
		{
				while ( ComponentLabel[FoundPatents[i]] < x ) i++;
				while ( ComponentLabel[FoundPatents[j]] > x ) j--;
				if ( i <= j )
				{

						iTempNumber = FoundPatents[i];
						FoundPatents[i] = FoundPatents[j];
						FoundPatents[j] = iTempNumber;

			
						dTempNumber = FoundPatentsScore[i];
						FoundPatentsScore[i] = FoundPatentsScore[j];
						FoundPatentsScore[j] = dTempNumber;


						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort__FindabilityScore (lo,j);
		if ( i<hi) quicksort__FindabilityScore (i,hi);
	
}





void quicksort_hat_rd_score ( int lo , int hi )
{
		int i = lo, j = hi;
		long h;
		double x = hat_rd_score[ (lo+hi)/2 ];
		int iTempNumber;
		double dTempNumber;
		do
		{
				while ( hat_rd_score[i] < x ) i++;
				while ( hat_rd_score[j] > x ) j--;
				if ( i <= j )
				{

						iTempNumber = DocID[i];
						DocID[i] = DocID[j];
						DocID[j] = iTempNumber;




						dTempNumber = hat_rd_score[i];
						hat_rd_score[i] = hat_rd_score[j];
						hat_rd_score[j] = dTempNumber;


						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort_hat_rd_score (lo,j);
		if ( i<hi) quicksort_hat_rd_score (i,hi);
	
}

void CreateDocumentsPartitions_rd_score(int Partitions)
{
		int i;
		int Factor;
		int Label;




			
		quicksort_hat_rd_score(0,35000);
	
		Factor = 35000/Partitions;


		for ( i = 0; i < 35000; i++ )
		{
				Label = i/Factor;
				ComponentLabel[DocID[i]] = Label;	
				
			//	printf("\n%lf...",hat_rd_score[i]);

		}

			//exit(1);
	


}
void ExtractPatentFeatures(int PatentNumber)
{
	int Low = 0;
	int i = 0,j=0;
	int PatentLoop = 0;
	int LastInterval = 0;
	int IntervalValue = 0;
	int IntervalJump = 0;
	long ANDValue = 65535;
	int Value = 0;
	int PatentLength = 0;
	int TermCount = 0;
	double AverageTF = 0;
	double SingleAverageTF = 0;
	double AverageIDF = 0;
	double SingleAverageIDF = 0;
	int AllTerms = 0;

	int f1 = 0;
	float f2 = 0;
	float f3 = 0;
	int f4 = 0;
	double SingleRatio = 0;
	int SingleFrequentTerms = 0;

	int TermsArray[60000];
	int TermsFreqArray[60000];

	int TermsArrayCount = 0;
	int SingleAllTerms = 0;
	double TermLengthRatio = 0;
	double NATF_Frequent_Terms = 0;
	double UpperSum = 0;
	double LowerSum = 0;
	double GINI = 0;
	double GINI_R = 0;
	int GINI_Frequent_Terms = 0;
	double NATF_GINI = 0;

	double Avg_DF_GINI = 0;
	long Avg_DF_GINI_Count = 0;
	







	
				PatentLoop = PatentNumber;




				PatentLength = 0;	
				AverageTF = 0;
				for  ( Low = 0; Low < DocTermsArray_TotalTerms[PatentNumber]; Low++ )
				{		

							TermsArray[TermsArrayCount] = DocTermsArray_TermNumber[PatentNumber][Low];
							TermsFreqArray[TermsArrayCount] = DocTermsArray_TermFrequency[PatentNumber][Low];
							TermsArrayCount++;

							PatentLength += DocTermsArray_TermFrequency[PatentNumber][Low];
				}
				

				





				
				for ( i = 0; i < TermsArrayCount; i++ )
				{
						   for ( j = i+1; j < TermsArrayCount; j++ )
						   {

										if (    (double)((double)(TermsFreqArray[i] + TermsFreqArray[j])/(double)PatentLength) > 0.05       )
										{											
												
													AverageTF += (double)((double)(TermsFreqArray[i]+TermsFreqArray[j])/(double)fullPatentLength[PatentNumber]);											
													if ( (double)((double)(TermsFreqArray[i]+TermsFreqArray[j])/(double)fullPatentLength[PatentNumber]) >= .110  )
															f1++;
											



													AverageIDF += (double)((double)IDFValue[TermsArray[i]]+(double)IDFValue[TermsArray[j]])/(double)(TotalPatents);
													AllTerms++;												
										}
									

						   }
						   if ( (double)((double)TermsFreqArray[i]/(double)PatentLength ) > 0.005 )
						   {											
												
										SingleAverageTF += (double)((double)(TermsFreqArray[i])/(double)fullPatentLength[PatentNumber]);											
										if ( (double)((double)(TermsFreqArray[i])/(double)fullPatentLength[PatentNumber]) >= .02 )
										{
												NATF_Frequent_Terms += (double)((double)(TermsFreqArray[i])/(double)fullPatentLength[PatentNumber]);
												SingleFrequentTerms++;
										}
											

										SingleAverageIDF += (double)((double)IDFValue[TermsArray[i]])/(double)(TotalPatents);
							
										SingleAllTerms++;
												
							}

						   
				}



				if ( AllTerms > 0 )
				{
						AverageTF = (double)((double)AverageTF/(double)AllTerms);
						AverageIDF = (double)((double)AverageIDF/(double)AllTerms);
				}

				if ( f1 > 0 )
				{
						f2 = (double)((double)f1/(double)AllTerms);
				}
				else
						f2 = 0;


				if ( SingleAllTerms > 0 )
				{
						SingleAverageTF = (double)((double)SingleAverageTF/(double)SingleAllTerms);									
						SingleAverageIDF = (double)((double)SingleAverageIDF/(double)SingleAllTerms);									
				}


				if ( SingleFrequentTerms > 0 && SingleAllTerms > 0)
				{
						SingleRatio = (double)((double)SingleFrequentTerms/(double)SingleAllTerms);
						NATF_Frequent_Terms = (double)((double)NATF_Frequent_Terms/(double)SingleFrequentTerms);
				}
				else
				{
						SingleRatio = 0;
						NATF_Frequent_Terms = 0;
				}

				

			






				for ( i = 0; i < TermsArrayCount; i++ )
				{
						for ( j = 0; j < TermsArrayCount-1; j++ )
						{
								if ( TermsFreqArray[j] > TermsFreqArray[i] )
								{
										f1 = TermsFreqArray[i];
										TermsFreqArray[i] = TermsFreqArray[j];
										TermsFreqArray[j] = f1;


										
										f1 = TermsArray[i];
										TermsArray[i] = TermsArray[j];
										TermsArray[j] = f1;

								}
						}
				}
				



				for ( i = 0; i < TermsArrayCount; i++ )
				{
						UpperSum += ((2*(i+1)-(TermsArrayCount)-1) * TermsFreqArray[i] );
						LowerSum += TermsFreqArray[i];	
					//	printf("\n%d...",TermsFreqArray[i]
					
				}
				LowerSum = LowerSum * ((TermsArrayCount)-1);

				if ( LowerSum > 0 )
					GINI = UpperSum / LowerSum;
				else
					GINI = 0;




				j = 0;

				while(1)
				{
						LowerSum = 0;
						UpperSum = 0;
						GINI_R = 0;
					

						if ( TermsArrayCount-j > 0 )
						{
								for ( i = 0; i < TermsArrayCount-j; i++ )
								{
										UpperSum += ((2*(i+1)-(TermsArrayCount-j)-1) * TermsFreqArray[i] );
										LowerSum += TermsFreqArray[i];						
								}
								LowerSum = LowerSum * ((TermsArrayCount-j)-1);
								GINI_R = UpperSum / LowerSum;
						}
						else
							break;


						if ( GINI_R < 0.10 )
						{
								GINI_Frequent_Terms = j;
								for ( i = TermsArrayCount-j; i < TermsArrayCount; i++ )
								{
										NATF_GINI += (double)((double)(TermsFreqArray[i])/(double)fullPatentLength[PatentNumber]);
									//	if ( TermsFreqArray[i] > 15 )
										{
												Avg_DF_GINI += (double)((double)(IDFValue[TermsArray[i]])/(double)TotalPatents);										
												Avg_DF_GINI_Count++;
										}
								}
								if ( j > 0 )
								{
									NATF_GINI = NATF_GINI/(double)(j);
									if ( Avg_DF_GINI_Count > 0 )
										Avg_DF_GINI = Avg_DF_GINI/(double)(Avg_DF_GINI_Count);

								}
								else
								{
									NATF_GINI = 0;
									Avg_DF_GINI = 0;
								}


								break;
						}
						j++;




				}














		

	//with patent number			fprintf(patentPropertiesFILE,"%d,%d,%d,%f,%f,%f,%d,%f,%f,%f,%d,%f,%f,%d,%f,",PatentNumber,PatentLength,AllTerms,AverageTF,AverageIDF,f2,f1,SingleAverageTF,SingleAverageIDF,SingleRatio,SingleFrequentTerms,NATF_Frequent_Terms,GINI,GINI_Frequent_Terms,NATF_GINI);
				fprintf(patentPropertiesFILE,"%f	%f	%f	%d	%f	%f	%f	%d	%f	%f	%d	%f	%lf	",AverageTF,AverageIDF,f2,f1,SingleAverageTF,SingleAverageIDF,SingleRatio,SingleFrequentTerms,NATF_Frequent_Terms,GINI,GINI_Frequent_Terms,NATF_GINI,Avg_DF_GINI);

	











	
}

void quicksort__Terms ( long lo , long hi )
{
		long i = lo, j = hi;
		long h;
		long x = TermsFreqArray[ (lo+hi)/2 ];
		long iTempNumber;
		long dTempNumber;
		do
		{
				while ( TermsFreqArray[i] < x ) i++;
				while ( TermsFreqArray[j] > x ) j--;
				if ( i <= j )
				{

						dTempNumber = TermsFreqArray[i];
						TermsFreqArray[i] = TermsFreqArray[j];
						TermsFreqArray[j] = dTempNumber;


						dTempNumber = TermsArray[i];
						TermsArray[i] = TermsArray[j];
						TermsArray[j] = dTempNumber;


							
			
					


						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort__Terms (lo,j);
		if ( i<hi) quicksort__Terms (i,hi);
	
}
void ExtractDocsFeatures(int PatentNumber)
{
	int Low = 0;
	int i = 0,j=0;
	int PatentLoop = 0;
	int LastInterval = 0;
	int IntervalValue = 0;
	int IntervalJump = 0;
	long ANDValue = 65535;
	int Value = 0;
	int PatentLength = 0;
	int TermCount = 0;
	double AverageTF = 0;
	double SingleAverageTF = 0;
	double AverageIDF = 0;
	double SingleAverageIDF = 0;
	int AllTerms = 0;

	int f1 = 0;
	float f2 = 0;
	float f3 = 0;
	int f4 = 0;
	double SingleRatio = 0;
	int SingleFrequentTerms = 0;

	int SingleAllTerms = 0;
	double TermLengthRatio = 0;
	double NATF_Frequent_Terms = 0;
	double UpperSum = 0;
	double LowerSum = 0;
	double GINI = 0;
	double GINI_R = 0;
	long GINI_Frequent_Terms = 0;
	double NATF_GINI = 0;

	double Avg_DF_GINI = 0;
	long Avg_DF_GINI_Count = 0;
	



	double NATF = 0;
	long freq = 0;
	double NATF_freq = 0;
	double ADF = 0;
	long freq_low_df = 0;
	double ADF_freq = 0;
	int ADF_CC = 0;





//	printf("\nstart %d...%d\n",PatentNumber,DocTermsArray_TotalTerms[PatentNumber]);
	
				PatentLoop = PatentNumber;

				




				PatentLength = 0;	
				AverageTF = 0;
				TermsArrayCount = 0;
				for  ( Low = 0; Low < DocTermsArray_TotalTerms[PatentNumber]; Low++ )
				{		

							TermsArray[TermsArrayCount] = DocTermsArray_TermNumber[PatentNumber][Low];
							TermsFreqArray[TermsArrayCount] = DocTermsArray_TermFrequency[PatentNumber][Low];
							TermsArrayCount++;

							PatentLength += DocTermsArray_TermFrequency[PatentNumber][Low];
				}


				//printf("\n%d....%d....\n",PatentNumber,TermsArrayCount);
				

				





				
				for ( i = 0; i < TermsArrayCount; i++ )
				{							
							NATF += (double)((double)TermsFreqArray[i]/(double)PatentLength);


						//	if ( IDFValue[TermsArray[i]] > 900 )
							{
									ADF += (double)((double)IDFValue[TermsArray[i]]/(double)TotalPatents);
									ADF_CC++;
							}

							if ( (double)((double)TermsFreqArray[i]/(double)PatentLength) >= 0.03 )
							{
									freq++;
									NATF_freq += (double)((double)TermsFreqArray[i]/(double)PatentLength);
							}

							if ( (double)((double)IDFValue[TermsArray[i]]/(double)TotalPatents) <= 0.05 )
							{
									freq_low_df++;
									ADF_freq += (double)((double)IDFValue[TermsArray[i]]/(double)TotalPatents);
							}



				}

				if ( TermsArrayCount > 0 )
				{
						NATF = (double)((double)NATF/(double)TermsArrayCount);
						ADF = (double)((double)ADF/(double)ADF_CC);
				}

				if ( freq > 0 )
				{
						NATF_freq = (double)((double)NATF_freq/(double)freq);
				}

				if ( freq_low_df > 0 )
				{
						ADF_freq = (double)((double)ADF_freq/(double)freq_low_df);
				}






		
				quicksort__Terms(0,TermsArrayCount-1);
				

			//	printf("\n%d....%d....\n",PatentNumber,TermsArrayCount);


				for ( i = 0; i < TermsArrayCount; i++ )
				{
						UpperSum += ((2*(i+1)-(TermsArrayCount)-1) * TermsFreqArray[i] );
						LowerSum += TermsFreqArray[i];	
				}
				LowerSum = LowerSum * ((TermsArrayCount)-1);

				if ( LowerSum > 0 )
					GINI = UpperSum / LowerSum;
				else
					GINI = 0;




			//	printf("\n%d....%d....\n",PatentNumber,TermsArrayCount);
				j = 0;

				while(1)
				{
						LowerSum = 0;
						UpperSum = 0;
						GINI_R = 0;
					

						if ( TermsArrayCount-j > 0 )
						{
								for ( i = 0; i < TermsArrayCount-j; i++ )
								{
										UpperSum += ((2*(i+1)-(TermsArrayCount-j)-1) * TermsFreqArray[i] );
										LowerSum += TermsFreqArray[i];						
								}
								LowerSum = LowerSum * ((TermsArrayCount-j)-1);
								if ( LowerSum > 0 )
									GINI_R = UpperSum / LowerSum;
								else
									GINI_R = 0;

							//	if ( PatentNumber == 1157386 )
							//		printf("\n%d...",i);
						}
						else
							break;


						if ( GINI_R < 0.10 )
						{
								GINI_Frequent_Terms = j;

								/*
								for ( i = TermsArrayCount-j; i < TermsArrayCount; i++ )
								{
										NATF_GINI += (double)((double)(TermsFreqArray[i])/(double)fullPatentLength[PatentNumber]);
									//	if ( TermsFreqArray[i] > 15 )
										{
												Avg_DF_GINI += (double)((double)(IDFValue[TermsArray[i]])/(double)TotalPatents);										
												Avg_DF_GINI_Count++;
										}
								}
								if ( j > 0 )
								{
									NATF_GINI = NATF_GINI/(double)(j);
									if ( Avg_DF_GINI_Count > 0 )
										Avg_DF_GINI = Avg_DF_GINI/(double)(Avg_DF_GINI_Count);

								}
								else
								{
									NATF_GINI = 0;
									Avg_DF_GINI = 0;
								}*/


								break;
						}
						j++;




				}


			//	printf("\n%d....%d....\n",PatentNumber,TermsArrayCount);

				fprintf(patentPropertiesFILE,"%f	%d	%f	%f	%d	%f	%d	%f	%d	%d",NATF,freq,NATF_freq,GINI,GINI_Frequent_Terms,ADF,freq_low_df,ADF_freq,fullPatentLength[PatentNumber],fullPatentUniqueItems[PatentNumber]);

			//	printf("\n%d....%d....\n",PatentNumber,TermsArrayCount);
				//	fullPatentLength[RowCount] += IntLength;
				//	fullPatentUniqueItems[RowCount]++;

	
}

int main (int argc, char *argv[])
{                              
	int clusterNumber = 0;
	int patentsCopyingFrom;
	int patentsCopyingTo;
	FILE *fptr;
	long V = 0;
	//int fileNumber;
	long Find5 = 0;
	long Find10 = 0;
	long Find15 = 0;
	long Find20 = 0;
	long Find25 = 0;
	long TotalQu = 0;
	char queriesFileName[200];

	
	system("clear");



	// first parameter = files folder path (like //home//bashir//)
	// second parameter = cluster number
	// third parameter = patentsCopyingFrom
	// forth parameter = patentsCopyingTo
	// fifth parameter = fileNumber - log	//



	strcpy(sourcePath,argv[1]);
	clusterNumber = atoi(argv[2]);

	patentsCopyingFrom = atoi(argv[3]);
	patentsCopyingTo = atoi(argv[4]);




	ReadItemFile(clusterNumber,patentsCopyingFrom,patentsCopyingTo);




	
	V = 0;





	sprintf(queriesFileName,"%sSurface_DocsFeatures.txt",sourcePath);//,fileNumber
	patentPropertiesFILE = fopen(queriesFileName,"w");



	for ( V = 0; V < TotalPatents; V++ )
	{	
					ExtractDocsFeatures(V);
					fprintf(patentPropertiesFILE,"\n");			
	

					if ( V%8000 == 0 )
						printf("\n Docs Features Extraction Completed = %d",V);
	}


	return 0;
}  
