

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h> 
//#include "GlobalVariables.h"
#include "QueryExpansion.h"

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



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



int RowTable[256][9] = {

{ 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 1, 7, 0, 0, 0, 0, 0, 0, 0},
{ 1, 6, 0, 0, 0, 0, 0, 0, 0},
{ 2, 7, 6, 0, 0, 0, 0, 0, 0},
{ 1, 5, 0, 0, 0, 0, 0, 0, 0},
{ 2, 7, 5, 0, 0, 0, 0, 0, 0},
{ 2, 6, 5, 0, 0, 0, 0, 0, 0},
{ 3, 7, 6, 5, 0, 0, 0, 0, 0},
{ 1, 4, 0, 0, 0, 0, 0, 0, 0},
{ 2, 7, 4, 0, 0, 0, 0, 0, 0},
{ 2, 6, 4, 0, 0, 0, 0, 0, 0},
{ 3, 7, 6, 4, 0, 0, 0, 0, 0},
{ 2, 5, 4, 0, 0, 0, 0, 0, 0},
{ 3, 7, 5, 4, 0, 0, 0, 0, 0},
{ 3, 6, 5, 4, 0, 0, 0, 0, 0},
{ 4, 7, 6, 5, 4, 0, 0, 0, 0},
{ 1, 3, 0, 0, 0, 0, 0, 0, 0},
{ 2, 7, 3, 0, 0, 0, 0, 0, 0},
{ 2, 6, 3, 0, 0, 0, 0, 0, 0},
{ 3, 7, 6, 3, 0, 0, 0, 0, 0},
{ 2, 5, 3, 0, 0, 0, 0, 0, 0},
{ 3, 7, 5, 3, 0, 0, 0, 0, 0},
{ 3, 6, 5, 3, 0, 0, 0, 0, 0},
{ 4, 7, 6, 5, 3, 0, 0, 0, 0},
{ 2, 4, 3, 0, 0, 0, 0, 0, 0},
{ 3, 7, 4, 3, 0, 0, 0, 0, 0},
{ 3, 6, 4, 3, 0, 0, 0, 0, 0},
{ 4, 7, 6, 4, 3, 0, 0, 0, 0},
{ 3, 5, 4, 3, 0, 0, 0, 0, 0},
{ 4, 7, 5, 4, 3, 0, 0, 0, 0},
{ 4, 6, 5, 4, 3, 0, 0, 0, 0},
{ 5, 7, 6, 5, 4, 3, 0, 0, 0},
{ 1, 2, 0, 0, 0, 0, 0, 0, 0},
{ 2, 7, 2, 0, 0, 0, 0, 0, 0},
{ 2, 6, 2, 0, 0, 0, 0, 0, 0},
{ 3, 7, 6, 2, 0, 0, 0, 0, 0},
{ 2, 5, 2, 0, 0, 0, 0, 0, 0},
{ 3, 7, 5, 2, 0, 0, 0, 0, 0},
{ 3, 6, 5, 2, 0, 0, 0, 0, 0},
{ 4, 7, 6, 5, 2, 0, 0, 0, 0},
{ 2, 4, 2, 0, 0, 0, 0, 0, 0},
{ 3, 7, 4, 2, 0, 0, 0, 0, 0},
{ 3, 6, 4, 2, 0, 0, 0, 0, 0},
{ 4, 7, 6, 4, 2, 0, 0, 0, 0},
{ 3, 5, 4, 2, 0, 0, 0, 0, 0},
{ 4, 7, 5, 4, 2, 0, 0, 0, 0},
{ 4, 6, 5, 4, 2, 0, 0, 0, 0},
{ 5, 7, 6, 5, 4, 2, 0, 0, 0},
{ 2, 3, 2, 0, 0, 0, 0, 0, 0},
{ 3, 7, 3, 2, 0, 0, 0, 0, 0},
{ 3, 6, 3, 2, 0, 0, 0, 0, 0},
{ 4, 7, 6, 3, 2, 0, 0, 0, 0},
{ 3, 5, 3, 2, 0, 0, 0, 0, 0},
{ 4, 7, 5, 3, 2, 0, 0, 0, 0},
{ 4, 6, 5, 3, 2, 0, 0, 0, 0},
{ 5, 7, 6, 5, 3, 2, 0, 0, 0},
{ 3, 4, 3, 2, 0, 0, 0, 0, 0},
{ 4, 7, 4, 3, 2, 0, 0, 0, 0},
{ 4, 6, 4, 3, 2, 0, 0, 0, 0},
{ 5, 7, 6, 4, 3, 2, 0, 0, 0},
{ 4, 5, 4, 3, 2, 0, 0, 0, 0},
{ 5, 7, 5, 4, 3, 2, 0, 0, 0},
{ 5, 6, 5, 4, 3, 2, 0, 0, 0},
{ 6, 7, 6, 5, 4, 3, 2, 0, 0},
{ 1, 1, 0, 0, 0, 0, 0, 0, 0},
{ 2, 7, 1, 0, 0, 0, 0, 0, 0},
{ 2, 6, 1, 0, 0, 0, 0, 0, 0},
{ 3, 7, 6, 1, 0, 0, 0, 0, 0},
{ 2, 5, 1, 0, 0, 0, 0, 0, 0},
{ 3, 7, 5, 1, 0, 0, 0, 0, 0},
{ 3, 6, 5, 1, 0, 0, 0, 0, 0},
{ 4, 7, 6, 5, 1, 0, 0, 0, 0},
{ 2, 4, 1, 0, 0, 0, 0, 0, 0},
{ 3, 7, 4, 1, 0, 0, 0, 0, 0},
{ 3, 6, 4, 1, 0, 0, 0, 0, 0},
{ 4, 7, 6, 4, 1, 0, 0, 0, 0},
{ 3, 5, 4, 1, 0, 0, 0, 0, 0},
{ 4, 7, 5, 4, 1, 0, 0, 0, 0},
{ 4, 6, 5, 4, 1, 0, 0, 0, 0},
{ 5, 7, 6, 5, 4, 1, 0, 0, 0},
{ 2, 3, 1, 0, 0, 0, 0, 0, 0},
{ 3, 7, 3, 1, 0, 0, 0, 0, 0},
{ 3, 6, 3, 1, 0, 0, 0, 0, 0},
{ 4, 7, 6, 3, 1, 0, 0, 0, 0},
{ 3, 5, 3, 1, 0, 0, 0, 0, 0},
{ 4, 7, 5, 3, 1, 0, 0, 0, 0},
{ 4, 6, 5, 3, 1, 0, 0, 0, 0},
{ 5, 7, 6, 5, 3, 1, 0, 0, 0},
{ 3, 4, 3, 1, 0, 0, 0, 0, 0},
{ 4, 7, 4, 3, 1, 0, 0, 0, 0},
{ 4, 6, 4, 3, 1, 0, 0, 0, 0},
{ 5, 7, 6, 4, 3, 1, 0, 0, 0},
{ 4, 5, 4, 3, 1, 0, 0, 0, 0},
{ 5, 7, 5, 4, 3, 1, 0, 0, 0},
{ 5, 6, 5, 4, 3, 1, 0, 0, 0},
{ 6, 7, 6, 5, 4, 3, 1, 0, 0},
{ 2, 2, 1, 0, 0, 0, 0, 0, 0},
{ 3, 7, 2, 1, 0, 0, 0, 0, 0},
{ 3, 6, 2, 1, 0, 0, 0, 0, 0},
{ 4, 7, 6, 2, 1, 0, 0, 0, 0},
{ 3, 5, 2, 1, 0, 0, 0, 0, 0},
{ 4, 7, 5, 2, 1, 0, 0, 0, 0},
{ 4, 6, 5, 2, 1, 0, 0, 0, 0},
{ 5, 7, 6, 5, 2, 1, 0, 0, 0},
{ 3, 4, 2, 1, 0, 0, 0, 0, 0},
{ 4, 7, 4, 2, 1, 0, 0, 0, 0},
{ 4, 6, 4, 2, 1, 0, 0, 0, 0},
{ 5, 7, 6, 4, 2, 1, 0, 0, 0},
{ 4, 5, 4, 2, 1, 0, 0, 0, 0},
{ 5, 7, 5, 4, 2, 1, 0, 0, 0},
{ 5, 6, 5, 4, 2, 1, 0, 0, 0},
{ 6, 7, 6, 5, 4, 2, 1, 0, 0},
{ 3, 3, 2, 1, 0, 0, 0, 0, 0},
{ 4, 7, 3, 2, 1, 0, 0, 0, 0},
{ 4, 6, 3, 2, 1, 0, 0, 0, 0},
{ 5, 7, 6, 3, 2, 1, 0, 0, 0},
{ 4, 5, 3, 2, 1, 0, 0, 0, 0},
{ 5, 7, 5, 3, 2, 1, 0, 0, 0},
{ 5, 6, 5, 3, 2, 1, 0, 0, 0},
{ 6, 7, 6, 5, 3, 2, 1, 0, 0},
{ 4, 4, 3, 2, 1, 0, 0, 0, 0},
{ 5, 7, 4, 3, 2, 1, 0, 0, 0},
{ 5, 6, 4, 3, 2, 1, 0, 0, 0},
{ 6, 7, 6, 4, 3, 2, 1, 0, 0},
{ 5, 5, 4, 3, 2, 1, 0, 0, 0},
{ 6, 7, 5, 4, 3, 2, 1, 0, 0},
{ 6, 6, 5, 4, 3, 2, 1, 0, 0},
{ 7, 7, 6, 5, 4, 3, 2, 1, 0},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0},
{ 2, 7, 0, 0, 0, 0, 0, 0, 0},
{ 2, 6, 0, 0, 0, 0, 0, 0, 0},
{ 3, 7, 6, 0, 0, 0, 0, 0, 0},
{ 2, 5, 0, 0, 0, 0, 0, 0, 0},
{ 3, 7, 5, 0, 0, 0, 0, 0, 0},
{ 3, 6, 5, 0, 0, 0, 0, 0, 0},
{ 4, 7, 6, 5, 0, 0, 0, 0, 0},
{ 2, 4, 0, 0, 0, 0, 0, 0, 0},
{ 3, 7, 4, 0, 0, 0, 0, 0, 0},
{ 3, 6, 4, 0, 0, 0, 0, 0, 0},
{ 4, 7, 6, 4, 0, 0, 0, 0, 0},
{ 3, 5, 4, 0, 0, 0, 0, 0, 0},
{ 4, 7, 5, 4, 0, 0, 0, 0, 0},
{ 4, 6, 5, 4, 0, 0, 0, 0, 0},
{ 5, 7, 6, 5, 4, 0, 0, 0, 0},
{ 2, 3, 0, 0, 0, 0, 0, 0, 0},
{ 3, 7, 3, 0, 0, 0, 0, 0, 0},
{ 3, 6, 3, 0, 0, 0, 0, 0, 0},
{ 4, 7, 6, 3, 0, 0, 0, 0, 0},
{ 3, 5, 3, 0, 0, 0, 0, 0, 0},
{ 4, 7, 5, 3, 0, 0, 0, 0, 0},
{ 4, 6, 5, 3, 0, 0, 0, 0, 0},
{ 5, 7, 6, 5, 3, 0, 0, 0, 0},
{ 3, 4, 3, 0, 0, 0, 0, 0, 0},
{ 4, 7, 4, 3, 0, 0, 0, 0, 0},
{ 4, 6, 4, 3, 0, 0, 0, 0, 0},
{ 5, 7, 6, 4, 3, 0, 0, 0, 0},
{ 4, 5, 4, 3, 0, 0, 0, 0, 0},
{ 5, 7, 5, 4, 3, 0, 0, 0, 0},
{ 5, 6, 5, 4, 3, 0, 0, 0, 0},
{ 6, 7, 6, 5, 4, 3, 0, 0, 0},
{ 2, 2, 0, 0, 0, 0, 0, 0, 0},
{ 3, 7, 2, 0, 0, 0, 0, 0, 0},
{ 3, 6, 2, 0, 0, 0, 0, 0, 0},
{ 4, 7, 6, 2, 0, 0, 0, 0, 0},
{ 3, 5, 2, 0, 0, 0, 0, 0, 0},
{ 4, 7, 5, 2, 0, 0, 0, 0, 0},
{ 4, 6, 5, 2, 0, 0, 0, 0, 0},
{ 5, 7, 6, 5, 2, 0, 0, 0, 0},
{ 3, 4, 2, 0, 0, 0, 0, 0, 0},
{ 4, 7, 4, 2, 0, 0, 0, 0, 0},
{ 4, 6, 4, 2, 0, 0, 0, 0, 0},
{ 5, 7, 6, 4, 2, 0, 0, 0, 0},
{ 4, 5, 4, 2, 0, 0, 0, 0, 0},
{ 5, 7, 5, 4, 2, 0, 0, 0, 0},
{ 5, 6, 5, 4, 2, 0, 0, 0, 0},
{ 6, 7, 6, 5, 4, 2, 0, 0, 0},
{ 3, 3, 2, 0, 0, 0, 0, 0, 0},
{ 4, 7, 3, 2, 0, 0, 0, 0, 0},
{ 4, 6, 3, 2, 0, 0, 0, 0, 0},
{ 5, 7, 6, 3, 2, 0, 0, 0, 0},
{ 4, 5, 3, 2, 0, 0, 0, 0, 0},
{ 5, 7, 5, 3, 2, 0, 0, 0, 0},
{ 5, 6, 5, 3, 2, 0, 0, 0, 0},
{ 6, 7, 6, 5, 3, 2, 0, 0, 0},
{ 4, 4, 3, 2, 0, 0, 0, 0, 0},
{ 5, 7, 4, 3, 2, 0, 0, 0, 0},
{ 5, 6, 4, 3, 2, 0, 0, 0, 0},
{ 6, 7, 6, 4, 3, 2, 0, 0, 0},
{ 5, 5, 4, 3, 2, 0, 0, 0, 0},
{ 6, 7, 5, 4, 3, 2, 0, 0, 0},
{ 6, 6, 5, 4, 3, 2, 0, 0, 0},
{ 7, 7, 6, 5, 4, 3, 2, 0, 0},
{ 2, 1, 0, 0, 0, 0, 0, 0, 0},
{ 3, 7, 1, 0, 0, 0, 0, 0, 0},
{ 3, 6, 1, 0, 0, 0, 0, 0, 0},
{ 4, 7, 6, 1, 0, 0, 0, 0, 0},
{ 3, 5, 1, 0, 0, 0, 0, 0, 0},
{ 4, 7, 5, 1, 0, 0, 0, 0, 0},
{ 4, 6, 5, 1, 0, 0, 0, 0, 0},
{ 5, 7, 6, 5, 1, 0, 0, 0, 0},
{ 3, 4, 1, 0, 0, 0, 0, 0, 0},
{ 4, 7, 4, 1, 0, 0, 0, 0, 0},
{ 4, 6, 4, 1, 0, 0, 0, 0, 0},
{ 5, 7, 6, 4, 1, 0, 0, 0, 0},
{ 4, 5, 4, 1, 0, 0, 0, 0, 0},
{ 5, 7, 5, 4, 1, 0, 0, 0, 0},
{ 5, 6, 5, 4, 1, 0, 0, 0, 0},
{ 6, 7, 6, 5, 4, 1, 0, 0, 0},
{ 3, 3, 1, 0, 0, 0, 0, 0, 0},
{ 4, 7, 3, 1, 0, 0, 0, 0, 0},
{ 4, 6, 3, 1, 0, 0, 0, 0, 0},
{ 5, 7, 6, 3, 1, 0, 0, 0, 0},
{ 4, 5, 3, 1, 0, 0, 0, 0, 0},
{ 5, 7, 5, 3, 1, 0, 0, 0, 0},
{ 5, 6, 5, 3, 1, 0, 0, 0, 0},
{ 6, 7, 6, 5, 3, 1, 0, 0, 0},
{ 4, 4, 3, 1, 0, 0, 0, 0, 0},
{ 5, 7, 4, 3, 1, 0, 0, 0, 0},
{ 5, 6, 4, 3, 1, 0, 0, 0, 0},
{ 6, 7, 6, 4, 3, 1, 0, 0, 0},
{ 5, 5, 4, 3, 1, 0, 0, 0, 0},
{ 6, 7, 5, 4, 3, 1, 0, 0, 0},
{ 6, 6, 5, 4, 3, 1, 0, 0, 0},
{ 7, 7, 6, 5, 4, 3, 1, 0, 0},
{ 3, 2, 1, 0, 0, 0, 0, 0, 0},
{ 4, 7, 2, 1, 0, 0, 0, 0, 0},
{ 4, 6, 2, 1, 0, 0, 0, 0, 0},
{ 5, 7, 6, 2, 1, 0, 0, 0, 0},
{ 4, 5, 2, 1, 0, 0, 0, 0, 0},
{ 5, 7, 5, 2, 1, 0, 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 quicksort__performance ( int lo , int hi, int ThreadNumber )
{
		int i = lo, j = hi;
		double x = FoundPatentsScore[ThreadNumber][ (lo+hi)/2 ];
		int iTempNumber;
		double dTempNumber;
		do
		{
				while ( FoundPatentsScore[ThreadNumber][i] > x ) i++;
				while ( FoundPatentsScore[ThreadNumber][j] < x ) j--;
				if ( i <= j )
				{

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

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


						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort__performance (lo,j,ThreadNumber);
		if ( i<hi) quicksort__performance (i,hi,ThreadNumber);
	
}


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 quicksort ( int lo , int hi )
{
		int i = lo, j = hi;
		double h;
		double x = FoundPatentsScore[ (lo+hi)/2 ];
		int TempNumber;
		do
		{
				while ( FoundPatentsScore[i] > x ) i++;
				while ( FoundPatentsScore[j] < x ) j--;
				if ( i <= j )
				{
						h = FoundPatentsScore[i];
						FoundPatentsScore[i] = FoundPatentsScore[j];
						FoundPatentsScore[j] = h;

						TempNumber = FoundPatents[i];
						FoundPatents[i] = FoundPatents[j];
						FoundPatents[j] = TempNumber;

						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort (lo,j);
		if ( i<hi) quicksort (i,hi);
	
}*/

void *Make_TFIDF_Indexing(void *ptr)
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0;
		long L_Start = 0;
		long L_End = 0;


		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;




		for ( i = L_Start; i < L_End; i++ )		
		{
				IDF = IDFValue[i];
				IDF = (double)((double)(((InputFileTotalPatents))) /(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].TFScore;
								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(void *ptr)
{
		int firstItem = 0;
		double TF = 0,SecondPart,SecondPart2;
		double IDF = 0;
		int i = 0,j=0;
 	    double Parameter  = 0.7;
	    double Parameter2  = 2000;

		long L_Start = 0;
		long L_End = 0;

		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;



		Parameter = Parameter_Value;


		for ( i = L_Start; i < L_End; i++ )		
		{
			//	IDF = (double)( (double)IDFValue[i]/(double)InputFileTotalPatents );

//				FoundPatentsCount = 0;
				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].TFScore;
	


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

								//if ( SecondPart == 0 )
								//	printf("\n hello222");



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




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


				}
		}

}



void Make_DirS_Indexing()
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0;
	    double Parameter  = 2000;



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

//				FoundPatentsCount = 0;
				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].TFScore;

								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(void *ptr)
{
		int firstItem = 0;
		double TF = 0,SecondPart;
		double IDF = 0;
		int i = 0,j=0;
	    double Parameter  = .7;
		long L_Start = 0;
		long L_End = 0;


		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;


		Parameter = Parameter_Value;


		for ( i = L_Start; i < L_End; i++ )		
		{
			//	IDF = (double)( (double)IDFValue[i]/(double)InputFileTotalPatents );

//				FoundPatentsCount = 0;
				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 ) );							  
								//IDF = (double) ( (double) (CollectionModel ) / IDF );							  


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

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






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




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


				}
		}

}

void *Make_SMART_Indexing(void *ptr)
{
		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;
		long L_Start = 0;
		long L_End = 0;
		

		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;




		for ( i = L_Start; i < L_End; i++ )		
		{
				IDF = IDFValue[i];
				IDF = (double)((double)(((InputFileTotalPatents))) /(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].TFScore;
								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)(InputFileTotalPatents+1)/(double)IDFValue[i]);

								AvgTF = AvgTF*TF;



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



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


	
				}
		}

}


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


		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;


		for ( i = L_Start; i < L_End; i++ )		
		{
				IDF = IDFValue[i];
				IDF = (double)((double)(((InputFileTotalPatents))) /(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].TFScore;
							//	TF = (double) ( (double)TF/(double)fullPatentLength[InvertedItemsNumbers[i][firstItem].DocumentNumber]);

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


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


	
				}
		}

}

