

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


int fork_size;
char find_query_name[300];
int cutoff_1;
int cutoff_2;
int cutoff_3;
int cutoff_4;
int cutoff_5;
char find_file_split[300];
char Term_Posting_File[300];
char offline_cluster_file[300];
char J48Rules[300];


double AvgUniqueTerms;
long **TermPresence;
double *hat_rd_score;
long *DocID;
long *ComponentLabel;
long GlobalPartitionNumber;

int CriticalSection = 0;
typedef struct str_thdata
{
    int ThreadNumber;
	int Local_GlobalPostingAge;
	int ExitStatus;

} thdata;
int ActualLeng = 0;

int terminate_threads;
long QE_FlagCounter;

long ParallelMaxSize;
long p_running;

long Exp_CM;
int PPS;

int TotalItems = 0;
int TotalPatents = 0;
char sourcePath[300];
long terminate_processes;



long **m_New32Projection;
long *m_New32Projection_Clean;
long *ExpansionTerm_Clean;
long *QueryClusterN;

long *TermFreqInRelevantDocuments;
long *FAST_TF_QE;
long *FAST_TF_QE_Flag;


double *LastPatentConsidered;
long ExpandedTerms[900];
double ExpandedTermsWeight[900];
double Minimum;
long MinimumIndex;
long TopExpandedTermsConsider;
long TopExpandedTermsAdded;
long ClaimQueryItemFrequency[100];
long *TermFreqInWholeCollection;
long *TermSingleFreqInAllDocuments;
long Projection_32RowCount;
long *TermFreqInAllDocuments;
unsigned long *ProjectionRows;
long SS;
long AllTermsOccurance;
long *Projection_FoundPatentMappingNumber;
double *NorFindability;
long Ex;
long GoodDocuments;




long QueryItemCount;
long QueryItemArray[100];
double *PatentRMScore;
long **DocFindability_High;
long *DocTotalQueries_High;
long HighQ;
long ExpansionCountHigh;
double *PostingScore;
double *PostingTFScore;
long *PostingQueryTermsPresence;
long *PostingAge;
long *FoundPatents;
double *FoundPatentsScore;
double *FoundPatentsTFScore;
long FoundPatentsCount;








int Size = 0;
long *IDFValue;
int Patent_QRelevance_Count;
double AveragePatentLength = 0;
long *fullPatentUniqueItems;
double Parameter_Value;
long **DocumentSimilarity;





long **q_Terms;
long *q_Terms_Count;
long **doc_Qs;
long **doc_Qs_R;
long *doc_Qs_Count;
long *doc_Qs_Count_Writing;






long TotalFoundHighFindableDocuments;
long TotalFoundLowFindableDocuments;


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

struct InvertedItemsArray
{
		double Score;
		double TFScore;
		int DocumentNumber;

};
struct InvertedItemsArray **InvertedItemsNumbers,*tempInvertedVariable;
int *InvertedItemsNumbersCount;
long **DocTermsArray_TermNumber;
long **DocTermsArray_TermFrequency;
long *DocTermsArray_TotalTerms;
long *fullPatentLength;



int GlobalPostingAge;









FILE *findability_log_High;
int fileNumber;
int RSType = 0;
int *TermCollectionSum;
long CollectionModel;






//QP Similar Terms Variables
long ***DocTermsArray_TermPosting;
struct J48_Tree
{
	char AttributeName[200];
	struct J48_Tree *RightNode;
	struct J48_Tree *LeftNode;
	int LeftNodeCondition;
	int RightNodeCondition;
	int LeftNodeRuleStatus;
	int RightNodeRuleStatus;
	double LeftNodeValue;
	double RightNodeValue;
	int nodeCompleted;

	int LeftNodeRuleClass;
	int RightNodeRuleClass;

	double LeftNodeRuleAccuracy;
	double RightNodeRuleAccuracy;
	int LeftMaximumSamples;
	int RightMaximumSamples;
	
	
};
struct J48_Tree *J48ParentNode;
long BestRuleMaximumSamples;
long GoodTermVotes;
long BadTermVotes;
double MaximumGoodBadPrediction;
long MaximumPredictionClass;
long RuleFire;
double unclassify_term_features[100];

long *PairPosting_QueryItem;
long **QueryTermsPosting;
long *QueryTermsPostingCount;
long *PairPosting_Min;
long *PairPosting_Max;
long PairPosting_Count;







const int BitTable[32] = {
                             2147483648UL, 1073741824UL, 536870912UL, 268435456UL,
                             134217728UL, 67108864UL, 33554432UL, 16777216UL,
                             8388608UL, 4194304UL, 2097152UL, 1048576UL,
                             524288UL, 262144UL, 131072UL, 65536UL,
                             32768UL, 16384UL, 8192UL, 4096UL,
                             2048UL, 1024UL, 512UL, 256UL,
                             128UL, 64UL, 32UL, 16UL,
                             8UL, 4UL, 2UL, 1UL
                         };
const char CountTable[256] = {
                                 0UL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 0, 0, 0},
{ 5, 6, 5, 2, 1, 0, 0, 0, 0},
{ 6, 7, 6, 5, 2, 1, 0, 0, 0},
{ 4, 4, 2, 1, 0, 0, 0, 0, 0},
{ 5, 7, 4, 2, 1, 0, 0, 0, 0},
{ 5, 6, 4, 2, 1, 0, 0, 0, 0},
{ 6, 7, 6, 4, 2, 1, 0, 0, 0},
{ 5, 5, 4, 2, 1, 0, 0, 0, 0},
{ 6, 7, 5, 4, 2, 1, 0, 0, 0},
{ 6, 6, 5, 4, 2, 1, 0, 0, 0},
{ 7, 7, 6, 5, 4, 2, 1, 0, 0},
{ 4, 3, 2, 1, 0, 0, 0, 0, 0},
{ 5, 7, 3, 2, 1, 0, 0, 0, 0},
{ 5, 6, 3, 2, 1, 0, 0, 0, 0},
{ 6, 7, 6, 3, 2, 1, 0, 0, 0},
{ 5, 5, 3, 2, 1, 0, 0, 0, 0},
{ 6, 7, 5, 3, 2, 1, 0, 0, 0},
{ 6, 6, 5, 3, 2, 1, 0, 0, 0},
{ 7, 7, 6, 5, 3, 2, 1, 0, 0},
{ 5, 4, 3, 2, 1, 0, 0, 0, 0},
{ 6, 7, 4, 3, 2, 1, 0, 0, 0},
{ 6, 6, 4, 3, 2, 1, 0, 0, 0},
{ 7, 7, 6, 4, 3, 2, 1, 0, 0},
{ 6, 5, 4, 3, 2, 1, 0, 0, 0},
{ 7, 7, 5, 4, 3, 2, 1, 0, 0},
{ 7, 6, 5, 4, 3, 2, 1, 0, 0},
{ 8, 7, 6, 5, 4, 3, 2, 1, 0}
    
	
	
};

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








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, double Score)
{
   	   /* struct InvertedItemsArray *firstItem = NULL;

		if ( InvertedItemsNumbers[ItemNumber] == NULL )
		{				
				tempInvertedVariable = (struct InvertedItemsArray *)malloc(sizeof(struct InvertedItemsArray));
				tempInvertedVariable->DocumentNumber = DocumentNumber;
				tempInvertedVariable->Score = Score;
				tempInvertedVariable->nextPtr = NULL;
				InvertedItemsNumbers[ItemNumber] = tempInvertedVariable;				
		}
		else
		{
			 
				firstItem = InvertedItemsNumbers[ItemNumber];
				tempInvertedVariable = (struct InvertedItemsArray *)malloc(sizeof(struct InvertedItemsArray));
				tempInvertedVariable->DocumentNumber = DocumentNumber;
				tempInvertedVariable->Score = Score;
				tempInvertedVariable->nextPtr = firstItem;
				InvertedItemsNumbers[ItemNumber] = tempInvertedVariable;				

		}*/

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




void Make_TFIDF_Indexing()
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;



		for ( i = 0; i < TotalItems+2; i++ )
		{
				IDF = IDFValue[i];
				IDF = (double)((double)(((TotalPatents))) /(double)IDF);
				IDF = (double)((double)log(IDF)/(double)log(2));

				
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
					printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
					exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{

							//	printf("\n%d.....%f",InvertedItemsNumbers[i][firstItem].DocumentNumber,InvertedItemsNumbers[i][firstItem].Score);


								TF = InvertedItemsNumbers[i][firstItem].Score;
								TF = (double) ( (double)TF/(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);

								InvertedItemsNumbers[i][firstItem].Score = IDF*TF;

							//	FoundPatentsScore[FoundPatentsCount] = firstItem->Score;
							//	FoundPatents[FoundPatentsCount] = firstItem->DocumentNumber;

							//	FoundPatentsCount++;



								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				


	
				}
		}

}

void Make_SMART_Indexing()
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double AvgTF = 0;
		double SecondPart = 0;
		double utf;
		double DocPart = 0;
		



		for ( i = 0; i < TotalItems+2; i++ )
		{
				IDF = IDFValue[i];
				IDF = (double)((double)(((TotalPatents))) /(double)IDF);
				IDF = (double)((double)log(IDF)/(double)log(2));

				
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
					printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
					exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{



								TF = InvertedItemsNumbers[i][firstItem].Score;
								TF = (double)((double)log(TF)/(double)log(2));
								TF = 1 + TF;

								AvgTF = ((double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]/(double)fullPatentUniqueItems[InvertedItemsNumbers[i][firstItem].DocumentNumber]);
								AvgTF = (double)((double)log(AvgTF)/(double)log(2));
								AvgTF = 1 + AvgTF;


								utf = (double)((double)fullPatentUniqueItems[InvertedItemsNumbers[i][firstItem].DocumentNumber]/(double)AvgUniqueTerms);
								SecondPart = (double)( (double)0.8 + (double)((double)0.2*(double)utf  ));
								SecondPart = (double) ( (double)1/(double)SecondPart);


								DocPart = AvgTF*SecondPart;


								TF = 1;
								TF = (double)((double)log(TF)/(double)log(2));
								TF = 1 + TF;

								AvgTF = (double)((double)(TotalPatents+1)/(double)IDFValue[i]);

								AvgTF = AvgTF*TF;



								InvertedItemsNumbers[i][firstItem].Score = DocPart*AvgTF;



								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				


	
				}
		}

}


void Make_UnNormTFIDF_Indexing()
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;



		for ( i = 0; i < TotalItems+2; i++ )
		{
				IDF = IDFValue[i];
				IDF = (double)((double)(((TotalPatents))) /(double)IDF);
				IDF = (double)((double)log(IDF)/(double)log(2));

				
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
					printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
					exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{



								TF = InvertedItemsNumbers[i][firstItem].Score;
							//	TF = (double) ( (double)TF/(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);

								InvertedItemsNumbers[i][firstItem].Score = IDF*TF;


								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				


	
				}
		}

}


void Make_TwoStage_Indexing()
{
		int firstItem = 0;
		double TF = 0,SecondPart,SecondPart2;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;
 	    double Parameter  = 0.7;
	    double Parameter2  = 2000;

		Parameter = Parameter_Value;


		for ( i = 0; i < TotalItems+2; i++ )
		{
			//	IDF = (double)( (double)IDFValue[i]/(double)TotalPatents );

			
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
						printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
						exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{


								IDF = (double)TermCollectionSum[i];
								IDF = (double) ( IDF / (double) (CollectionModel ) );							  


								TF = InvertedItemsNumbers[i][firstItem].Score;
	


								SecondPart2 = (double)( TF + (double)( (double)Parameter2 *(double)IDF));
								SecondPart2 = (double) ( SecondPart2/ (double)( (double) fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber] + (double)(Parameter2)) );
								SecondPart = (double)((double)(1-Parameter)*(SecondPart2)) +(Parameter * (double)IDF);
							//	printf("\n%f...",SecondPart);



								InvertedItemsNumbers[i][firstItem].Score = SecondPart;




								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				


				}
		}

}



void Make_DirS_Indexing()
{
		int firstItem = 0;
		double TF = 0,SecondPart;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;
	    double Parameter  = 2000;



		for ( i = 0; i < TotalItems+2; i++ )
		{
				IDF = (double)( (double)IDFValue[i]/(double)TotalPatents );

			
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
						printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
						exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{



								TF = InvertedItemsNumbers[i][firstItem].Score;

								TF = (double) ( (double)TF/(double)(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber] );
	
//								SecondPart = (double)( TF + (double) ((double)Parameter *(double)IDF));
//								SecondPart = SecondPart / (double) ( (double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber] + (double) Parameter    );




								InvertedItemsNumbers[i][firstItem].Score = TF;




								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				


				}
		}

}

