Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, March 26, 2013

Cosine Similarity implementation in java

 Here is the implementation of Cosine similarity of two vectors (vec1,vec2) using java
------------------------------------------------------------------------------------------
public class cosine {

   
    public static void main(String[] args) {

        int vec1[] = {1,2,5,0,2,3};
        int vec2[] = {2,1,3,2,0,1};

        double cos_sim = cosine_similarity(vec1,vec2);
        System.out.println("Cosine Similarity="+cos_sim);
    }

    private static double cosine_similarity(int[] vec1, int[] vec2) {
        double dp = dot_product(vec1,vec2);
        double magnitudeA = find_magnitude(vec1);
        double magnitudeB = find_magnitude(vec2);
        return (dp)/(magnitudeA*magnitudeB);
    }

    private static double find_magnitude(int[] vec) {
        double sum_mag=0;
        for(int i=0;i<vec.length;i++)
        {
            sum_mag = sum_mag + vec[i]*vec[i];
        }
        return Math.sqrt(sum_mag);
    }

    private static double dot_product(int[] vec1, int[] vec2) {
        double sum=0;
        for(int i=0;i<vec1.length;i++)
        {
            sum = sum + vec1[i]*vec2[i];
        }
        return sum;
    }

}

Jaccard coeffecient and implementation


The Jaccard index, also known as the Jaccard similarity coefficient , is a statistic used for comparing the similarity and diversity of sample sets.
The Jaccard coefficient measures similarity between sample sets, and is defined as the size of the intersection divided by the size of the union of the sample sets:
 J(A,B) = {{|A \cap B|}\over{|A \cup B|}}. 
 
Here is the simple implementation of jaccard coeffecient using java.
--------------------------------------------------------------------
import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;


class jaccard {

   
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("Enter 1st word ");
        String s1=scan.next();
        System.out.println("Enter 2nd word ");
        String s2=scan.next();

        jaccard_coeffecient(s1,s2);
       

    }

    private static void jaccard_coeffecient(String s1, String s2) {

        double j_coeffecient;
        ArrayList<String> j1 = new ArrayList<String>();
        ArrayList<String> j2 = new ArrayList<String>();
        HashSet<String> set1 = new HashSet<String>();
        HashSet<String> set2 = new HashSet<String>();
       
            s1="$"+s1+"$";
            s2="$"+s2+"$";
            int j=0;
            int i=3;
       
            while(i<=s1.length())
            {
                j1.add(s1.substring(j, i));
                    j++;
                    i++;
            }   
            j=0;
            i=3;
            while(i<=s2.length())
            {
                j2.add(s2.substring(j, i));
                    j++;
                    i++;
            }   

           
            Iterator<String> itr1 = j1.iterator();
            while (itr1.hasNext()) {
                  String element = itr1.next();
                  System.out.print(element + " ");
                }
                System.out.println();
                Iterator<String> itr2 = j2.iterator();
                while (itr2.hasNext()) {
                  String element = itr2.next();
                  System.out.print(element + " ");
                }
                System.out.println();
           
               
                set2.addAll(j2);
                set2.addAll(j1);
                set1.addAll(j1);
                set1.retainAll(j2);
               
                   
                System.out.println("Union="+set2.size());
                System.out.println("Intersection="+set1.size());
               
                j_coeffecient=((double)set1.size())/((double)set2.size());
                System.out.println("Jaccard coeffecient="+j_coeffecient);

    }
   

    }

Monday, January 28, 2013

How to use gdb debugger ? (Debugging with GDB)

Steps to use gdb :
1) Compile your source code (xyz.c file) using -g option as follows
$ gcc -g xyz.c

2) Execute your ./a.out file as follows
$ gdb ./a.out

3) you will get gdb command line environment and then to start debugging you need to run the program.So, before running ,you need to set the breakpoint to start debug.
gdb$ b xyz.c.15  
Here 15 indicates the line number in your source code file.

4) After setting the breakpoint ,run the program as follows
gdb$ r
If the program needs command line arguments,then enter
gdb$ r arg1 arg2......so on

5) Now ,it starts execution and stops at the given breakpoint.

6) Next,you can run the program line by line as follows
gdb$ n 
or
gdb$ s
Here ,option s also works similar to n(next line),but if you want to trace the program completely(including functions),then use "s" option.

7) Whenever you get segmentation fault,you can actually trace back from where this fault came by "bt"(backtrack) option.
gdb$ bt

8) And also ,you can print the value of any variable at any stage using "p" (print ) option as follows.
For example,if you have a variable like 'i'
gdb$ p i
or
gdb$ print i

There are lot more options which you can try similarly,for more information about options,you can refer to man page of gdb
$ man gdb

thank you for reading !!!
cheers !
sGk