void *Make_AbsDis_Indexing(void *ptr)
{
		int firstItem = 0;
		double TF = 0,SecondPart;
		double IDF = 0;
		int i = 0,j=0;
	    double Parameter  = 0.7;
		long L_Start;
		long L_End;

		


		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;




		Parameter = Parameter_Value;

		for ( i = L_Start; i < L_End; i++ )
		{
			

//				FoundPatentsCount = 0;
				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].TFScore;
								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%d...",fullPatentUniqueItems[InvertedItemsNumbers[i][firstItem].DocumentNumber]);

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




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


				}
		}

}


void *Make_BM25_Indexing(void *ptr)
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0;
        double k = 2.0;
        double b = 0.75;
		double IDFUpper, IDFLower;
  		double WeightedLength = 0;
        double MainUpper = 0;
        double MainLower = 0;
        double Main = 0;
		long L_Start = 0;
		long L_End = 0;



		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;


		b = Parameter_Value;
	

		for ( i = L_Start; i < L_End; i++ )		
		{
			//	IDF = IDFValue[i];
			//	IDF = (double)((double)(((InputFileTotalPatents))) /(double)IDF);
			//	IDF = (double)((double)log(IDF)/(double)log(2));

//				FoundPatentsCount = 0;
				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].TFScore;
							//	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)(InputFileTotalPatents))- (double)IDF + (double)0.5);
								IDFLower = (double)IDF + (double)0.5;
								IDF = (double) (IDFUpper/IDFLower);
								IDF = (double)((double)log(IDF)/(double)log(2));
								//IDF = (double)((double)log(IDF));

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






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




				}
		}

}


void *Make_LM2000_Indexing(void *ptr)
{
		int firstItem = 0;
		double TF = 0;
		double IDF = 0;
		int i = 0,j=0;
        double k = 2.0;
        double b = .75;
		double IDFUpper;
  		double WeightedLength = 0;
        double MainUpper = 0;
        double MainLower = 0;
        double Main = 0;

		long L_Start =0;
		long L_End = 0;


		thdata *data;            
		data = (thdata *) ptr;  

		L_Start =  data->ThreadNumber;
		L_End = data->Local_GlobalPostingAge;


		for ( i = L_Start; i < L_End; i++ )		
		{

//				FoundPatentsCount = 0;
				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].TFScore;								
								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 Make_Weighting_With_Threads()
{

	pthread_t thread1[200];  
	thdata data1[200];

	long Partition_Maximum;
	long Start = 0;
	long End;
	int i = 0;


	Partition_Maximum = ((TotalItems+2)/50);

	for ( i = 0; i < 50; i++ )
	{
			End = Start+Partition_Maximum;


			data1[i].ThreadNumber = Start;
			data1[i].Local_GlobalPostingAge = End;

			if ( RSType == 1 )
				pthread_create (&thread1[i], NULL, (void *) &Make_TFIDF_Indexing, (void *) &data1[i]);									
			if ( RSType == 2 )
				pthread_create (&thread1[i], NULL, (void *) &Make_BM25_Indexing, (void *) &data1[i]);									
			if ( RSType == 3 )
				pthread_create (&thread1[i], NULL, (void *) &Make_LM2000_Indexing, (void *) &data1[i]);									
			if ( RSType == 4 )
				pthread_create (&thread1[i], NULL, (void *) &Make_JM_Indexing, (void *) &data1[i]);									
			if ( RSType == 5 )
				pthread_create (&thread1[i], NULL, (void *) &Make_AbsDis_Indexing, (void *) &data1[i]);									
			if ( RSType == 6 )
				pthread_create (&thread1[i], NULL, (void *) &Make_TwoStage_Indexing, (void *) &data1[i]);									
			if ( RSType == 7 )
				pthread_create (&thread1[i], NULL, (void *) &Make_UnNormTFIDF_Indexing, (void *) &data1[i]);									
			if ( RSType == 8 )
				pthread_create (&thread1[i], NULL, (void *) &Make_SMART_Indexing, (void *) &data1[i]);									


			if ( RSType == 10 )
				pthread_create (&thread1[i], NULL, (void *) &Make_JM_Indexing, (void *) &data1[i]);									

			if ( RSType == 11 )
				pthread_create (&thread1[i], NULL, (void *) &Make_JM_Indexing, (void *) &data1[i]);									


			if ( RSType == 12 )
				pthread_create (&thread1[i], NULL, (void *) &Make_JM_Indexing, (void *) &data1[i]);									


			if ( RSType == 21 )
				pthread_create (&thread1[i], NULL, (void *) &Make_JM_Indexing, (void *) &data1[i]);									

			Start = End;
	}


	
												
												

										
	
	for ( i = 0; i  < 50; i++ )
		pthread_join(thread1[i], NULL);


	

	
}


void MAREC_Seperation(long clusterNumber)
{
	FILE *itemfptr,*fullTextfptr;
	char itemFileName[300];
	char rowFileName[300];
	char fullTextFileName[300];

	long i, j,k;
	long ItemNumber = 0;
	long eof = 0;
	long RowCount = 0;
	long BlockCount = 0;

	long IntLength = 0;
	long IntervalCount = 0;
	long LastItem = 0;
	long Difference = 0;
	long ANDValue = 65535;
	long idfcount = 0;
	char printText[300];
	int PostingCounter = 0;
	int partitionN = 0;
	 
	



	sprintf(fullTextFileName,"%spatentsPATHCluster%d.txt",sourcePath,clusterNumber);
	fullTextfptr = fopen(fullTextFileName,"r");



	IDFValue = (long *)malloc((410000)*sizeof(long));
	for ( i = 0; i < 410000; i++ )
		IDFValue[i] = 0;

	sprintf(fullTextFileName,"%sDoc_ID.txt",sourcePath);
	itemfptr = fopen(fullTextFileName,"r");
	while(1)
	{
			if ( fscanf (itemfptr,"%d",&partitionN) == EOF )
				break;

			IDFValue[partitionN] = 1;
	}
	fclose(itemfptr);















	sprintf(fullTextFileName,"%sDoc_Path_ID_EP.txt",sourcePath);
	itemfptr = fopen(fullTextFileName,"w");




	RowCount = 0;
	IntervalCount = 0;
	Patent_QRelevance_Count = 0;
	while(1)
	{
			if ( fscanf(fullTextfptr,"%s",printText) == EOF )
					break;

			if ( RowCount >= 100000 && RowCount < 200000 )
			{
					if ( IDFValue[RowCount] == 1 )
							fprintf(itemfptr,"%s\n",printText);
			}

	
			RowCount++;

				

	}
	Patent_QRelevance_Count = 0;

	exit(1);
	fclose(fullTextfptr);
	if ( itemfptr != NULL )
		fclose(itemfptr);





/*

	sprintf(fullTextFileName,"%sfullText%d_WO.txt",sourcePath,clusterNumber);
	itemfptr = fopen(fullTextFileName,"w");




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

			if ( ItemNumber != -17  )
			{
					if ( RowCount >= 300000 && RowCount < 400000 )
					{
						    if ( IntLength > 0 && IDFValue[RowCount] == 1 )
								fprintf(itemfptr,"%ld %ld ",ItemNumber,IntLength);

							IntervalCount++;
					}
			}

			if ( ItemNumber == -17 )
			{

					

					if ( RowCount >= 300000 && RowCount < 400000 )
					{
//							if ( IntervalCount > 30000 )
//								printf("\n%d...%d",IntervalCount,RowCount);
							if ( IDFValue[RowCount] == 1  )
									fprintf(itemfptr,"-17 -17\n");	
					}
					IntervalCount = 0;


					RowCount++;
					if ( RowCount >= 400000 )
					{																	
							fclose(itemfptr);
							break;								
					}
					if ( RowCount%10000 == 0 )
						printf("\n Docs Reading = %d",RowCount);

			}
				

	}
	Patent_QRelevance_Count = 0;

	exit(1);
	fclose(fullTextfptr);
	if ( itemfptr != NULL )
		fclose(itemfptr);
*/




}


void Partition_Big_File(long clusterNumber,long patentsCopyingFrom, long patentsCopyingTo)
{
	FILE *itemfptr,*fullTextfptr;
	char itemFileName[300];
	char rowFileName[300];
	char fullTextFileName[300];

	long i, j,k;
	long ItemNumber = 0;
	long eof = 0;
	long RowCount = 0;
	long BlockCount = 0;

	long IntLength = 0;
	long IntervalCount = 0;
	long LastItem = 0;
	long Difference = 0;
	long ANDValue = 65535;
	long idfcount = 0;
	char printText[300];
	int PostingCounter = 0;
	int partitionN = 0;
	 


	TermCollectionSum = (long *)malloc((1440000)*sizeof(long));
	for ( i = 0; i < 1440000; i++ )
		TermCollectionSum[i] = 0;
	


	RowCount = 0;
	sprintf(fullTextFileName,"%sClusters//TopDocs.txt",sourcePath);
	fullTextfptr = fopen(fullTextFileName,"r");
	while(1)
	{
			if ( fscanf(fullTextfptr,"%d",&k) == EOF )
				break;

			if ( TermCollectionSum[k] == 0 )
				RowCount++;

			TermCollectionSum[k] = 1;			
	}
	fclose(fullTextfptr);

	printf("\n%d...\n",RowCount);

	


	srand(time(NULL));
	k = 0;
	while(1)
	{
			
			
			if ( TermCollectionSum[k] == 0 )
			{
					TermCollectionSum[k] = 1;
					RowCount++;
			}
			k++;

			if ( RowCount >= 430000 )
				break;
	}





	sprintf(itemFileName,"%sItemsetProcessing%d.txt",sourcePath,clusterNumber);
	itemfptr = fopen(itemFileName,"r");
	fscanf(itemfptr,"%d",&InputFileTotalPatents);
	fscanf(itemfptr,"%d",&TotalItems);
	TotalItems = TotalItems + 3;
	IDFValue = (long *)malloc((TotalItems)*sizeof(long));
	for ( i = 0; i < TotalItems; i++ )
		IDFValue[i] = 0;

	fclose(itemfptr);
	


	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	fullTextfptr = fopen(fullTextFileName,"r");


	sprintf(fullTextFileName,"%sfullText%d_part%d.txt",sourcePath,clusterNumber,partitionN);
	itemfptr = fopen(fullTextFileName,"w");



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

			if ( ItemNumber != -17 && ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo) )
			{
					
				     if ( IntLength > 0 && TermCollectionSum[RowCount] == 1 ) 
					 {
							fprintf(itemfptr,"%ld %ld ",ItemNumber,IntLength);
							IDFValue[ItemNumber]++;
							AveragePatentLength += IntLength;
					 }
			}

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{
							if ( TermCollectionSum[RowCount] == 1 )
							{
									IntervalCount++;
									fprintf(itemfptr,"-17 -17\n");	
									if ( (IntervalCount%14000) == 0 )
									{																	
											fclose(itemfptr);										
											partitionN++;
											//if ( partitionN == 2 )
											//	exit(1);
											sprintf(fullTextFileName,"%sfullText%d_part%d.txt",sourcePath,clusterNumber,partitionN);
											itemfptr = fopen(fullTextFileName,"w");
									}
							}
							RowCount++;
							if ( RowCount%10000 == 0 )
								printf("\n Reading Completed = %d",RowCount);

	
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;

	AveragePatentLength = (double)((double)AveragePatentLength/(double)400000);
	printf("\nAvg Patent Length = %lf\n",AveragePatentLength);


	fclose(fullTextfptr);
	fclose(itemfptr);





	sprintf(itemFileName,"%sIDFValues.txt",sourcePath,clusterNumber);
	itemfptr = fopen(itemFileName,"w");
	for ( j = 0; j < TotalItems; j++ )
	{
			fprintf(itemfptr,"%ld\n",IDFValue[j]);
			
	}
	fclose(itemfptr);





}
void *ParallelFileReading(void *ptr)
{
	long RowCount = 0;
	char fullTextFileName[300];
	FILE *fullTextfptr;
	long ItemNumber,IntLength;
	int clusterNumber;
	int ThreadNumber;


	thdata *data;            
	data = (thdata *) ptr;  


	clusterNumber = data->Local_GlobalPostingAge;
	ThreadNumber = data->ThreadNumber;





	sprintf(fullTextFileName,"%sfullText%d_part%d.txt",sourcePath,clusterNumber,ThreadNumber);
	fullTextfptr = fopen(fullTextFileName,"r");

	
	

	if ( fullTextfptr == NULL )
		return;

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

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

			if ( ItemNumber != -17  )
			{

				     if ( IntLength > 0 )
					 {
							IDFValue_Part[ThreadNumber][ItemNumber]++;
							DocTermsArray_TotalTerms_Part[ThreadNumber][RowCount]+=IntLength;
					 }
			}

			if ( ItemNumber == -17 )
			{
					RowCount++;
					if (RowCount%10000 == 0 )
						printf("\nIndexing...%d.....%d",RowCount,ThreadNumber);	

			}
				

	}
	fclose(fullTextfptr);

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

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

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


						i++;
						j--;
				}

		}
		while (i<=j);

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