void Make_JM_Indexing()
{
		int firstItem = 0;
		double TF = 0,SecondPart;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;
	    double Parameter  = .7;

		Parameter = Parameter_Value;


		for ( i = 0; i < TotalItems+2; i++ )
		{
			//	IDF = (double)( (double)IDFValue[i]/(double)TotalPatents );

			
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
						printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
						exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{


								IDF = (double)TermCollectionSum[i];
								IDF = (double) ( (double)IDF / (double) (CollectionModel ) );							  


								TF = InvertedItemsNumbers[i][firstItem].Score;
								TF = (double) ( (double)TF/(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);


						//		if ( Parameter == 1.0 )
						//			TF = 0;
						//		else
								TF = (double)((double)(1-Parameter)*(double)TF);


								SecondPart = (double)((double)Parameter*(double)IDF);

								InvertedItemsNumbers[i][firstItem].Score = (double)((double)TF+(double)SecondPart);




								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				


				}
		}

}

void Make_AbsDis_Indexing()
{
		int firstItem = 0;
		double TF = 0,SecondPart;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;
	    double Parameter  = 0.7;

		Parameter = Parameter_Value;


		for ( i = 0; i < TotalItems+2; i++ )
		{
			

			
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{						
						exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{


								IDF = (double)TermCollectionSum[i];
								IDF = (double) ( IDF / (double) (CollectionModel ) );							  


								TF = InvertedItemsNumbers[i][firstItem].Score;
								TF = TF  - Parameter;
								TF = (double) ( (double)TF/(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);


								SecondPart = (double)((double)fullPatentUniqueItems[InvertedItemsNumbers[i][firstItem].DocumentNumber]*(double)Parameter);
								SecondPart = (double) ( (double)SecondPart/(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);								
								SecondPart = (double)((double)SecondPart*(double)IDF);


								TF = (double)((double)TF+(double)SecondPart);

							//	printf("\n%f...%f...%d...%d",TF,SecondPart,InvertedItemsNumbers[i][firstItem].DocumentNumber,fullPatentUniqueItems[InvertedItemsNumbers[i][firstItem].DocumentNumber]);



								InvertedItemsNumbers[i][firstItem].Score = TF;
								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				


				}
		}

}


void Make_BM25_Indexing()
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;
        double k = 2.0;
        double b = .75;
		double TermFreq, IDFUpper, IDFLower;
  		double WeightedLength = 0;
        double MainUpper = 0;
        double MainLower = 0;
        double Main = 0;

		b = Parameter_Value;

	//	printf("\n\n%f...\n\n",b);
	//	exit(1);

		for ( i = 0; i < TotalItems+2; i++ )
		{
			//	IDF = IDFValue[i];
			//	IDF = (double)((double)(((TotalPatents))) /(double)IDF);
			//	IDF = (double)((double)log(IDF)/(double)log(2));

			
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
					printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
					exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{


								TF = InvertedItemsNumbers[i][firstItem].Score;
							//	TF = (double) ( (double)TF/(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);

							//	InvertedItemsNumbers[i][firstItem].Score = IDF*TF;

		

								

								WeightedLength = 0;
								WeightedLength = (double)((double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);

								IDF = (double)IDFValue[i];
							  
				
								IDFUpper = (double)(((double)(TotalPatents))- (double)IDF + (double)0.5);
								IDFLower = (double)IDF + (double)0.5;
								IDF = (double) (IDFUpper/IDFLower);
								IDF = (double)((double)log(IDF)/(double)log(2));

                    
								MainUpper = (double)(TF*(k+1));
								MainLower = (double)(TF + k*(1-b+(b*(WeightedLength/AveragePatentLength))));
								Main = MainUpper/MainLower;
								InvertedItemsNumbers[i][firstItem].Score  = (IDF*Main);






								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				




				}
		}

}


void Make_LM2000_Indexing()
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0,z,z1;
		double tempdouble;
		int tempInt;
        double k = 2.0;
        double b = .75;
		double TermFreq, IDFUpper, IDFLower;
  		double WeightedLength = 0;
        double MainUpper = 0;
        double MainLower = 0;
        double Main = 0;


		for ( i = 0; i < TotalItems+2; i++ )
		{

			
				if ( IDFValue[i] != InvertedItemsNumbersCount[i] )
				{
					printf("\n%d....%d...%d",i,IDFValue[i],InvertedItemsNumbersCount[i]);
					exit(1);
				}

				if ( InvertedItemsNumbersCount[i] > 0  )
				{
						firstItem = 0;
						while(1)
						{


								
						

		

								TF = InvertedItemsNumbers[i][firstItem].Score;								
								WeightedLength = 0;
								WeightedLength = (double)((double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);



								IDF = (double)TermCollectionSum[i];
								IDF = (double) ( IDF / (double) (CollectionModel ) );							  

								

				
								IDFUpper = TF +  ( (double) ( (double) Parameter_Value * IDF ) );
								MainLower = (double) ( WeightedLength + (double) Parameter_Value );



								Main = IDFUpper/MainLower;
								InvertedItemsNumbers[i][firstItem].Score= Main;




								firstItem++;
								if ( firstItem == InvertedItemsNumbersCount[i] )
									break;
						}
				




				}
		}

}




void ReadDocumentSimilarity()
{
	int P1 = 0, P2 = 0;
	int Value1 = 0,Value2= 0,Value3= 0;
	int MakeTransactionCount = 0;
	int ColCount2 = 0;
	FILE *fptr;
	int Change = 0;
	int DD = 0;
	char mm_FileName[200];

	for ( P1 = 0; P1 < TotalPatents; P1++ )
	{
		for ( P2 = 0; P2 < (TotalPatents/32)+1; P2++ )
		{
				DocumentSimilarity[P1][P2] = 0;
		}
	}


	sprintf(mm_FileName,"%s",offline_cluster_file);


	fptr = fopen(mm_FileName,"r");
	while (1)
	{
						MakeTransactionCount = 0;
						ColCount2 = 0;

						fscanf(fptr,"%d",&Value1);	
						fscanf(fptr,"%d",&Value2);	
						fscanf(fptr,"%d",&Value3);	


						if ( Value1 == -19 && Value2 == -19 && Value3 == -19 )
								break;

			

						if ( Value1 < TotalPatents && Value2 < TotalPatents  )
						{

						

								if (Value1 != Change && Value1%1000 == 0)
								{
										printf("\nReading Cluster = %d...",Value1);
										DD = 0;
								}
								if (Value1 != Change )
									DD = 0;


								


								if ( Value1 == Change )
								{

										ColCount2 = Value2/32;
										if ( DD < 100 )
											DocumentSimilarity[Value1][ColCount2] = DocumentSimilarity[Value1][ColCount2] | BitTable[(Value2%32)];

						
										DD++;
								}

								Change = Value1;



						}
						
	 }
	 fclose(fptr);




}

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

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

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


						i++;
						j--;
				}

		}
		while (i<=j);

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

void C_quicksortFirst(long low, long high) 
{

	long i = low;
	long j = high;
	long y = 0;
	int z1 = 0;
	int z2 = 0;
	double z = NorFindability	[(low + high) / 2];
	int tempValue;
	double tempdoubleValue;

	do
	{
				
				while(NorFindability[i] < z) i++;
				while(NorFindability[j] > z) j--;

				if(i <= j) 
				{


							z1 = i;
							z2 = j;



	


							tempdoubleValue = NorFindability[z1];
							NorFindability[z1] = NorFindability[z2];
							NorFindability[z2] = tempdoubleValue;

						i++; 
						j--;
				}
	} 
	while(i <= j);


	if(low < j) 
		C_quicksortFirst(low, j);

	if(i < high) 
		C_quicksortFirst(i, high); 
}

/*
void CalculateGINI_Length()
{






	double UpperSum = 0;
	double LowerSum = 0;
	double GINI = 0;


	int TP = 0;
	int i = 0;
	int j = 0;


	


	TP = 0;

	for ( i = 0; i < TotalPatents; i++ )
	{
				NorFindability[TP] = fullPatentLength[i];					
				TP++;
	}
	
	C_quicksortFirst(0,TP-1);	
	UpperSum = 0;
	LowerSum = 0;

					for ( j = 0; j < TP; j++ )
					{
							
					
									UpperSum += ((2*(j+1)-(TP)-1) * NorFindability[j] );
									LowerSum += NorFindability[j];					
									
					}

					LowerSum = LowerSum * ((TP)-1);
					GINI = UpperSum / LowerSum;

				
	
				

					printf("\nGini-Coefficient with Top is = %f...%d\n",GINI,TP);


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

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


	Exp_CM = 0;



	sprintf(itemFileName,"%sItemsetProcessing%d.txt",sourcePath,clusterNumber);
	if ( RSType != 21 )
		sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	if ( RSType == 21 )
		sprintf(fullTextFileName,"//space//home//sbashir//TREC_GA_Exp//fullText36.txt");

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



//	sprintf(itemFileName,"%sDoc_Length%d.txt",sourcePath,clusterNumber);
//	w_write = fopen(itemFileName,"w");







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


	if ( patentsCopyingTo > TotalPatents )
		patentsCopyingTo = TotalPatents;


	TotalPatents = (patentsCopyingTo-patentsCopyingFrom);


//	printf("\n%d...%d\n",TotalPatents,TotalItems);

	




	RowCount = 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;
	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) )
			{
					 if ( IntLength != -1 )
					 {
							 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);



//	sprintf(itemFileName,"%sDocs_Length.txt",sourcePath);	
//	idf_fptr = fopen(itemFileName,"w");
//	for ( j = 0; j < TotalPatents; j++ )
//	{
//			fprintf(idf_fptr,"%d\n",DocTermsArray_TotalTerms[j]);		
//	}

//	fclose(idf_fptr);
//	exit(1);



	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;
	}

			

	fullPatentLength = (long *)malloc((TotalPatents)*sizeof(long));
	fullPatentUniqueItems = (long *)malloc((TotalPatents)*sizeof(long));


	



			PostingScore = (double *)malloc((TotalPatents)*sizeof(double));
			PostingTFScore = (double *)malloc((TotalPatents)*sizeof(double));
			PostingQueryTermsPresence = (long *)malloc((TotalPatents)*sizeof(long));
			PostingAge = (long *)malloc((TotalPatents)*sizeof(long));
			FoundPatents = (long *)malloc((TotalPatents)*sizeof(long));
			FoundPatentsScore = (double *)malloc((TotalPatents)*sizeof(double));
			FoundPatentsTFScore = (double *)malloc((TotalPatents)*sizeof(double));
			QueryClusterN = (long *)malloc((TotalPatents )*sizeof(long));

			TermFreqInWholeCollection = (long *)malloc((TotalItems)*sizeof(long));
			TermSingleFreqInAllDocuments = (long *)malloc((TotalItems)*sizeof(long));
			TermFreqInRelevantDocuments = (long *)malloc((TotalItems)*sizeof(long));
			FAST_TF_QE = (long *)malloc((TotalItems+2)*sizeof(long));
			FAST_TF_QE_Flag = (long *)malloc((TotalItems+2)*sizeof(long));
			LastPatentConsidered = (double *)malloc((TotalItems)*sizeof(double));
			NorFindability = (double *)malloc((TotalPatents)*sizeof(double));
			TermFreqInAllDocuments = (long *)malloc((TotalItems)*sizeof(long));
			Projection_FoundPatentMappingNumber = (long *)malloc((TotalPatents)*sizeof(long));
			PatentRMScore = (double *)malloc((TotalPatents )*sizeof(double));
			DocTotalQueries_High = (long *) malloc ( (TotalPatents)  * sizeof(long));
			DocFindability_High = (long *) malloc ( (7)  * sizeof(long));

			TermPresence = (long *) malloc ( (TotalPatents)  * sizeof(long));

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


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

		
			if ( RSType == 101 )
			{

					
					q_Terms = (long *) malloc ( (2100000)  * sizeof(long)); 
					q_Terms_Count = (long *) malloc ( (2100000)  * sizeof(long)); 
					doc_Qs = (long *) malloc ( (TotalPatents)  * sizeof(long)); 
					doc_Qs_R = (long *) malloc ( (TotalPatents)  * sizeof(long)); 
					doc_Qs_Count = (long *) malloc ( (TotalPatents)  * sizeof(long)); 
					doc_Qs_Count_Writing = (long *) malloc ( (TotalPatents)  * sizeof(long)); 

					for ( j = 0; j < 2100000; j++ )
					{
							q_Terms[j]  = (long *) malloc ( (6)  * sizeof(long)); 
							if ( j < TotalPatents )
							{
									doc_Qs[j] = (long *) malloc ( (3000)  * sizeof(long)); 
									doc_Qs_R[j] = (long *) malloc ( (3000)  * sizeof(long)); 
							}
					}
					for ( j = 0; j < TotalPatents; j++ )
					{
							doc_Qs_Count[j] = 0;
							doc_Qs_Count_Writing[j] = 0;
					}

					printf("\n Memory Allocation for queries generation completed\n");

					
			}



			for ( j = 0; j < 7; j ++ )
			{
					DocFindability_High[j] = (long *) malloc ( TotalPatents  * sizeof(long));
					for ( k = 0; k < TotalPatents; k++ )
							DocFindability_High[j][k] = 0;
			}
			for ( j = 0; j < TotalPatents; j++ )
			{
					PostingAge[j] = -1;
					DocTotalQueries_High[j] = 0;
					TermPresence[j] = (long *) malloc ( 6  * sizeof(long));
					for ( i = 0; i < 6; i++ )
						TermPresence[j][i] = -1;
			}





			if ( RSType == 21 )
			{
					DocTermsArray_TermPosting = (long *)malloc((TotalPatents)*sizeof(long));
					for ( j = 0; j < TotalPatents; j++ )
						DocTermsArray_TermPosting[j] = (long *)malloc((DocTermsArray_TotalTerms[j])*sizeof(long));

					PairPosting_QueryItem = (long *)malloc((40000)*sizeof(long));
					PairPosting_Min = (long *)malloc((40000)*sizeof(long));
					PairPosting_Max = (long *)malloc((40000)*sizeof(long));


					QueryTermsPosting = (long *)malloc((400)*sizeof(long));
					QueryTermsPostingCount = (long *)malloc((400)*sizeof(long));



			}




			if ( RSType == 10 || RSType == 11 || RSType == 12 || RSType == 13 || RSType == 21 )
			{
					Size = (3000/32)+4;
					if ( RSType == 10 )
						m_New32Projection = (long*)malloc((TotalItems)*sizeof(long));

					m_New32Projection_Clean = (long*)malloc((TotalItems)*sizeof(long));
					ExpansionTerm_Clean = (long*)malloc((TotalItems)*sizeof(long));
					ProjectionRows = (unsigned long*)malloc((Size)*sizeof(unsigned long));
					for ( j = 0; j < Size; j++ )
						ProjectionRows[j] = 1;

					for (  j = 0; j < TotalItems; j++ )
					{
							if ( RSType == 10   )
							{
								m_New32Projection[j] = (unsigned long*)malloc((Size)*sizeof(unsigned long));
								for ( k = 0; k < Size; k++ )
								{
										m_New32Projection[j][k] = 0;
								}
							}
							m_New32Projection_Clean[j] = -2;
							ExpansionTerm_Clean[j] = -2;
					}
			}



		



	if ( RSType == 10 || RSType == 13 )
	{			
			DocumentSimilarity = (long*)malloc((TotalPatents )*sizeof(long));
			for ( i = 0; i < TotalPatents; i ++ )
				DocumentSimilarity[i] = (long *) malloc ( ( (TotalPatents/32) + 1) * sizeof(long));

	}
	if ( RSType == 10 || RSType == 11 || RSType == 12 || RSType == 13 || RSType == 21)
	{
			DocTermsArray_TermNumber = (long *)malloc((TotalPatents)*sizeof(long));
			DocTermsArray_TermFrequency = (long *)malloc((TotalPatents)*sizeof(long));
	}
	GlobalPostingAge = 0;
	for ( j = 0; j < TotalPatents; j++ )
	{
			fullPatentLength[j] = 0;
			fullPatentUniqueItems[j] = 0;
			if ( RSType == 10 || RSType == 11 || RSType == 12 || RSType == 13 || RSType == 21 )
			{
					DocTermsArray_TermNumber[j] = (long *)malloc((DocTermsArray_TotalTerms[j])*sizeof(long));
					DocTermsArray_TermFrequency[j] = (long *)malloc((DocTermsArray_TotalTerms[j])*sizeof(long));
			}
			DocTermsArray_TotalTerms[j] = 0;			
	}








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


	AvgUniqueTerms = 0;
	CollectionModel = 0;
	RowCount = 0;
	Patent_QRelevance_Count = 0;
	AveragePatentLength = 0;
	FoundPatentsCount = 0;


	if ( RSType != 21 )
	{


				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) )
						{
							
										FoundPatents[FoundPatentsCount] = ItemNumber;
										FoundPatentsScore[FoundPatentsCount] = IntLength;
										FoundPatentsCount++;		
												
						}

						if ( ItemNumber == -17 )
						{
								if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
								{						
									
										quicksort__Terms(0,FoundPatentsCount-1);
										for ( j = 0; j < FoundPatentsCount; j++ )
										{							
											
												ItemNumber = FoundPatents[j];
												IntLength = FoundPatentsScore[j];

												if ( RSType == 101 )
												{
														if ( IntLength > 2 && IntLength < 40 )
														{

														
																InsertItem_InvertedIndex(RowCount,ItemNumber,IntLength);	
																fullPatentLength[RowCount] += IntLength;
																fullPatentUniqueItems[RowCount]++;

																if ( RSType == 10 || RSType == 11 || RSType == 12 || RSType == 13 || RSType == 21 )
																{
																		DocTermsArray_TermNumber[RowCount][DocTermsArray_TotalTerms[RowCount]] = ItemNumber; 
																		DocTermsArray_TermFrequency[RowCount][DocTermsArray_TotalTerms[RowCount]] = IntLength; 
																		DocTermsArray_TotalTerms[RowCount]++;
																}


																TermCollectionSum[ItemNumber] += IntLength;
																CollectionModel+=IntLength;
																Exp_CM+=IntLength;
																IDFValue[ItemNumber]++;

														}
												}
												else
												{
														InsertItem_InvertedIndex(RowCount,ItemNumber,IntLength);	
														fullPatentLength[RowCount] += IntLength;
														fullPatentUniqueItems[RowCount]++;

														if ( RSType == 10 || RSType == 11 || RSType == 12 || RSType == 13 || RSType == 21 )
														{
																DocTermsArray_TermNumber[RowCount][DocTermsArray_TotalTerms[RowCount]] = ItemNumber; 
																DocTermsArray_TermFrequency[RowCount][DocTermsArray_TotalTerms[RowCount]] = IntLength; 
																DocTermsArray_TotalTerms[RowCount]++;
														}

														TermCollectionSum[ItemNumber] += IntLength;
														CollectionModel+=IntLength;
														Exp_CM+=IntLength;
														IDFValue[ItemNumber]++;

												}

														
												
												if ( RSType == 101 )
												{
														if ( j >= 150 )
															break;
												}
										}
										
										AveragePatentLength += fullPatentLength[RowCount];
										AvgUniqueTerms += fullPatentUniqueItems[RowCount];
										RowCount++;
										FoundPatentsCount = 0;
										if (RowCount%5000 == 0 )
										{
												if ( p_running == 1 )
													printf("\nIndexing Collection Completed %d/%d",RowCount,TotalPatents);
										}

								}
								Patent_QRelevance_Count++;

						}
						if ( Patent_QRelevance_Count == patentsCopyingTo )
								break;
							

				}
				Patent_QRelevance_Count = 0;
				fclose(fullTextfptr);

	}



//	printf("\n hello %s\n",fullTextFileName);

	if ( RSType == 21 )
	{
				RowCount = 0;
				IntervalCount = 0;
				Patent_QRelevance_Count = 0;				
				sprintf(fullTextFileName,"%s",Term_Posting_File);
				fullTextfptr = fopen(fullTextFileName,"r");
				PostingCounter = 0;

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


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

												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+=IntLength;
												Exp_CM+=IntLength;
												IDFValue[ItemNumber]++;


												IntervalCount = fullPatentUniqueItems[RowCount]-1;

												
						
												

												DocTermsArray_TermPosting[RowCount][IntervalCount] = (long *)malloc((IntLength+6)*sizeof(long));

									



												PostingCounter = 0;
								}
								else
								{
												if ( IntervalCount != -1 )
												{											
														DocTermsArray_TermPosting[RowCount][IntervalCount][PostingCounter] = ItemNumber;
														PostingCounter++;
												}
								}
								

								
						}
						

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

										RowCount++;
										if (RowCount%5000 == 0 )
											printf("\nIndexing Documents Completed %d/%d",RowCount,TotalPatents);
								}
								Patent_QRelevance_Count++;

						}
						if ( Patent_QRelevance_Count == patentsCopyingTo )
								break;
					
							

				}
				Patent_QRelevance_Count = 0;
				fclose(fullTextfptr);


	}






//	CalculateGINI_Length();
//	exit(1);
	if ( RSType == 1 || RSType == 101 || RSType == 61 )
	{
			Make_TFIDF_Indexing();
//			printf("\n TFIDF algorithm selected");
	}
	



	AveragePatentLength = (double)(AveragePatentLength/(double)TotalPatents);
	AvgUniqueTerms = (double)(AvgUniqueTerms/(double)TotalPatents);

	if ( RSType == 2 || RSType == 62)
	{
			Make_BM25_Indexing();
//			printf("\n BM25 algorithm selected");

	}

	if  ( RSType == 3|| RSType == 63 )
	{
			Make_LM2000_Indexing();
//			printf("\n LM algorithm selected");

	}


	if  ( RSType == 4 || RSType == 64)
	{
			Make_JM_Indexing();
	//		printf("\n JM algorithm selected");

	}

	if  ( RSType == 5 || RSType == 65)
	{
		//	printf("\n Absolute Discounting Smooting algorithm selected");
			Make_AbsDis_Indexing();

	}

	if  ( RSType == 6 || RSType == 66)
	{
		//	printf("\n Two Stage Smooting algorithm selected");
			Make_TwoStage_Indexing();
	}


	if  ( RSType == 7 || RSType == 67)
	{
		//	printf("\n Un Normalized TFIDF algorithm selected");
			Make_UnNormTFIDF_Indexing();
	}

	if  ( RSType == 8 || RSType == 68)
	{
		//	printf("\n SMART algorithm selected");
			Make_SMART_Indexing();
	}

	if ( RSType == 10 )
	{
			Make_BM25_Indexing();
		//	printf("\n QE FT BM25 algorithm selected");

	}
	if ( RSType == 11 )
	{
			Make_BM25_Indexing();
		//	printf("\n QE TS BM25 algorithm selected");

	}
	if ( RSType == 12 )
	{
			Make_BM25_Indexing();
		//	printf("\n QE KLD BM25 algorithm selected");

	}
	if ( RSType == 13 )
	{
			Make_BM25_Indexing();
		//	printf("\n QE Lee Clustering algorithm selected");
	}
	if ( RSType == 21 )
	{
			Make_BM25_Indexing();
		//	printf("\n QP TS Clustering algorithm selected");
	}

//	printf("\ncluster Number = %d",clusterNumber);
//	printf("\nDocuments Copying From = %d",patentsCopyingFrom);
//	printf("\nDocuments Copying To = %d",patentsCopyingTo);
	


	
/*	sprintf(itemFileName,"%spartition_input.txt",sourcePath);
	itemfptr = fopen(itemFileName,"w");
	for ( i =0; i < TotalPatents; i ++ )
	{
			fprintf(itemfptr,"%d\n",fullPatentUniqueItems[i]);



	}
	fclose(itemfptr);
*/




	if ( p_running == 1 )
	{
			printf("\nIndexing Collection Completed Successfully\n");
			printf("\nNow Processing Queries\n");
	}



}

void ProcessQueryInInvertedIndex(int Local_GlobalPostingAge)
{
		long z = 0;
		long firstItem[10];
		double MinimumScore = 999999;
		double TopKCheck = 0;
		long NULLCount = 0;
		long DocumentNumber = 0;
		double Score = 0;
		double TFScore = 0;
		long Length = 0;
		long z1;

	    double Parameter  = 0.7;
	    double Parameter2  = 2000;
		double TF = 0,SecondPart,SecondPart2;
		double IDF = 0;

		Parameter = Parameter_Value;
		Parameter2 = Parameter_Value;



		for ( z = 0; z < QueryItemCount-1; z++ )
		{
					firstItem[z] = 0;
					if ( InvertedItemsNumbersCount[QueryItemArray[z]] == 0 )
					{
						//	exit(1);
							return;
					}
		}




		while(1)
		{

					for ( z = 0; z < QueryItemCount-1; z++ )
					{
								if ( firstItem[z] != -1  )
								{									        

											DocumentNumber = InvertedItemsNumbers[QueryItemArray[z]][firstItem[z]].DocumentNumber;
											Score = InvertedItemsNumbers[QueryItemArray[z]][firstItem[z]].Score;
											TFScore = InvertedItemsNumbers[QueryItemArray[z]][firstItem[z]].TFScore;


											Length = fullPatentLength[DocumentNumber];


											if ( PostingAge[DocumentNumber] != Local_GlobalPostingAge )
											{
														PostingAge[DocumentNumber] = Local_GlobalPostingAge;
														PostingQueryTermsPresence[DocumentNumber] = 1;

														//IDF = (double)TermCollectionSum[QueryItemArray[z]];
														//IDF = (double) (  (double) IDF / (double) (CollectionModel) );							  

													//	PostingScore[DocumentNumber] = 100;
														PostingScore[DocumentNumber] = Score;

														PostingTFScore[DocumentNumber] = (double)( (double)TFScore/(double)Length );

														TermPresence[DocumentNumber][z] = Local_GlobalPostingAge;

											}
											else
											{
														PostingQueryTermsPresence[DocumentNumber]++;
														if ( RSType == 1 || RSType == 2 || RSType == 7 || RSType == 8 || RSType == 101 || RSType == 61 || RSType == 62 || RSType == 67 || RSType == 68 )
														{
																PostingScore[DocumentNumber] += Score;
																PostingTFScore[DocumentNumber] += (double)( (double)TFScore/(double)Length );
																TermPresence[DocumentNumber][z] = Local_GlobalPostingAge;
														}
														if ( RSType == 10 || RSType == 11 || RSType == 12 || RSType == 13 || RSType == 21  )
														{
																PostingScore[DocumentNumber] += Score;
																PostingTFScore[DocumentNumber] += (double)( (double)TFScore/(double)Length );
																TermPresence[DocumentNumber][z] = Local_GlobalPostingAge;
														}

														if ( RSType == 3 || RSType == 4 || RSType == 5 || RSType == 6 || RSType == 63 || RSType == 64 || RSType == 65 || RSType == 66 )
														{

																//IDF = (double)TermCollectionSum[QueryItemArray[z]];
																//IDF = (double) ( IDF / (double) (CollectionModel) );							  

																PostingScore[DocumentNumber] *= Score;
																PostingTFScore[DocumentNumber] += (double)( (double)TFScore/(double)Length );
																TermPresence[DocumentNumber][z] = Local_GlobalPostingAge;

														}


											}
														if ( PostingQueryTermsPresence[DocumentNumber] == QueryItemCount-1 )
														{

																if ( ComponentLabel[DocumentNumber] == 1 )
																		TotalFoundHighFindableDocuments++;
																if ( ComponentLabel[DocumentNumber] == 0 )
																		TotalFoundLowFindableDocuments++;


																FoundPatents[FoundPatentsCount] = DocumentNumber;
																FoundPatentsScore[FoundPatentsCount] = PostingScore[DocumentNumber];
																FoundPatentsTFScore[FoundPatentsCount] = PostingTFScore[DocumentNumber];
																//if ( MinimumScore > FoundPatentsScore[FoundPatentsCount] )
																//		MinimumScore = FoundPatentsScore[FoundPatentsCount];

																FoundPatentsCount++;
														}

								}

					}


					










					//NULLCount = 0;
					for ( z = 0; z < QueryItemCount-1; z++ )
					{
							if ( firstItem[z] != -1 )
								firstItem[z]++;

							if ( firstItem[z] == InvertedItemsNumbersCount[QueryItemArray[z]] )
							{
									NULLCount++;
									firstItem[z] = -1;

							}
					}
					if ( NULLCount == QueryItemCount-1 )
						break;


		}
		

/*

		for ( z = 0; z < TotalPatents; z ++ )
		{								
				
					DocumentNumber = z;
					if ( PostingAge[DocumentNumber] == Local_GlobalPostingAge && PostingQueryTermsPresence[DocumentNumber] > 0 )
					{
	
							
							SecondPart = 0;
							TF = 0;
							IDF = 0;
							SecondPart2 = 0;



							if ( RSType == 4  )
							{
									//PostingScore[DocumentNumber] = 1;
									for ( z1 = 0; z1 < QueryItemCount-1; z1++ )
									{
												if ( TermPresence[DocumentNumber][z1] != Local_GlobalPostingAge )
												{
														IDF = (double)TermCollectionSum[QueryItemArray[z1]];
														IDF = (double) (  (double) IDF / (double) (CollectionModel) );							  

														TF = 0;

														TF = (double)((double)(1-Parameter)*TF);
														SecondPart = (double)((double)Parameter*(double)IDF);

														PostingScore[DocumentNumber] *= (SecondPart+TF);
														
												}

									}
							}

							if ( RSType == 5 )
							{
									for ( z1 = 0; z1 < QueryItemCount-1; z1++ )
									{
												if ( TermPresence[DocumentNumber][z1] != Local_GlobalPostingAge )
												{
						

														IDF = (double)TermCollectionSum[QueryItemArray[z1]];
														IDF = (double) ( IDF / (double) (CollectionModel ) );							  

														
														
														
														TF = 0;
														SecondPart = (double)((double)fullPatentUniqueItems[DocumentNumber]*(double)Parameter);
														SecondPart = (double) ( (double)SecondPart/(double)fullPatentLength[DocumentNumber]);
														
														SecondPart = (double)((double)SecondPart*(double)IDF);
														TF = (double)((double)TF + (double)SecondPart);



														PostingScore[DocumentNumber] *= TF;
														
												}

									}
							}

							if ( RSType == 3 )
							{
									for ( z1 = 0; z1 < QueryItemCount-1; z1++ )
									{
												if ( TermPresence[DocumentNumber][z1] != Local_GlobalPostingAge )
												{
													

														TF = 0;								
														IDF = (double)TermCollectionSum[QueryItemArray[z1]];
														IDF = (double) ( IDF / (double) ( CollectionModel) );							  

										
														SecondPart = TF +  ( (double) ( (double) Parameter_Value * IDF ) );
														SecondPart2 = (double) ( fullPatentLength[DocumentNumber] + (double) Parameter_Value );
														SecondPart = SecondPart/SecondPart2;

														PostingScore[DocumentNumber] *= SecondPart;

														
												}

									}
							}

							if ( RSType == 6 )
							{
									Parameter2 = 2000;
									for ( z1 = 0; z1 < QueryItemCount-1; z1++ )
									{
												if ( TermPresence[DocumentNumber][z1] != Local_GlobalPostingAge )
												{
														IDF = (double)TermCollectionSum[QueryItemArray[z1]];
														IDF = (double) ( IDF / (double) (CollectionModel ) );							  


														TF = 0;								


														SecondPart2 = (double)( TF + (double)( (double)Parameter2 *(double)IDF));
														SecondPart2 = (double) ( SecondPart2/ (double)( (double) fullPatentLength[DocumentNumber] + (double)(Parameter2)) );
														SecondPart = (double)((double)(1-Parameter)*(SecondPart2)) +(Parameter * (double)IDF);							
														PostingScore[DocumentNumber] *= SecondPart;
														
												}

									}
							}



					}
		
					if ( PostingAge[DocumentNumber] == Local_GlobalPostingAge )
					{
							if ( PostingQueryTermsPresence[DocumentNumber] > 0 )
							{									
									FoundPatents[FoundPatentsCount] = DocumentNumber;
									FoundPatentsScore[FoundPatentsCount] = PostingScore[DocumentNumber];
									FoundPatentsTFScore[FoundPatentsCount] = PostingTFScore[DocumentNumber];
									FoundPatentsCount++;
							}
					}

					PostingScore[DocumentNumber] = 0;
					PostingQueryTermsPresence[DocumentNumber] = 0;
					PostingAge[DocumentNumber] = -1;





		}
*/


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


			


}


int BitSimilarityValue(int S, int D)
{
	unsigned char *currentIndex;
	int z = 0;
	int Pos = 0;
	int Number = 0;
	int PatentNumber = 0;
	int BytePos = 0;
	int TotalBits;
	Pos = D/32;
	Number = D - (Pos*32);
	BytePos = 3-(Number/8);

//	printf("\n%d....%d...%d",Pos,Number,BytePos);
				
			
	
			currentIndex = (unsigned char *) & DocumentSimilarity[S][Pos]  ;
			for ( z = 0; z < BytePos; z++ )
				currentIndex++;

			TotalBits = RowTable[*currentIndex][0];
			for ( z = 1; z <= TotalBits; z++ )
			{
					    PatentNumber = Pos*32;
						PatentNumber += ((Number/8)*8);
						PatentNumber += RowTable[*currentIndex][z];
					//	printf("\n Patent Number %d",PatentNumber);
						if ( PatentNumber == D )
							return 1;
			}
			return 0;


}
int NumberSimilarTermsImprove(int SourcePatent, int PatentNumber)
{
	int rValue = 0;
	int RowNumber = 0;
	int BitNumber = 0;

				if ( SourcePatent == PatentNumber )
					return 1;

				//BitSimilarityValue(int S, int D)
//				printf("\n%d....",BitSimilarityValue(0,21431));
//				exit(1);

				if ( BitSimilarityValue(SourcePatent,PatentNumber) == 1 )
				{
						PatentNumber = Projection_FoundPatentMappingNumber [ PatentNumber ];


						RowNumber = PatentNumber/32;
						BitNumber = PatentNumber - (RowNumber*32);						
						ProjectionRows[RowNumber] = ProjectionRows[RowNumber] | BitTable[(BitNumber%32)];


						rValue = 1;
				}
				else
						rValue = 0;


				return rValue;

}

void CalcualteSingleTermFrequency(int Version) 
{
	int z = 0, i = 0,z1;
	int Term = 0;
	long RowCount = 0;
	int MakeTransactionCount;




	Projection_32RowCount = (FoundPatentsCount/32)+4;


	RowCount = 0;
	MakeTransactionCount = 0;
    for ( z = 0; z < FoundPatentsCount; z++)
    {
					    

						for  ( i = 0; i < DocTermsArray_TotalTerms[FoundPatents[z]]; i++ )
						{						
									Term = DocTermsArray_TermNumber[FoundPatents[z]][i];

									if ( m_New32Projection_Clean[Term] != Version )
									{
											for ( z1 = 0; z1 < Projection_32RowCount; z1++ )
													m_New32Projection[Term][z1] = 0;

											TermSingleFreqInAllDocuments[Term] = 0;
									}



									TermSingleFreqInAllDocuments[Term]++;
									m_New32Projection[Term][RowCount] = m_New32Projection[Term][RowCount] | BitTable[(MakeTransactionCount%32)];

									m_New32Projection_Clean[Term] = Version;

								//	if ( i > 200 )
								//		break;

						}

						MakeTransactionCount++;
						if ( MakeTransactionCount == 32 )
						{
									MakeTransactionCount = 0;
									RowCount++;
						}


						Projection_FoundPatentMappingNumber[ FoundPatents[z] ] = z;


						if ( z > 10 ) 
							break;

	}





}

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

						dTempNumber = PatentRMScore[i];
						PatentRMScore[i] = PatentRMScore[j];
						PatentRMScore[j] = dTempNumber;

			
						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__performance (lo,j);
		if ( i<hi) quicksort__performance (i,hi);
	
}

