1,105,208 Community Members

how to make a loop that exactly modifies three columns?

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
0
 

i have written following code so far.
But now i am stuck with one porblem.
I have made a loop that exactly modify Numeric Quasi identifier by one by one.
Now problem is:
I hv made loop that modifies exactly once,twice and all four Q.I.(Quasi IDentifier)
How can i make a loop that exactly modify 3 Q.I.?

Also suggest me how can i make this procedure iteratively.As i have made it sequentially.

package CSVRead;

import java.io.*;
import java.util.*;
//import java.util.concurrent.ConcurrentHashMap;

/**
 * @author JAL
 */

public class NewClass {

        public static HashMap<Integer,String[]> m1 = new HashMap<Integer,String[]>();
        public static Map<Integer,String[]> asMap = new TreeMap<Integer,String[]>();
        public static HashMap<Integer,String[]> temp = new HashMap<Integer, String[]>();
        public static HashMap<Integer,String[]> temp2 = new HashMap<Integer, String[]>();
        public static HashMap<Integer,String[]> temp3 = new HashMap<Integer, String[]>();

        static int temp11;
        public static void main(String args[]) throws IOException
        {
            //======================================================================
            int q,i1,loc[],min[],max[],len[];


            //DataInputStream din = new DataInputStream(System.in);
            Scanner input=new Scanner(System.in);

            System.out.println("Enter No. of Quasi Idenifier =");
            //q=Integer.parseInt(din.readLine());
            q=input.nextInt();
            loc= new int[q];
            min=new int[q];
            max=new int[q];
            len=new int[q];

            for(i1=0;i1<q;i1++)
            {
                System.out.println("Enter "+i1+"th Quasi identifier location :");
                //loc[i1]=Integer.parseInt(din.readLine());
                loc[i1]=input.nextInt();
                min[i1]=Integer.MAX_VALUE;
                max[i1]=Integer.MIN_VALUE;
                len[i1]=0;
            }

            System.out.println("Enter the value of K=");
            int k=input.nextInt();

//=========================================================================================================
            BufferedReader CSVFile = new BufferedReader(new FileReader("original dataset1.csv"));
            String dataRow = CSVFile.readLine();
            int count=1;
            int count1 = 1;
            while(dataRow!=null)
            {
                String dataArray[] = dataRow.split(",");
                String dataArray1[] = dataRow.split(",");
                m1.put(count, dataArray1);
                temp.put(count1, dataArray);
                count++;
                count1++;
                dataRow=CSVFile.readLine();
            }
            int a = m1.size();
//======  First Pass ============================================================
      calc1(a,asMap,q,loc,k,temp);
//======FIND MIN AND MAX FROM REMAINING VALUE OF Temp========================================
            int p1=temp.size();
            System.out.println("length of map temp = "+p1);
            Set s1=temp.keySet();
            Iterator it=s1.iterator();

            while(it.hasNext())
            {

                String[] aa1=temp.get(it.next());
                for(int p=0;p<q;p++)
                {
                    System.out.print(aa1[loc[p]]+"\t");
                }
                for(int p=0;p<q;p++)
                {
                    if( min[p] > Integer.parseInt(aa1[loc[p]]))
                    {
                        min[p] = Integer.parseInt(aa1[loc[p]]);
                    }
                    if( max[p] < Integer.parseInt(aa1[loc[p]]))
                    {
                        max[p] = Integer.parseInt(aa1[loc[p]]);
                    }
                    if(min[p]<0)
                    {
                        len[p]=Integer.toString(min[p]).length()-1;
                        if(len[p]>Integer.toString(max[p]).length())
                            len[p]=Integer.toString(min[p]).length();
                        else
                            len[p]=Integer.toString(max[p]).length();
                    }
                    else
                    {
                        len[p]=Integer.toString(min[p]).length();
                        if(len[p]>Integer.toString(max[p]).length())
                            len[p]=Integer.toString(min[p]).length();
                        else
                            len[p]=Integer.toString(max[p]).length();
                    }
                }
                System.out.println();
            }
            for(int p=0;p<q;p++)
            {
                System.out.println("Quasi identifier "+loc[p]+" minimum value "+min[p]);
                System.out.println("Quasi identifier "+loc[p]+" maximum value "+max[p]);
                System.out.println("Quasi identifier "+loc[p]+" length is "+len[p]);
            }
//======================================================================================
            int ano=0,flag=1;
            int ja[]= new int[q];
            double[] anon_gain = new double[q];
            ArrayList<Integer> arrskip = new ArrayList<Integer>();
           for(int b=0;b<q;b++)
           {
               ano=0;
            for(int r = 0;r<q;r++)
            {
                Set s = temp.keySet();
                Iterator it1 = s.iterator();
                int map_size = s.size();
                System.out.println("\n"+s);
                StringBuilder sb = null;
                int rep=0;

                if(arrskip.contains(r))
                {
                    //break;
                    continue;
                }
                else
                {
                while(it1.hasNext())
                {
                    String[] a1 = temp.get((Integer)it1.next());
                    System.out.println("\n /////////////////////////////////////////////////////////////////////////");
                    if(len[r]>2 && !(a1[loc[r]].endsWith("*")))
                    {
                        if(a1[loc[r]].length()==1)
                        {
                            sb = new StringBuilder(a1[loc[r]]);

                            sb.setCharAt(0,'*');                            
                            a1[loc[r]]=sb.toString();
                        }
                        else
                        {
                            sb = new StringBuilder(a1[loc[r]]);
                            rep = a1[loc[r]].length()-1;                            
                            sb.setCharAt(rep,'*');
                            a1[loc[r]]=sb.toString();
                        }
                    }
                    if(len[r]<=2)
                    {
                        int x = Integer.parseInt(a1[loc[r]]);
                        if(x>=0 && x<5){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[0-5]");}
                        if (x >= 5 && x < 10) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[5-10]");}
                        if (x >= 10 && x < 15) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[10-15]");}
                        if(x >= 15 && x < 20) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[15-20]");}
                        if(x >= 20 && x < 25) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[20-25]");}
                        if(x >= 25 && x < 30) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[25-30]");}
                        if(x >= 30 && x < 35) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[30-35]");}
                        if(x>=35 && x<40){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[35-40]");}
                        if(x>=40 && x<45){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[40-45]");}
                        if(x>=45 && x<50){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[45-50]");}
                        if(x>=50 && x<55){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[50-55]");}
                        if(x>=55 && x<60){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[55-60]");}
                        if(x>=60 && x<65){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[60-65]");}
                        if(x>=65 && x<70){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[65-70]");}
                        if(x>=70 && x<75){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[70-75]");}
                        if(x>=75 && x<80){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[75-80]");}
                        if(x>=80 && x<85){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[80-85]");}
                        if(x>=85 && x<90){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[85-90]");}
                        if(x>=90 && x<95){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[90-95]");}
                        if(x>=95 && x<100){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[95-100]");}
                        if(x>=100 && x<2000){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[100-2000]");}
                    }

                    for(int as=0;as<a1.length;as++)
                    {
                        System.out.print(a1[as]+"   \t");
                    }

                }

                ja[r]=test(a,asMap,q,loc,k,temp);

                ano=ano_gain(r,ja,loc,map_size,q,ano,anon_gain);

                System.out.println("\nvalue to ano q.i = "+loc[ano]+" location is = "+ano);

                replace(m1,temp);

                } 
            }

                vis(a,ano,temp,q,len,loc,k);

                for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet())
                {
                    int key3 = mapEntry2.getKey();
                    //System.out.print("\nvalue of key3 = "+key3);
                    String[] aa5 = (String[]) temp.get(key3);
                    String[] newAA = new String[aa5.length];  // create a new array with the same length
                    System.arraycopy(aa5, 0, newAA, 0, aa5.length);
                    temp2.put(key3, newAA);
                }
                replace(m1,temp);
                arrskip.add((Integer)ano);
           }
//=========================================================================================
 System.out.println("==========  Map - asMap  =============================================");
//=========================================================================================
//=================================================================================================
//2nd time

arrskip.clear();
for(int b=0;b<q;b++){
    ano=0;
for(int r = 0;r<q;r++)
            {
                Set s = temp.keySet();
                Iterator it1 = s.iterator();
                int map_size = s.size();
                System.out.println("\n"+s);
                StringBuilder sb = null;
                int rep=0;
                if(arrskip.contains(r))
                {
                    continue;
                }
                else
                {

                while(it1.hasNext())
                {
                    String[] a1 = temp.get((Integer)it1.next());
                    System.out.println("\n /////////////////////////////////////////////////////////////////////////");
                    if(len[r]>2 && !(a1[loc[r]].endsWith("*")))
                    {
                        if(a1[loc[r]].length()==1)
                        {
                            sb = new StringBuilder(a1[loc[r]]);
                            //rep = a1[loc[r]].length()-1;
                            sb.setCharAt(0,'*');
                            a1[loc[r]]=sb.toString();
                        }
                        else
                        {
                            sb = new StringBuilder(a1[loc[r]]);
                            rep = a1[loc[r]].length()-1;
                            sb.setCharAt(rep,'*');
                            a1[loc[r]]=sb.toString();
                        }
                    }
                    if(len[r]<=2 && !(a1[loc[r]].endsWith("]")))
                    {
                        int x = Integer.parseInt(a1[loc[r]]);
                        if(x>=0 && x<5){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[0-5]");}
                        if (x >= 5 && x < 10) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[5-10]");}
                        if (x >= 10 && x < 15) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[10-15]");}
                        if(x >= 15 && x < 20) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[15-20]");}
                        if(x >= 20 && x < 25) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[20-25]");}
                        if(x >= 25 && x < 30) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[25-30]");}
                        if(x >= 30 && x < 35) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[30-35]");}
                        if(x>=35 && x<40){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[35-40]");}
                        if(x>=40 && x<45){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[40-45]");}
                        if(x>=45 && x<50){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[45-50]");}
                        if(x>=50 && x<55){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[50-55]");}
                        if(x>=55 && x<60){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[55-60]");}
                        if(x>=60 && x<65){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[60-65]");}
                        if(x>=65 && x<70){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[65-70]");}
                        if(x>=70 && x<75){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[70-75]");}
                        if(x>=75 && x<80){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[75-80]");}
                        if(x>=80 && x<85){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[80-85]");}
                        if(x>=85 && x<90){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[85-90]");}
                        if(x>=90 && x<95){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[90-95]");}
                        if(x>=95 && x<100){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[95-100]");}
                        if(x>=100 && x<2000){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[100-2000]");}
                    }

                    for(int as=0;as<a1.length;as++)
                    {
                        System.out.print(a1[as]+"   \t");
                    }

                }
                }
                ja[r]=test(a,asMap,q,loc,k,temp);

                if(ja[r]>0){flag = 0;}else{flag = 1;}

                ano=ano_gain(r,ja,loc,map_size,q,ano,anon_gain);

                System.out.println("\nvalue to ano q.i = "+loc[ano]+" location is = "+ano);

                replace(temp2,temp);   
            }
vis(a,ano,temp,q,len,loc,k);

for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet())
                {
                    int key3 = mapEntry2.getKey();
                    //System.out.print("\nvalue of key3 = "+key3);
                    String[] aa5 = (String[]) temp.get(key3);
                    String[] newAA = new String[aa5.length];  // create a new array with the same length
                    System.arraycopy(aa5, 0, newAA, 0, aa5.length);
                    temp3.put(key3, newAA);
                }
replace(m1,temp);

arrskip.add((Integer)ano);
}              

//3rd time*/
//**********************************************************
System.out.println("Size of temp map is = "+temp.size());
System.out.println("Size of temp asMap is = "+asMap.size());
System.out.println("Size of temp temp3 is = "+temp3.size());
//=================================0000==========================

for(Map.Entry<Integer, String[]> mapEntry1 : temp3.entrySet())
        {
        int key2 = mapEntry1.getKey();
        String[] aaa=mapEntry1.getValue();
        System.out.print("Key=> "+key2);
        for(int p=0;p<aaa.length;p++)
        {
        System.out.print("\t"+aaa[p]+"\t");
        }
        System.out.println();
        }

arrskip.clear();
replace(m1,temp);
for(int r=0;r<q;r++)
{

            Set s = temp.keySet();
                Iterator it1 = s.iterator();
                int map_size = s.size();
                System.out.println("\n"+s);
                StringBuilder sb = null;
                int rep=0;
                if(arrskip.contains(r))
                {
                    continue;
                }
                else
                {

                while(it1.hasNext())
                {
                    String[] a1 = temp.get((Integer)it1.next());
                    System.out.println("\n /////////////////////////////////////////////////////////////////////////");
                    if(len[r]>2 && !(a1[loc[r]].endsWith("*")))
                    {
                        if(a1[loc[r]].length()==1)
                        {
                            sb = new StringBuilder(a1[loc[r]]);

                            sb.setCharAt(0,'*');
                            a1[loc[r]]=sb.toString();
                        }
                        else
                        {
                            sb = new StringBuilder(a1[loc[r]]);
                            rep = a1[loc[r]].length()-1;
                            sb.setCharAt(rep,'*');
                            a1[loc[r]]=sb.toString();
                        }
                    }
                    if(len[r]<=2 && !(a1[loc[r]].endsWith("]")))
                    {
                        int x = Integer.parseInt(a1[loc[r]]);
                        if(x>=0 && x<5){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[0-5]");}
                        if (x >= 5 && x < 10) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[5-10]");}
                        if (x >= 10 && x < 15) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[10-15]");}
                        if(x >= 15 && x < 20) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[15-20]");}
                        if(x >= 20 && x < 25) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[20-25]");}
                        if(x >= 25 && x < 30) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[25-30]");}
                        if(x >= 30 && x < 35) {a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), "[30-35]");}
                        if(x>=35 && x<40){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[35-40]");}
                        if(x>=40 && x<45){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[40-45]");}
                        if(x>=45 && x<50){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[45-50]");}
                        if(x>=50 && x<55){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[50-55]");}
                        if(x>=55 && x<60){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[55-60]");}
                        if(x>=60 && x<65){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[60-65]");}
                        if(x>=65 && x<70){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[65-70]");}
                        if(x>=70 && x<75){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[70-75]");}
                        if(x>=75 && x<80){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[75-80]");}
                        if(x>=80 && x<85){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[80-85]");}
                        if(x>=85 && x<90){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[85-90]");}
                        if(x>=90 && x<95){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[90-95]");}
                        if(x>=95 && x<100){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[95-100]");}
                        if(x>=100 && x<2000){a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x),"[100-2000]");}
                    }

                    for(int as=0;as<a1.length;as++)
                    {
                        System.out.print(a1[as]+"   \t");
                    }

                }
                }
                ja[r]=test(a,asMap,q,loc,k,temp);

                if(ja[r]>0){flag = 0;}else{flag = 1;}

                ano=ano_gain(r,ja,loc,map_size,q,ano,anon_gain);

                System.out.println("\nvalue to ano q.i = "+loc[ano]+" location is = "+ano);

            }
vis(a,ano,temp,q,len,loc,k);

System.out.println("Size of temp map is = "+temp.size());
System.out.println("Size of temp asMap is = "+asMap.size());
System.out.println("Size of temp temp3 is = "+temp3.size());
}

public static void replace(HashMap<Integer,String[]> m1,HashMap<Integer,String[]> temp)
{
    for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet())
                {
                    int key3 = mapEntry2.getKey();
                    //System.out.print("\nvalue of key3 = "+key3);
                    String[] aa5 = (String[]) m1.get(key3);
                    String[] newAA = new String[aa5.length];  // create a new array with the same length
                    System.arraycopy(aa5, 0, newAA, 0, aa5.length);
                    temp.put(key3, newAA);
                }
}

public static int ano_gain(int r,int[] ja,int[] loc,int map_size,int q,int an,double[] anon_gain)
{
                System.out.println("\nValue inside ano_gain loop r = "+r);

                anon_gain[r] = (ja[r]*100)/map_size;
                System.out.println("\nAnonymity gain = "+anon_gain[r]+" %");
                System.out.println("\nNo. of tuples anonymized at Q.I. "+loc[r]+" = "+ja[r]);
                System.out.println("\ninside gain b4 Value of temp11="+temp11+" and value of an = "+an);
                if(r==0){temp11=(int)anon_gain[0];}
                if(r>0)
                {
                    if((int)anon_gain[r]>temp11)
                    {
                        temp11 = (int) anon_gain[r];
                        an = r;
                    }
                }
                System.out.println("\ninside gain after Value of temp11="+temp11+" and value of an = "+an);
                return an;
}

public static void disp(Map<Integer,String[]> m1,Map<Integer,String[]> asMap,Map<Integer,String[]> temp)
{
        System.out.println("\n=============== MAP - m1============================================");
for(Map.Entry<Integer,String[]> mapEntry: m1.entrySet())
        {
        int key1 = mapEntry.getKey();
        String[] aa4=(String[]) mapEntry.getValue();
        System.out.print("Key=> "+key1);
        for(int p=0;p<aa4.length;p++)
        {
        System.out.print("\t"+aa4[p]+"\t");
        }
        System.out.println();
        }
System.out.println("==========  Map - asMap  =============================================");
//=========================================================================================
for(Map.Entry<Integer, String[]> mapEntry1 : asMap.entrySet())
        {
        int key2 = mapEntry1.getKey();
        String[] aaa=mapEntry1.getValue();
        System.out.print("Key=> "+key2);
        for(int p=0;p<aaa.length;p++)
        {
        System.out.print("\t"+aaa[p]+"\t");
        }
        System.out.println();
        }
//==========================================================================================
System.out.println("---------------  Map - temp  ------------------------------------------");
for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet())
        {
        int key3 = mapEntry2.getKey();
        String[] aa5=mapEntry2.getValue();

        System.out.print("Key=> "+key3);
        for(int p=0;p<aa5.length;p++)
        {
        System.out.print("\t   "+aa5[p]+"\t");
        }
        System.out.println();

     }
    }
public static void calc1(int a,Map asMap,int q,int loc[],int k,Map temp)
{   
        int am[]=new int[a];
        int row[]=new int[a];
        int remove[] = new int[a];
        int flagp[] = new int[a];
        int f1[] = new int[a];
        for(int t=0;t<a;t++){am[t]=0; row[t]=0;remove[t]=0;flagp[t]=0;f1[t]=0;}

        for(int j=1;j<=a;j++)
        {
            //System.out.println("Value of j = "+j);
          int cnt1=0;
          if(am[j-1]==1)
          {
              //break;
              continue;
          }
          else
          {
          for(int p=j+1;p<=a;p++)
          {

                //System.out.println("Value of p = "+p);
              //System.out.println("hello");
               String[] aa2=null;
               String[] aa3=null;

               if(temp.containsKey(j))
               {
                 aa2 =  (String[]) temp.get(j);
              }
               else{continue;}
              if(temp.containsKey(p))
                {
                 aa3 =  (String[]) temp.get(p);
              }
                else{continue;}
              int h=0;
              int cnt=0;
              while(h<q)
              {
              if(aa2[loc[h]].equals(aa3[loc[h]]))
              {
                  cnt++;
              }
              h++;
              }
              if(cnt==q)
              {
                  remove[j-1]=1;
                  remove[p-1]=1;
                  cnt1++;
                  am[p-1]=1;
                  row[j-1]=cnt1+1;
                  }
              }
             if(row[j-1]>=k)
                  {
                        for(int p1=j-1;p1<a;p1++)
                        {
                            if(remove[p1]==1 && flagp[p1]==0)
                            {
                                flagp[p1]=1;
                                f1[p1]=p1+1;
                                asMap.put(p1+1,temp.get(p1+1));
                            }
                        }
                  }
          }
        }
        for(int jj=0;jj<f1.length;jj++)
        {

            if(f1[jj]!=0)
                if(temp.containsKey(f1[jj]))
                {
                    temp.remove(f1[jj]);
                }
                else
                {
                    System.out.println("No such key is found...so value doesn't deleted");
                }
            }
}

public static int test(int a,Map asMap,int q,int loc[],int k,Map temp)
{
        int am[]=new int[a];
        int row[]=new int[a];
        int remove[] = new int[a];
        int flagp[] = new int[a];
        int f1[] = new int[a];
        for(int t=0;t<a;t++){am[t]=0; row[t]=0;remove[t]=0;flagp[t]=0;f1[t]=0;}

        for(int j=1;j<=a;j++)
        {

          int cnt1=0;
          if(am[j-1]==1)
          {
              continue;
          }
          else
          {
          for(int p=j+1;p<=a;p++)
          {
               String[] aa2=null;
               String[] aa3=null;

               if(temp.containsKey(j))
               {
                 aa2 =  (String[]) temp.get(j);
              }
               else{continue;}
              if(temp.containsKey(p))
                {
                 aa3 =  (String[]) temp.get(p);
              }
                else{continue;}

              int h=0;
              int cnt=0;
              while(h<q)
              {

              if(aa2[loc[h]].equals(aa3[loc[h]]))
              {
                  cnt++;
              }
              h++;

              }
              if(cnt==q)
              {
                  remove[j-1]=1;
                  remove[p-1]=1;
                  cnt1++;
                  am[p-1]=1;
                  row[j-1]=cnt1+1;
                  }
              }
             if(row[j-1]>=k)
                  {
                        for(int p1=j-1;p1<a;p1++)
                        {
                        if(remove[p1]==1 && flagp[p1]==0)
                            {
                                flagp[p1]=1;            
                                f1[p1]=p1+1;            
                            }
                        }
                  }
          }
        }
int counter=0;
        for(int jj=0;jj<f1.length;jj++)
        {
            if(f1[jj]!=0)

                if(temp.containsKey(f1[jj]))
                {
                    counter++;

                }
                else
                {
                    System.out.println("No such key is found...so value doesn't deleted");
                }  
        }
        return counter;
}

    private static void vis(int a,int ano, Map temp,int q,int[] len,int[] loc,int k) {

                Set s = temp.keySet();
                Iterator it1 = s.iterator();
                StringBuilder sb = null;
                int rep=0;
                System.out.println("size of temp b4 anon = "+s.size());
                while(it1.hasNext())
                {
                    String[] a1 = (String[]) temp.get((Integer)it1.next());

                    if(len[ano]>2 && !(a1[loc[ano]].endsWith("*")))
                    {
                        if(a1[loc[ano]].length()==1)
                        {
                            sb = new StringBuilder(a1[loc[ano]]);
                            //rep = a1[loc[r]].length()-1;
                            sb.setCharAt(0,'*');
                            a1[loc[ano]]=sb.toString();
                        }
                        else
                        {
                            sb = new StringBuilder(a1[loc[ano]]);
                            rep = a1[loc[ano]].length()-1;
                            sb.setCharAt(rep,'*');
                            a1[loc[ano]]=sb.toString();
                        }
                    }
                    if(len[ano]<=2 && !(a1[loc[ano]].endsWith("]")))
                    {
                        int x = Integer.parseInt(a1[loc[ano]]);
                        if(x>=0 && x<5){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[0-5]");}
                        if (x >= 5 && x < 10) {a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x), "[5-10]");}
                        if (x >= 10 && x < 15) {a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x), "[10-15]");}
                        if(x >= 15 && x < 20) {a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x), "[15-20]");}
                        if(x >= 20 && x < 25) {a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x), "[20-25]");}
                        if(x >= 25 && x < 30) {a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x), "[25-30]");}
                        if(x >= 30 && x < 35) {a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x), "[30-35]");}
                        if(x>=35 && x<40){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[35-40]");}
                        if(x>=40 && x<45){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[40-45]");}
                        if(x>=45 && x<50){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[45-50]");}
                        if(x>=50 && x<55){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[50-55]");}
                        if(x>=55 && x<60){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[55-60]");}
                        if(x>=60 && x<65){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[60-65]");}
                        if(x>=65 && x<70){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[65-70]");}
                        if(x>=70 && x<75){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[70-75]");}
                        if(x>=75 && x<80){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[75-80]");}
                        if(x>=80 && x<85){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[80-85]");}
                        if(x>=85 && x<90){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[85-90]");}
                        if(x>=90 && x<95){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[90-95]");}
                        if(x>=95 && x<100){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[95-100]");}
                        if(x>=100 && x<2000){a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x),"[100-2000]");}
                    }
                }
                calc1(a,asMap,q,loc,k,temp);
                Set ss1=temp.keySet();
                System.out.println("size of temp after anon = "+ss1.size());
                }
}

Thanks
I have also attached dataset1.csv file that i have used.(just remove the .txt extension)
And when you run your program, input given is
No. of Q.I.
4
Location of Q.I. 0
1
Location of Q.I. 1
3
Location of Q.I. 2
4
Location of Q.I. 3
6
Value of k
3

please reply me as soon as possible.

Again thanks in advance.

Attachments original_dataset1.csv_.txt (224.38KB)
Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
1
 

The code is so lengthy, redundant, and overwhelming. You copy and paste a whole code section to do 1st, 2nd, and 3rd time instead of pulling the whole chunk that you are going to reuse into another method. Your variable naming is so bad that I cannot follow it even though I scroll up and down to look at the code over and over again. Even worse, you have NO COMMENT in your code that explains what you are doing in each method! What calc1 does??? If you have a couple hundred lines of code, you need to give a lot more information than this. It is rude to dump your code, let others figure, and ask questions especially when the code is this ugly.

I have to give up on cracking this code because it is so bad. After formatting the whole code, I am too tired to deal with it. If you ever come back to look at this, you need to make comments on your code (at least on the formatted code below).

Below is the format I adjusted and modified certain things to make it shorter, such as rearrange all indentations, get rid of inefficient if-else statements, remove useless else statement, remove some debugging messages (println()), implement range string from an integer, etc. There are still a lot to do... Tired right now...

public class NewClass {

  public static HashMap<Integer,String[]> m1 = new HashMap<Integer,String[]>();
  public static Map<Integer,String[]> asMap = new TreeMap<Integer,String[]>();
  public static HashMap<Integer,String[]> temp = new HashMap<Integer, String[]>();
  public static HashMap<Integer,String[]> temp2 = new HashMap<Integer, String[]>();
  public static HashMap<Integer,String[]> temp3 = new HashMap<Integer, String[]>();

  static int temp11;
  public static void main(String args[]) throws IOException {
    int q,i1,loc[],min[],max[],len[];

    Scanner input=new Scanner(System.in);

    System.out.println("Enter No. of Quasi Idenifier =");
    q=input.nextInt();
    loc= new int[q];
    min=new int[q];
    max=new int[q];
    len=new int[q];

    for(i1=0;i1<q;i1++) {
      System.out.println("Enter "+i1+"th Quasi identifier location :");
      //loc[i1]=Integer.parseInt(din.readLine());
      loc[i1]=input.nextInt();
      min[i1]=Integer.MAX_VALUE;
      max[i1]=Integer.MIN_VALUE;
      len[i1]=0;
    }

    System.out.println("Enter the value of K=");
    int k=input.nextInt();

    BufferedReader CSVFile = new BufferedReader(new FileReader("original dataset1.csv"));
    String dataRow = CSVFile.readLine();
    int count = 1;
    while(dataRow!=null) {
      m1.put(count, dataRow.split(","));
      temp.put(count, dataRow.split(","));
      count++;
      dataRow=CSVFile.readLine();
    }
    int a = m1.size();

    calc1(a,asMap,q,loc,k,temp);
    int p1=temp.size();
    System.out.println("length of map temp = "+p1);
    Set s1=temp.keySet();
    Iterator it=s1.iterator();

    while(it.hasNext()) {
      String[] aa1=temp.get(it.next());
      for(int p=0;p<q;p++) { System.out.print(aa1[loc[p]]+"\t"); }
      for(int p=0;p<q;p++) {
        if( min[p] > Integer.parseInt(aa1[loc[p]])) {
          min[p] = Integer.parseInt(aa1[loc[p]]);
        }
        if( max[p] < Integer.parseInt(aa1[loc[p]])) {
          max[p] = Integer.parseInt(aa1[loc[p]]);
        }
        if(min[p]<0) {
          len[p]=Integer.toString(min[p]).length()-1;
          if(len[p]>Integer.toString(max[p]).length())
            len[p]=Integer.toString(min[p]).length();
          else
            len[p]=Integer.toString(max[p]).length();
        }
        else {
          len[p]=Integer.toString(min[p]).length();
          if(len[p]>Integer.toString(max[p]).length())
            len[p]=Integer.toString(min[p]).length();
          else
            len[p]=Integer.toString(max[p]).length();
        }
      }
      System.out.println();
    }

    for(int p=0;p<q;p++) {
      System.out.println("Quasi identifier "+loc[p]+" minimum value "+min[p]);
      System.out.println("Quasi identifier "+loc[p]+" maximum value "+max[p]);
      System.out.println("Quasi identifier "+loc[p]+" length is "+len[p]);
    }

    int ano=0,flag=1;
    int ja[]= new int[q];
    double[] anon_gain = new double[q];
    ArrayList<Integer> arrskip = new ArrayList<Integer>();
    for(int b=0;b<q;b++) {
      ano=0;
      for(int r = 0;r<q;r++) {
        Set s = temp.keySet();
        Iterator it1 = s.iterator();
        int map_size = s.size();
        System.out.println("\n"+s);
        StringBuilder sb = null;
        int rep=0;

        if(arrskip.contains(r)) { continue; }
        while(it1.hasNext()) {
          String[] a1 = temp.get((Integer)it1.next());
          System.out.println("\n       ///////////////////////////////////////");
          if(len[r]>2 && !(a1[loc[r]].endsWith("*"))) {
            if(a1[loc[r]].length()==1) {
              sb = new StringBuilder(a1[loc[r]]);
              sb.setCharAt(0,'*');              
              a1[loc[r]]=sb.toString();
            }
            else {
              sb = new StringBuilder(a1[loc[r]]);
              rep = a1[loc[r]].length()-1;              
              sb.setCharAt(rep,'*');
              a1[loc[r]]=sb.toString();
            }
          }
          if(len[r]<=2) {
            String x = a1[loc[r]];
            String replaceString = getRangeString(x);
            if (!replaceString.isEmpty()) {
              a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), replaceString);
            }
          }

          for(int as=0;as<a1.length;as++) { System.out.print(a1[as]+"  \t"); }
        }

        ja[r]=test(a,asMap,q,loc,k,temp);
        ano=ano_gain(r,ja,loc,map_size,q,ano,anon_gain);
        System.out.println("\nvalue to ano q.i = "+loc[ano]+" location is = "+ano);
        replace(m1,temp);
      }

      vis(a,ano,temp,q,len,loc,k);
      for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet()) {
        int key3 = mapEntry2.getKey();
        //System.out.print("\nvalue of key3 = "+key3);
        String[] aa5 = (String[]) temp.get(key3);
        String[] newAA = new String[aa5.length]; // same length array
        System.arraycopy(aa5, 0, newAA, 0, aa5.length);
        temp2.put(key3, newAA);
      }
      replace(m1,temp);
      arrskip.add((Integer)ano);
    }


    // 2nd time
    arrskip.clear();
    for(int b=0;b<q;b++) {
      ano=0;
      for(int r = 0;r<q;r++) {
        Set s = temp.keySet();
        Iterator it1 = s.iterator();
        int map_size = s.size();
        System.out.println("\n"+s);
        StringBuilder sb = null;
        int rep=0;
        if(arrskip.contains(r)) { continue; }

        while(it1.hasNext()) {
          String[] a1 = temp.get((Integer)it1.next());
          System.out.println("\n             //////////////////////////////////");
          if(len[r]>2 && !(a1[loc[r]].endsWith("*"))) {
            if(a1[loc[r]].length()==1) {
              sb = new StringBuilder(a1[loc[r]]);
              //rep = a1[loc[r]].length()-1;
              sb.setCharAt(0,'*');
              a1[loc[r]]=sb.toString();
            }
            else {
              sb = new StringBuilder(a1[loc[r]]);
              rep = a1[loc[r]].length()-1;
              sb.setCharAt(rep,'*');
              a1[loc[r]]=sb.toString();
            }
          }
          if(len[r]<=2 && !(a1[loc[r]].endsWith("]"))) {
            String x = a1[loc[r]];
            String replaceString = getRangeString(x);
            if (!replaceString.isEmpty()) {
              a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), replaceString);
            }
          }

          for(int as=0;as<a1.length;as++) { System.out.print(a1[as]+"  \t"); }
        }

        ja[r]=test(a,asMap,q,loc,k,temp);
        flag = ja[r]>0 ? 0 : 1;  // better version of if-else below -- ternary
  //      if(ja[r]>0) { flag = 0; }
  //      else { flag = 1; }

        ano=ano_gain(r,ja,loc,map_size,q,ano,anon_gain);
        System.out.println("\nvalue to ano q.i = "+loc[ano]+" location is = "+ano);
        replace(temp2,temp);  
      }
      vis(a,ano,temp,q,len,loc,k);

      for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet()) {
        int key3 = mapEntry2.getKey();
        //System.out.print("\nvalue of key3 = "+key3);
        String[] aa5 = (String[]) temp.get(key3);
        String[] newAA = new String[aa5.length]; // array with the same length
        System.arraycopy(aa5, 0, newAA, 0, aa5.length);
        temp3.put(key3, newAA);
      }
      replace(m1,temp);
      arrskip.add((Integer)ano);
    }

    //3rd time
    System.out.println("Size of temp map is = "+temp.size());
    System.out.println("Size of temp asMap is = "+asMap.size());
    System.out.println("Size of temp temp3 is = "+temp3.size());

    for(Map.Entry<Integer, String[]> mapEntry1 : temp3.entrySet()) {
      int key2 = mapEntry1.getKey();
      String[] aaa=mapEntry1.getValue();
      System.out.print("Key=> "+key2);
      for(int p=0;p<aaa.length;p++) { System.out.print("\t"+aaa[p]+"\t"); }
      System.out.println();
    }

    arrskip.clear();
    replace(m1,temp);
    for(int r=0;r<q;r++) {
      Set s = temp.keySet();
      Iterator it1 = s.iterator();
      int map_size = s.size();
      System.out.println("\n"+s);
      StringBuilder sb = null;
      int rep=0;
      if(arrskip.contains(r)) { continue; }

      while(it1.hasNext()) {
        String[] a1 = temp.get((Integer)it1.next());
        System.out.println("\n            //////////////////////////////////");
        if(len[r]>2 && !(a1[loc[r]].endsWith("*"))) {
          if(a1[loc[r]].length()==1) {
            sb = new StringBuilder(a1[loc[r]]);
            sb.setCharAt(0,'*');
            a1[loc[r]]=sb.toString();
          }
          else {
            sb = new StringBuilder(a1[loc[r]]);
            rep = a1[loc[r]].length()-1;
            sb.setCharAt(rep,'*');
            a1[loc[r]]=sb.toString();
          }
        }
        if(len[r]<=2 && !(a1[loc[r]].endsWith("]"))) {
          String x = a1[loc[r]];
          String replaceString = getRangeString(x);
          if (!replaceString.isEmpty()) {
            a1[loc[r]] = a1[loc[r]].replace(String.valueOf(x), replaceString);
          }
        }

        for(int as=0;as<a1.length;as++) { System.out.print(a1[as]+"  \t"); }
      }

      ja[r]=test(a,asMap,q,loc,k,temp);
      flag = ja[r]>0 ? 0 : 1;  // better version of if-else below -- ternary
  //    if(ja[r]>0) { flag = 0; }
  //    else { flag = 1; }

      ano=ano_gain(r,ja,loc,map_size,q,ano,anon_gain);
      System.out.println("\nvalue to ano q.i = "+loc[ano]+" location is = "+ano);
    }
    vis(a,ano,temp,q,len,loc,k);

    System.out.println("Size of temp map is = "+temp.size());
    System.out.println("Size of temp asMap is = "+asMap.size());
    System.out.println("Size of temp temp3 is = "+temp3.size());
  }

  public static void replace(HashMap<Integer,String[]> m1,
                             HashMap<Integer,String[]> temp) {
    for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet()) {
      int key3 = mapEntry2.getKey();
      //System.out.print("\nvalue of key3 = "+key3);
      String[] aa5 = (String[]) m1.get(key3);
      String[] newAA = new String[aa5.length]; // create a new array with the same length
      System.arraycopy(aa5, 0, newAA, 0, aa5.length);
      temp.put(key3, newAA);
    }
  }

  public static int ano_gain(int r, int[] ja, int[] loc, int map_size,
                             int q, int an, double[] anon_gain) {
    System.out.println("\nValue inside ano_gain loop r = "+r);

    anon_gain[r] = (ja[r]*100)/map_size;
    System.out.println("\nAnonymity gain = "+anon_gain[r]+" %");
    System.out.println("\nNo. of tuples anonymized at Q.I. "+loc[r]+" = "+ja[r]);
    System.out.println("\ninside gain b4 Value of temp11="+temp11+" and value of an = "+an);
    if(r==0) { temp11=(int)anon_gain[0]; }
    else if(r>0) {
      if((int)anon_gain[r]>temp11) {
        temp11 = (int) anon_gain[r];
        an = r;
      }
    }
    System.out.println("\ninside gain after Value of temp11="+temp11+" and value of an = "+an);
    return an;
  }

  public static void disp(Map<Integer,String[]> m1,
                          Map<Integer,String[]> asMap,
                          Map<Integer,String[]> temp) {
    for(Map.Entry<Integer,String[]> mapEntry: m1.entrySet()) {
      int key1 = mapEntry.getKey();
      String[] aa4=(String[]) mapEntry.getValue();
      System.out.print("Key=> "+key1);
      for(int p=0;p<aa4.length;p++) { System.out.print("\t"+aa4[p]+"\t"); }
      System.out.println();
    }

    for(Map.Entry<Integer, String[]> mapEntry1 : asMap.entrySet()) {
      int key2 = mapEntry1.getKey();
      String[] aaa=mapEntry1.getValue();
      System.out.print("Key=> "+key2);
      for(int p=0;p<aaa.length;p++) { System.out.print("\t"+aaa[p]+"\t"); }
      System.out.println();
    }

    for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet()) {
      int key3 = mapEntry2.getKey();
      String[] aa5=mapEntry2.getValue();

      System.out.print("Key=> "+key3);
      for(int p=0;p<aa5.length;p++) { System.out.print("\t  "+aa5[p]+"\t"); }
      System.out.println();
    }
  }


  public static void calc1(int a,Map asMap,int q,int loc[],int k,Map temp) {  
    int am[]=new int[a];
    int row[]=new int[a];
    int remove[] = new int[a];
    int flagp[] = new int[a];
    int f1[] = new int[a];
    for(int t=0;t<a;t++) { am[t]=0; row[t]=0;remove[t]=0;flagp[t]=0;f1[t]=0; }

    for(int j=1;j<=a;j++) {
      int cnt1=0;
      if(am[j-1]==1) { continue; }

      for(int p=j+1;p<=a;p++) {
        String[] aa2=null;
        String[] aa3=null;

        if(temp.containsKey(j)) { aa2 = (String[]) temp.get(j); }
        else { continue; }
        if(temp.containsKey(p)) { aa3 = (String[]) temp.get(p); }
        else { continue; }
        int h=0;
        int cnt=0;
        while(h<q) {
          if(aa2[loc[h]].equals(aa3[loc[h]])) { cnt++; }
          h++;
        }
        if(cnt==q) {
          remove[j-1]=1;
          remove[p-1]=1;
          cnt1++;
          am[p-1]=1;
          row[j-1]=cnt1+1;
        }
      }
      if(row[j-1]>=k) {
        for(int p1=j-1;p1<a;p1++) {
          if(remove[p1]==1 && flagp[p1]==0) {
            flagp[p1]=1;
            f1[p1]=p1+1;
            asMap.put(p1+1,temp.get(p1+1));
          }
        }
      }
    }

    for(int jj=0;jj<f1.length;jj++) {
      if(f1[jj]!=0)
        if(temp.containsKey(f1[jj])) { temp.remove(f1[jj]); }
        else {
          System.out.println("No such key is found...so value doesn't deleted");
        }
    }
  }

  public static int test(int a,Map asMap,int q,int loc[],int k,Map temp) {
    int am[]=new int[a];
    int row[]=new int[a];
    int remove[] = new int[a];
    int flagp[] = new int[a];
    int f1[] = new int[a];
    for(int t=0;t<a;t++) { am[t]=0; row[t]=0;remove[t]=0;flagp[t]=0;f1[t]=0; }

    for(int j=1;j<=a;j++) {
      int cnt1=0;
      if(am[j-1]==1) { continue; }

      for(int p=j+1;p<=a;p++) {
        String[] aa2=null;
        String[] aa3=null;

        if(temp.containsKey(j)) { aa2 = (String[]) temp.get(j); }
        else { continue; }
        if(temp.containsKey(p)) { aa3 = (String[]) temp.get(p); }
        else { continue; }

        int h=0;
        int cnt=0;
        while(h<q) {
          if(aa2[loc[h]].equals(aa3[loc[h]])) { cnt++; }
          h++;
        }
        if(cnt==q) {
          remove[j-1]=1;
          remove[p-1]=1;
          cnt1++;
          am[p-1]=1;
          row[j-1]=cnt1+1;
        }
      }
      if(row[j-1]>=k) {
        for(int p1=j-1;p1<a;p1++) {
          if(remove[p1]==1 && flagp[p1]==0) {
            flagp[p1]=1;      
            f1[p1]=p1+1;      
          }
        }
      }
    }

    int counter=0;
    for(int jj=0;jj<f1.length;jj++) {
      if(f1[jj]!=0)
        if(temp.containsKey(f1[jj])) { counter++; }
        else {
          System.out.println("No such key is found...so value doesn't deleted");
        } 
    }
    return counter;
  }

  private static void vis(int a,int ano, Map temp,int q,int[] len,int[] loc,int k) {
    Set s = temp.keySet();
    Iterator it1 = s.iterator();
    StringBuilder sb = null;
    int rep=0;
    System.out.println("size of temp b4 anon = "+s.size());
    while(it1.hasNext()) {
      String[] a1 = (String[]) temp.get((Integer)it1.next());
      if(len[ano]>2 && !(a1[loc[ano]].endsWith("*"))) {
        if(a1[loc[ano]].length()==1) {
          sb = new StringBuilder(a1[loc[ano]]);
          sb.setCharAt(0,'*');
          a1[loc[ano]]=sb.toString();
        }
        else {
          sb = new StringBuilder(a1[loc[ano]]);
          rep = a1[loc[ano]].length()-1;
          sb.setCharAt(rep,'*');
          a1[loc[ano]]=sb.toString();
        }
      }

      if(len[ano]<=2 && !(a1[loc[ano]].endsWith("]"))) {
        String x = a1[loc[ano]];
        String replaceString = getRangeString(x);
        if (!replaceString.isEmpty()) {
          a1[loc[ano]] = a1[loc[ano]].replace(String.valueOf(x), replaceString);
        }
      }
      calc1(a,asMap,q,loc,k,temp);
      Set ss1=temp.keySet();
      System.out.println("size of temp after anon = "+ss1.size());
    }
  }


  /*****
   * Return string of integer range between 0 to 2000 inside a pair of square
   * bracket after converting the given number string. Return an empty string
   * if the incoming value is out of range or invalid.
   *
   * @param  v  a number string
   * @return  a customized range string within a pair of square bracket
   */
  private static String getRangeString(String v) {
    try {
      int x = Integer.parseInt(v);
      if (x<0 || x>2000) { return ""; }
      if (x>=100) { return "[100-2000]"; }
      int div = x/5;   // get the lower bound
      return ("["+(div*5)+"-"+((div+1)*5)+"]");
    }
    catch (Exception e) { return ""; }  // invalid string
  }
}
Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
0
 

First of all,Thanks a lot...Taywin.

calc1 method puts all values into asMap that satisfies k,and removes it from temp.

test method calculates how many tuples/rows satisfies k.

vis method modifies the row sent it to calc1 and gives the size of temp before and after modification.

Here i am attaching documentation on all methods i have created.

Attachments Documentation.pdf (184.07KB)
Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
1
 

OK, thanks for a much better clarification. I am still trying to understand how you compute all your min/max with your code. I have tried it and there is a bug as early as computing the min/max. Your method expects keys as integer from selected column. When I entered a column which is not an integer, it broke the code. Also, there is another bug which I can't pin point until I completely understand what you are trying to do here. This bug happens in your vis() method. If you are going to modify a set or collection size, you cannot use an iterator to do so because it will break the loop.

/*
Ignore the line numbers because I've rearranged your codes and added some
others. Though, the original exception is still in your original methd --
vis().
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
at java.util.HashMap$KeyIterator.next(HashMap.java:845)
at NewClass.vis(NewClass.java:406)
at NewClass.computeQI(NewClass.java:540)
at NewClass.main(NewClass.java:90)
*/

By the way, you should take a step back now. You need to get the anonymization work for 1 QI first. If you could get it work for the one, then you can add a loop to go through each column number to do the anonymization again.

You need to structure your code better because it is very confusing right now. What you should do is somewhat as follows...

// loc <- an array of selected column for QI
for (int i=0; i<loc.length; i++) {
  // compute min/max of the specified QI column
  // compute rows that satisfy k-value
  // compute anonimization gain
  // replace string
  // adjust the result Map
}

Each computation can be taken out to become a method. In this case, it is much easier to read and debug. However, you should try with for (int i=0; i<1; i++) first because you need to get all of them correct before you can do it all.

PS: What are your generalization heirarchy for all columns you are working on?

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
1
 

After I look more into your code, I am not so sure why you need to compute anonimity gain. I don't see the point of getting the value because of your generalization. The only reason I see from computing anonimity gain is to automatically select a column in generalization. However, your program asks for specified columns to be used as QI from a user.

From what I understand, you suppress information by replacing the last character with '*' if the length of String is 2 or shorter. And you generalize a supposed number string (length 2 or less) by replacing it with a range number string.

It is simple but not very effective due to not enough suppression. However, it is good enough for your practice. When you want to improve it later on, you just need to redo only the suppression/generalization part.

PS: I still don't understand your question of the post. Why are you asking about doing it only 3 columns? Do you mean a user may give more than 3 QIs, but your program will select up to 3 QIs that give the most anonimity gain to work with?

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
0
 

Thanks a lot taywin..for your knd suggestion...
As you have said in the last post that you don't understand why i have compute anonymity gain.
The use of it is for selecting the Q.I. who have highest anonymity gain and modify it first.

One more clarification is: This program is run only for numeric QI.Not for nominal QI.So you have to give only column no. containing numeric attributes only.

From what I understand, you suppress information by replacing the last character with '*' if the length of String is 2 or shorter. And you generalize a supposed number string (length 2 or less) by replacing it with a range number string.

It's just generalization with two different method.E.g. in age you can convert into age-group and in pincode you can put * at different position to make it same woth other rows to satisfy k.

In my given code,i have copied same loop thrice,
1st time it will modify only one QI at a time,pick the QI with highest anonymization gain and modify it,then again calculate anonymization gain for remaining QI,and do the same until all QI turns out.

Then in 2nd loop it modifies 2 QI simultaneously and finds QI with highest anonymization gain and modify it,then again calculate anonymization gain for remaining QI,and do the same until all QI turns out.Like 1-3,1-4,1-6.

Now my question is how can i modify 3 QI simultaneously and do the rest
then how can i modify 4 QI simultaneously for QI=5.

I will modify my code as per kind siggestion,but also let me know how can i do this?

Thanks a lot.

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
1
 

OK, just a quick answer for now.

The use of it is for selecting the Q.I. who have highest anonymity gain and modify it first.

I see. That still doesn't give you a better result in your current code. The reason is that the order of generalize/suppress information of a column does not affect the outcome unless you are talking about automatically selection or user can pick 3 or more columns but the program will select only first 3 that have the highest anonymity gain.

how can i modify 3 QI simultaneously

You mean you want to generalize/suppress information in 1 loop for all 3 QIs? If so, you need to collect information of k-anonymity of all selected columns first (may have 3 separated variables to store). Then go through each record (of m1) and check each QI column of the record to see whether the column satisfy k-anonymity. If so, do the generalization/suppression; otherwise, skip the record.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article