void SOMLib_File_Coversion(long clusterNumber,long patentsCopyingFrom, long patentsCopyingTo)
{
	FILE *itemfptr,*fullTextfptr,*doc_path,*find_score;
	char itemFileName[300];
	char rowFileName[300];
	char fullTextFileName[300];
	char doc_path_text[300];

	long i, j,k;
	long ItemNumber = 0;
	long eof = 0;
	long RowCount = 0;
	long BlockCount = 0;

	long IntLength = 0;
	long IntervalCount = 0;
	long LastItem = 0;
	long Difference = 0;
	long ANDValue = 65535;
	long idfcount = 0;
	char printText[300];
	int PostingCounter = 0;
	int partitionN = 0;
	double IDF,TF;
	int Find5;
	int Find10;
	int Find15;
	int Find20;
	int Find25;
	int TQueries;
	long TotalDimensions = 0;
	long *term_min;
	long *term_max;
	long *term_collection_freq;

	 

	




	sprintf(itemFileName,"%sItemsetProcessing%d.txt",sourcePath,clusterNumber);
	itemfptr = fopen(itemFileName,"r");
	fscanf(itemfptr,"%d",&InputFileTotalPatents);
	fscanf(itemfptr,"%d",&TotalItems);



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

	term_min = (long *)malloc((TotalItems)*sizeof(long));;
	term_max = (long *)malloc((TotalItems)*sizeof(long));;
	term_collection_freq = (long *)malloc((TotalItems)*sizeof(long));;

	for ( i = 0; i < TotalItems; i++ )
	{
		IDFValue[i] = 0;
		TermCollectionSum[i] = 0;
		term_min[i] = 9999999;
		term_max[i] = 0;
		term_collection_freq[i] = 0;
	}

	fclose(itemfptr);

	fullPatentLength = (long *)malloc((InputFileTotalPatents+3)*sizeof(long));
	for ( i = 0; i < InputFileTotalPatents; i++ )
		fullPatentLength[i] = 0;
	


	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	fullTextfptr = fopen(fullTextFileName,"r");
	AveragePatentLength = 0;
	RowCount = 0;
	IntervalCount = 0;
	Patent_QRelevance_Count = 0;
	while(1)
	{
			if ( fscanf(fullTextfptr,"%ld %ld",&ItemNumber,&IntLength) == EOF )
					break;

			if ( ItemNumber != -17 && ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo) )
			{
					
				     if ( IntLength > 0  ) 
					 {							
							IDFValue[ItemNumber]++;
							term_collection_freq[ItemNumber] += IntLength;
							if ( term_min[ItemNumber] > IntLength )
								term_min[ItemNumber] = IntLength;
							if ( term_max[ItemNumber] < IntLength )
								term_max[ItemNumber] = IntLength;


							fullPatentLength[RowCount] += IntLength;
							if ( RowCount < 35200 )
									TermCollectionSum[ItemNumber]++;
							
					 }
			}

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{
							RowCount++;
							if ( RowCount%10000 == 0 )
								printf("\n Reading Completed = %d",RowCount);

	
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);




	//Construct SOM Vectors

	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	fullTextfptr = fopen(fullTextFileName,"r");


	sprintf(fullTextFileName,"%sSOMLib_TFIDF%d.txt",sourcePath,clusterNumber);
	itemfptr = fopen(fullTextFileName,"w");


	sprintf(fullTextFileName,"%sDocuments_IDs.txt",sourcePath);
	doc_path = fopen(fullTextFileName,"r");


	sprintf(fullTextFileName,"%sWorkingFolder_real//AND_Queries_Results//TFIDF_extra.txt",sourcePath);
	find_score = fopen(fullTextFileName,"r");

	TotalDimensions = 0;
	for ( i = 0; i < TotalItems; i++ )
	{
			if (TermCollectionSum[i] > 0 )
				TotalDimensions++;
	}

	fprintf(itemfptr,"$TYPE vec_tfxidf\n");
	fprintf(itemfptr,"$XDIM %ld\n",TotalDimensions);
	fprintf(itemfptr,"$YDIM 3\n");
	fprintf(itemfptr,"$VEC_DIM 34205\n");


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

			if ( ItemNumber != -17 && ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo) )
			{
					
				     if ( IntLength > 0 && RowCount >= 995 ) 
					 {							
							
							IDF = IDFValue[ItemNumber];
							IDF = (double)((double)(((InputFileTotalPatents))) /(double)IDF);
							IDF = (double)((double)log(IDF)/(double)log(2));

								
							TF = IntLength;
							TF = (double) ( (double)TF/(double)fullPatentLength[RowCount]);											


							TF = TF*IDF;
							
							fprintf(itemfptr,"%d:%lf ",ItemNumber,TF);
											
							
					 }
			}

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{
							fscanf(doc_path,"%s",doc_path_text);
							for ( i = strlen(doc_path_text)-1; i >= 0; i-- )
							{
									if ( doc_path_text[i] == '\\' )
									{
											j = i;
											break;
									}
							}
							for ( i = j+1; i < strlen(doc_path_text); i++ )
							{
									if ( RowCount >= 995 )
										fprintf(itemfptr,"%c",doc_path_text[i] );
							}

							if ( RowCount >= 995 )
								fprintf(itemfptr," ");




							fscanf(find_score,"%d	%d	%d	%d	%d	%d",&Find5,&Find10,&Find15,&Find20,&Find25,&TQueries);
							if ( RowCount >= 995 )
								fprintf(itemfptr,"%d	%lf",Find10,(double)((double)Find10/(double)TQueries));
							

							


							if ( RowCount >= 995 )
								fprintf(itemfptr,"\n");

							RowCount++;
							if ( RowCount%10000 == 0 )
								printf("\n Reading Completed = %d",RowCount);

							if ( RowCount >= 35200 )
								break;
	
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);
	close(itemfptr);
	fclose(doc_path);
	fclose(find_score);




	sprintf(fullTextFileName,"%sSOMLib_Attribute_Structure%d.txt",sourcePath,clusterNumber);
	itemfptr = fopen(fullTextFileName,"w");

	fprintf(itemfptr,"$TYPE Attribute_Structure\n");
	fprintf(itemfptr,"$XDIM 7\n");
	fprintf(itemfptr,"$YDIM 1\n");
	fprintf(itemfptr,"$VEC_DIM %ld\n",TotalDimensions);

	sprintf(fullTextFileName,"%sTermID_Word_Mapping.txt",sourcePath);
	doc_path = fopen(fullTextFileName,"r");

	
	while(1)
	{
			if ( fscanf(doc_path,"%ld	%s",&ItemNumber,doc_path_text) == EOF )
				break;

			if ( TermCollectionSum[ItemNumber] > 0 )
			{
					fprintf(itemfptr,"%ld	%s	%ld	%ld	%ld	%ld %lf\n",ItemNumber,doc_path_text,IDFValue[ItemNumber],term_collection_freq[ItemNumber],term_min[ItemNumber],term_max[ItemNumber],(double)((double)term_collection_freq[ItemNumber]/(double)IDFValue[ItemNumber]));
			}
	}

	fclose(itemfptr);
	fclose(doc_path);






}