int CalculateClusterFrequentItemFrequency(int Item)
{
		unsigned int *Address1;
		unsigned char *currentIndex;
		unsigned int *Memory;

		int Support = 0 , j = 0;
		Address1 = m_New32Projection[Item]; 
		for ( j = 0; j < Projection_32RowCount; j++ )
		{
				Memory = Address1[j] & ProjectionRows[j];
				if ( Memory > 0 )
				{
						currentIndex = (unsigned char *) & Memory ;
						Support += CountTable[*currentIndex];
						currentIndex++;
						Support += CountTable[*currentIndex];
						currentIndex++;
						Support += CountTable[*currentIndex];
						currentIndex++;
						Support += CountTable[*currentIndex];
				}
		}
		return Support;

}

void InsertBestClusterNeighbor(int SourcePatent, int PatentNumber)
{
				if ( SourcePatent == PatentNumber )
					return;

				if ( BitSimilarityValue(SourcePatent,PatentNumber) == 1  )
					QueryClusterN[PatentNumber]++;
}

void InsertBestTerms(long Term, double TermWeight, double DistributionR, double DistributionW)
{
		 long j = 0;
		 long Found = 0;
                                 

					if ( TermWeight > Minimum  )
                    {                
                                if ( TopExpandedTermsAdded == TopExpandedTermsConsider )
                                {
										ExpandedTerms[MinimumIndex]	= Term;
										ExpandedTermsWeight[MinimumIndex] = TermWeight;
                                }
                                if ( TopExpandedTermsAdded  < TopExpandedTermsConsider)
                                {
										ExpandedTerms[TopExpandedTermsAdded] = Term;
										ExpandedTermsWeight[TopExpandedTermsAdded] = TermWeight;                                        
                                        TopExpandedTermsAdded++;
                                }
                                if ( TopExpandedTermsAdded  == TopExpandedTermsConsider)
                                {
                                        Minimum = 9999;
                                        for ( j = 0; j < TopExpandedTermsAdded; j++)
                                        {
                                                if ( ExpandedTermsWeight[j] < Minimum )
                                                {
                                                        Minimum = ExpandedTermsWeight[j];
                                                        MinimumIndex = j;
                                                }
                                        }
                                }
                    }
}


void CalculteQueryItemsFrequency(long PatentNumber, long Terms,long SectionNumber)
{
	long loop = 0, i = 0;
	long LastOccurence = -24;



				
						//for ( loop = 0; loop < Terms; loop ++ )
						//		ClaimQueryItemFrequency[loop] = 0;

						QE_FlagCounter++;
						if ( QE_FlagCounter > 600000 )
							QE_FlagCounter = 0;
						

							 

						for  ( i = 0; i < DocTermsArray_TotalTerms[PatentNumber]; i++ )
						{
								FAST_TF_QE [ DocTermsArray_TermNumber[PatentNumber][i] ] = DocTermsArray_TermFrequency[PatentNumber][i];
								FAST_TF_QE_Flag[ DocTermsArray_TermNumber[PatentNumber][i] ] = QE_FlagCounter;
						}

						for ( loop = 0; loop < Terms; loop ++ )
						{

								ClaimQueryItemFrequency[loop] = 0;

															
								if ( FAST_TF_QE_Flag[ QueryItemArray[loop] ] == QE_FlagCounter )
								{
										ClaimQueryItemFrequency[loop] =  FAST_TF_QE [ QueryItemArray[loop] ];
								}
							
								
						}
							

					


					


}


