Android Java implementation cosine matching algorithm sample code _android

Source: Internet
Author: User

Java-based cosine matching algorithm

Recently in a communications dating project, the project has a demand, through the user's interests and hobbies, for users to find recommendations of similar interests close friends. In fact, the idea is very simple, the user's interests and hobbies and other users of the interests of a match, when their hobby similarity is higher when the two sides to recommend. So how to compare is a problem, in fact, we can use the cosine matching algorithm to compare the interests of users, based on the calculated value to get a similar list of interests and hobbies, and to sort.

Because I do the project is the Android side, so the algorithm is implemented through Java, nonsense not too much said, the following is the implementation of the algorithm:

Package com; 
Import Java.util.HashMap; 
Import Java.util.Iterator; 
 
Import Java.util.Map; 
   /** * * * cosine matching algorithm * */public class Similardegreebycos {/** * Calculates the similarity of two strings, simple cosine calculation, does not add weight * @param str1  
    * @param str2 * @return Returns the degree of acquaintance of the calculation */public static double Getsimilardegree (string str1, String str2) { Create a vector space model, using the MAP implementation, the primary key for the word term, the value is 2 of the array, storing the corresponding word items in the number of occurrences of the string map<string, int[]> vectorspace = new Hashmap<st  
     Ring, int[]> ();  int[] Itemcountarray = null;//To avoid the frequent generation of local variables, the Itemcountarray is declared in this//by a space delimiter, exploded string strarray[] =  
     Str1.split (""); for (int i=0; i<strarray.length; ++i) {if (Vectorspace.containskey (strarray[i)) + + + (Vectorspa  
       Ce.get (Strarray[i]) [0]);  
         else {Itemcountarray = new int[2];  
         Itemcountarray[0] = 1;  
         ITEMCOUNTARRAY[1] = 0;  
       Vectorspace.put (Strarray[i], itemcountarray);  
     }  
     }  
     Strarray = Str2.split (""); for (int i=0; i<strarray.length; ++i) {if (Vectorspace.containskey (strarray[i)) + + + (Vectorspa  
       Ce.get (Strarray[i]) [1]);  
         else {Itemcountarray = new int[2];  
         Itemcountarray[0] = 0;  
         ITEMCOUNTARRAY[1] = 1;  
       Vectorspace.put (Strarray[i], itemcountarray); Double Vector1modulo = 0.00;//vector 1 modulo double vector2modulo = 0.00;//vector 2 modulo dou ble vectorproduct = 0.00;  
     Vector Product Iterator iter = Vectorspace.entryset (). iterator ();  
       while (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.next ();  
         
       Itemcountarray = (int[]) entry.getvalue ();  
       Vector1modulo + = itemcountarray[0]*itemcountarray[0];  
         
       Vector2modulo + = itemcountarray[1]*itemcountarray[1];  
     Vectorproduct + = itemcountarray[0]*itemcountarray[1]; } Vector1modulo = Math.sqrt (vector1mOdulo);  
      
     Vector2modulo = Math.sqrt (Vector2modulo);  
   Returns the similarity return (vectorproduct/(Vector1modulo*vector2modulo));  
     /** * Main Method */public static void Main (string args[]) {string str1 = "Sunny lively running badminton";  
     String str2 = "Love Gourmet running basketball football ice baseball riding swimming";  
     String STR3 = "The Love of Beauty cartoon tourism";  
     String STR4 = "Sunshine lively Love Game Love Code Farm";  
     String STR5 = "Badminton Badminton shuttlecock Badminton";  
       
     String STR6 = "Sunshine lively running badminton";  
     System.out.println ("str1 and str2 Acquaintance degree:" + similardegreebycos.getsimilardegree (str1, str2));  
     System.out.println ("str1 and STR3 acquaintance degree:" + similardegreebycos.getsimilardegree (str1, STR3));  
     System.out.println ("str1 and STR4 acquaintance degree:" + similardegreebycos.getsimilardegree (str1, STR4));  
     System.out.println ("str1 and STR5 acquaintance degree:" + similardegreebycos.getsimilardegree (str1, STR5));  
   System.out.println ("str1 and STR6 acquaintance degree:" + similardegreebycos.getsimilardegree (str1, STR6)); 

 }  
 
}

Effect:

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.