void quicksort__lengthRelevance ( long lo , long hi)
{
		long k = 0;
		long i = lo, j = hi;
		long x = fullPatentLength[ (lo+hi)/2 ];
		long dTempNumber;
		double fTempNumber;
		do
		{
				while ( fullPatentLength[i] < x ) i++;
				while ( fullPatentLength[j] > x ) j--;
				if ( i <= j )
				{

						dTempNumber = TermCollectionSum[i];
						TermCollectionSum[i] = TermCollectionSum[j];
						TermCollectionSum[j] = dTempNumber;

						dTempNumber = fullPatentLength[i];
						fullPatentLength[i] = fullPatentLength[j];
						fullPatentLength[j] = dTempNumber;

						for ( k = 0; k < 10; k++ )
						{

								fTempNumber = surface_features[k][i];
								surface_features[k][i] = surface_features[k][j];
								surface_features[k][j] = fTempNumber;
						}




						i++;
						j--;
				}

		}
		while (i<=j);

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


void LengthRelevanceProbability(long clusterNumber,long patentsCopyingFrom, long patentsCopyingTo)
{
	FILE *itemfptr,*fullTextfptr,*doc_path,*find_score;
	char itemFileName[300];
	char rowFileName[300];
	char fullTextFileName[300];
	char doc_path_text[300];

	long i, j,k;
	long ItemNumber = 0;
	long eof = 0;
	long RowCount = 0;
	long BlockCount = 0;

	long IntLength = 0;
	long IntervalCount = 0;
	long LastItem = 0;
	long Difference = 0;
	long ANDValue = 65535;
	long idfcount = 0;
	char printText[300];
	int PostingCounter = 0;
	int partitionN = 0;
	double IDF,TF;
	int Find5;
	int Find10;
	int Find15;
	int Find20;
	int Find25;
	int TQueries;
	long TotalDimensions = 0;
	long *term_min;
	long *term_max;
	long *term_collection_freq;

	 

	




	sprintf(itemFileName,"%sItemsetProcessing%d.txt",sourcePath,clusterNumber);
	itemfptr = fopen(itemFileName,"r");
	fscanf(itemfptr,"%d",&InputFileTotalPatents);
	fscanf(itemfptr,"%d",&TotalItems);
	fclose(itemfptr);



	fullPatentLength = (long *)malloc((InputFileTotalPatents+3)*sizeof(long));
	IDFValue = (long *)malloc((InputFileTotalPatents+3)*sizeof(long));
	TermCollectionSum = (long *)malloc((InputFileTotalPatents+3)*sizeof(long));
	for ( i = 0; i < InputFileTotalPatents; i++ )
	{
			fullPatentLength[i] = 0;
			IDFValue[i] = 0;
			TermCollectionSum[i] = 0;
	}
	


	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	fullTextfptr = fopen(fullTextFileName,"r");
	AveragePatentLength = 0;
	RowCount = 0;
	IntervalCount = 0;
	Patent_QRelevance_Count = 0;
	while(1)
	{
			if ( fscanf(fullTextfptr,"%ld %ld",&ItemNumber,&IntLength) == EOF )
					break;

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

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{
						
							IDFValue[RowCount] = IntervalCount;

							//IntervalCount = IntervalCount/50;

							fullPatentLength[RowCount] = IntervalCount;

							IntervalCount = 0;
							RowCount++;
							if ( RowCount%10000 == 0 )
								printf("\n Reading Completed = %d",RowCount);

	
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);



	AveragePatentLength = 0;

	sprintf(fullTextFileName,"%sCitationList.txt",sourcePath);
	fullTextfptr = fopen(fullTextFileName,"r");
	RowCount = 0;
	while(1)
	{
			if ( fscanf(fullTextfptr,"%d",&Find5) == EOF )
				break;


			if ( Find5 != -17 )
			{
					IntervalCount = IDFValue[Find5];
				//	IntervalCount = IntervalCount/50;
					TermCollectionSum[Find5]++;	
					AveragePatentLength ++;
				
			}
	}
	fclose(fullTextfptr);


//	InputFileTotalPatents = patentsCopyingTo;

	quicksort__lengthRelevance(0,InputFileTotalPatents-1);
	IntervalCount = InputFileTotalPatents/30;
	
	IDF = 0;
	TF = 0;
	sprintf(fullTextFileName,"%sLength_Relevance_Distribution.txt",sourcePath);
	fullTextfptr = fopen(fullTextFileName,"w");
	for ( i = 0; i < InputFileTotalPatents; i ++ )
	{
			IDF += fullPatentLength[i];
			TF += TermCollectionSum[i];

			if ( i%IntervalCount == 0 )
			{
					IDF = (double) ( (double) IDF / (double) IntervalCount );
					TF = (double) ( (double) TF / (double) AveragePatentLength );

					fprintf(fullTextfptr,"%lf	%lf\n", IDF , TF );

					IDF = 0;
					TF = 0;

			}
	}
	fclose(fullTextfptr);






}







void LengthKNNScoreProbability(long clusterNumber,long patentsCopyingFrom, long patentsCopyingTo)
{
	FILE *itemfptr,*fullTextfptr,*doc_path,*find_score;
	char itemFileName[300];
	char rowFileName[300];
	char fullTextFileName[300];
	char doc_path_text[300];

	long i, j,k;
	long ItemNumber = 0;
	long eof = 0;
	long RowCount = 0;
	long BlockCount = 0;

	long IntLength = 0;
	long IntervalCount = 0;
	long LastItem = 0;
	long Difference = 0;
	long ANDValue = 65535;
	long idfcount = 0;
	char printText[300];
	int PostingCounter = 0;
	int partitionN = 0;
	double IDF,TF;
	int Find5;
	int Find10;
	int Find15;
	int Find20;
	int Find25;
	int TQueries;
	long TotalDimensions = 0;
	long *term_min;
	long *term_max;
	long *term_collection_freq;


	double *summ;

	 

	




	sprintf(itemFileName,"%sItemsetProcessing%d.txt",sourcePath,clusterNumber);
	itemfptr = fopen(itemFileName,"r");
	fscanf(itemfptr,"%d",&InputFileTotalPatents);
	fscanf(itemfptr,"%d",&TotalItems);
	fclose(itemfptr);



	fullPatentLength = (long *)malloc((InputFileTotalPatents+3)*sizeof(long));
	IDFValue = (long *)malloc((InputFileTotalPatents+3)*sizeof(long));
	TermCollectionSum = (long *)malloc((InputFileTotalPatents+3)*sizeof(long));
	doc_avg_dl = (double *)malloc((InputFileTotalPatents+3)*sizeof(double));
	surface_features = (double *)malloc((15)*sizeof(double));
	summ = (double *)malloc((15)*sizeof(double));
	for ( i = 0; i < 15; i++ )
		surface_features[i] = (double *)malloc((InputFileTotalPatents+3)*sizeof(double));
		
	for ( i = 0; i < InputFileTotalPatents; i++ )
	{
			fullPatentLength[i] = 0;
			IDFValue[i] = 0;
			TermCollectionSum[i] = 0;
			doc_avg_dl[i] = 0;
			
	}
	


	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	fullTextfptr = fopen(fullTextFileName,"r");
	AveragePatentLength = 0;
	RowCount = 0;
	IntervalCount = 0;
	Patent_QRelevance_Count = 0;
	while(1)
	{
			if ( fscanf(fullTextfptr,"%ld %ld",&ItemNumber,&IntLength) == EOF )
					break;

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

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{
						
							IDFValue[RowCount] = IntervalCount;

							//IntervalCount = IntervalCount/50;

							fullPatentLength[RowCount] = IntervalCount;

							IntervalCount = 0;
							RowCount++;
							if ( RowCount%10000 == 0 )
								printf("\n Reading Completed = %d",RowCount);

	
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);





	AveragePatentLength = 0;
/*	sprintf(fullTextFileName,"%sSurface_DocsFeatures.txt",sourcePath);
	fullTextfptr = fopen(fullTextFileName,"r");
	RowCount = 0;
	while(1)
	{
			for ( i = 0; i < 10; i ++ )
			{
					if ( fscanf(fullTextfptr,"%lf",&TF) == EOF )
						break;

					surface_features[i][RowCount] = TF;				
			}
			RowCount++;
			if ( RowCount >= InputFileTotalPatents )
				break;
			
	}
	fclose(fullTextfptr);*/


	sprintf(fullTextFileName,"%sKNN_Prediction_LowHigh_TFIDF.txt",sourcePath);
	fullTextfptr = fopen(fullTextFileName,"r");
	RowCount = 0;
	while(1)
	{
			if ( fscanf(fullTextfptr,"%lf",&TF) == EOF )
				break;

			surface_features[9][RowCount] = TF;				
			
			RowCount++;
			if ( RowCount >= InputFileTotalPatents )
				break;
			
	}
	fclose(fullTextfptr);




	InputFileTotalPatents = patentsCopyingTo;


	quicksort__lengthRelevance(0,InputFileTotalPatents-1);
	IntervalCount = InputFileTotalPatents/1000;
	
	IDF = 0;
	TF = 0;
	for ( j = 0; j < 10; j ++ )
		summ[j] = 0;

	sprintf(fullTextFileName,"%sLength_Relevance_Distribution.txt",sourcePath);
	fullTextfptr = fopen(fullTextFileName,"w");
	for ( i = 0; i < InputFileTotalPatents; i ++ )
	{
			IDF += fullPatentLength[i];
			for ( j = 0; j < 10; j ++ )
				summ[j] += surface_features[j][i];

			if ( i%IntervalCount == 0 )
			{
					IDF = (double) ( (double) IDF / (double) IntervalCount );

					
					fprintf(fullTextfptr,"%lf	", IDF );
					for ( j = 0; j < 10; j++ )
					{

							summ[j] = (double) ( (double) summ[j] / (double) IntervalCount );
							fprintf(fullTextfptr,"%lf	",summ[j]);
							summ[j] = 0;
					}
					fprintf(fullTextfptr,"\n");



					IDF = 0;
					TF = 0;

			}
	}
	fclose(fullTextfptr);






}


void ReadItemFile(long clusterNumber,long patentsCopyingFrom, long patentsCopyingTo)
{
	FILE *itemfptr,*fullTextfptr;
	char itemFileName[300];
	char rowFileName[300];
	char fullTextFileName[300];
//	char wordNumberFileName[300];
//	char IDFFileName[300];

	long i, j,k;
	long ItemNumber = 0;
	long eof = 0;
	long RowCount = 0;
	long BlockCount = 0;

	long IntLength = 0;
	long IntervalCount = 0;
	long LastItem = 0;
	long Difference = 0;
	long ANDValue = 65535;
	long idfcount = 0;
	char printText[300];
	int PostingCounter = 0;
	 
	pthread_t thread1[200];  
	thdata data1[200];



	TarD = 45000;

	sprintf(itemFileName,"%sItemsetProcessing%d.txt",sourcePath,clusterNumber);
	sprintf(fullTextFileName,"%sfullText%d.txt",sourcePath,clusterNumber);
	itemfptr = fopen(itemFileName,"r");
	fullTextfptr = fopen(fullTextFileName,"r");


	fscanf(itemfptr,"%d",&InputFileTotalPatents);
	fscanf(itemfptr,"%d",&TotalItems);
	TotalItems++;
	if ( patentsCopyingTo > InputFileTotalPatents )
	{	
			patentsCopyingTo = InputFileTotalPatents;
	}
	InputFileTotalPatents = (patentsCopyingTo-patentsCopyingFrom);


	


	RowCount = 0;

	
/*

	IDFValue_Part = (long *)malloc((36)*sizeof(long));
	DocTermsArray_TotalTerms_Part = (long *)malloc((36)*sizeof(long));
	for ( i = 0; i < 36; i++ )
	{
			IDFValue_Part[i] = (long *)malloc((TotalItems+2)*sizeof(long));			
			DocTermsArray_TotalTerms_Part[i] = (long *)malloc((InputFileTotalPatents)*sizeof(long));			
			for ( j = 0; j < TotalItems+2; j++ )
				IDFValue_Part[i][j] = 0;

			for ( j = 0; j < InputFileTotalPatents; j++ )
				DocTermsArray_TotalTerms_Part[i][j] = 0;
	}

*/

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


	DocTermsArray_TotalTerms = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	fullPatentLength_AllOver = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	fullPatentLength_RetrievalProbability = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	for ( j = 0; j < InputFileTotalPatents; j++ )
	{
			DocTermsArray_TotalTerms[j] = 0;
			fullPatentLength_AllOver[j] = 0;
			fullPatentLength_RetrievalProbability[j] = 0;
	}





	
	IntervalCount = 0;
	Patent_QRelevance_Count = 0;	

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

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

				     if ( IntLength > 0 )
					 {
							DocTermsArray_TotalTerms[RowCount]++;
							fullPatentLength_AllOver[IntervalCount]++;
							IDFValue[ItemNumber]++;


					 }
			}

			if ( ItemNumber == -17 )
			{
					if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
					{
							if ( DocTermsArray_TotalTerms[RowCount] > 0 )
								RowCount++;
							else
								InputFileTotalPatents--;

							IntervalCount++;

							if (RowCount%10000 == 0 )
								printf("\nReading Documents %d",RowCount);
	
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);
	

	//(long IntervalCount, long Patent_QRelevance_Count,int ThreadNumber)

	/*
	for ( i = 0; i < 36; i++ )
	{
			data1[i].Local_GlobalPostingAge = clusterNumber;
			data1[i].ThreadNumber = i;
			pthread_create (&thread1[i], NULL, (void *) &ParallelFileReading, (void *) &data1[i]);									
	}

	for ( i = 0; i < 36; i++ )
			pthread_join(thread1[i], NULL);



	

	for ( i = 0; i < 36; i ++ )
	{
			for ( j = 0; j < TotalItems+2; j++ )
					IDFValue[j] += IDFValue_Part[i][j];

			for ( j = 0; j < InputFileTotalPatents; j++ )
					DocTermsArray_TotalTerms[j] += DocTermsArray_TotalTerms_Part[i][j];			
	}*/




/*
	AveragePatentLength = 0;
	fclose(itemfptr);
	sprintf(itemFileName,"%sIDFValues.txt",sourcePath,clusterNumber);
	itemfptr = fopen(itemFileName,"w");

	for ( j = 0; j < TotalItems+2; j++ )
	{
			fprintf(itemfptr,"%ld\n",IDFValue[j]);
			
	}
	for ( j = 0; j < InputFileTotalPatents; j++ )
	{
			AveragePatentLength += DocTermsArray_TotalTerms[j];
	}

	fclose(itemfptr);

	AveragePatentLength = (double)((double)AveragePatentLength/(double)InputFileTotalPatents);
	printf("\n\n Avg Docs Length = %lf\n\n",AveragePatentLength);




	exit(1);
	*/


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

	
	doc_avg_dl = (double *)malloc((InputFileTotalPatents+3)*sizeof(double));

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


	PostingScore = (double *)malloc((200)*sizeof(double));
	PostingTFScore = (double *)malloc((200)*sizeof(double));
	PostingQueryTermsPresence = (long *)malloc((200)*sizeof(long));
	PostingAge = (long *)malloc((200)*sizeof(long));
	FoundPatents = (long *)malloc((200)*sizeof(long));
	FoundPatentsScore = (double *)malloc((200)*sizeof(double));
	PatentRMScore  = (double *)malloc((200)*sizeof(double));
	FoundPatentsTFScore = (double *)malloc((200)*sizeof(double));
	TermPresence = (long *)malloc((55)*sizeof(long));


	for ( j = 0; j < 55; j++ )
	{
			PostingScore[j] = (double *)malloc((InputFileTotalPatents)*sizeof(double));
			PostingTFScore[j] = (double *)malloc((InputFileTotalPatents)*sizeof(double));
			PostingQueryTermsPresence[j] = (long *)malloc((InputFileTotalPatents)*sizeof(long));
			PostingAge[j] = (long *)malloc((InputFileTotalPatents)*sizeof(long));
			FoundPatents[j] = (long *)malloc((InputFileTotalPatents)*sizeof(long));
			FoundPatentsScore[j] = (double *)malloc((InputFileTotalPatents)*sizeof(double));
			PatentRMScore[j] = (double *)malloc((InputFileTotalPatents)*sizeof(double));
			FoundPatentsTFScore[j] = (double *)malloc((InputFileTotalPatents)*sizeof(double));

			if ( j < 55 )
				TermPresence[j] = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	}


	b_prefFoundQP = (double *)malloc((InputFileTotalPatents)*sizeof(double));
	b_prefTotalQP = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	MAPFoundQP = (double *)malloc((InputFileTotalPatents)*sizeof(double));
	MAPTotalQP = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	RecallFoundQP = (double *)malloc((InputFileTotalPatents)*sizeof(double));
	RecallTotalQP = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	PrecisionFoundQP = (double *)malloc((InputFileTotalPatents)*sizeof(double));
	PrecisionTotalQP = (long *)malloc((InputFileTotalPatents)*sizeof(long));

	MRRFoundQP = (double *)malloc((InputFileTotalPatents)*sizeof(double));
	MRRTotalQP = (long *)malloc((InputFileTotalPatents)*sizeof(long));


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


	rd_score = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	hat_rd_score = (double *)malloc((InputFileTotalPatents)*sizeof(double));
	DocID = (long *)malloc((InputFileTotalPatents)*sizeof(long));



	longQueries_TotalQueries = (long *)malloc((InputFileTotalPatents)*sizeof(long));
	longQueries_Find = (long *)malloc((InputFileTotalPatents)*sizeof(long));


//	FoundPatentsCount = 0;
	GlobalPostingAge = 0;


	for ( i = 0; i < 55; i ++ )
	{
			for ( j = 0; j < InputFileTotalPatents; j++ )
			{
					TermPresence[i][j] = (long *)malloc((33)*sizeof(long));
					for ( k = 0; k < 33; k ++ )
						TermPresence[i][j][k] = -1;
			}
	}

	
	InitializeQueryExpansionVariables(55);

	



	for ( j = 0; j < InputFileTotalPatents; j++ )
	{
			fullPatentLength[j] = 0;
			fullPatentUniqueItems[j] = 0;
			ComponentLabel[j] = 0;
			longQueries_TotalQueries[j] = 0;
			longQueries_Find[j] = 0;

			doc_avg_dl[j] = 0;

			b_prefFoundQP[j] = (double *)malloc((30)*sizeof(double));
			b_prefTotalQP[j] = (long *)malloc((30)*sizeof(long));
			MAPFoundQP[j] = (double *)malloc((30)*sizeof(double));
			MAPTotalQP[j] = (long *)malloc((30)*sizeof(long));
			RecallFoundQP[j] = (double *)malloc((30)*sizeof(double));
			RecallTotalQP[j] = (long *)malloc((30)*sizeof(long));
			PrecisionFoundQP[j] = (double *)malloc((30)*sizeof(double));
			PrecisionTotalQP[j] = (long *)malloc((30)*sizeof(long));

			MRRFoundQP[j] = (double *)malloc((40)*sizeof(double));
			MRRTotalQP[j] = (long *)malloc((40)*sizeof(long));

		

			for ( i = 0; i < 300; i ++ )
			{
					if ( i < 30 )
					{
							b_prefFoundQP[j][i] = 0.0;
							b_prefTotalQP[j][i] = 0;

							MAPFoundQP[j][i] = 0.0;
							MAPTotalQP[j][i] = 0;

							RecallFoundQP[j][i] = 0.0;
							RecallTotalQP[j][i] = 0;

							PrecisionFoundQP[j][i] = 0.0;
							PrecisionTotalQP[j][i] = 0;


							MRRFoundQP[j][i] = 0.0;
							MRRTotalQP[j][i] = 0;

					}			

			}
	}







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

	FoundPatentsCount[0] = 0;

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


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

												

							InsertItem_InvertedIndex(RowCount,ItemNumber,IntLength);								
							TermCollectionSum[ItemNumber] += IntLength;
							CollectionModel+=IntLength;
							CollEx += IntLength;
							doc_avg_dl[RowCount] += IDFValue[ItemNumber];

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

							fullPatentLength[RowCount] += IntLength;
							fullPatentUniqueItems[RowCount]++;				

						
							PostingCounter = 0;
					}
		

					
			}
			

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


							doc_avg_dl[RowCount] = (double) ( (double) doc_avg_dl[RowCount] / (double) fullPatentUniqueItems[RowCount]  );

							AveragePatentLength += fullPatentLength[RowCount];
							AvgUniqueTerms += fullPatentUniqueItems[RowCount];


							if ( fullPatentLength[RowCount] > 0 )
								RowCount++;
								
							
							if (RowCount%10000 == 0 )
								printf("\nNow Preparing Document Indexing = %d",RowCount);
					}
					Patent_QRelevance_Count++;

			}
			if ( Patent_QRelevance_Count == patentsCopyingTo )
					break;
				

	}
	Patent_QRelevance_Count = 0;
	fclose(fullTextfptr);


	// ReadProximity
	if ( RSType == 21 )
	{
				RowCount = 0;
				IntervalCount = 0;
				Patent_QRelevance_Count = 0;				
				sprintf(fullTextFileName,"%s",Term_Posting_File);

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

				fullTextfptr = fopen(fullTextFileName,"r");
				PostingCounter = 0;

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


						if ( ItemNumber != -17 && ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo) )
						{
								if ( IntLength > 0 )
								{
										if ( RowCount < 1050 )
										{
											
												IntervalCount = -1;
												for ( i = 0; i < DocTermsArray_TotalTerms[RowCount]; i++ )
												{
														if ( DocTermsArray_TermNumber[RowCount][i] == ItemNumber )
														{
																IntervalCount = i;
																break;
														}
												}

												if ( IntervalCount != -1 )
													DocTermsArray_TermPosting[RowCount][IntervalCount] = (long *)malloc((IntLength+1)*sizeof(long));

										}

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

								
						}
						

						if ( ItemNumber == -17 )
						{
								if ( Patent_QRelevance_Count >= patentsCopyingFrom && Patent_QRelevance_Count < patentsCopyingTo )
								{																
										RowCount++;
										if (RowCount%10000 == 0 )
											printf("\nNow Preparing Document Indexing = %d",RowCount);
								}
								Patent_QRelevance_Count++;

						}
						if ( Patent_QRelevance_Count == patentsCopyingTo )
								break;
						if ( Patent_QRelevance_Count >= 1050 )
								break;
							

				}
				Patent_QRelevance_Count = 0;
				fclose(fullTextfptr);


	}






//	printf("\n Total Docs = %d\n",InputFileTotalPatents);
//	exit(1);




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

	Make_Weighting_With_Threads();

	/*

	if ( RSType == 1 )
	{
		Make_TFIDF_Indexing();
		printf("\n TFIDF algorithm selected");
	}
	

	



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

	}

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

	}

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

	}

	if  ( RSType == 5 )
	{
		Make_AbsDis_Indexing();
		printf("\n Absolute Discouting algorithm selected");

	}

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

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

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


	if  ( RSType == 10 )
	{
			printf("\n QE with Frequent Terms Clustering selected");			
			Make_JM_Indexing();
	}
	if  ( RSType == 11 )
	{
			printf("\n QE with TS Clustering selected");			
			Make_JM_Indexing();
	}
	if  ( RSType == 12 )
	{
			printf("\n QE with KLD Clustering selected");			
			Make_JM_Indexing();
	}

	if  ( RSType == 21 )
	{
			printf("\n QE with TS_QP Clustering selected");			
			Make_JM_Indexing();
	}*/

	

	printf("\ncluster Number = %d",clusterNumber);
	

	printf("\npatents Copying From = %d",patentsCopyingFrom);
	

	printf("\npatents Copying To = %d",patentsCopyingTo);
	






}
void quicksort__Queries ( long lo , long hi, int ThreadNumber )
{
		long i = lo, j = hi;
		long x = QueryItemArray_Freq[ThreadNumber][ (lo+hi)/2 ];
		long dTempNumber;
		do
		{
				while ( QueryItemArray_Freq[ThreadNumber][i] > x ) i++;
				while ( QueryItemArray_Freq[ThreadNumber][j] < x ) j--;
				if ( i <= j )
				{

						dTempNumber = QueryItemArray[ThreadNumber][i];
						QueryItemArray[ThreadNumber][i] = QueryItemArray[ThreadNumber][j];
						QueryItemArray[ThreadNumber][j] = dTempNumber;


						dTempNumber = QueryItemArray_Freq[ThreadNumber][i];
						QueryItemArray_Freq[ThreadNumber][i] = QueryItemArray_Freq[ThreadNumber][j];
						QueryItemArray_Freq[ThreadNumber][j] = dTempNumber;


						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort__Queries (lo,j,ThreadNumber);
		if ( i<hi) quicksort__Queries (i,hi,ThreadNumber);
	
}

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

						dTempNumber = longQueries_Find[i];
						longQueries_Find[i] = longQueries_Find[j];
						longQueries_Find[j] = dTempNumber;

						dTempNumber = longQueries_TotalQueries[i];
						longQueries_TotalQueries[i] = longQueries_TotalQueries[j];
						longQueries_TotalQueries[j] = dTempNumber;

						dTempNumber = fullPatentLength_RetrievalProbability[i];
						fullPatentLength_RetrievalProbability[i] = fullPatentLength_RetrievalProbability[j];
						fullPatentLength_RetrievalProbability[j] = dTempNumber;



						i++;
						j--;
				}

		}
		while (i<=j);

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