void ExectureBM25ExpandedVersion(long PatentNumber , long Terms ,long ScoreInsertion)
{

	
        
	

           double FinalScore = 1;
		   double TF = 0;
		   double IDF = 0;
		   long FFreq = 0;
		   double WeightedLength = 0;
		   double SumDistributionRelevant = 0;
		   double SumDistributionAll = 0;

		   double SumDistributionRelevantOther = 0;
		   double SumDistributionAllOther = 0;
		   double GiniSum = 0;
		   double GiniSumWhole = 0;
		   double FSum = 0;

		   double AverageDifferenceDistribution = 0;
		   long j = 0;
		   long Count = 0;
		   double FreqSum = 0;
		   
		   double SecondPart = 0;

 		   double Parameter  = 0.7;
		   double Parameter2  = 2000;
		   double SecondPart2 = 0;

		   double k = 2.0;
		   double b = .75;
		   double TermFreq, IDFUpper, IDFLower;

		   double MainUpper = 0;
		   double MainLower = 0;
		   double Main = 0;

		   FinalScore = 1;
           for ( j = 0; j < Terms; j++ )
           {
			   
							TF = 0;				
							FFreq = ClaimQueryItemFrequency[j];

							

						





							/*
							IDF = (double)TermCollectionSum[QueryItemArray[j]];
							IDF = (double) ( IDF / (double) (Exp_CM));
							
							TF = FFreq;

							SecondPart2 = (double)( TF + (double)( (double)Parameter2 *(double)IDF));
							SecondPart2 = (double) ( SecondPart2/ (double)( (double) fullPatentLength[PatentNumber] + (double)(Parameter2)) );
							SecondPart = (double)((double)(1-Parameter)*(SecondPart2)) +(Parameter * (double)IDF);
							
							FinalScore *= SecondPart;*/




								// jm


							
							
						
							
							IDF = (double)TermCollectionSum[QueryItemArray[j]];
							IDF = (double) ( IDF / (double) (Exp_CM) );							  


							TF = FFreq;
							TF = (double) ( (double)TF/(double)fullPatentLength[PatentNumber]);

							TF = TF*ExpandedTermsWeight[j];

							TF = (double)((double)(1-Parameter)*TF);
							SecondPart = Parameter*IDF;

							FinalScore *= ( SecondPart + TF ) ;







						//	BM25

							
/*
							TF += (double)((double)FFreq) ;
							if ( FFreq > 0 )
								Count++;		

							WeightedLength = 0;
		                    WeightedLength += (double)((double)fullPatentLength[PatentNumber] );

					
							IDF = (double)IDFValue[QueryItemArray[j]];
							  
				
							IDFUpper = (double)(((double)(TotalPatents))- (double)IDF + (double)0.5);
							IDFLower = (double)IDF + (double)0.5;
							IDF = (double) (IDFUpper/IDFLower);
							IDF = (double)((double)log(IDF)/(double)log(2));

                    
							MainUpper = (double)(TF*(k+1));
							MainLower = (double)(TF + k*(1-b+(b*(WeightedLength/AveragePatentLength))));
							Main = MainUpper/MainLower;
							FinalScore  += (IDF*Main);*/


							

							
				



           }



		   


		  // if ( FinalScore == 0 )
		 //  printf("\n%d....",Terms);
	

		   PatentRMScore[ScoreInsertion] = FinalScore;



}


void ExpandQueryKLDMethod(int Version)
{
	long z = 0, i = 0, j = 0;
	double FirstSide = 0;
	double SecondSide = 0;
	double ThirdSide = 0;
	double Upper = 0;
	double Lower = 0;
	double KLD = 0;
	long TermNumber;
	int RelevantDocumentsConsider = GoodDocuments;
	TopExpandedTermsConsider = 30;
	TopExpandedTermsAdded = 0;
	MinimumIndex = 0;
	Minimum  = -99999;



	AllTermsOccurance = 0;

    for ( z = 0; z < FoundPatentsCount; z++)
    {
						for  ( i = 0; i < DocTermsArray_TotalTerms[FoundPatents[z]]; i++ )
						{

									TermNumber = DocTermsArray_TermNumber[FoundPatents[z]][i];
									if ( ExpansionTerm_Clean[TermNumber] != Version )
									{											
												TermFreqInRelevantDocuments[TermNumber] = 0;
												LastPatentConsidered[TermNumber] = -1;
												TermFreqInAllDocuments[TermNumber] = 0;
												TermSingleFreqInAllDocuments[TermNumber] = 0;								
									}
									ExpansionTerm_Clean[TermNumber] = Version;


									if ( z < RelevantDocumentsConsider )
										TermFreqInRelevantDocuments[ DocTermsArray_TermNumber[FoundPatents[z]][i] ]++;

									TermSingleFreqInAllDocuments[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] ++;


									TermFreqInAllDocuments[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] += DocTermsArray_TermFrequency[FoundPatents[z]][i];
									AllTermsOccurance += DocTermsArray_TermFrequency[FoundPatents[z]][i];

						}
					//	if ( z > 100 )
					//		break;


	}


	for ( i = 0; i < TotalItems; i++ )
	{
				if ( TermFreqInRelevantDocuments[i] > 0 && ExpansionTerm_Clean[i] == Version )
				{		
						FirstSide = (double)((double)TermFreqInRelevantDocuments[i]/(double)RelevantDocumentsConsider);
						Upper = (double)((double)AllTermsOccurance+(double)0.01*(double)TotalItems);
						Lower = (double)((double)TermFreqInRelevantDocuments[i] + (double)0.01);
						ThirdSide = Upper/Lower;

						SecondSide = FirstSide * ThirdSide;
	                    SecondSide = (double)((double)log(SecondSide)/(double)log(2));
						KLD = FirstSide * SecondSide;


						InsertBestTerms(i, KLD,0,0);
				}
	}



	QueryItemCount--;
	for ( i = 0; i < TopExpandedTermsAdded; i ++ )
	{

			/*
			FirstSide = (double)((double)1/(double)3);
			Upper = (double)((double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5); 
			Lower = (double)((double)TermSingleFreqInAllDocuments[ExpandedTerms[i]] - (double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5 );
			SecondSide = Upper/Lower;

			Upper = (double) ( (double)RelevantDocumentsConsider - (double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5 );
			Lower = (double) ( (double)TotalPatents  - (double)TermSingleFreqInAllDocuments[ExpandedTerms[i]] - (double)RelevantDocumentsConsider + (double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5 );
			ThirdSide = Upper/Lower;

			SecondSide = SecondSide/ThirdSide;
			SecondSide = (double)((double)log(SecondSide)/(double)log(2));

			ExpandedTermsWeight[i] = FirstSide * SecondSide;

			//printf("\nTerm %d ,  Weight = %f",ExpandedTerms[i],ExpandedTermsWeight[i] );*/

			QueryItemArray[QueryItemCount] = ExpandedTerms[i];
			QueryItemCount++;
	}



    for ( z = 0; z < FoundPatentsCount; z++)
    {
				CalculteQueryItemsFrequency(FoundPatents[z],QueryItemCount,1);
				ExectureBM25ExpandedVersion(FoundPatents[z],QueryItemCount,z);
	}
	//exit(1);
}

void ExpandQueryLanguageModelMethod(int Version)
{
	long z = 0, i = 0, j = 0;
	double FirstSide = 0;
	double SecondSide = 0;
	double ThirdSide = 0;
	double Upper = 0;
	double Lower = 0;
	double Score = 0;
	double Parameter = 0.7;
	long DocNum = 0;
	long TFF;
	long RelevantDocumentsConsider = GoodDocuments;
	long TermNumber;
	TopExpandedTermsConsider = 30;
	TopExpandedTermsAdded = 0;
	MinimumIndex = 0;
	Minimum  = -99999;



/*	for ( i = 0; i < TotalItems; i++ )
	{
				TermFreqInRelevantDocuments[i] = -1;
				LastPatentConsidered[i] = 0;
				TermFreqInAllDocuments[i] = 0;
				TermSingleFreqInAllDocuments[i] = 0;
				TermFreqInWholeCollection[i] = 0;

	}*/
	AllTermsOccurance = 0;

	
    for ( z = 0; z < FoundPatentsCount; z++)
    {

						for  ( i = 0; i < DocTermsArray_TotalTerms[FoundPatents[z]]; i++ )
						{
									TermNumber = DocTermsArray_TermNumber[FoundPatents[z]][i];
									if ( DocTermsArray_TermFrequency[FoundPatents[z]][i] > 0 )
									{
											if ( ExpansionTerm_Clean[TermNumber] != Version )
											{											
													TermFreqInRelevantDocuments[TermNumber] = -1;
													LastPatentConsidered[TermNumber] = 0;
													TermFreqInAllDocuments[TermNumber] = 0;
													TermSingleFreqInAllDocuments[TermNumber] = 0;
													TermFreqInWholeCollection[TermNumber] = 0;									
											}
											ExpansionTerm_Clean[TermNumber] = Version;

											TermFreqInAllDocuments[ TermNumber ] += DocTermsArray_TermFrequency[FoundPatents[z]][i];
											AllTermsOccurance += DocTermsArray_TermFrequency[FoundPatents[z]][i];

									}
						}
				
	}




    for ( z = 0; z < GoodDocuments; z++)
    {
			
				

						for  ( i = 0; i < DocTermsArray_TotalTerms[FoundPatents[z]]; i++ )
						{
									TermFreqInWholeCollection[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] = DocTermsArray_TermFrequency[FoundPatents[z]][i]; //old
						}

				//TermFreqInRelevantDocuments[i] = 0;
				//LastPatentConsidered[i] = -1;
				//TermFreqInAllDocuments[i] = 0;
				//TermSingleFreqInAllDocuments[i] = 0;

						for  ( i = 0; i < DocTermsArray_TotalTerms[FoundPatents[z]]; i++ )
						{
								

									if ( DocTermsArray_TermNumber[FoundPatents[z]][i] > 0 )
									{
											if ( TermFreqInWholeCollection[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] > 0  )
													FirstSide = Parameter * (double)( (double) TermFreqInWholeCollection[ DocTermsArray_TermNumber[FoundPatents[z]][i]  ]/(double)fullPatentLength[ FoundPatents[z] ] );
											else
													FirstSide = 0;

											SecondSide  = (double)((double)1-Parameter) * (double)( (double)TermFreqInAllDocuments[ DocTermsArray_TermNumber[FoundPatents[z]][i] ]/(double)AllTermsOccurance );


											FirstSide = FirstSide + SecondSide;


											if ( TermFreqInWholeCollection[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] > 0  )
													TFF = TermFreqInWholeCollection[ DocTermsArray_TermNumber[FoundPatents[z]][i] ];
											else
													TFF = 0;

											SecondSide = (double)((Parameter * (double)(((double) TFF/(double)fullPatentLength[ FoundPatents[z] ] ) *  (double)( (double)AllTermsOccurance/(double)TermFreqInAllDocuments[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] )) ) + (double)(1 - Parameter));
										    SecondSide = (double)((double)log(SecondSide)/(double)log(2));

											

											LastPatentConsidered[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] += FirstSide+SecondSide;


										//	TermSingleFreqInAllDocuments[ DocTermsArray_TermNumber[FoundPatents[z]][i] ] = Version;
									}
								
						}

				
					//	if ( z == 40 )
					//		break;
	}








	for ( i = 0; i < TotalItems; i++ )
	{
				if ( LastPatentConsidered[i] > 0 && ExpansionTerm_Clean[i] == Version  )
				{						
						InsertBestTerms(i, LastPatentConsidered[i],0,0 );
				}
	}



	QueryItemCount--;

	for ( i = 0; i < TopExpandedTermsAdded; i ++ )
	{

		
		/*	FirstSide = (double)((double)1/(double)3);
			Upper = (double)((double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5); 
			Lower = (double)((double)TermFreqInWholeCollection[ExpandedTerms[i]] - (double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5 );
			SecondSide = Upper/Lower;

			Upper = (double) ( (double)RelevantDocumentsConsider - (double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5 );
			Lower = (double) ( (double)TotalPatents - (double)TermFreqInWholeCollection[ExpandedTerms[i]] - (double)RelevantDocumentsConsider + (double)TermFreqInRelevantDocuments[ExpandedTerms[i]] + (double)0.5 );
			ThirdSide = Upper/Lower;

			SecondSide = SecondSide/ThirdSide;
			SecondSide = (double)((double)log(SecondSide)/(double)log(2));	
			ExpandedTermsWeight[QueryItemCount] = FirstSide * SecondSide;*/



			QueryItemArray[QueryItemCount] = ExpandedTerms[i];
			QueryItemCount++;
	}

//	printf("\n%d...%d",GoodDocuments,QueryItemCount);


    for ( z = 0; z < FoundPatentsCount; z++)
    {
				CalculteQueryItemsFrequency(FoundPatents[z],QueryItemCount,1);
				ExectureBM25ExpandedVersion(FoundPatents[z],QueryItemCount , z );
	}

	
    if ( QueryItemCount > 6 && Ex == 1 )
			ExpansionCountHigh++;





}



void CalculateGINI_Short()
{






	double UpperSum = 0;
	double LowerSum = 0;
	double GINI = 0;


	int TP = 0;
	int i = 0;
	int j = 0;


	


	TP = 0;

	for ( i = 0; i < TotalPatents; i++ )
	{
				if ( DocTotalQueries_High[i] > 5 )
				{
						NorFindability[TP] = (double) ( (double) DocFindability_High[1][i] / (double) DocTotalQueries_High[i] );					
						TP++;
				}	
				//if ( i > 35200 )
				//	break;
	}
	
	C_quicksortFirst(0,TP-1);	
	UpperSum = 0;
	LowerSum = 0;

					for ( j = 0; j < TP; j++ )
					{
							
					
									UpperSum += ((2*(j+1)-(TP)-1) * NorFindability[j] );
									LowerSum += NorFindability[j];					
									
					}

					LowerSum = LowerSum * ((TP)-1);
					GINI = UpperSum / LowerSum;

				
	
				

					printf("\nGini-Coefficient with Top is = %f...%d....%d\n",GINI,TP,p_running);
}
int ChildTerminationStatus(int ExpectedDocs)
{

	FILE *fptr;	
	char m_FindabilityFiles[300];
	char m_FileName[300];
	int i = 0;
	int CTS = 0;
	int Find5;
	int Find10;
	int Find15;
	int Find20;
	int Find25;	
	int Total;




	char RM_Name[300];
	char Query_Type[300];


	if ( PPS == 1 )
		strcpy(Query_Type,"high");
	if ( PPS == 2 )
		strcpy(Query_Type,"medium");
	if ( PPS == 3 )
		strcpy(Query_Type,"low");
	if ( PPS == 4 )
		strcpy(Query_Type,"full");
	if ( PPS == 5 )
		strcpy(Query_Type,"extra");
	if ( PPS == 6 )
		strcpy(Query_Type,"partition");
	if ( PPS == 11 )
		strcpy(Query_Type,"high");
	if ( PPS == 12 )
		strcpy(Query_Type,"medium");
	if ( PPS == 13 )
		strcpy(Query_Type,"low");



	if ( RSType == 1 )
		strcpy(RM_Name,"TFIDF");
	if ( RSType == 2 )
		strcpy(RM_Name,"BM25");
	if ( RSType == 3 )
		strcpy(RM_Name,"DirS");
	if ( RSType == 4 )
		strcpy(RM_Name,"JM");
	if ( RSType == 5 )
		strcpy(RM_Name,"AbsDis");
	if ( RSType == 6 )
		strcpy(RM_Name,"TwoStage");
	if ( RSType == 7 )
		strcpy(RM_Name,"UnNormTFIDF");
	if ( RSType == 8 )
		strcpy(RM_Name,"SMART");

	if ( RSType == 10 )
		strcpy(RM_Name,"QE_FT");
	if ( RSType == 11 )
		strcpy(RM_Name,"QE_TS");
	if ( RSType == 12 )
		strcpy(RM_Name,"QE_KLD");
	if ( RSType == 13 )
		strcpy(RM_Name,"QE_Lee_Cluster");
	if ( RSType == 21 )
		strcpy(RM_Name,"PA_TS");


	if ( RSType == 61 )
		strcpy(RM_Name,"Part_TFIDF");
	if ( RSType == 62 )
		strcpy(RM_Name,"Part_BM25");
	if ( RSType == 63 )
		strcpy(RM_Name,"Part_DirS");
	if ( RSType == 64 )
		strcpy(RM_Name,"Part_JM");
	if ( RSType == 65 )
		strcpy(RM_Name,"Part_AbsDis");
	if ( RSType == 66 )
		strcpy(RM_Name,"Part_TwoStage");
	if ( RSType == 67 )
		strcpy(RM_Name,"Part_UnNormTFIDF");
	if ( RSType == 68 )
		strcpy(RM_Name,"Part_SMART");

	for ( i = 0; i < ParallelMaxSize; i ++ )
	{
					sprintf(m_FileName,"%sWorkingFolder//%s_%s_%d_%lf.txt",sourcePath,RM_Name,Query_Type,i,Parameter_Value);
					fptr = fopen(m_FileName, "r");
				//	printf("\n%s..%d...%d",m_FileName,RM_Name,Query_Type);
					TotalPatents = 0;
					if ( fptr == NULL )
					{
					}
					else
					{
							while(1)
							{
									if ( fscanf(fptr,"%d	%d	%d	%d	%d	%d",&Find5,&Find10,&Find15,&Find20,&Find25,&Total) == EOF )
											break;

									TotalPatents++;

							}

					
							

							if ( TotalPatents == ExpectedDocs )
								CTS++;


							fclose(fptr);			
					}

	}
//	printf("\n%d...",CTS);

	return CTS;

}
void RemoveOldFiles()
{

	FILE *fptr;	
	char m_FindabilityFiles[300];
	char m_FileName[300];
	int i = 0;
	int CTS = 0;



	char RM_Name[300];
	char Query_Type[300];


	if ( PPS == 1 )
		strcpy(Query_Type,"high");
	if ( PPS == 2 )
		strcpy(Query_Type,"medium");
	if ( PPS == 3 )
		strcpy(Query_Type,"low");
	if ( PPS == 4 )
		strcpy(Query_Type,"full");
	if ( PPS == 5 )
		strcpy(Query_Type,"extra");
	if ( PPS == 6 )
		strcpy(Query_Type,"partition");
	if ( PPS == 11 )
		strcpy(Query_Type,"high");
	if ( PPS == 12 )
		strcpy(Query_Type,"medium");
	if ( PPS == 13 )
		strcpy(Query_Type,"low");



	if ( RSType == 1 )
		strcpy(RM_Name,"TFIDF");
	if ( RSType == 2 )
		strcpy(RM_Name,"BM25");
	if ( RSType == 3 )
		strcpy(RM_Name,"DirS");
	if ( RSType == 4 )
		strcpy(RM_Name,"JM");
	if ( RSType == 5 )
		strcpy(RM_Name,"AbsDis");
	if ( RSType == 6 )
		strcpy(RM_Name,"TwoStage");
	if ( RSType == 7 )
		strcpy(RM_Name,"UnNormTFIDF");
	if ( RSType == 8 )
		strcpy(RM_Name,"SMART");


	if ( RSType == 10 )
		strcpy(RM_Name,"QE_FT");
	if ( RSType == 11 )
		strcpy(RM_Name,"QE_TS");
	if ( RSType == 12 )
		strcpy(RM_Name,"QE_KLD");
	if ( RSType == 13 )
		strcpy(RM_Name,"QE_Lee_Cluster");
	if ( RSType == 21 )
		strcpy(RM_Name,"PA_TS");


	if ( RSType == 61 )
		strcpy(RM_Name,"Part_TFIDF");
	if ( RSType == 62 )
		strcpy(RM_Name,"Part_BM25");
	if ( RSType == 63 )
		strcpy(RM_Name,"Part_DirS");
	if ( RSType == 64 )
		strcpy(RM_Name,"Part_JM");
	if ( RSType == 65 )
		strcpy(RM_Name,"Part_AbsDis");
	if ( RSType == 66 )
		strcpy(RM_Name,"Part_TwoStage");
	if ( RSType == 67 )
		strcpy(RM_Name,"Part_UnNormTFIDF");
	if ( RSType == 68 )
		strcpy(RM_Name,"Part_SMART");

	for ( i = 0; i < ParallelMaxSize; i ++ )
	{
					sprintf(m_FileName,"%sWorkingFolder//%s_%s_%d_%lf.txt",sourcePath,RM_Name,Query_Type,i,Parameter_Value);
					fptr = fopen(m_FileName, "r");
					if ( fptr == NULL )					
					{
					}
					else
					{
						fclose(fptr);			
						remove(m_FileName);
					}

	}

	

}