void ReadDocumentSimilarity()
{
	int P1 = 0, P2 = 0;
	long Value1 = 0,Value2= 0;
	double Value3= 0;
	int MakeTransactionCount = 0;
	int ColCount2 = 0;
	FILE *fptr;
	int Change = 0;
	int DD = 0;
	char m_FileName[300];
	int Loop = 0;

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


	for ( Loop = 0; Loop < 1; Loop++ )
	{
			sprintf(m_FileName,"%s",offline_Cluster_File);
			fptr = fopen(m_FileName,"r");
			if ( fptr != NULL )
			{
					while (1)
					{
										MakeTransactionCount = 0;
										ColCount2 = 0;

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



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

									//	printf("\n%ld...%ld...%lf",Value1,Value2,Value3);
							
									//	if ( Value1 >= 64400 )
									//			break;
										if ( Value1 != -17 && Value2 != -17 )
										{

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

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


														


														if ( Value1 == Change )
														{
																
																ColCount2 = Value2/32;
																if ( DD < 300 )
																{
																		ColCount2 = DocumentSimilarityCount[Value1];
																		DocumentSimilarity[Value1][ColCount2] = Value2;
																		DocumentSimilarityCount[Value1]++;


																		//DocumentSimilarity[Value1][Value2] = DocumentSimilarity[Value1][ColCount2] | BitTable[(Value2%32)];
																}


																DD++;
														}


														Change = Value1;



												}
										}
										if ( Value1 > InputFileTotalPatents )
											break;
										
					 }
					 fclose(fptr);
			}
			else
			{
				printf("\n File Reading Error\n");
			}


	}




}

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


				
			
	
/*			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;
			*/

	for ( z = 0; z < DocumentSimilarityCount[S]; z++ )
	{
			if ( DocumentSimilarity[S][z] == D )
			{
					return 1;
			}
	}

	return 0;


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

				if ( SourcePatent == PatentNumber )
					return;

				//BitSimilarityValue(int S, int D)
				if ( BitSimilarityValue(SourcePatent,PatentNumber) == 1 && BitSimilarityValue(PatentNumber,SourcePatent) == 1  )
				{
					//	QueryClusterN[PatentNumber]++;
					//	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 *ProcessQueryInInvertedIndex(void *ptr)
{
		long z = 0,z1 = 0,i=0;
		long firstItem[100];
		double MinimumScore = 999999;
		double TopKCheck = 0;
		long NULLCount = 0;
		long DocumentNumber = 0;
		double Score = 0;
		double TFScore = 0;
		long Length = 0;
		long zero = 0;
		long CCS = 0;
		long ThreadNumber;
		long Local_GlobalPostingAge;
		long QueryPatentNumber;
		long Deter;
		double TotalMatch = 0;
		long SimilarDocs = 0;


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

		Parameter = Parameter_Value;	
		Parameter2 = Parameter_Value;

		thdata *data;            
		data = (thdata *) ptr;  

		ThreadNumber=  data->ThreadNumber;
		Local_GlobalPostingAge = data->Local_GlobalPostingAge;
		QueryPatentNumber = data->QueryPatentNumber;


		FoundNG = IDFValue[1000+Local_GlobalPostingAge];		
		FoundNG = FoundNG%20;

		for ( z1 = 0; z1 < InputFileTotalPatents; z1++ )
		{
				PostingAge[ThreadNumber][z1] = -1;
				Deter = z1%12;
				if ( z1 > (TarD+FoundNG) )
				{
					if ( Deter <= 2 )
						PostingAge[ThreadNumber][z1] = RecurNum;						
				}
				PostingQueryTermsPresence[ThreadNumber][z1]  = 0;
				PostingScore[ThreadNumber][z1] = 0;
		}



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


	
		
		


		while(1)
		{

					for ( z = 0; z < QueryItemCount[ThreadNumber]-1; z++ )
					{
								
				

								if ( firstItem[z] != -1 )
								{
									        

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


											Length = fullPatentLength[DocumentNumber];
											 


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


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


														
											}
											else if ( PostingAge[ThreadNumber][ DocumentNumber ] == Local_GlobalPostingAge )
											{
												
														PostingQueryTermsPresence[ThreadNumber][DocumentNumber]++;
														if ( RSType == 1 || RSType == 2  || RSType == 7 || RSType == 8 )
														{
																PostingScore[ThreadNumber][DocumentNumber] += Score;
																PostingTFScore[ThreadNumber][DocumentNumber] += (double)( (double)TFScore/(double)Length );

																TermPresence[ThreadNumber][DocumentNumber][z] = Local_GlobalPostingAge;
														}
														if ( RSType == 3 || RSType == 4 || RSType == 5 || RSType == 6 || RSType == 10 || RSType == 11 || RSType == 12 || RSType == 21 )
														{
																PostingScore[ThreadNumber][DocumentNumber] *= Score;
																PostingTFScore[ThreadNumber][DocumentNumber] += (double)( (double)TFScore/(double)Length );

																TermPresence[ThreadNumber][DocumentNumber][z] = Local_GlobalPostingAge;

														}
														//CCS++;
														//if ( CCS == QueryItemCount[[ThreadNumber]-1 )
														//	FoundNG = TFScore;

											}

								}

					}
					


					

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

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

							}
					}
					if ( NULLCount == QueryItemCount[ThreadNumber]-1 )
						break;

			
		}


		
		for ( z = 0; z < InputFileTotalPatents; z ++ )
		{
					DocumentNumber = z;
					SecondPart = 0;
					TF = 0;
					IDF = 0;
					SecondPart2 = 0;
		


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


												TF = 0;

												TF = (double)((double)(1-Parameter)*TF);
												SecondPart = Parameter*IDF;
												PostingScore[ThreadNumber][DocumentNumber] *= (SecondPart+TF);
												
										}

							}
					}


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

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


												//IDF = IDFValue[QueryItemArray[z1]];
												//IDF = (double)((double)(((InputFileTotalPatents))) /(double)IDF);
												//IDF = (double)((double)log(IDF)/(double)log(2));
								
												SecondPart = TF +  ( (double) ( (double) Parameter_Value * IDF ) );
												SecondPart2 = (double) ( fullPatentLength[DocumentNumber] + (double) Parameter_Value );
												SecondPart = SecondPart/SecondPart2;

												PostingScore[ThreadNumber][DocumentNumber] *= SecondPart;

												
										}

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

												IDF = (double)TermCollectionSum[QueryItemArray[ThreadNumber][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[ThreadNumber][DocumentNumber] *= TF;
												
										}

							}

					}
			


					if ( RSType == 6 )
					{
							Parameter2 = 2000;
							for ( z1 = 0; z1 < QueryItemCount[ThreadNumber]-1; z1++ )
							{
										if ( TermPresence[ThreadNumber][DocumentNumber][z1] != Local_GlobalPostingAge )
										{
												IDF = (double)TermCollectionSum[QueryItemArray[ThreadNumber][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[ThreadNumber][DocumentNumber] *= SecondPart;
												
										}

							}
					}

				

							if ( PostingQueryTermsPresence[ThreadNumber][DocumentNumber] > 0 )
							{
										

													if ( Know_Item_Search_Flag == 2 )
													{
														if ( ComponentLabel[DocumentNumber] == 1 )
																TotalFoundHighFindableDocuments[ThreadNumber]++;
														if ( ComponentLabel[DocumentNumber] == 0 )
																TotalFoundLowFindableDocuments[ThreadNumber]++;
													}



													FoundPatents[ThreadNumber][FoundPatentsCount[ThreadNumber]] = DocumentNumber;
													FoundPatentsScore[ThreadNumber][FoundPatentsCount[ThreadNumber]] = PostingScore[ThreadNumber][DocumentNumber];

													FoundPatentsTFScore[ThreadNumber][FoundPatentsCount[ThreadNumber]] = PostingTFScore[ThreadNumber][DocumentNumber];
													if ( MinimumScore > FoundPatentsScore[ThreadNumber][FoundPatentsCount[ThreadNumber]] )
															MinimumScore = FoundPatentsScore[ThreadNumber][FoundPatentsCount[ThreadNumber]];
													FoundPatentsCount[ThreadNumber]++;



							}
							PostingQueryTermsPresence[ThreadNumber][DocumentNumber]  = 0;
							PostingScore[ThreadNumber][DocumentNumber] = 0;
			

		}
	
		quicksort__performance (0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);
		if ( RSType == 10 )
		{
						GoodDocuments[ThreadNumber] = 0;	
						for ( z = 0; z < FoundPatentsCount[ThreadNumber]; z++ )													
								PatentRMScore[ThreadNumber][z] = 0;

						for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1 ++ )
						{
								TotalMatch = 0;
								SimilarDocs = 0;
								for ( z = 0; z < FoundPatentsCount[ThreadNumber]; z++)
								{				
										 if ( NumberSimilarTermsImprove(FoundPatents[ThreadNumber][z1],FoundPatents[ThreadNumber][z]) > 0 )
										 {							
												if ( z != z1  )
												{
														TotalMatch+=((11-z)*(11-z));
														//TotalMatch+=(FoundPatentsScore[ThreadNumber][z1]*FoundPatentsScore[ThreadNumber][z1]);
														SimilarDocs++;
												}
										 }
										 if ( z >= 10 )
											 break;
								}
								if ( SimilarDocs >= 1 )
										GoodDocuments[ThreadNumber]++;
													
								
								PatentRMScore[ThreadNumber][z1] = TotalMatch;
						}
						printf("\n%d....\n",GoodDocuments[ThreadNumber]);

						if ( GoodDocuments[ThreadNumber] > 10 )
						{					
								for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1 ++ )
								{
										FoundPatentsScore[ThreadNumber][z1] = PatentRMScore[ThreadNumber][z1];
								}

								if ( FoundPatentsCount[ThreadNumber] > 1500 )
										quicksort__performance(0,1499,ThreadNumber);
								else
										quicksort__performance(0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);
						}


		}
		if ( RSType == 11 || RSType == 10)
		{
			
					GoodDocuments[ThreadNumber] = 10;

					ExpandQueryLanguageModelMethod(Local_GlobalPostingAge,ThreadNumber);																				
					for (z = 0; z < FoundPatentsCount[ThreadNumber]; z++ )
							FoundPatentsScore[ThreadNumber][z] = PatentRMScore[ThreadNumber][z];

					quicksort__performance (0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);


		}
		if ( RSType == 12 )
		{
					GoodDocuments[ThreadNumber] = 10;

					//ExpandQueryLanguageModelMethod(Local_GlobalPostingAge,ThreadNumber);
					ExpandQueryKLDMethod(Local_GlobalPostingAge,ThreadNumber);			
					for (z = 0; z < FoundPatentsCount[ThreadNumber]; z++ )
							FoundPatentsScore[ThreadNumber][z] = PatentRMScore[ThreadNumber][z];

					quicksort__performance (0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);
		}
		if ( RSType == 21 )
		{
					MineQueryPatentTerms(Local_GlobalPostingAge,QueryPatentNumber,ThreadNumber);
					for (z = 0; z < FoundPatentsCount[ThreadNumber]; z++ )
							FoundPatentsScore[ThreadNumber][z] = PatentRMScore[ThreadNumber][z];

					quicksort__performance (0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);


					
					GoodDocuments[ThreadNumber] = 10;
					ExpandQueryLanguageModelMethod(Local_GlobalPostingAge,ThreadNumber);																				
					for (z = 0; z < FoundPatentsCount[ThreadNumber]; z++ )
							FoundPatentsScore[ThreadNumber][z] = PatentRMScore[ThreadNumber][z];

					quicksort__performance (0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);

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

		}
		


		

																AllRecall[ThreadNumber] = 0;
																AllPrecision[ThreadNumber] = 0;
																AllMAP[ThreadNumber] = 0;
																Allb_pref[ThreadNumber] = 0;																
																for ( i = 0; i < GlobalPartitionNumber; i++ )
																{
																			ComponentRecall[ThreadNumber][i] = 0;
																			ComponentPrecision[ThreadNumber][i] = 0;
																			ComponentMAP[ThreadNumber][i] = 0;
																			Componentb_pref[ThreadNumber][i] = 0;																			
																}				
																
																if ( Know_Item_Search_Flag == 0 )
																	Calculate_Performance_Scores(ThreadNumber);						
																else
																	Calculate_Performance_Scores_with_partition(ThreadNumber);
																					



																Allb_pref[ThreadNumber] = (double)((double)Allb_pref[ThreadNumber]/(double)QrelCount[ThreadNumber]);				
																AllMAP[ThreadNumber] = (double)((double)AllMAP[ThreadNumber]/(double)QrelCount[ThreadNumber]);				
																AllPrecision[ThreadNumber] = (double)((double)AllPrecision[ThreadNumber]/(double)30);				
																AllRecall[ThreadNumber] = (double)((double)AllRecall[ThreadNumber]/(double)QrelCount[ThreadNumber]);							
																





																for ( i = 0; i < GlobalPartitionNumber; i++ )
																{
																		if ( ComponentQrel[ThreadNumber][i] > 0 )
																		{															
																				Componentb_pref[ThreadNumber][i] = (double)((double)Componentb_pref[ThreadNumber][i]/(double)ComponentQrel[ThreadNumber][i]);																			
																				ComponentMAP[ThreadNumber][i] = (double)((double)ComponentMAP[ThreadNumber][i]/(double)ComponentQrel[ThreadNumber][i]);																			
																				ComponentRecall[ThreadNumber][i] = (double)((double)ComponentRecall[ThreadNumber][i]/(double)ComponentQrel[ThreadNumber][i]);				
																				ComponentPrecision[ThreadNumber][i] = (double)((double)ComponentPrecision[ThreadNumber][i]/(double)30);																			

																			//	printf("\n%lf...%lf\n",ComponentRecall[ThreadNumber][i],ComponentPrecision[ThreadNumber][i]);
																		}
																}	
																


		ThreadTerminationCount++;



}

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

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

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


						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort__FindabilityScore (lo,j,ThreadNumber);
		if ( i<hi) quicksort__FindabilityScore (i,hi,ThreadNumber);
	
}
void longQueries_Find_Check(int ThreadNumber)
{

	long z1, z2 = 0;
	double score1, score2;
	int number1, number2;
	double b_pref_local;
	long V = 0;
	long Found = 0;


	long CLabel;
	double MapLocal;
	double HighFindableDocPercentage;
	double LowFindableDocPercentage;
	long HighTopLimit;
	long LowTopLimit;
	





//	quicksort__FindabilityScore (0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);
//	quicksort__performance (0,TotalFoundHighFindableDocuments[ThreadNumber]-1,ThreadNumber);
//	quicksort__performance (TotalFoundHighFindableDocuments[ThreadNumber],FoundPatentsCount[ThreadNumber]-1,ThreadNumber);


	HighFindableDocPercentage = (double)((double)TotalFoundHighFindableDocuments[ThreadNumber]/(double)FoundPatentsCount[ThreadNumber]);
	LowFindableDocPercentage = (double)((double)TotalFoundLowFindableDocuments[ThreadNumber]/(double)FoundPatentsCount[ThreadNumber]);


	//Recall
	HighTopLimit = 100*HighFindableDocPercentage;
	LowTopLimit = 100*LowFindableDocPercentage;
//	HighTopLimit = 50;
//	LowTopLimit = 50;

	printf("\n%lf...%lf...%ld...%ld",HighFindableDocPercentage,LowFindableDocPercentage,HighTopLimit,LowTopLimit);

	for ( z1 = 0; z1 < TotalFoundHighFindableDocuments[ThreadNumber]; z1++ )
	{				

					longQueries_Find[   FoundPatents[ThreadNumber][z1]       ]++;

					if ( z1 > HighTopLimit )
						break;

	}


	Found = 0;
	for ( z1 = TotalFoundHighFindableDocuments[ThreadNumber]; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{

					longQueries_Find[   FoundPatents[ThreadNumber][z1]       ]++;

					if ( Found > LowTopLimit )
						break;

					Found++;

	}




														
	
}

void Calculate_Performance_Scores_with_partition(int ThreadNumber)
{
	long z1, z2 = 0;
	double score1, score2;
	int number1, number2;
	double b_pref_local;
	long V = 0;
	long Found = 0;


	long CLabel;
	double MapLocal;
	double HighFindableDocPercentage;
	double LowFindableDocPercentage;
	long HighTopLimit;
	long LowTopLimit;
	



	for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{
			for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
			{
					if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
					{									
							CLabel = ComponentLabel[FoundPatents[ThreadNumber][z1]];
							ComponentQrel[ThreadNumber][CLabel]++;
					}
			}
	}
//	if ( (double) ( (double)ComponentQrel[ThreadNumber][0] / (double)QrelCount[ThreadNumber] ) < 0.58 )
//	{}
//	else
//	{
//		return;
//	}
	


	quicksort__FindabilityScore (0,FoundPatentsCount[ThreadNumber]-1,ThreadNumber);


	quicksort__performance (0,TotalFoundHighFindableDocuments[ThreadNumber]-1,ThreadNumber);
	quicksort__performance (TotalFoundHighFindableDocuments[ThreadNumber],FoundPatentsCount[ThreadNumber]-1,ThreadNumber);


	HighFindableDocPercentage = (double)((double)TotalFoundHighFindableDocuments[ThreadNumber]/(double)FoundPatentsCount[ThreadNumber]);
	LowFindableDocPercentage = (double)((double)TotalFoundLowFindableDocuments[ThreadNumber]/(double)FoundPatentsCount[ThreadNumber]);


	//Recall
	HighTopLimit = 100*HighFindableDocPercentage;
	LowTopLimit = 100*LowFindableDocPercentage;
//	HighTopLimit = 50;
//	LowTopLimit = 50;

	printf("\n%lf...%lf...%ld...%ld",HighFindableDocPercentage,LowFindableDocPercentage,HighTopLimit,LowTopLimit);

	for ( z1 = 0; z1 < TotalFoundHighFindableDocuments[ThreadNumber]; z1++ )
	{				
					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{
									AllRecall[ThreadNumber]++;							


									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 1 )
											ComponentRecall[ThreadNumber][1]++;
									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 0 )
											ComponentRecall[ThreadNumber][0]++;

									break;
							}
							
					}
					if ( z1 > HighTopLimit )
						break;

	}


	Found = 0;
	for ( z1 = TotalFoundHighFindableDocuments[ThreadNumber]; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{


					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{
									AllRecall[ThreadNumber]++;

									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 1 )
											ComponentRecall[ThreadNumber][1]++;
									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 0 )
											ComponentRecall[ThreadNumber][0]++;


									break;
							}

					}
					if ( Found > LowTopLimit )
						break;

					Found++;

	}






	//Precision
	HighTopLimit = 30*HighFindableDocPercentage;
	LowTopLimit = 30*LowFindableDocPercentage;