void MergeResultingFiles()
{

	FILE *fptr;	
	char m_FindabilityFiles[300];
	char m_FileName[300];
	int i = 0;
	int Find5;
	int Find10;
	int Find15;
	int Find20;
	int Find25;
	int Total;
	int j = 0;


	char RM_Name[300];
	char Query_Type[300];


	if ( PPS == 1 )
		strcpy(Query_Type,"high");
	if ( PPS == 2 )
		strcpy(Query_Type,"medium");
	if ( PPS == 3 )
		strcpy(Query_Type,"low");
	if ( PPS == 4 )
		strcpy(Query_Type,"full");
	if ( PPS == 5 )
		strcpy(Query_Type,"extra");
	if ( PPS == 6 )
		strcpy(Query_Type,"partition");
	if ( PPS == 11 )
		strcpy(Query_Type,"high");
	if ( PPS == 12 )
		strcpy(Query_Type,"medium");
	if ( PPS == 13 )
		strcpy(Query_Type,"low");



	if ( RSType == 1 )
		strcpy(RM_Name,"TFIDF");
	if ( RSType == 2 )
		strcpy(RM_Name,"BM25");
	if ( RSType == 3 )
		strcpy(RM_Name,"DirS");
	if ( RSType == 4 )
		strcpy(RM_Name,"JM");
	if ( RSType == 5 )
		strcpy(RM_Name,"AbsDis");
	if ( RSType == 6 )
		strcpy(RM_Name,"TwoStage");
	if ( RSType == 7 )
		strcpy(RM_Name,"UnNormTFIDF");
	if ( RSType == 8 )
		strcpy(RM_Name,"SMART");


	if ( RSType == 10 )
		strcpy(RM_Name,"QE_FT");
	if ( RSType == 11 )
		strcpy(RM_Name,"QE_TS");
	if ( RSType == 12 )
		strcpy(RM_Name,"QE_KLD");
	if ( RSType == 13 )
		strcpy(RM_Name,"QE_Lee_Cluster");
	if ( RSType == 21 )
		strcpy(RM_Name,"PA_TS");



	if ( RSType == 61 )
		strcpy(RM_Name,"Part_TFIDF");
	if ( RSType == 62 )
		strcpy(RM_Name,"Part_BM25");
	if ( RSType == 63 )
		strcpy(RM_Name,"Part_DirS");
	if ( RSType == 64 )
		strcpy(RM_Name,"Part_JM");
	if ( RSType == 65 )
		strcpy(RM_Name,"Part_AbsDis");
	if ( RSType == 66 )
		strcpy(RM_Name,"Part_TwoStage");
	if ( RSType == 67 )
		strcpy(RM_Name,"Part_UnNormTFIDF");
	if ( RSType == 68 )
		strcpy(RM_Name,"Part_SMART");

	TotalPatents = 0;

	for ( i = 0; i < ParallelMaxSize; i ++ )
	{
					sprintf(m_FileName,"%sWorkingFolder//%s_%s_%d_%lf.txt",sourcePath,RM_Name,Query_Type,i,Parameter_Value);				

				//	printf("\n%s..............\n",m_FileName);
					fptr = fopen(m_FileName, "r");
					while(1)
					{
						//	printf("\n%d...",TotalPatents);
							if ( fscanf(fptr,"%d	%d	%d	%d	%d	%d",&Find5,&Find10,&Find15,&Find20,&Find25,&Total) == EOF )
									break;

							
					
							TotalPatents++;


					}
				//	if ( fptr == NULL )
				//		printf("\n\n hello \n\n");
					fclose(fptr);
					break;

	}

//	printf("\n%d....\n\n",TotalPatents);
//	exit(1);


	DocFindability_High = (long *) malloc ( 7  * sizeof(long));
	DocTotalQueries_High = (long *) malloc ( (TotalPatents)  * sizeof(long));
	NorFindability = (double *)malloc((TotalPatents)*sizeof(double));

	for ( j = 0; j < 7; j ++ )
	{
					DocFindability_High[j] = (long *) malloc ( TotalPatents  * sizeof(long));
					for ( i = 0; i < TotalPatents; i++ )
							DocFindability_High[j][i] = 0;
	}




	for ( i = 0; i < ParallelMaxSize; i ++ )
	{
					sprintf(m_FileName,"%sWorkingFolder//%s_%s_%d_%lf.txt",sourcePath,RM_Name,Query_Type,i,Parameter_Value);				

				//	printf("\n\n%s...\n\n",m_FileName);
					fptr = fopen(m_FileName, "r");
					TotalPatents = 0;
					while(1)
					{
							if ( fscanf(fptr,"%d	%d	%d	%d	%d	%d",&Find5,&Find10,&Find15,&Find20,&Find25,&Total) == EOF )
									break;

							

						
					//fprintf(findability_log_High,"%d	%d	%d	%d	%d	%d\n",DocFindability_High[0][i],DocFindability_High[1][i],DocFindability_High[2][i],DocFindability_High[3][i],DocFindability_High[4][i],DocTotalQueries_High[i]);


							if ( i == 0 )
							{
									DocFindability_High[0][TotalPatents] = Find5;
									DocFindability_High[1][TotalPatents] = Find10;
									DocFindability_High[2][TotalPatents] = Find15;
									DocFindability_High[3][TotalPatents] = Find20;
									DocFindability_High[4][TotalPatents] = Find25;
									DocTotalQueries_High[TotalPatents] = Total;
							}
							else
							{
									DocFindability_High[0][TotalPatents] += Find5;
									DocFindability_High[1][TotalPatents] += Find10;
									DocFindability_High[2][TotalPatents] += Find15;
									DocFindability_High[3][TotalPatents] += Find20;
									DocFindability_High[4][TotalPatents] += Find25;
									DocTotalQueries_High[TotalPatents] += Total;

							}


							TotalPatents++;


					}


				//	printf("\n\n%s...%d\n\n",m_FileName,TotalPatents);
					fclose(fptr);

					remove(m_FileName);


	}



	if ( PPS == 5 )
	{
			sprintf(m_FileName,"%sWorkingFolder//%s_full_%lf.txt",sourcePath,RM_Name,Parameter_Value);
			fptr = fopen(m_FileName, "r");
			TotalPatents = 0;
			while(1)
			{
					if ( fscanf(fptr,"%d	%d	%d	%d	%d	%d",&Find5,&Find10,&Find15,&Find20,&Find25,&Total) == EOF )
							break;

					DocFindability_High[0][TotalPatents] += Find5;
					DocFindability_High[1][TotalPatents] += Find10;
					DocFindability_High[2][TotalPatents] += Find15;
					DocFindability_High[3][TotalPatents] += Find20;
					DocFindability_High[4][TotalPatents] += Find25;
					DocTotalQueries_High[TotalPatents] += Total;
					
					TotalPatents++;
					
			}
			fclose(fptr);
	}
	


	sprintf(m_FileName,"%sWorkingFolder//%s_%s_%lf.txt",sourcePath,RM_Name,Query_Type,Parameter_Value);
	fptr = fopen(m_FileName, "w");

	fprintf(fptr,"$Ist Column = Findability Scores with rank cutoff level = %d\n",cutoff_1);
	fprintf(fptr,"$2nd Column = Findability Scores with rank cutoff level = %d\n",cutoff_2);
	fprintf(fptr,"$3rd Column = Findability Scores with rank cutoff level = %d\n",cutoff_3);
	fprintf(fptr,"$4th Column = Findability Scores with rank cutoff level = %d\n",cutoff_4);
	fprintf(fptr,"$5th Column = Findability Scores with rank cutoff level = %d\n",cutoff_5);
	fprintf(fptr,"$6th Column = Total Queries\n");


	for ( i = 0; i < TotalPatents; i++ )
	{
					fprintf(fptr,"%d	%d	%d	%d	%d	%d\n",DocFindability_High[0][i],DocFindability_High[1][i],DocFindability_High[2][i],DocFindability_High[3][i],DocFindability_High[4][i],DocTotalQueries_High[i]);
	}

	fclose(fptr);



}


void J48Prediction(struct J48_Tree *J48CurrentNode,int TreeLevel)
{
	int LoopStatus = 0;
	int pl = 0;
	int nodeFeature;
	int conditionMatch = 0;

	if ( TreeLevel == 0 )
	{
			GoodTermVotes = 0;
			BadTermVotes = 0;
			MaximumGoodBadPrediction = 0;
			MaximumPredictionClass = 0;
			BestRuleMaximumSamples = 0;
	}

	RuleFire++;
	//unclassify_term_features
	while(1)
	{
			if ( LoopStatus == 0 )// left node
			{
					nodeFeature = atoi(J48CurrentNode->AttributeName);
					conditionMatch = 0;

					if ( J48CurrentNode->LeftNodeCondition == 1 ) // <
					{
							if ( unclassify_term_features[nodeFeature] < J48CurrentNode->LeftNodeValue )
								conditionMatch = 1;
					}
					else if ( J48CurrentNode->LeftNodeCondition == 2 ) // <=
					{
							if ( unclassify_term_features[nodeFeature] <= J48CurrentNode->LeftNodeValue )
								conditionMatch = 1;

					}
					else if ( J48CurrentNode->LeftNodeCondition == 3 ) // >
					{
							if ( unclassify_term_features[nodeFeature] > J48CurrentNode->LeftNodeValue )
								conditionMatch = 1;

					}
					else if ( J48CurrentNode->LeftNodeCondition == 4 ) // >=
					{
							if ( unclassify_term_features[nodeFeature] >= J48CurrentNode->LeftNodeValue )
								conditionMatch = 1;

					}
					else if ( J48CurrentNode->LeftNodeCondition == 5 ) // =
					{
							if ( unclassify_term_features[nodeFeature] == J48CurrentNode->LeftNodeValue )
								conditionMatch = 1;
					}


				//	printf("%s %lf %d %lf\n",J48CurrentNode->AttributeName,J48CurrentNode->LeftNodeValue,J48CurrentNode->LeftNodeRuleStatus,J48CurrentNode->LeftNodeRuleAccuracy);
					if ( J48CurrentNode->LeftNodeRuleStatus == 1 )
					{

							 if ( J48CurrentNode->LeftNodeRuleClass == 1  )
									GoodTermVotes++;
							 if ( J48CurrentNode->LeftNodeRuleClass == 0 )
									BadTermVotes++;
							


							 if ( J48CurrentNode->LeftNodeRuleAccuracy >= MaximumGoodBadPrediction )
							 {
									 if ( J48CurrentNode->LeftMaximumSamples > BestRuleMaximumSamples )
									 {
											 MaximumGoodBadPrediction = J48CurrentNode->LeftNodeRuleAccuracy;										 
											 MaximumPredictionClass = J48CurrentNode->LeftNodeRuleClass;
											 BestRuleMaximumSamples = J48CurrentNode->LeftMaximumSamples;

									 }
							 }


					}

					if ( J48CurrentNode->LeftNodeRuleStatus == 0 && conditionMatch == 1)
							J48Prediction(J48CurrentNode->LeftNode,TreeLevel+1);


					LoopStatus++;
			}
			if ( LoopStatus == 1 )
			{
					nodeFeature = atoi(J48CurrentNode->AttributeName);
					conditionMatch = 0;
					if ( J48CurrentNode->RightNodeCondition == 1 ) // <
					{
							if ( unclassify_term_features[nodeFeature] < J48CurrentNode->RightNodeValue )
								conditionMatch = 1;
					}
					else if ( J48CurrentNode->RightNodeCondition == 2 ) // <=
					{
							if ( unclassify_term_features[nodeFeature] <= J48CurrentNode->RightNodeValue )
								conditionMatch = 1;

					}
					else if ( J48CurrentNode->RightNodeCondition == 3 ) // >
					{
							if ( unclassify_term_features[nodeFeature] > J48CurrentNode->RightNodeValue )
								conditionMatch = 1;

					}
					else if ( J48CurrentNode->RightNodeCondition == 4 ) // >=
					{
							if ( unclassify_term_features[nodeFeature] >= J48CurrentNode->RightNodeValue )
								conditionMatch = 1;

					}
					else if ( J48CurrentNode->RightNodeCondition == 5 ) // =
					{
							if ( unclassify_term_features[nodeFeature] == J48CurrentNode->RightNodeValue )
								conditionMatch = 1;
					}


					//printf("%s %lf %d %lf\n",J48CurrentNode->AttributeName,J48CurrentNode->RightNodeValue,J48CurrentNode->RightNodeRuleStatus,J48CurrentNode->RightNodeRuleAccuracy);
					if ( J48CurrentNode->RightNodeRuleStatus == 1 )
					{

							 if ( J48CurrentNode->RightNodeRuleClass == 1 )
									 GoodTermVotes++;
							 if ( J48CurrentNode->RightNodeRuleClass == 0 )
									 BadTermVotes++;

							 if ( J48CurrentNode->RightNodeRuleAccuracy >= MaximumGoodBadPrediction )
							 {
 									 if ( J48CurrentNode->RightMaximumSamples > BestRuleMaximumSamples )
									 {
											 MaximumGoodBadPrediction = J48CurrentNode->RightNodeRuleAccuracy;										 
											 MaximumPredictionClass = J48CurrentNode->RightNodeRuleClass;
											 BestRuleMaximumSamples = J48CurrentNode->RightMaximumSamples;
									 }
							 }

							
					}

					if ( J48CurrentNode->RightNodeRuleStatus == 0 && conditionMatch == 1)
								J48Prediction(J48CurrentNode->RightNode,TreeLevel+1);

					LoopStatus++;
			}


			if (LoopStatus == J48CurrentNode->nodeCompleted )
				break;


	}


		
}

int J48ParseNode(struct J48_Tree *J48CurrentNode,FILE *fptr,int TreeLevel)
{

	char text[200];
	int informationStatus = 0; //0 for node // 1 for condition // 2 for value;
	int currentNodeTreeLevelCount = 0;
	char cleanText[200];
	char tempText[10];
	int i = 0;
	double TotalSamples = 0;
	double WrongClassify  = 0;
	int returnStatus = 0;
	int NotCheck = 0;


	

	while(1)
	{
			if ( fscanf(fptr,"%s",text) == EOF )
				break;


			if ( strcmp(text,"|") != 0 )
			{


							if ( currentNodeTreeLevelCount != TreeLevel )
							{
									return currentNodeTreeLevelCount;
							}


							if ( informationStatus == 0 )
							{
									strcpy(J48CurrentNode->AttributeName,text);
							}
							if ( informationStatus == 1 )
							{

									if (J48CurrentNode->nodeCompleted == 0 )
									{
											if ( strcmp(text,"<") == 0 )
												J48CurrentNode->LeftNodeCondition = 1;
											if ( strcmp(text,"<=") == 0 )
												J48CurrentNode->LeftNodeCondition = 2;
											if ( strcmp(text,">") == 0 )
												J48CurrentNode->LeftNodeCondition = 3;
											if ( strcmp(text,">=") == 0 )
												J48CurrentNode->LeftNodeCondition = 4;
											if ( strcmp(text,"=") == 0 )
												J48CurrentNode->LeftNodeCondition = 5;


											

									}
									else if (J48CurrentNode->nodeCompleted == 1 )
									{



											if ( strcmp(text,"<") == 0 )
												J48CurrentNode->RightNodeCondition = 1;
											if ( strcmp(text,"<=") == 0 )
												J48CurrentNode->RightNodeCondition = 2;
											if ( strcmp(text,">") == 0 )
												J48CurrentNode->RightNodeCondition = 3;
											if ( strcmp(text,">=") == 0 )
												J48CurrentNode->RightNodeCondition = 4;
											if ( strcmp(text,"=") == 0 )
												J48CurrentNode->RightNodeCondition = 5;
									}

							}
							if ( informationStatus == 2 )
							{
									if (J48CurrentNode->nodeCompleted == 0 )
									{
											if ( text[strlen(text)-1] == ':' ) // rule
											{
													currentNodeTreeLevelCount = 0;
													informationStatus = -1;

													J48CurrentNode->LeftNodeRuleStatus = 1;
													strcpy(cleanText,"");
													for ( i = 0; i < strlen(text)-1; i ++ )
													{
															sprintf(tempText,"%c",text[i]);
															strcat(cleanText,tempText);
													}
													J48CurrentNode->LeftNodeValue = atof(cleanText);



													fscanf(fptr,"%s",text); //Rule
													J48CurrentNode->LeftNodeRuleClass = atoi(text);
												//	printf("\n%d...",J48CurrentNode->LeftNodeRuleClass);
												//	exit(1);

													//int LeftNodeRuleClass;
													//int RightNodeRuleClass;

													fscanf(fptr,"%s",text); //Accuracy
													strcpy(cleanText,"");
													for ( i = 0; i < strlen(text); i ++ )
													{
															NotCheck = 0;
															if ( text[i] == '(' || text[i] == ')' )
															{
																	NotCheck = 1;
															}
															if ( text[i] == '/' )
															{
																	TotalSamples = atof(cleanText);
																	strcpy(cleanText,"");
																	NotCheck = 1;
																//	printf("\n..%lf....%lf...%s",TotalSamples,WrongClassify,cleanText);
																	
															}
															if ( NotCheck == 0 )
															{
																	sprintf(tempText,"%c",text[i]);
																	strcat(cleanText,tempText);
															}
													}
													WrongClassify = atof(cleanText);
													if ( TotalSamples != 0 )
													{
														J48CurrentNode->LeftNodeRuleAccuracy = (double) ( (double)(TotalSamples-WrongClassify)/(double)TotalSamples);
														J48CurrentNode->LeftMaximumSamples = TotalSamples;
													}
													else
													{
														J48CurrentNode->LeftNodeRuleAccuracy = (double) ( 1);
														J48CurrentNode->LeftMaximumSamples = WrongClassify;
													}
													//double LeftNodeRuleAccuracy;
													//double RightNodeRuleAccuracy;

													
													

											}
											else
											{
													

													J48CurrentNode->LeftNodeValue = atof(text);
													J48CurrentNode->LeftNodeRuleStatus = 0;

													J48CurrentNode->LeftNode = (struct J48_Tree *)malloc(sizeof(struct J48_Tree));
													J48CurrentNode->LeftNode->nodeCompleted = 0;
													returnStatus = J48ParseNode(J48CurrentNode->LeftNode,fptr,(TreeLevel+1));// 0 for parent

													if ( returnStatus != TreeLevel )
													{
														J48CurrentNode->nodeCompleted++;
														return returnStatus;
													}
													else
													{
														informationStatus = 0;
													//	printf("\n%d...%d.. %s...%d....%d.Yes",returnStatus,TreeLevel,J48CurrentNode->AttributeName,J48CurrentNode->nodeCompleted,currentNodeTreeLevelCount);

													}



											}
											J48CurrentNode->nodeCompleted++;

											

									}
									else if (J48CurrentNode->nodeCompleted == 1 && informationStatus == 2 )
									{
		



										
											if ( text[strlen(text)-1] == ':' ) // rule
											{


													currentNodeTreeLevelCount = 0;
													informationStatus = -1;

													J48CurrentNode->RightNodeRuleStatus = 1;
													strcpy(cleanText,"");
													for ( i = 0; i < strlen(text)-1; i ++ )
													{
															sprintf(tempText,"%c",text[i]);
															strcat(cleanText,tempText);
													}
													J48CurrentNode->RightNodeValue = atof(cleanText);




													fscanf(fptr,"%s",text); //Rule
													J48CurrentNode->RightNodeRuleClass = atoi(text);
													//int LeftNodeRuleClass;
													//int RightNodeRuleClass;

													fscanf(fptr,"%s",text); //Accuracy
													strcpy(cleanText,"");
													for ( i = 0; i < strlen(text); i ++ )
													{
															if ( text[i] == ')' || text[i] == '(' )
															{
															}
															else if ( text[i] == '/' )
															{
																	TotalSamples = atof(cleanText);
																	strcpy(cleanText,"");
																	
															}
															else
															{
																	sprintf(tempText,"%c",text[i]);
																	strcat(cleanText,tempText);
															}
													}
													WrongClassify = atof(cleanText);
													if ( TotalSamples != 0 )
													{
														J48CurrentNode->RightNodeRuleAccuracy = (double) ( (double)(TotalSamples-WrongClassify)/(double)TotalSamples);
														J48CurrentNode->RightMaximumSamples = TotalSamples;
													}
													else
													{
														J48CurrentNode->RightNodeRuleAccuracy = (double) ( 1);
														J48CurrentNode->RightMaximumSamples = WrongClassify;
													}



											}
											else
											{
													

													J48CurrentNode->RightNodeValue = atof(text);
													J48CurrentNode->RightNodeRuleStatus = 0;

													J48CurrentNode->RightNode = (struct J48_Tree *)malloc(sizeof(struct J48_Tree));
													J48CurrentNode->RightNode->nodeCompleted = 0;
													returnStatus = J48ParseNode(J48CurrentNode->RightNode,fptr,(TreeLevel+1));// 0 for parent
													if ( returnStatus != TreeLevel )
													{
													//	if ( strcmp(J48CurrentNode->AttributeName,"S_AvgMaxDistance") == 0 )
													//		printf("\nYes = %d...%d",returnStatus,TreeLevel);
														J48CurrentNode->nodeCompleted++;
														return returnStatus;
													}
													else
														informationStatus = 0;


											}
											J48CurrentNode->nodeCompleted++;
									}

							}

							informationStatus++;
			}
			else
			{
							currentNodeTreeLevelCount++;

			}



			
	

			

	}

	return 0;


}

void J48TreeConstruction()
{

	char J48TreeFileName[200];
	FILE *fptr;
	char text[200];
	struct J48_Tree *J48CurrentNode;
	int TreeLevel = 0;
	int informationStatus = 0; //0 for node // 1 for condition // 2 for value;


	sprintf( J48TreeFileName,"%s",J48Rules);
	fptr = fopen( J48TreeFileName, "r");

	J48ParentNode = (struct J48_Tree *)malloc(sizeof(struct J48_Tree));
	J48ParentNode->nodeCompleted = 0;
	
	
	J48ParseNode(J48ParentNode,fptr,0);// 0 for parent



	fclose(fptr);
}

void CalculateTermsFreq_TopDocuments(int Version) 
{
	int z = 0, i = 0,z1;
	int Term = 0;
	long RowCount = 0;
	int MakeTransactionCount;
	long DocNumber;



	RowCount = 0;
	MakeTransactionCount = 0;
    for ( z = 0; z < FoundPatentsCount; z++)
    {			    
			DocNumber = FoundPatents[z];
			for  ( i = 0; i < DocTermsArray_TotalTerms[DocNumber]; i++ )
			{											
						Term = DocTermsArray_TermNumber[DocNumber][i];
						if ( DocTermsArray_TermFrequency[DocNumber][i] > 10 )
						{
								if ( m_New32Projection_Clean[Term] != Version )
								{				
										TermSingleFreqInAllDocuments[Term] = 0;
								}
								TermSingleFreqInAllDocuments[Term]++;									
								m_New32Projection_Clean[Term] = Version;
						}
							
			}
			if ( z > 30 )
				break;					
	}





}