//	HighTopLimit = 5;
//	LowTopLimit = 25;

//	printf("\n%lf...%lf...%ld...%ld",HighFindableDocPercentage,LowFindableDocPercentage,HighTopLimit,LowTopLimit);

	for ( z1 = 0; z1 < TotalFoundHighFindableDocuments[ThreadNumber]; z1++ )
	{				
					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{
									AllPrecision[ThreadNumber]++;	
									
									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 1 )
											ComponentPrecision[ThreadNumber][1]++;
									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 0 )
											ComponentPrecision[ThreadNumber][0]++;

									break;
							}
							
					}
					if ( z1 > HighTopLimit )
						break;

	}


	Found = 0;
	for ( z1 = TotalFoundHighFindableDocuments[ThreadNumber]; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{


					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{
									AllPrecision[ThreadNumber]++;

									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 1 )
											ComponentPrecision[ThreadNumber][1]++;
									if ( ComponentLabel[FoundPatents[ThreadNumber][z1]] == 0 )
											ComponentPrecision[ThreadNumber][0]++;

									break;
							}

					}
					if ( Found > LowTopLimit )
						break;

					Found++;

	}



}



void Calculate_Performance_Scores(int ThreadNumber)
{
	int z1, z2 = 0;
	double b_pref_local;
	int V = 0;
	int Found = 0;
	int Deter = 0;


	int Number_of_nRelevantAll = 0;
	int Number_of_nRelevant[200];


	int CLabel;
	double MapLocal;



	int RAll = 0;
	int R[200];

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

	for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{
					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{									
									CLabel = ComponentLabel[FoundPatents[ThreadNumber][z1]];

									ComponentQrel[ThreadNumber][CLabel]++;
							}
					}
	}
	


	for ( z1 = 0; z1 < 200; z1 ++ )
	{
			Number_of_nRelevant[z1] = 0;
			R[z1] = 0;
	}



	for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{
					Found = 0;
					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{
									Found = 1;
									CLabel = ComponentLabel[FoundPatents[ThreadNumber][z1]];
									for ( z2 = 0; z2 < GlobalPartitionNumber; z2 ++ )
									{									
											if ( CLabel != z2 )
												Number_of_nRelevant[z2]++;
									}

								//	Number_of_nRelevant[CLabel]++;
									break;
							}				
					}
					if ( Found == 0 )
					{
							CLabel = ComponentLabel[FoundPatents[ThreadNumber][z1]];
							Number_of_nRelevantAll++;		
							Number_of_nRelevant[CLabel]++;								
					}

				//	printf("\n%f...%d",FoundPatentsScore[z1],FoundPatentsCount);

					if ( z1 > 3000 )
						break;

					

					
					

	}

	

	for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{

					Found = 0;
					CLabel = ComponentLabel[FoundPatents[ThreadNumber][z1]];
					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{
									if ( Number_of_nRelevant[CLabel] > 0 )
									{
											b_pref_local = (double)((double)R[CLabel]/(double)(Number_of_nRelevant[CLabel]));
											b_pref_local = (double)(1 - b_pref_local);
											Componentb_pref[ThreadNumber][CLabel] += b_pref_local;
									}
									
									Found = 1;
									break;

							}	
							else
							{
							}
					}


					if ( Found == 1 )
					{
							b_pref_local = (double)((double)RAll/(double)(Number_of_nRelevantAll));
							b_pref_local = (double)(1 - b_pref_local);
							Allb_pref[ThreadNumber] += b_pref_local;

						

							for ( z2 = 0; z2 < GlobalPartitionNumber; z2 ++ )
							{									
									if ( CLabel != z2 )
											R[z2]++;
							}

					}
					else
					{
							RAll++;
							R[CLabel]++;
					}

					if ( z1 > 3000 )
						break;



	}





	for ( z1 = 0; z1 < 200; z1 ++ )
	{
			Number_of_nRelevant[z1] = 0;			
	}
	Number_of_nRelevantAll = 0;


	for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{

					Found = 0;
					CLabel = ComponentLabel[FoundPatents[ThreadNumber][z1]];
					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{

							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{
									if ( z1 < Precision_cutoff )
									{
										AllPrecision[ThreadNumber]++;
										ComponentPrecision[ThreadNumber][CLabel]++;
									}
									if ( z1 < Recall_cutoff )
									{
										AllRecall[ThreadNumber]++;
										ComponentRecall[ThreadNumber][CLabel]++;
									}

									Number_of_nRelevant[CLabel]++;
									




									MapLocal = (double)((double)Number_of_nRelevant[CLabel]/(double)(z1+1));
									ComponentMAP[ThreadNumber][CLabel] += MapLocal;											

											
									Found = 1;
									break;

							}
							
					}

				//	if ( z1 < 30 )
				//		printf("\n%d	%d",CLabel,z1);

					if ( Found == 1 )
					{
							
							Number_of_nRelevantAll++;

							MapLocal = (double)((double)Number_of_nRelevantAll/(double)(z1+1));
							AllMAP[ThreadNumber] += MapLocal;
					}


	}





}










int Calculate_MRR_Scores(int ThreadNumber)
{
	int z1, z2 = 0;
	double b_pref_local;
	int V = 0;
	int Found = 0;
	int Deter = 0;


	int Number_of_nRelevantAll = 0;
	int Number_of_nRelevant[200];


	int CLabel;
	double MapLocal;



	int RAll = 0;
	int R[200];







	Found = -1;

	for ( z1 = 0; z1 < FoundPatentsCount[ThreadNumber]; z1++ )
	{
					
					for ( V = 0; V < QrelCount[ThreadNumber]; V++ )
					{
							if ( Qrel[ThreadNumber][V] == FoundPatents[ThreadNumber][z1] )
							{

									CLabel = ComponentLabel[FoundPatents[ThreadNumber][z1]];
									
								//	if ( z1 >= 3 )
									{
											AllMRR = (double) ( (double)1/(double)(z1+1));
											ComponentMRR[CLabel] = (double) ( (double)1/(double)(z1+1));


											//AllMRR = (double) ( (double)z1);
											//ComponentMRR[CLabel] = (double) ( (double)z1);
	
											Found = 1;
									}
											

									


									break;
							}				
					}							

	}

//	if ( Found == 1 )
		return 1;
//	else
//		return 0;
	



}



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


						iTempNumber = rd_score[i];
						rd_score[i] = rd_score[j];
						rd_score[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_rd_score (lo,j);
		if ( i<hi) quicksort_rd_score (i,hi);
	
}


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 = rd_score[i];
				//		rd_score[i] = rd_score[j];
				//		rd_score[j] = iTempNumber;


						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];
		int TT  = 0;

/*
		if ( clusterNumber != 1000 )
			quicksort_hat_rd_score(0,InputFileTotalPatents-1);
		else
			quicksort_hat_rd_score(0,49721);

		

		if ( clusterNumber == 1000 )
			Factor = 49722/Partitions;
		else
			Factor = InputFileTotalPatents/Partitions;

		if ( clusterNumber == 1000 )
		{
				for ( i = 0; i < 49722; i++ )
				{
						Label = i/Factor;
						ComponentLabel[DocID[i]] = Label;		
				}
		}
		else
		{
				for ( i = 0; i < InputFileTotalPatents; i++ )
				{
						Label = i/Factor;
						ComponentLabel[DocID[i]] = Label;							
				}
		}


		sprintf(m_FileName,"%sWorkingFolder//FindabilityLables.txt",sourcePath);
		fptr = fopen(m_FileName,"w");
		for ( i = 0; i < 49722; i++ )
		{
				if ( ComponentLabel[i] == 6 )
					ComponentLabel[i]--;

				fprintf(fptr,"%d\n",5-ComponentLabel[i]);
		}
		fclose(fptr);


		
*/

















		if ( clusterNumber != 1000 )
			quicksort_hat_rd_score(0,InputFileTotalPatents-1);
		else
			quicksort_hat_rd_score(0,35199);

		

		if ( clusterNumber == 1000 )
			Factor = 35200/Partitions;
		else
			Factor = InputFileTotalPatents/Partitions;

		if ( clusterNumber == 1000 )
		{
				for ( i = 0; i < 35200; i++ )
				{
						Label = i/Factor;
						ComponentLabel[DocID[i]] = Label;	

						if ( Label == 1 )
							TT++;					
					//	printf("\n%lf...",hat_rd_score[i]);
				}
		}
		else
		{
				for ( i = 0; i < InputFileTotalPatents; i++ )
				{
						Label = i/Factor;
						ComponentLabel[DocID[i]] = Label;	
						
					//	printf("\n%lf...",hat_rd_score[i]);
				}
		}




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





}


void partition_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];
		int TT  = 0;


		quicksort_hat_rd_score(0,InputFileTotalPatents-1);

		

		Factor = InputFileTotalPatents/Partitions;

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





}

void ProcessQueries(int clusterNumber)
{
	FILE *fptr,*fptrQrel,*writePerformanceLog,*topDocuments;
	int queryWord;
	
	char queriesFileName[300];
	int returnValue = 0;
	int z = 0,i,j,k;
	int OldNumber = 0;
	int V = 0;
	int tt = 0;
	int DocumentsLowHighTotal = 0;
//	int TP = 0;
	double Process;
	double SumDF5 = 0,SumDF4 = 0,SumDF3 = 0,SumDF2 = 0,SumDF1 = 0,SumDF = 0;
	int OldPatentNumber = -1;
	int Local_rd_score,local_DocTotalQueries;
	int Find_File;
	int ThreadCount;
	int TotalQueriesProcess = 0;
	char RS_Name[300];
	int Find5,Find10,Find15,Find20,Find25;

	pthread_t thread1[200];  
	thdata data1[200];


	RecurNum = 2000;








	for ( j = 0; j < InputFileTotalPatents; j++ )
	{
			for ( i = 0; i < 300; i ++ )
			{
					if ( i < 30 )
					{
							b_prefFoundQP[j][i] = 0.0;
							b_prefTotalQP[j][i] = 0;

							MAPFoundQP[j][i] = 0.0;
							MAPTotalQP[j][i] = 0;

							RecallFoundQP[j][i] = 0.0;
							RecallTotalQP[j][i] = 0;

							PrecisionFoundQP[j][i] = 0.0;
							PrecisionTotalQP[j][i] = 0;

							MRRFoundQP[j][i] = 0.0;
							MRRTotalQP[j][i] = 0;

					}			

			}
			longQueries_Find[j] = 0;
			longQueries_TotalQueries[j] = 0;
			fullPatentLength_RetrievalProbability[j] = fullPatentLength[j];
	}

	for ( i = 0; i < 55; i ++ )
	{
			for ( j = 0; j < InputFileTotalPatents; j++ )
			{
					for ( k = 0; k < 33; k ++ )
						TermPresence[i][j][k] = -1;
			}
	}
		
	
	Find_File = RSType;
	if ( RSType >= 10 )
		Find_File = 4;


//	exit(1);

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

	if ( RSType == 10 )
		strcpy(RS_Name,"Cluster");
	if ( RSType == 11 )
		strcpy(RS_Name,"QE_TS");
	if ( RSType == 12 )
		strcpy(RS_Name,"KLD");
	if ( RSType == 21 )
		strcpy(RS_Name,"QP_TS");


	if ( Know_Item_Search_Flag == 0 || Know_Item_Search_Flag == 10)
		sprintf(queriesFileName,"%sWorkingFolder//ProbabilityRetrieved_%s.txt",sourcePath,RS_Name);
	else
		sprintf(queriesFileName,"%sWorkingFolder//ProbabilityRetrieved_%s_Partition.txt",sourcePath,RS_Name);

	topDocuments = fopen(queriesFileName, "w");


	
	if ( Know_Item_Search_Flag == 0 || Know_Item_Search_Flag == 10)
		sprintf(queriesFileName,"%sWorkingFolder//Effectiveness_Topics_%s_%lf.txt",sourcePath,RS_Name,Parameter_Value);//,fileNumber
	else
		sprintf(queriesFileName,"%sWorkingFolder//Effectiveness_Topics_%s_Partition_%lf.txt",sourcePath,RS_Name,Parameter_Value);//,fileNumber


	writePerformanceLog = fopen(queriesFileName, "w");


	V = 0;
	tt = 0;

	if ( Know_Item_Search_Flag == 10   )
	{
			if ( RSType < 10 )
				sprintf(queriesFileName,"%sWorkingFolder//%s_high.txt",sourcePath,RS_Name);//,fileNumber
			else
				sprintf(queriesFileName,"%sWorkingFolder//JM_high.txt",sourcePath,RS_Name);//,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 ( fullPatentLength_AllOver[tt] > 0 ) 
					{
							if ( local_DocTotalQueries > 10 )
							{
									if ( PPS == 0 )
										Process = (double) ( (double) Local_rd_score/ (double) local_DocTotalQueries );
									if ( PPS == 1 )
										Process = (double) ( (double) Local_rd_score);
									if ( PPS == 2 )
										Process = (double) fullPatentLength[V];
									if ( PPS == 3 )
										Process = doc_avg_dl[V];
							}
							else
							{
									if ( PPS == 0 )
									{
										if ( fullPatentLength_AllOver[tt] > 0 )
											Process = 0;
										else
											Process = 0;
									}
									if ( PPS == 1 )
										Process = (double) ( (double) Local_rd_score);

									if ( PPS == 2 )
										Process = (double) fullPatentLength[V];

									if ( PPS == 3 )
										Process = doc_avg_dl[V];


							}

							hat_rd_score[V] = Process;
							DocID[V] = V;
							V++;
					}
					else
					{
					//		printf("\n hello at %d\n",tt);
					}
				



					tt++;

					if ( tt >= 100000 )
						break;

			}
			fclose(fptr);
			

	}
	if ( Know_Item_Search_Flag == 0  )
	{
			for ( V  = 0; V < InputFileTotalPatents; V++ )
			{			
					hat_rd_score[V] = V;
					DocID[V] = V;			
			}
	
		


	}



	if ( Know_Item_Search_Flag == 2  )
	{
			sprintf(queriesFileName,"%s.txt",LowHigh_Labels);//,fileNumber			
			fptr = fopen(queriesFileName, "r");
			V = 0;
			tt = 0;
			while(1)
			{
					if ( fscanf(fptr,"%lf",&SumDF) == EOF )
						break;

				//	printf("\n%lf...",SumDF);

					if ( fullPatentLength_AllOver[tt] > 0 ) 
					{
	

							//ComponentLabel[V] = Find5;
							hat_rd_score[V] = SumDF;
							DocID[V] = V;
							V++;
					}

			

					tt++;
					if ( tt >= InputFileTotalPatents )
						break;

			}
			fclose(fptr);

		


	}


	V = 0;
	tt = 0;


	

	for ( z = 0; z < 200; z ++ )
		QueryItemCount[z] = 0;

	sprintf(queriesFileName,"%s",topic_File);