void MineQueryPatentTerms(int Local_GlobalPostingAge,int QueryPatent)
{
		long i = 0, z = 0,z1 =0,z2=0,p1,p2;
		long Term;
		long Posting1;
		long Posting2;
		long Distance = 0;
		long Distance1 = 0;
		long Distance2 = 0;
		long MinimumDistance = 99999;
		double S_AvgMinimumDistance = 0;
		double S_AvgMaximumDistance = 0;
		double P_AvgMaximumDistance = 0;

		double S_MinDistance = 0;
		double P_MinDistance = 0;

		long S_AvgMinimumDistanceCount = 0;

		double P_AvgMinimumDistance = 0;
		long P_AvgMinimumDistanceCount = 0;

		long S_CoOccuranceCount = 0;
		long S_CoOccuranceCount_Length = 0;
		double S_CoOccuranceCount_Normalized = 0;

		long P_CoOccuranceCount = 0;
		long P_CoOccuranceCount_Length = 0;
		double P_CoOccuranceCount_Normalized = 0;

		long S_MinimumDistanceCount = 0;
		long P_MinimumDistanceCount = 0;
		long OldQueryItem = 0;
		long NewQueryItem = 0;
		long OnlyOnce = 0;
		double InitialP = 0;
		int IDStatus = 0;
		long lTemp;
		double dTemp;
		double MaximumDistance = 0;
		long OldQuerySize;



		if ( QueryPatent > TotalPatents )
			return;


		OldQuerySize = QueryItemCount;
		CalculateTermsFreq_TopDocuments(Local_GlobalPostingAge);



	//	printf("\n in in in %d\n",GlobalPostingAge);

	


		for ( i = 0; i < QueryItemCount-1; i++ )
		{
				for  ( z = 0; z < DocTermsArray_TotalTerms[QueryPatent]; z++ )
				{				
						Term = DocTermsArray_TermNumber[QueryPatent][z];											
						if ( Term == QueryItemArray[i] )
						{
								QueryTermsPosting[i] = DocTermsArray_TermPosting[QueryPatent][z];
								QueryTermsPostingCount[i] = DocTermsArray_TermFrequency[QueryPatent][z];								
								break;

						}
				}
		}

		

		//Cache Pair Proximity
		PairPosting_Count = 0;
		for ( z1 = 0; z1 < QueryItemCount-1; z1++ )
		{
				for  ( z2 = z1+1; z2 < QueryItemCount-1; z2++ )
				{			
					
						for ( p1 = 0; p1 < QueryTermsPostingCount[z1]; p1++ )
						{
								for ( p2 = 0; p2 < QueryTermsPostingCount[z2]; p2++ )
								{
											Posting1 = QueryTermsPosting[z1][p1];
											Posting2 = QueryTermsPosting[z2][p2];

											if ( Posting1 >= Posting2 )
												Distance = Posting1 - Posting2;
											else
												Distance = Posting2 - Posting1;


											if ( Distance < 100 )
											{
													if ( Posting1 >= Posting2 )
													{
															PairPosting_Min[PairPosting_Count] = Posting2;
															PairPosting_Max[PairPosting_Count] = Posting1;

															PairPosting_QueryItem[PairPosting_Count] = z1;

													}
													else
													{
															PairPosting_Min[PairPosting_Count] = Posting1;
															PairPosting_Max[PairPosting_Count] = Posting2;

															PairPosting_QueryItem[PairPosting_Count] = z1;

													}

													PairPosting_Count++;
											}


								}
						}
						
				}
		}


		for  ( z = 0; z < DocTermsArray_TotalTerms[QueryPatent]; z++ )
		{		
			    S_AvgMinimumDistance = 0;
				S_AvgMinimumDistanceCount = 0;

			    P_AvgMinimumDistance = 0;
				P_AvgMinimumDistanceCount = 0;

				S_CoOccuranceCount = 0;
				S_CoOccuranceCount_Length = 0;
				S_CoOccuranceCount_Normalized = 0;

				P_CoOccuranceCount = 0;
				P_CoOccuranceCount_Length = 0;
				P_CoOccuranceCount_Normalized = 0;


				S_MinimumDistanceCount = 0;
				P_MinimumDistanceCount = 0;


				S_MinDistance = 0;
				P_MinDistance = 0;


				P_AvgMaximumDistance = 0;
				S_AvgMaximumDistance = 0;



				for ( i = 0; i < QueryItemCount-1; i++ )
				{			
						MinimumDistance = 99999;
						MaximumDistance = 0;

						for ( z1 = 0; z1 < DocTermsArray_TermFrequency[QueryPatent][z]; z1++ )
						{
							
								Posting1 = DocTermsArray_TermPosting[QueryPatent][z][z1];

						
								for ( z2 = 0; z2 < QueryTermsPostingCount[i]; z2++ )
								{	
									
											Posting2 = QueryTermsPosting[i][z2];
											if ( Posting1 >= Posting2 )
												Distance = Posting1 - Posting2;
											else
												Distance = Posting2 - Posting1;

										
											if ( Distance < MinimumDistance )
													MinimumDistance = Distance;

											if ( Distance > 90 )
													S_AvgMaximumDistance++;

											if ( Distance < 14 )
											{
												S_CoOccuranceCount++;
												
											}
											if ( Distance < 40 )
											{
												S_CoOccuranceCount_Length++;
												S_MinDistance += Distance;
											}


										
										
								}
						}
						if ( MinimumDistance != 99999 )
						{
								S_AvgMinimumDistance += MinimumDistance;
							//	S_AvgMaximumDistance += MaximumDistance;

								S_AvgMinimumDistanceCount++;

								if ( MinimumDistance < 10 )
										S_MinimumDistanceCount++;					

						}
							
				}
				if ( S_AvgMinimumDistanceCount > 0 )
				{
						S_AvgMinimumDistance = (double) ( (double)S_AvgMinimumDistance/(double)S_AvgMinimumDistanceCount);
				//		S_AvgMaximumDistance = (double) ( (double)S_AvgMaximumDistance/(double)S_AvgMinimumDistanceCount);
				}

				if ( S_CoOccuranceCount_Length > 0 && S_CoOccuranceCount > 0 )
						S_CoOccuranceCount_Normalized = (double) ( (double)S_CoOccuranceCount/(double)S_CoOccuranceCount_Length);
				else
						S_CoOccuranceCount_Normalized = 0;


				if (  S_CoOccuranceCount_Length > 0 )
						S_MinDistance = (double) ( (double)S_MinDistance/(double)S_CoOccuranceCount_Length);
				else
						S_MinDistance = 0;


				MinimumDistance = 99999;
				OldQueryItem = -1;
				NewQueryItem = -1;
				OnlyOnce = 0;
				for ( z1 = 0; z1 < DocTermsArray_TermFrequency[QueryPatent][z]; z1++ )
				{
							Posting1 = DocTermsArray_TermPosting[QueryPatent][z][z1];


							for ( z2 = 0; z2 < PairPosting_Count; z2++ )
							{
										if ( Posting1 >= PairPosting_Min[z2] )
												Distance1 = Posting1 - PairPosting_Min[z2];
										else
												Distance1 = PairPosting_Min[z2] - Posting1;

										
										if ( Posting1 >= PairPosting_Max[z2] )
												Distance2 = Posting1 - PairPosting_Max[z2];
										else
												Distance2 = PairPosting_Max[z2] - Posting1;
										

										Distance = Distance1 + Distance2;

										if ( Distance < 25 )
										{
												P_CoOccuranceCount++;											
										}
										if ( Distance < 40 )
										{
												P_CoOccuranceCount_Length++;
												P_MinDistance += Distance;
										}
										if ( Distance > 90 )
										{
												P_AvgMaximumDistance++;
										}


										NewQueryItem = PairPosting_QueryItem[z2];
										if ( OldQueryItem != NewQueryItem )
										{											
												if ( OnlyOnce == 1 )
												{
															if ( MinimumDistance != 99999 )
															{
																	P_AvgMinimumDistance += MinimumDistance;
																	P_AvgMinimumDistanceCount++;

																	if ( MinimumDistance < 10 )
																		P_MinimumDistanceCount++;

																		
															}

												}
												OnlyOnce = 1;
												MinimumDistance = 99999;
										}
										OldQueryItem = NewQueryItem;



										if ( Distance < MinimumDistance )
												MinimumDistance = Distance;


							}


						
				}
				if ( P_CoOccuranceCount_Length > 0 && P_CoOccuranceCount > 0 )
						P_CoOccuranceCount_Normalized = (double) ( (double)P_CoOccuranceCount/(double)P_CoOccuranceCount_Length);
				else
						P_CoOccuranceCount_Normalized = 0;


				if ( P_AvgMinimumDistanceCount > 0 )
						P_AvgMinimumDistance = (double) ( (double)P_AvgMinimumDistance/(double)P_AvgMinimumDistanceCount);


				if (  P_CoOccuranceCount_Length > 0 )
						P_MinDistance = (double) ( (double)P_MinDistance/(double)P_CoOccuranceCount_Length);
				else
						P_MinDistance = 0;



				unclassify_term_features[0] = S_AvgMinimumDistance;
				unclassify_term_features[1] = S_CoOccuranceCount_Normalized;
				unclassify_term_features[2] = S_CoOccuranceCount;
				unclassify_term_features[3] = S_MinimumDistanceCount;
				unclassify_term_features[4] = P_AvgMinimumDistance;
				unclassify_term_features[5] = P_CoOccuranceCount_Normalized;
				unclassify_term_features[6] = P_CoOccuranceCount;
				unclassify_term_features[7] = P_MinimumDistanceCount;
				unclassify_term_features[8] = (double)((double)DocTermsArray_TermFrequency[QueryPatent][z]/(double)fullPatentLength[QueryPatent]);
				unclassify_term_features[9] = IDFValue[DocTermsArray_TermNumber[QueryPatent][z]];
				unclassify_term_features[10] = S_MinDistance;
				unclassify_term_features[11] = P_MinDistance;
				unclassify_term_features[12] = TermSingleFreqInAllDocuments[DocTermsArray_TermNumber[QueryPatent][z]];
				unclassify_term_features[13] = S_AvgMaximumDistance;
				unclassify_term_features[14] = P_AvgMaximumDistance;



                //fprintf(wekaOutputFile,"%lf,%lf,%ld,%ld,%lf,%lf,%ld,%ld,%lf,%d,%lf,%lf,%ld,%lf,%lf,?\n",S_AvgMinimumDistance,S_CoOccuranceCount_Normalized,S_CoOccuranceCount,S_MinimumDistanceCount,P_AvgMinimumDistance,P_CoOccuranceCount_Normalized,P_CoOccuranceCount,P_MinimumDistanceCount,(double)((double)DocTermsArray_TermFrequency[QueryPatent][z]/(double)fullPatentLength[QueryPatent]),IDFValue[DocTermsArray_TermNumber[QueryPatent][z]],S_MinDistance,P_MinDistance,TermSingleFreqInAllDocuments[DocTermsArray_TermNumber[QueryPatent][z]],S_AvgMaximumDistance,P_AvgMaximumDistance);



				MaximumGoodBadPrediction = 0;
				MaximumPredictionClass = 0;
				BestRuleMaximumSamples = 0;
				RuleFire = 0;
				MaximumPredictionClass  = 0;
				J48Prediction(J48ParentNode,0);
				//if ( MaximumPredictionClass == 1  )
				{
					//	if ( (GoodTermVotes) > (BadTermVotes) )
						{								
								if ( GoodTermVotes > BadTermVotes )
								{ 
										QueryItemArray[QueryItemCount-1] = DocTermsArray_TermNumber[QueryPatent][z];
										QueryItemCount++;
								}
						}
						
				}
				if ( QueryItemCount == 30 ) 
					break;

			//	printf("\n%d...%d...%d...%lf....%d..%d",z,GoodTermVotes,BadTermVotes,MaximumGoodBadPrediction,MaximumPredictionClass,QueryItemCount);

  

				if ( z > 150 )
					break;


		}
		IDStatus = QueryItemCount;




		for ( z = 0; z < FoundPatentsCount; z++)
		{
					CalculteQueryItemsFrequency(FoundPatents[z],QueryItemCount,1);
					ExectureBM25ExpandedVersion(FoundPatents[z],QueryItemCount , z );
		}


		QueryItemCount = OldQuerySize;


	//	printf("\n out out out %d\n",GlobalPostingAge);
}

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 ProcessQueriesInThread(long GlobalPostingAge)
{
	long z = 0;
	long k = 0;
	long z1 = 0;
	long TotalMatch;
	double SimilarFreq;
	long RareCount;
	long QueryItem;
	long RowCountBit;
	char Query_Type[300];
	int SimilarDocs;
	double ScoreRare;
	double IDF;

	double HighFindableDocPercentage;
	double LowFindableDocPercentage;

	long HighTopLimit5;
	long LowTopLimit5;

	long HighTopLimit10;
	long LowTopLimit10;

	long HighTopLimit15;
	long LowTopLimit15;
			
	long HighTopLimit20;
    long LowTopLimit20;

	long HighTopLimit25;
	long LowTopLimit25;
	long Found = 0;





	if ( PPS == 1 )
		strcpy(Query_Type,"high");
	if ( PPS == 2 )
		strcpy(Query_Type,"medium");
	if ( PPS == 3 )
		strcpy(Query_Type,"low");
	if ( PPS == 4 )
		strcpy(Query_Type,"full");
	if ( PPS == 5 )
		strcpy(Query_Type,"extra");

	if ( PPS == 6 )
		strcpy(Query_Type,"partition");
	if ( PPS == 11 )
		strcpy(Query_Type,"high");
	if ( PPS == 12 )
		strcpy(Query_Type,"medium");
	if ( PPS == 13 )
		strcpy(Query_Type,"low");






	FoundPatentsCount = 0;

	ProcessQueryInInvertedIndex(GlobalPostingAge);
	GoodDocuments  = 0;													


	if ( FoundPatentsCount == 0 )
		return;


	for ( z = 0; z < FoundPatentsCount; z++ )	
	{
			PatentRMScore[z] = FoundPatentsScore[z];
	}

	

	quicksort__performance(0,FoundPatentsCount-1);							












	if ( RSType == 101 )
	{
			//	q_Terms = (long *) malloc ( (2200000)  * sizeof(long)); 
			//	doc_Qs = (long *) malloc ( (TotalPatents)  * sizeof(long)); 
			//	docs_Qs_Count = (long *) malloc ( (TotalPatents)  * sizeof(long)); 
			//	GlobalPostingAge
				for ( z = 0; z < QueryItemCount-1; z++ )
						q_Terms[GlobalPostingAge][z] = QueryItemArray[z]; 

				q_Terms_Count[GlobalPostingAge] = QueryItemCount-1;


				for ( z = 0; z < FoundPatentsCount; z++)
				{								
						 if ( doc_Qs_Count[ FoundPatents[z] ] < 2800 )
						 {
								 doc_Qs[ FoundPatents[z] ][ doc_Qs_Count[ FoundPatents[z] ] ] = GlobalPostingAge;
								 doc_Qs_Count[ FoundPatents[z] ] ++;
						 }
				}
				return;
	}
				
	



	
	if ( RSType == 10 )
	{
						for ( z = 0; z < FoundPatentsCount; z++ )													
									PatentRMScore[z] = 0;

						for ( k = 0; k < TotalPatents; k++ )
									QueryClusterN[k] = 0;

					//	CalcualteSingleTermFrequency(GlobalPostingAge);
																
						GoodDocuments = 0;	
						SS = 0;
						for ( z1 = 0; z1 < FoundPatentsCount; z1 ++ )
						{
									TotalMatch = 0;
									SS = 0;
									SimilarDocs = 0;
									ScoreRare = 0;
									for ( z = 0; z < FoundPatentsCount; z++)
									{				
										
											if ( NumberSimilarTermsImprove(FoundPatents[z1],FoundPatents[z]) > 0 )
											{							
													if ( z != z1 )
													{
															TotalMatch+=((11-z)*(11-z));
															SimilarDocs++;
															ScoreRare +=  (double)((double)(11-z)*FoundPatentsScore[z]);
														//	printf("\n%lf...%d..%d",FoundPatentsScore[z],z1,z);
															//QueryClusterN[FoundPatents[z1]]++;
													}
											 }
											 if ( z >= 10 )
												 break;
											 SS++;
									}
									SimilarFreq = 0;									
									RareCount = 0;									
									if ( SimilarDocs != 0 )
									{
										/*	for ( z = 0; z < DocTermsArray_TotalTerms[FoundPatents[z1]]; z++ )
											{
													QueryItem = DocTermsArray_TermNumber[FoundPatents[z1]][z];
													if (  IDFValue[QueryItem] < TotalPatents && DocTermsArray_TermNumber[FoundPatents[z1]][z] > 0 )
													{																																
																RowCountBit = CalculateClusterFrequentItemFrequency(QueryItem);
																if ( (double)((double)RowCountBit/(double)SimilarDocs) >= .40 )
																{
																		RareCount ++;
																}
													}
											}*/
											GoodDocuments++;
									}
									
										

								//	if ( (double)((double)RareCount/(double)DocTermsArray_TotalTerms[FoundPatents[z1]]) >= .10 )
								//	{
										PatentRMScore[z1] = TotalMatch;									
								//	}
								//	else																						
								//		PatentRMScore[z1] = 0;

									
									if ( z1 > 1500 )
										break;
						}
									
					//	printf("\n%d...",GoodDocuments);
						Ex = 0;
						if ( GoodDocuments > 9 )
						{

								if ( FoundPatentsCount > 1500 )
										quicksort__performance(0,1499);
								else
										quicksort__performance(0,FoundPatentsCount-1);

								Ex = 1;
						}

						for ( z = 0; z < FoundPatentsCount; z++ )													
							PatentRMScore[z] = FoundPatentsScore[z];

						quicksort__performance(GoodDocuments,FoundPatentsCount-1);						

	}

	if ( RSType == 13 )
	{
						for ( z = 0; z < FoundPatentsCount; z++ )													
									PatentRMScore[z] = 0;

						for ( k = 0; k < TotalPatents; k++ )
									QueryClusterN[k] = 0;
						
																
						GoodDocuments = 0;	
						SS = 0;
						for ( z1 = 0; z1 < FoundPatentsCount; z1 ++ )
						{
									TotalMatch = 0;
									SS = 0;
									SimilarDocs = 0;
									for ( z = 0; z < FoundPatentsCount; z++)
									{				
											if ( NumberSimilarTermsImprove(FoundPatents[z1],FoundPatents[z]) > 0 )
											{							
													if ( z != z1 )
														QueryClusterN[FoundPatents[z]]++;
											}
											if ( z > 100 )
													break;

									}	
						}
						
						for ( z = 0; z < FoundPatentsCount; z++ )
							PatentRMScore[z] = QueryClusterN[FoundPatents[z]];

						quicksort__performance(0,FoundPatentsCount-1);

	}


	if ( RSType == 21 )
	{
			MineQueryPatentTerms(GlobalPostingAge,QueryItemArray[QueryItemCount-1]);
			quicksort__performance(0,FoundPatentsCount-1);
	}


	GoodDocuments = 10;

	if ( RSType == 10 || RSType == 11 || RSType == 13 || RSType == 21 )
	{
			ExpandQueryLanguageModelMethod(GlobalPostingAge);				
			quicksort__performance(0,FoundPatentsCount-1);
	}
	if ( RSType == 12 )
	{
			ExpandQueryKLDMethod(GlobalPostingAge);											
			quicksort__performance(0,FoundPatentsCount-1);
	}



															
						
//	printf("\n%d....",FoundPatentsCount[ThreadNumber]);

						

	if ( RSType < 60 )
	{

			for ( z = 0; z < FoundPatentsCount; z++)
			{			
							
					 if ( PPS > 10 )
					 {
							 if ( FoundPatents[z] != QueryItemArray[QueryItemCount-1] )
							 {
									 if ( z < cutoff_1 )
										 DocFindability_High[0][FoundPatents[z]]++;
									 if ( z < cutoff_2 )
										 DocFindability_High[1][FoundPatents[z]]++;
									 if ( z < cutoff_3 )
										 DocFindability_High[2][FoundPatents[z]]++;
									 if ( z < cutoff_4 )
										 DocFindability_High[3][FoundPatents[z]]++;
									 if ( z < cutoff_5 )
										 DocFindability_High[4][FoundPatents[z]]++;
							 

									DocTotalQueries_High[FoundPatents[z]]++;																
							 }
					 }
					 else
					 {
							 if ( z < cutoff_1 )
								 DocFindability_High[0][FoundPatents[z]]++;
							 if ( z < cutoff_2 )
								 DocFindability_High[1][FoundPatents[z]]++;
							 if ( z < cutoff_3 )
								 DocFindability_High[2][FoundPatents[z]]++;
							 if ( z < cutoff_4 )
								 DocFindability_High[3][FoundPatents[z]]++;
							 if ( z < cutoff_5 )
								 DocFindability_High[4][FoundPatents[z]]++;
							 

							DocTotalQueries_High[FoundPatents[z]]++;																
					 }
			}
	}
	if ( RSType > 60 )
	{

					quicksort__FindabilityScore (0,FoundPatentsCount-1);

					quicksort__performance (0,TotalFoundHighFindableDocuments-1);
					quicksort__performance (TotalFoundHighFindableDocuments,FoundPatentsCount-1);


					HighFindableDocPercentage = (double)((double)TotalFoundHighFindableDocuments/(double)FoundPatentsCount);
					LowFindableDocPercentage = (double)((double)TotalFoundLowFindableDocuments/(double)FoundPatentsCount);

					HighTopLimit5 = cutoff_1*HighFindableDocPercentage;
					LowTopLimit5 = cutoff_1*LowFindableDocPercentage;


					HighTopLimit10 = cutoff_2*HighFindableDocPercentage;
					LowTopLimit10 = cutoff_2*LowFindableDocPercentage;


					HighTopLimit15 = cutoff_3*HighFindableDocPercentage;
					LowTopLimit15 = cutoff_3*LowFindableDocPercentage;
					
					HighTopLimit20 = cutoff_4*HighFindableDocPercentage;
					LowTopLimit20 = cutoff_4*LowFindableDocPercentage;


					HighTopLimit25 = cutoff_5*HighFindableDocPercentage;
					LowTopLimit25 = cutoff_5*LowFindableDocPercentage;



					if ( HighFindableDocPercentage > 0 && LowFindableDocPercentage > 0 )
					{
							if ( HighFindableDocPercentage > LowFindableDocPercentage )
							{
									LowTopLimit5++;
									LowTopLimit10++;
									LowTopLimit15++;
									LowTopLimit20++;
									LowTopLimit25++;
							}
							
							if ( HighFindableDocPercentage < LowFindableDocPercentage )
							{
									HighTopLimit5++;
									HighTopLimit10++;
									HighTopLimit15++;
									HighTopLimit20++;
									HighTopLimit25++;
							}
					}


					for ( z = 0; z < TotalFoundHighFindableDocuments; z++ )
					{
								
									 if ( z < HighTopLimit5 )
											 DocFindability_High[0][FoundPatents[z]]++;
									 if ( z < HighTopLimit10 )
											DocFindability_High[1][FoundPatents[z]]++;
									 if ( z < HighTopLimit15 )
											 DocFindability_High[2][FoundPatents[z]]++;
									 if ( z < HighTopLimit20 )
											 DocFindability_High[3][FoundPatents[z]]++;
									 if ( z < HighTopLimit25 )
											 DocFindability_High[4][FoundPatents[z]]++;

									 DocTotalQueries_High[FoundPatents[z]]++;											
					}


					
					Found = 0;
					for ( z = TotalFoundHighFindableDocuments; z < FoundPatentsCount; z++ )
					{

									 if ( Found < LowTopLimit5 )
											 DocFindability_High[0][FoundPatents[z]]++;
									 if ( Found < LowTopLimit10 )
											DocFindability_High[1][FoundPatents[z]]++;
									 if ( Found < LowTopLimit15 )
											 DocFindability_High[2][FoundPatents[z]]++;
									 if ( Found < LowTopLimit20 )
											 DocFindability_High[3][FoundPatents[z]]++;
									 if ( Found < LowTopLimit25 )
											 DocFindability_High[4][FoundPatents[z]]++;

									 DocTotalQueries_High[FoundPatents[z]]++;						
									 Found++;
					}

	}
		 



	HighQ++;



										
	if ( HighQ%4000 == 0 )
	{

			printf("\nQueries Completed = %ld	Process ID = %d\n",GlobalPostingAge,p_running);
	

	/*		if ( RSType == 7 )
					printf("\n Completed (_UnNormTFIDF_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 3 )
					printf("\n Completed (._DirS_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 2 )
					printf("\n Completed (_BM25_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 4 )
					printf("\n Completed (_JM_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 5 )
					printf("\n Completed (_AbsDis_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 6 )
					printf("\n Completed (_TwoStage_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 8 )
					printf("\n Completed (__SMART_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 1 )
					printf("\n Completed (__TFIDF._%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 10 )
					printf("\n Completed (_FT_Cluster_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 11 )
					printf("\n Completed (_PQE_TS_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 12 )
					printf("\n Completed (_QE_KLD_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 13 )
					printf("\n Completed (_QE_Lee_Clust_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 21 )
					printf("\n Completed (_PA_TS_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);

	
	
			if ( RSType == 67 )
					printf("\n Completed (Part_UnNormTFIDF_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 63 )
					printf("\n Completed (Part_DirS_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 62 )
					printf("\n Completed (Part_BM25_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 64 )
					printf("\n Completed (Part_JM_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 65 )
					printf("\n Completed (Part_AbsDis_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 66 )
					printf("\n Completed (Part_TwoStage_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 68 )
					printf("\n Completed (Part_SMART_%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);
			if ( RSType == 61 )
					printf("\n Completed (Part_TFIDF._%s) == %ld....%ld......(%ld/%ld)....%d.",Query_Type,GlobalPostingAge,FoundPatentsCount,HighQ,ExpansionCountHigh,p_running);*/


	
	
	}



//	if ( HighQ%8000 == 0 )
//		CalculateGINI_Short();
															

													


	QueryItemCount = 0;											






											

												

}

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

						iTempNumber = doc_Qs_R[doc][i];
						doc_Qs_R[doc][i] = doc_Qs_R[doc][j];
						doc_Qs_R[doc][j] = iTempNumber;


						iTempNumber = doc_Qs[doc][i];
						doc_Qs[doc][i] = doc_Qs[doc][j];
						doc_Qs[doc][j] = iTempNumber;


						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort__doc_Qs (lo,j,doc);
		if ( i<hi) quicksort__doc_Qs (i,hi,doc);
	
}

void quicksort_hat_rd_score ( int lo , int hi )
{
		int i = lo, j = hi;
		double x = hat_rd_score[ (lo+hi)/2 ];
		long 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 clusterNumber)
{
		int i;
		int Factor;
		int Label;
		double Average_rd = 0;
		double Average_hat_rd = 0;
		int Count = 0;
		FILE *fptr;
		char m_FileName[300];


		quicksort_hat_rd_score(0,TotalPatents-1);
		Factor = TotalPatents/Partitions;

		for ( i = 0; i < TotalPatents; i++ )
		{
				Label = i/Factor;
				ComponentLabel[DocID[i]] = Label;	
						
		}


}


void ProcessQueries(int clusterNumber)
{
	FILE *fptr,*fptr2,*score_prediction;
	int queryWord,k;
	char queriesFileName[300];
	int returnValue = 0;
	int z = 0,i;
	double PercentageLower,PercentageUpper;
	int OldNumber = 0;
	int V = 0;
	int tt = 0;
	int CheckPoints[80];
	char FileName[300];
	int DocumentsLowHigh[35200];
	FILE *score;
	int DocumentsLowHighTotal = 0;
	int TP = 0;
	double Process;
	double DF;
	int z1 = 0;
	long TotalMatch;
	char RM_Name[300];
	int j = 0;
	long qg_CC;



	double SimilarFreq;
	long RareCount;
	long QueryItem;
	long RowCountBit;

	int QueriesComp = 0;
	int OkP = 0;
	int Loop = 0;



	char Query_Type[300];
	char RS_Name[300];
	int Find5;
	int	Local_rd_score;
	int	Find15;
	int	Find20;
	int	Find25;
	int local_DocTotalQueries;
    	

	terminate_threads = 0;
	qg_CC = 0;

	if ( PPS == 1 )
		strcpy(Query_Type,"high");
	if ( PPS == 2 )
		strcpy(Query_Type,"medium");
	if ( PPS == 3 )
		strcpy(Query_Type,"low");
	if ( PPS == 4 )
		strcpy(Query_Type,"full");
	if ( PPS == 5 )
		strcpy(Query_Type,"extra");
	if ( PPS == 6 )
		strcpy(Query_Type,"partition");
	if ( PPS == 11 )
		strcpy(Query_Type,"high");
	if ( PPS == 12 )
		strcpy(Query_Type,"medium");
	if ( PPS == 13 )
		strcpy(Query_Type,"low");



	









	if ( RSType == 1 )
		strcpy(RM_Name,"TFIDF");
	if ( RSType == 2 )
		strcpy(RM_Name,"BM25");
	if ( RSType == 3 )
		strcpy(RM_Name,"DirS");
	if ( RSType == 4 )
		strcpy(RM_Name,"JM");
	if ( RSType == 5 )
		strcpy(RM_Name,"AbsDis");
	if ( RSType == 6 )
		strcpy(RM_Name,"TwoStage");
	if ( RSType == 7 )
		strcpy(RM_Name,"UnNormTFIDF");
	if ( RSType == 8 )
		strcpy(RM_Name,"SMART");

	if ( RSType == 10 )
		strcpy(RM_Name,"QE_FT");
	if ( RSType == 11 )
		strcpy(RM_Name,"QE_TS");
	if ( RSType == 12 )
		strcpy(RM_Name,"QE_KLD");
	if ( RSType == 13 )
		strcpy(RM_Name,"QE_Lee_Cluster");
	if ( RSType == 21 )
		strcpy(RM_Name,"PA_TS");


	if ( RSType == 61 )
		strcpy(RM_Name,"Part_TFIDF");
	if ( RSType == 62 )
		strcpy(RM_Name,"Part_BM25");
	if ( RSType == 63 )
		strcpy(RM_Name,"Part_DirS");
	if ( RSType == 64 )
		strcpy(RM_Name,"Part_JM");
	if ( RSType == 65 )
		strcpy(RM_Name,"Part_AbsDis");
	if ( RSType == 66 )
		strcpy(RM_Name,"Part_TwoStage");
	if ( RSType == 67 )
		strcpy(RM_Name,"Part_UnNormTFIDF");
	if ( RSType == 68 )
		strcpy(RM_Name,"Part_SMART");


	HighQ = 0;
	ExpansionCountHigh = 0;







	QE_FlagCounter = 0;
	GlobalPostingAge = 0;





	if ( RSType >= 61 )
	{

				if ( RSType == 61 )
					strcpy(RS_Name,"TFIDF");
				if ( RSType == 62 )
					strcpy(RS_Name,"BM25");
				if ( RSType == 63 )
					strcpy(RS_Name,"DirS");
				if ( RSType == 64 )
					strcpy(RS_Name,"JM");
				if ( RSType == 65 )
					strcpy(RS_Name,"AbsDis");
				if ( RSType == 66 )
					strcpy(RS_Name,"TwoStage");
				if ( RSType == 67 )
					strcpy(RS_Name,"UnNormTFIDF");
				if ( RSType == 68 )
					strcpy(RS_Name,"SMART");


				//if ( clusterNumber == 37 )
					sprintf(queriesFileName,"%sWorkingFolder//AND_Results//%s_extra.txt",sourcePath,RS_Name);//,fileNumber
				//else
				//	sprintf(queriesFileName,"%sWorkingFolder_real//AND_Queries_Results//%s_full.txt",sourcePath,RS_Name);//,fileNumber
				//sprintf(queriesFileName,"%s",find_file_split);//,fileNumber


				fptr = fopen(queriesFileName, "r");
				while(1)
				{
						if ( fscanf(fptr,"%d	%d	%d	%d	%d	%d",&Find5,&Local_rd_score,&Find15,&Find20,&Find25,&local_DocTotalQueries) == EOF )
							break;


						if ( local_DocTotalQueries > 5 )
							Process = (double) ( (double) Local_rd_score/ (double) local_DocTotalQueries );
						else
							Process = 0;

						hat_rd_score[V] = Process;
						DocID[V] = V;
						V++;		

				}
				fclose(fptr);
		

	}


	GlobalPartitionNumber = 2;
	CreateDocumentsPartitions_rd_score(GlobalPartitionNumber,clusterNumber);




//	if ( PPS < 10 )
//		sprintf(queriesFileName,"%squeries_3_4_100_27.txt",sourcePath,Query_Type);//,fileNumber
//	if ( PPS > 10 )
//		sprintf(queriesFileName,"%squeries_%s_EC_PA.txt",sourcePath,Query_Type);//,fileNumber


	sprintf(queriesFileName,"%s",find_query_name);//,fileNumber



	fptr = fopen(queriesFileName, "r");
	QueryItemCount = 0;
	while ( 1 )
	{

					if ( fscanf(fptr,"%d",&queryWord) == EOF ) 
								break;


					if ( queryWord != -17 )
					{						
								QueryItemArray[QueryItemCount] = queryWord;
					}
					QueryItemCount++;
					if ( queryWord == -17 )
					{
								if ( PPS > 10 )
									QueryItemCount = QueryItemCount-1;

								ActualLeng = QueryItemCount-1;
								GlobalPostingAge++;
								if ( RSType != 101 )
								{
										TotalFoundHighFindableDocuments = 0;
										TotalFoundLowFindableDocuments = 0;

										if ( GlobalPostingAge%ParallelMaxSize == p_running )
										{
												ProcessQueriesInThread(GlobalPostingAge);
										}		
								}
								if ( RSType == 101 )
								{
										ProcessQueriesInThread(GlobalPostingAge);
										if ( GlobalPostingAge%2000 == 0 )
												printf("\n Queries Completed = %d	%d",GlobalPostingAge,p_running);
								}

								if ( GlobalPostingAge > 60000 )
									break;

								
								QueryItemCount = 0;				
								
					}

	}
	fclose(fptr);




	if ( RSType == 101 )
	{
				
				sprintf(queriesFileName,"%squeries_%s_EC_PA.txt",sourcePath,Query_Type);//,fileNumber
				fptr2 = fopen(queriesFileName, "w");

				for ( i = 0; i < TotalPatents; i++ )
				{
						srand(time(NULL));
						for ( j = 0; j < doc_Qs_Count[i]; j++ )
								doc_Qs_R[i][j] = rand();

						 quicksort__doc_Qs( 0 , doc_Qs_Count[i]-1, i);
				}


				while(1)
				{
						Loop = 0;
						for ( i = 0; i < TotalPatents; i++ )
						{
								if ( doc_Qs_Count_Writing[i] < doc_Qs_Count[i] )
								{
										for ( j = 0; j < q_Terms_Count[ doc_Qs[i][ doc_Qs_Count_Writing[i] ] ]; j++ )
										{
												//printf("%d	",q_Terms[ doc_Qs[i][ doc_Qs_Count_Writing[i] ] ] [j] );
												fprintf(fptr2,"%d	",q_Terms[ doc_Qs[i][ doc_Qs_Count_Writing[i] ] ] [j] );

										}
										fprintf(fptr2,"%d	-17\n",i);

									//	printf("\n");			

										doc_Qs_Count_Writing[i]++;
										qg_CC++;
								}
								else
								{
										Loop++;
								}
						}
						if ( Loop == TotalPatents)
							break;

						if ( qg_CC >= 2000000 )
							break;
				}
				printf("\nTotal Queries Write %d......\n",qg_CC);
				fclose(fptr2);


				sprintf(queriesFileName,"%squeries_%s_EC_PA_Per_Doc.txt",sourcePath,Query_Type);//,fileNumber
				fptr2 = fopen(queriesFileName, "w");
				for ( i = 0; i < TotalPatents; i++ )
						fprintf(fptr2,"%d	%d\n",i,doc_Qs_Count_Writing[i]);
	
				fclose(fptr2);

				exit(1);
	}








	if ( RSType != 101 )
	{



				sprintf(queriesFileName,"%sWorkingFolder//%s_%s_%d_%lf.txt",sourcePath,RM_Name,Query_Type,p_running,Parameter_Value);
				findability_log_High = fopen(queriesFileName, "w");


				for ( i = 0; i < TotalPatents; i++ )
				{
						fprintf(findability_log_High,"%d	%d	%d	%d	%d	%d\n",DocFindability_High[0][i],DocFindability_High[1][i],DocFindability_High[2][i],DocFindability_High[3][i],DocFindability_High[4][i],DocTotalQueries_High[i]);
				}

				fclose(findability_log_High);

	}








}




int main (int argc, char *argv[])
{                              
	int clusterNumber = 0;
	int patentsCopyingFrom;
	int patentsCopyingTo;
	int i = 0;
	int counter = 0;
	FILE *prog_settings;
	char m_FileName[300];
	pid_t pID[100];

	



	
	system("clear");



	// first parameter = files folder path (like //home//bashir//)
	// second parameter = cluster number
	// third parameter = patentsCopyingFrom
	// forth parameter = patentsCopyingTo
	// fifth parameter = fileNumber - log
	// Retrieval System type = 1. TFIDF, 2. BM25, 3. LM
	// Parameter value

//	execl("/bin/ls","/bin/ls",0);		



	strcpy(sourcePath,argv[1]);
	clusterNumber = atoi(argv[2]);
	patentsCopyingFrom = atoi(argv[3]);
	patentsCopyingTo = atoi(argv[4]);
	fileNumber = atoi(argv[5]);
	RSType = atoi(argv[6]);
	Parameter_Value = atof(argv[7]);
	//PPS = atoi(argv[8]);
	PPS = 4;


	sprintf(m_FileName,"%sSettings.txt",sourcePath);
	prog_settings = fopen(m_FileName,"r");
	if ( prog_settings == NULL )
	{
			printf("\n Settings file is missing. Please place Setting.txt in %s\n",sourcePath);

			exit(1);
	}

	fscanf(prog_settings,"%d",&fork_size);
	fscanf(prog_settings,"%s",find_query_name);
	fscanf(prog_settings,"%d",&cutoff_1);
	fscanf(prog_settings,"%d",&cutoff_2);
	fscanf(prog_settings,"%d",&cutoff_3);
	fscanf(prog_settings,"%d",&cutoff_4);
	fscanf(prog_settings,"%d",&cutoff_5);
	fscanf(prog_settings,"%s",find_file_split);
	fscanf(prog_settings,"%s",offline_cluster_file);
	fscanf(prog_settings,"%s",J48Rules);
	fscanf(prog_settings,"%s",Term_Posting_File);


//	printf("\n%d...%d...%d...%d..%d\n",cutoff_1,cutoff_2,cutoff_3,cutoff_4,cutoff_5);

	if ( RSType == 1 || RSType == 101 || RSType == 61 )
	{			
			printf("\nTFIDF algorithm selected");
	}

	if ( RSType == 2 || RSType == 62)
	{			
			printf("\nBM25 algorithm selected");

	}

	if  ( RSType == 3|| RSType == 63 )
	{	
			printf("\nLM algorithm selected");
	}


	if  ( RSType == 4 || RSType == 64)
	{			
			printf("\nJM algorithm selected");

	}

	if  ( RSType == 5 || RSType == 65)
	{
			printf("\nAbsolute Discounting Smooting algorithm selected");			

	}

	if  ( RSType == 6 || RSType == 66)
	{
			printf("\nTwo Stage Smooting algorithm selected");
			
	}


	if  ( RSType == 7 || RSType == 67)
	{
			printf("\nUn Normalized TFIDF algorithm selected");
			
	}

	if  ( RSType == 8 || RSType == 68)
	{
			printf("\nSMART algorithm selected");			
	}

	if ( RSType == 10 )
	{	
			printf("\nQE FT BM25 algorithm selected");

	}
	if ( RSType == 11 )
	{			
			printf("\nQE TS BM25 algorithm selected");

	}
	if ( RSType == 12 )
	{			
			printf("\nQE KLD BM25 algorithm selected");

	}
	if ( RSType == 13 )
	{
			printf("\nQE Lee Clustering algorithm selected");
	}
	if ( RSType == 21 )
	{
			printf("\nQP TS Clustering algorithm selected");
	}


	printf("\n\n");


	fclose(prog_settings);



//	printf("\nProcessing Queries\n");

	p_running = 0;
	ParallelMaxSize = 1;
	if ( RSType != 101 )
	{
					ParallelMaxSize = fork_size;


					RemoveOldFiles();



					pID[0] = fork();
					counter++;
					p_running = counter-1;
					if ( pID[0] > 0 )
					{
							while(1)
							{
									if ( counter < ParallelMaxSize && pID[0] > 0 )
									{
											counter++;
											pID[0] = fork();
											p_running = counter-1;
									}
									if ( pID[0] > 0 )
									{
											if ( ParallelMaxSize == ChildTerminationStatus(patentsCopyingTo) )
											{
													MergeResultingFiles();
												//	CalculateGINI_Short();
													printf("\nQuery Processing Completed Successfully\n");
													exit(1);
											}
											
									}
									if ( pID[0] == 0 )
										break;
							}
								
							
					}

	}
	


	







	

	strcpy(sourcePath,argv[1]);
	clusterNumber = atoi(argv[2]);
	patentsCopyingFrom = atoi(argv[3]);
	patentsCopyingTo = atoi(argv[4]);
	fileNumber = atoi(argv[5]);
	RSType = atoi(argv[6]);
	Parameter_Value = atof(argv[7]);
	//PPS = atoi(argv[8]);

	PPS = 4;

	
	if ( RSType == 21 )
		J48TreeConstruction();





	ReadItemFile(clusterNumber,patentsCopyingFrom,patentsCopyingTo);
	
	
	if( RSType == 10 || RSType == 13 )
	{
		ReadDocumentSimilarity();
		printf("\nReading Initial Clusters Completed\n");
	}





	

	
	ProcessQueries(clusterNumber);
///	GenerateQueries();

	return 0;
}  