//	printf("\n%s\n",queriesFileName);
	fptr = fopen(queriesFileName, "r");

	sprintf(queriesFileName,"%s",Qrel_File);
	fptrQrel = fopen(queriesFileName, "r");
	



	

	if ( Know_Item_Search_Flag == 10 )
	{
			GlobalPartitionNumber = 5;
			CreateDocumentsPartitions_rd_score(GlobalPartitionNumber,clusterNumber);
	}
	if ( Know_Item_Search_Flag == 0 )
	{
			GlobalPartitionNumber = 2;
			//CreateDocumentsPartitions_rd_score(GlobalPartitionNumber,clusterNumber);
			partition_CreateDocumentsPartitions_rd_score(GlobalPartitionNumber,clusterNumber);
	}

	if ( Know_Item_Search_Flag == 2 )
	{
			GlobalPartitionNumber = 2;
			partition_CreateDocumentsPartitions_rd_score(GlobalPartitionNumber,clusterNumber);
	}


	GlobalPostingAge = 0;
	CriticalSection = 0;
	ThreadCount = 0;

	



	while ( 1 )
	{

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

				


										
					if ( queryWord != -17 )
					{
								
								if ( ((double)IDFValue[queryWord]/(double)InputFileTotalPatents) < 0.7 )
								{
										QueryItemArray[GlobalPostingAge%50][QueryItemCount[GlobalPostingAge%50]] = queryWord;
										QueryItemArray_Freq[GlobalPostingAge%50][QueryItemCount[GlobalPostingAge%50]] = tt;
										QueryItemCount[GlobalPostingAge%50]++;
								}
								
								

					}
					
					if ( queryWord == -17 )
					{


				
								
								quicksort__Queries(0,QueryItemCount[GlobalPostingAge%50]-1,GlobalPostingAge%50);							
								if ( QueryItemCount[GlobalPostingAge%50] > 10 )
										QueryItemCount[GlobalPostingAge%50] = 10;					
				
								
	
								QrelCount[GlobalPostingAge%50] = 0;
								for ( i = 0; i < GlobalPartitionNumber; i++ )
										ComponentQrel[GlobalPostingAge%50][i] = 0;
								
								while(1)
								{
										fscanf(fptrQrel,"%d",&V);
										if ( V == -17 )
											break;

										Qrel[GlobalPostingAge%50][QrelCount[GlobalPostingAge%50]] = V;
									//	ComponentQrel[GlobalPostingAge%50][ComponentLabel[V]]++;																			
										QrelCount[GlobalPostingAge%50]++;
								}
								TotalQueriesProcess++;







								GlobalPostingAge++;	
						

								if ( (GlobalPostingAge%50) == 0 || GlobalPostingAge == total_topic_File )
								{
											printf("\n Now Processing Queries \n");

											
											for ( z = 0; z < TotalQueriesProcess; z ++ )
											{
													FoundPatentsCount[z] = 0;


													if ( Know_Item_Search_Flag == 2 )
													{
															TotalFoundHighFindableDocuments[z] = 0;
															TotalFoundLowFindableDocuments[z] = 0;
													}


													data1[z].ThreadNumber = z;
													data1[z].Local_GlobalPostingAge = z+GlobalPostingAge;
													data1[z].QueryPatentNumber = (ThreadCount*50)+z;
													pthread_create (&thread1[z], NULL, (void *) &ProcessQueryInInvertedIndex, (void *) &data1[z]);									

												//	ProcessQueryInInvertedIndex(&data1[z]);
												
												

											}
											
										
											ThreadTerminationCount = 0;

											
											for ( z = 0; z  < TotalQueriesProcess; z++ )
												pthread_join(thread1[z], NULL);

											
											

									
											ThreadCount++;

									


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

												
													if ( FoundPatentsCount[z] > 0 )
													{											
																



																
																for ( j = 0; j < FoundPatentsCount[z]; j++ )
																{
																	//	fprintf(topDocuments,"%d\n",FoundPatents[z][j]);							
																	
																	if ( Know_Item_Search_Flag == 0 )
																	{
																		if ( j < Recall_cutoff )
																			longQueries_Find[   FoundPatents[z][j]       ]++;
																	}

																		
																	longQueries_TotalQueries[      FoundPatents[z][j]    ]++;

																//	if ( j > 5000 )
																//		break;
																}
																
																if ( Know_Item_Search_Flag == 2 )
																	longQueries_Find_Check(z);



													

																fprintf(writePerformanceLog,"%d	%lf	%lf	%lf	%lf	%ld\n",GlobalPostingAge+z,AllRecall[z],AllPrecision[z],AllMAP[z],Allb_pref[z],QrelCount[z]);


																V++;								
																b_prefFoundQP[0][0] += Allb_pref[z];													
																b_prefTotalQP[0][0]++;
																for ( i = 0; i < GlobalPartitionNumber; i++ )
																{
																		
																		if ( ComponentQrel[z][i] > 0 )
																		{
																			b_prefFoundQP[0][i+1] += Componentb_pref[z][i];
																			b_prefTotalQP[0][i+1]++;
																		}
																}
														

																
																MAPFoundQP[0][0] += AllMAP[z];													
																MAPTotalQP[0][0]++;
																for ( i = 0; i < GlobalPartitionNumber; i++ )
																{
																		if ( ComponentQrel[z][i] > 0 )
																		{
																			MAPFoundQP[0][i+1] += ComponentMAP[z][i];
																			MAPTotalQP[0][i+1]++;
																		}
																}

																
																RecallFoundQP[0][0] += AllRecall[z];													
																RecallTotalQP[0][0]++;
																for ( i = 0; i < GlobalPartitionNumber; i++ )
																{
																		if ( ComponentQrel[z][i] > 0 )
																		{
																			RecallFoundQP[0][i+1] += ComponentRecall[z][i];
																			RecallTotalQP[0][i+1]++;
																		}
																}


																PrecisionFoundQP[0][0] += AllPrecision[z];													
																PrecisionTotalQP[0][0]++;
																for ( i = 0; i < GlobalPartitionNumber; i++ )
																{
																		if ( ComponentQrel[z][i] > 0 )
																		{
																			PrecisionFoundQP[0][i+1] += ComponentPrecision[z][i];
																			PrecisionTotalQP[0][i+1]++;
																		}
																}

													

														
																

													}//FoundPatentsCount > 0

													


													printf("\nProcessing Queries = %d",(int)(z+(ThreadCount*50)));

													QueryItemCount[z] = 0;
 

											}//loop z end
											TotalQueriesProcess = 0;

								 }//GlobalPostingAge50 End



			
						

							
						


						


								if ( GlobalPostingAge%2000 == 0 )
									printf("\n Completed = %d",GlobalPostingAge);

						
								
								RecurNum++;
								if ( GlobalPostingAge >= total_topic_File )
									break;
							

					
								
					}

	}
	fclose(fptr);
	
	fclose(fptrQrel);
//	fclose(fptrQualityScore);
//	fclose(writePerformanceLog);
//	exit(1);

	quicksort__DocsLongQueries(0,InputFileTotalPatents-1);
	RecurNum = InputFileTotalPatents/30;
	

	Find5 = 0;
	Find10 = 0;
	SumDF = 0;
	for ( z = 0; z < InputFileTotalPatents; z++ )
	{
			Find5 += longQueries_Find[z];
			Find10 += longQueries_TotalQueries[z];
			SumDF += fullPatentLength_RetrievalProbability[z];

			if ( (z%RecurNum) == 0 )
			{
					SumDF = (double) ( (double)SumDF/ (double) RecurNum );

					if ( Find10 > 0 )			
						fprintf(topDocuments,"%d	%lf	%d	%d	%lf\n",z,SumDF,Find5,Find10,(double) (  (double) Find5/ (double) Find10 ) );
					else
						fprintf(topDocuments,"%d	%lf	%d	%d	%lf\n",z,SumDF,Find5,Find10,0 );


					Find5 = 0;
					Find10 = 0;
					SumDF =0;

			}
	}



	printf("\n\nb_pref = ");
	fprintf(writePerformanceLog,"\n\nb_pref = ");
	for ( z = 0; z < 1; z++ )
	{
			SumDF = (double)((double)b_prefFoundQP[z][0]/(double)b_prefTotalQP[z][0]);
			SumDF5 = (double)((double)b_prefFoundQP[z][1]/(double)b_prefTotalQP[z][1]);
			SumDF4 = (double)((double)b_prefFoundQP[z][2]/(double)b_prefTotalQP[z][2]);
			SumDF3 = (double)((double)b_prefFoundQP[z][3]/(double)b_prefTotalQP[z][3]);
			SumDF2 = (double)((double)b_prefFoundQP[z][4]/(double)b_prefTotalQP[z][4]);
			SumDF1 = (double)((double)b_prefFoundQP[z][5]/(double)b_prefTotalQP[z][5]);

			
			if ( Know_Item_Search_Flag == 10 )
			{
					printf("\n%d	%f	%f	%f	%f	%f	%f",b_prefTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
					fprintf(writePerformanceLog,"\n%d	%f	%f	%f	%f	%f	%f",b_prefTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
			}
			if ( Know_Item_Search_Flag == 0 || Know_Item_Search_Flag == 2 )
			{
					printf("%f",SumDF);
					fprintf(writePerformanceLog,"%f",SumDF);
			}

	}




	printf("\n\nMAP = ");
	fprintf(writePerformanceLog,"\n\nMAP = ");
	for ( z = 0; z < 1; z++ )
	{
			SumDF = (double)((double)MAPFoundQP[z][0]/(double)MAPTotalQP[z][0]);
			SumDF5 = (double)((double)MAPFoundQP[z][1]/(double)MAPTotalQP[z][1]);
			SumDF4 = (double)((double)MAPFoundQP[z][2]/(double)MAPTotalQP[z][2]);
			SumDF3 = (double)((double)MAPFoundQP[z][3]/(double)MAPTotalQP[z][3]);
			SumDF2 = (double)((double)MAPFoundQP[z][4]/(double)MAPTotalQP[z][4]);
			SumDF1 = (double)((double)MAPFoundQP[z][5]/(double)MAPTotalQP[z][5]);

			
			if ( Know_Item_Search_Flag == 10 )
			{
					printf("\n%d	%f	%f	%f	%f	%f	%f",MAPTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
					fprintf(writePerformanceLog,"\n%d	%f	%f	%f	%f	%f	%f",MAPTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
			}


			if ( Know_Item_Search_Flag == 0 || Know_Item_Search_Flag == 2 )
			{
					printf("%f",SumDF);
					fprintf(writePerformanceLog,"%f",SumDF);
			}


	}




	printf("\n\nRecall@%d = ",Recall_cutoff);
	fprintf(writePerformanceLog,"\n\nRecall = ");
	for ( z = 0; z < 1; z++ )
	{
			SumDF = (double)((double)RecallFoundQP[z][0]/(double)RecallTotalQP[z][0]);
			SumDF5 = (double)((double)RecallFoundQP[z][1]/(double)RecallTotalQP[z][1]);
			SumDF4 = (double)((double)RecallFoundQP[z][2]/(double)RecallTotalQP[z][2]);
			SumDF3 = (double)((double)RecallFoundQP[z][3]/(double)RecallTotalQP[z][3]);
			SumDF2 = (double)((double)RecallFoundQP[z][4]/(double)RecallTotalQP[z][4]);
			SumDF1 = (double)((double)RecallFoundQP[z][5]/(double)RecallTotalQP[z][5]);

			
			if ( Know_Item_Search_Flag == 10 )
			{
					printf("\n%d	%f	%f	%f	%f	%f	%f",RecallTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
					fprintf(writePerformanceLog,"\n%d	%f	%f	%f	%f	%f	%f",RecallTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
			}


			if ( Know_Item_Search_Flag == 0 || Know_Item_Search_Flag == 2 )
			{
					printf("%f",SumDF);
					fprintf(writePerformanceLog,"%f",SumDF);
			}


	}



	printf("\n\nPrecision@%d = ",Precision_cutoff);
	fprintf(writePerformanceLog,"\n\nPrecision = ");
	for ( z = 0; z < 1; z++ )
	{
			SumDF = (double)((double)PrecisionFoundQP[z][0]/(double)PrecisionTotalQP[z][0]);
			SumDF5 = (double)((double)PrecisionFoundQP[z][1]/(double)PrecisionTotalQP[z][1]);
			SumDF4 = (double)((double)PrecisionFoundQP[z][2]/(double)PrecisionTotalQP[z][2]);
			SumDF3 = (double)((double)PrecisionFoundQP[z][3]/(double)PrecisionTotalQP[z][3]);
			SumDF2 = (double)((double)PrecisionFoundQP[z][4]/(double)PrecisionTotalQP[z][4]);
			SumDF1 = (double)((double)PrecisionFoundQP[z][5]/(double)PrecisionTotalQP[z][5]);

			
			if ( Know_Item_Search_Flag == 10 )
			{
					printf("\n%d	%f	%f	%f	%f	%f	%f",PrecisionTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
					fprintf(writePerformanceLog,"\n%d	%f	%f	%f	%f	%f	%f",PrecisionTotalQP[z][0],SumDF,SumDF5,SumDF4,SumDF3,SumDF2,SumDF1);
			}


			if ( Know_Item_Search_Flag == 0 || Know_Item_Search_Flag == 2 )
			{
					printf("%f",SumDF);
					fprintf(writePerformanceLog,"%f",SumDF);
			}


	}


	printf("\n\n\n");


	fclose(topDocuments);
	fclose(writePerformanceLog);



}


void quicksort__Correlation( long lo , long hi , double *Array, long *ID)
{
		long i = lo, j = hi;
		double h;
		double x = Array[ (lo+hi)/2 ];
		long TempNumber;
		do
		{
				while ( Array[i] < x ) i++;
				while ( Array[j] > x ) j--;
				if ( i <= j )
				{
						h = Array[i];
						Array[i] = Array[j];
						Array[j] = h;



						TempNumber = ID[i];
						ID[i] = ID[j];
						ID[j] = TempNumber;

						
						i++;
						j--;
				}

		}
		while (i<=j);

		if ( lo<j) quicksort__Correlation (lo,j,Array,ID);
		if ( i<hi) quicksort__Correlation (i,hi,Array,ID);
	
}

void Calculate_Correlation(FILE *fptrWriting)
{
		long i = 0;
		long z1 , z2 = 0;
		double GINI = 0;
		double Ratio = 0;
		double BigSum = 0;
	
		long start = 0;
		double VVV = 0;
		long Start2 = 0;
		long TotalC = 0;

		long Temp_Min = 0;
		long Temp_Max = 0;
		long Current_Min = 0;
		long Current_Max = 0;



		unsigned long Similarity;
		unsigned long Similarity1;
		unsigned long Similarity2;
		unsigned long Similarity3;
		unsigned long Similarity4;



		double AverageFindability;

		double FSTemp[200];
		long FSTempID[200];
		long FSTempRank[200];

		double FSCurrent[200];
		long FSCurrentID[200];
		long FSCurrentRank[200];
		int Start = 0;
		int End = GlobalPartitionNumber;
		double SumDF;





		for ( z1 = Start; z1 < End; z1++ )
		{
				SumDF = (double)((double)MRRFoundQP[0][z1+1]/(double)MRRTotalQP[0][z1+1]);
				FSTemp[z1] = SumDF;
				FSTempID[z1] = z1;

		}

	

		for ( z1 = Start; z1 < End; z1++)
		{
				FSCurrent[z1] = GlobalPartitionNumber-z1;
				FSCurrentID[z1] = z1;	
		}

		
	
		

		BigSum = 0;
		quicksort__Correlation(0,(End-Start)-1,FSTemp,FSTempID);


		
		for ( z1 = 0; z1 < (End-Start); z1++ )		
		{
				Temp_Min = FSTemp[z1];
				Temp_Max = 1;
				z2 = z1+1;
				Current_Min = z1;
				while(1)
				{
						if ( FSTemp[z2] != Temp_Min )
							break;

						Temp_Max++;
						Current_Min += z2;
						z2++;
						if ( z2 > (End-Start)-1 )
							break;
				}

					
				if ( Temp_Max > 1 )
				{
					
						AverageFindability = (double)((double)Current_Min/(double)Temp_Max);
						z2 = z1+1;						
						while(1)
						{
								
								if ( FSTemp[z2] != Temp_Min )
									break;

								FSTempRank[FSTempID[z2]] = (int)AverageFindability;
								z2++;

								if ( z2 > (End-Start)-1 )
									break;
						}
				}
				else
				{
						FSTempRank[FSTempID[z1]] = z1;
				}
				


				if ( z1 > (End-Start)-1 )
					break;


				z1 = z2-1;

				

		}

	
		



		quicksort__Correlation(0,(End-Start)-1,FSCurrent,FSCurrentID);



		for ( z1 = 0; z1 < (End-Start); z1++ )		
		{
				Temp_Min = FSCurrent[z1];
				Temp_Max = 1;
				z2 = z1+1;
				Current_Min = z1;
				while(1)
				{
						if ( FSCurrent[z2] != Temp_Min )
							break;

						Temp_Max++;
						Current_Min += z2;
						z2++;
						if ( z2 > (End-Start)-1 )
							break;
				}
				if ( Temp_Max > 1 )
				{
						AverageFindability = (double)((double)Current_Min/(double)Temp_Max);
						z2 = z1+1;						
						while(1)
						{
								
								if ( FSCurrent[z2] != Temp_Min )
									break;

								FSCurrentRank[FSCurrentID[z2]] = (int)AverageFindability;
								z2++;

								if ( z2 > (End-Start)-1 )
									break;

						}
				}
				else
						FSCurrentRank[FSCurrentID[z1]] = z1;
				


				if ( z1 > (End-Start)-1 )
					break;


				z1 = z2-1;

				

		}

		
		Similarity3 = 0;

		for ( z1 = 0; z1 < (End-Start); z1++ )
		{
					Similarity1 = FSTempRank[FSTempID[z1]];
					Similarity2 = FSCurrentRank[FSTempID[z1]];

					Similarity = Similarity1 - Similarity2;
					Similarity = Similarity * Similarity;

					Similarity3 += Similarity;
		}
		Similarity3 = Similarity3 * 6;

	
		Similarity4 =  ( ((End-Start)*(End-Start)) - 1 );
		Similarity4 = (End-Start)* Similarity4;





		
		AverageFindability = (double) ( (double)Similarity3/(double)Similarity4);
		AverageFindability = (double) (1 - AverageFindability);


	//	if ( Similarity4 > 0 && Similarity3 > 0)
			printf("\nCorrelation is	=	%lf\n",AverageFindability);

			fprintf(fptrWriting,"\nCorrelation is	=	%lf\n",AverageFindability);




							







}


void Known_Item_Queries(int clusterNumber)
{
	FILE *fptr,*fptrQrel,*writePerformanceLog,*fptrWriting;
	int queryWord;
	
	char queriesFileName[300];
	int returnValue = 0;
	int z = 0,i,j,k;
	int OldNumber = 0;
	int V = 0;
	int tt = 0;
	int DocumentsLowHighTotal = 0;
//	int TP = 0;
	double Process;
	double SumDF5 = 0,SumDF4 = 0,SumDF3 = 0,SumDF2 = 0,SumDF1 = 0,SumDF = 0;
	int OldPatentNumber = -1;
	int Local_rd_score,local_DocTotalQueries;
	int Find_File;
	int ThreadCount;
	int Test;
	int RandomTopic_PartitionNum;
	int Partition_LowestNum;
	int Parition_TotalDocs;
	int Known_Items_Part = 0;
	int Total_Known_Items = 0;
	int Find5,Find10,Find15,Find20,Find25;

	char RS_Name[300];


	pthread_t thread1[200];  
	thdata data1[200];



	for ( j = 0; j < InputFileTotalPatents; j++ )
	{
			for ( i = 0; i < 40; i ++ )
			{
					MRRFoundQP[j][i] = 0.0;
					MRRTotalQP[j][i] = 0;
			}
	}

	for ( i = 0; i < 55; i ++ )
	{
			for ( j = 0; j < InputFileTotalPatents; j++ )
			{
					for ( k = 0; k < 33; k ++ )
						TermPresence[i][j][k] = -1;
			}
	}
	
	
	Find_File = RSType;
	if ( RSType >= 10 )
		Find_File = 4;

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




	V = 0;
	tt = 0;
	sprintf(queriesFileName,"%sWorkingFolder//%s_extra.txt",sourcePath,RS_Name);//,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 ( fullPatentLength_AllOver[tt] > 0 ) 
			{
					if ( local_DocTotalQueries > 10 )
					{
							if ( PPS == 0 )
								Process = (double) ( (double) Local_rd_score/ (double) local_DocTotalQueries );
							if ( PPS == 1 )
								Process = (double) ( (double) Local_rd_score);
					}
					else
					{
							if ( PPS == 0 )
							{
								if ( fullPatentLength_AllOver[tt] > 0 )
									Process = 1;
								else
									Process = 1;
							}
							if ( PPS == 1 )
								Process = (double) ( (double) Local_rd_score);
					}

					hat_rd_score[V] = Process;
					DocID[V] = V;
					V++;
			}
			else
			{
				//	printf("\n%d....",tt);
			}



			tt++;

			if ( tt >= 100000 )
				break;

	}
	fclose(fptr);





	V = 0;
	tt = 0;


	

	for ( z = 0; z < 200; z ++ )
		QueryItemCount[z] = 0;
	




	GlobalPartitionNumber = 30;
	CreateDocumentsPartitions_rd_score(GlobalPartitionNumber,clusterNumber);




	GlobalPostingAge = 0;
	CriticalSection = 0;
	ThreadCount = 0;
	srand (100);

	RandomTopic_PartitionNum = 0;
	Known_Items_Part = 0;
	Total_Known_Items = 0;
	while(1)
	{

								

								if ( clusterNumber != 1000 )
									Parition_TotalDocs = (InputFileTotalPatents/GlobalPartitionNumber);
								else
									Parition_TotalDocs = (35200/GlobalPartitionNumber);

								Partition_LowestNum = RandomTopic_PartitionNum*Parition_TotalDocs;
								
								Test = (rand()%Parition_TotalDocs); 
								Test = Test + Partition_LowestNum;
								
								Test = DocID[Test];




								Known_Items_Part++;
								Total_Known_Items++;
								if ( Known_Items_Part >= 40 )
								{
										RandomTopic_PartitionNum++; 
										Known_Items_Part = 0;
								}







								for  ( z = 0; z < DocTermsArray_TotalTerms[Test]; z++ )
								{				
										queryWord = DocTermsArray_TermNumber[Test][z];											
										tt = DocTermsArray_TermFrequency[Test][z];											
										if ( ((double)IDFValue[queryWord]/(double)InputFileTotalPatents) < 0.7 && ((double)IDFValue[queryWord]/(double)InputFileTotalPatents) > .002 )
										{
													QueryItemArray[GlobalPostingAge%50][QueryItemCount[GlobalPostingAge%50]] = queryWord;
													QueryItemArray_Freq[GlobalPostingAge%50][QueryItemCount[GlobalPostingAge%50]] = tt;
													QueryItemCount[GlobalPostingAge%50]++;
										}
								}						
									
					

								
								if ( QueryItemCount[GlobalPostingAge%50] > 2 )
								{


												quicksort__Queries(0,QueryItemCount[GlobalPostingAge%50]-1,GlobalPostingAge%50);	
											
												for ( i = 0; i < QueryItemCount[GlobalPostingAge%50]; i++ )
												{
														QueryItemArray_Freq[GlobalPostingAge%50][i] = (rand()%QueryItemCount[GlobalPostingAge%50])+1;

														if ( i > 30 )
															break;
												}
												if ( QueryItemCount[GlobalPostingAge%50] > 30 )
													quicksort__Queries(0,29,GlobalPostingAge%50);
												else
													quicksort__Queries(0,QueryItemCount[GlobalPostingAge%50]-1,GlobalPostingAge%50);



												i = rand()%4;
												
												if ( QueryItemCount[GlobalPostingAge%50] > (i+4) )
														QueryItemCount[GlobalPostingAge%50] = (i+4);					
								
					
												QrelCount[GlobalPostingAge%50] = 0;
												for ( i = 0; i < GlobalPartitionNumber; i++ )
														ComponentQrel[GlobalPostingAge%50][i] = 0;
												
												V = Test;
												Qrel[GlobalPostingAge%50][QrelCount[GlobalPostingAge%50]] = V;
												ComponentQrel[GlobalPostingAge%50][ComponentLabel[V]]++;																			
												QrelCount[GlobalPostingAge%50]++;					




												GlobalPostingAge++;	
										

												if ( (GlobalPostingAge%50) == 0 )
												{
															printf("\n Now Processing Queries ...%d \n",RandomTopic_PartitionNum);

													

															for ( z = 0; z < 50; z ++ )
															{
																	FoundPatentsCount[z] = 0;



																	data1[z].ThreadNumber = z;
																	data1[z].Local_GlobalPostingAge = z+GlobalPostingAge;
																	data1[z].QueryPatentNumber = (ThreadCount*50)+z;
																	pthread_create (&thread1[z], NULL, (void *) &ProcessQueryInInvertedIndex, (void *) &data1[z]);									
																
																	//ProcessQueryInInvertedIndex((void *) &data1[z]);
																	//printf("\n%d....%d",QrelCount[z],FoundPatentsCount[z]);
																

															}
															
														
															ThreadTerminationCount = 0;

															
															for ( z = 0; z  < 50; z++ )
																pthread_join(thread1[z], NULL);

															
															

															//while (1)
															//{
															//	printf("\n%d...",ThreadTerminationCount);
															//	if ( ThreadTerminationCount == 50 )
															//		break;

															//}
															ThreadCount++;

													


															for ( z = 0; z < 50; z++ )
															{
																
																	if ( FoundPatentsCount[z] > 0 )
																	{											

																				AllMRR = 0;
																				for ( i = 0; i < GlobalPartitionNumber; i++ )
																							ComponentMRR[i] = 0;

																				if ( Calculate_MRR_Scores(z) == 1 )
																				{
																							
																					
																																			
																																			
																						//	printf("\n%lf....",AllMRR);

																							MRRFoundQP[0][0] += AllMRR;													
																							MRRTotalQP[0][0]++;
																							for ( i = 0; i < GlobalPartitionNumber; i++ )
																							{
																									MRRFoundQP[0][i+1] += ComponentMRR[i];
																									if ( ComponentQrel[z][i] > 0 )
																										MRRTotalQP[0][i+1]++;
																							}




																							
																							



																				}
																				

																	}//FoundPatentsCount > 0

																	


																	printf("\nQuery Processing At %d..\n",(int)(z+(ThreadCount*50)));

																	QueryItemCount[z] = 0;


															}//loop z end

												 }//GlobalPostingAge50 End



			
						

								}
							
						


						


								if ( GlobalPostingAge%2000 == 0 )
									printf("\n Completed = %d",GlobalPostingAge);



						
								
								RecurNum++;
								if ( Total_Known_Items >= 1200 )
									break;
							

					
								
					

	}










	if ( PPS == 1 )
	{
			sprintf(queriesFileName,"%sWorkingFolder_real//KnownItemsSearch_UnNorm%s.txt",sourcePath,RS_Name);//,fileNumber
			fptrWriting = fopen(queriesFileName, "w");
	}
	if ( PPS == 0 )
	{
			sprintf(queriesFileName,"%sWorkingFolder_real//KnownItemsSearch_Norm%s.txt",sourcePath,RS_Name);//,fileNumber
			fptrWriting = fopen(queriesFileName, "w");
	}


	printf("\n\nMRR\n");
	for ( z = 0; z < 1; z++ )
	{
			SumDF = (double)((double)MRRFoundQP[z][0]/(double)MRRTotalQP[z][0]);

			for ( i = 1; i <= GlobalPartitionNumber; i++ )
			{			
					SumDF = (double)((double)MRRFoundQP[z][i]/(double)MRRTotalQP[z][i]);

					printf("%d	%ld	%f\n",i,MRRTotalQP[z][i],SumDF);

					fprintf(fptrWriting,"%d	%ld	%lf\n",i,MRRTotalQP[z][i],SumDF);
					
			}


	}

	Calculate_Correlation(fptrWriting);
	printf("\n\n\n");

	
	fclose(fptrWriting);

}



int main (int argc, char *argv[])
{                              
	int clusterNumber = 0;
	int patentsCopyingFrom;
	int patentsCopyingTo;
	FILE *prog_settings;
	char m_FileName[300];
	//int fileNumber;

	
	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;


	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 = 0 for Normalized Retreivability and PPS = 1 for Un Norm Retreivability



	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,"%s",topic_File);
	fscanf(prog_settings,"%d",&total_topic_File);
	fscanf(prog_settings,"%s",Qrel_File);
	fscanf(prog_settings,"%d",&Recall_cutoff);
	fscanf(prog_settings,"%d",&Precision_cutoff);
	fscanf(prog_settings,"%s",LowHigh_Labels);
	fscanf(prog_settings,"%s",offline_Cluster_File);
	fscanf(prog_settings,"%s",Term_Posting_File);
	fscanf(prog_settings,"%s",J48Rules);

    fclose(prog_settings);

	
//	Partition_Big_File(clusterNumber,patentsCopyingFrom,patentsCopyingTo);
//	MAREC_Seperation(clusterNumber);
//	exit(1);
//	SOMLib_File_Coversion(clusterNumber,patentsCopyingFrom,patentsCopyingTo);
//	exit(1);
//	LengthRelevanceProbability(clusterNumber,patentsCopyingFrom,patentsCopyingTo);
//	exit(1);
	
//	LengthKNNScoreProbability(clusterNumber,patentsCopyingFrom,patentsCopyingTo);
//	exit(1);


	if ( RSType == 21 )
		J48TreeConstruction();


	


	



	Know_Item_Search_Flag = fileNumber;
	ReadItemFile(clusterNumber,patentsCopyingFrom,patentsCopyingTo);


	
	
	if ( RSType == 10 )
		ReadDocumentSimilarity();

	
	if ( Know_Item_Search_Flag == 0 || Know_Item_Search_Flag == 2 )
		ProcessQueries(clusterNumber);
	else
		Known_Item_Queries(clusterNumber);



/*

	RSType = 1;
	Parameter_Value = 1;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);




	RSType = 2;
	Parameter_Value = .75;
	Know_Item_Search_Flag = 0;
	Make_Weighting_With_Threads();
	ProcessQueries(clusterNumber);


	RSType = 2;
	Parameter_Value = .75;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);





	RSType = 3;
	Parameter_Value = 2000;
	Know_Item_Search_Flag = 0;	
	Make_Weighting_With_Threads();
	ProcessQueries(clusterNumber);


	RSType = 3;
	Parameter_Value = 2000;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);





	RSType = 4;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 0;	
	Make_Weighting_With_Threads();
	ProcessQueries(clusterNumber);


	RSType = 4;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);





	RSType = 5;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 0;	
	Make_Weighting_With_Threads();
	ProcessQueries(clusterNumber);



	RSType = 5;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);





	RSType = 6;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 0;	
	Make_Weighting_With_Threads();
	ProcessQueries(clusterNumber);



	RSType = 6;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);



	RSType = 7;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 0;	
	Make_Weighting_With_Threads();
	ProcessQueries(clusterNumber);



	RSType = 7;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);



	RSType = 8;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 0;	
	Make_Weighting_With_Threads();
	ProcessQueries(clusterNumber);


	RSType = 8;
	Parameter_Value = .7;
	Know_Item_Search_Flag = 2;	
	ProcessQueries(clusterNumber);
*/
	return 0;
}  
