Java tutorial
//package com.java2s; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.Vector; public class Main { public static HashMap<Integer, HashMap<String, Integer>> CommentWordCount = new HashMap<Integer, HashMap<String, Integer>>(); public static HashMap<Integer, Integer> Topic = new HashMap<Integer, Integer>(); public static void cosineSimilarityCW() { Iterator<Integer> ids = CommentWordCount.keySet().iterator(); while (ids.hasNext()) { int com_id = ids.next(); Set<String> words1; words1 = CommentWordCount.get(com_id).keySet(); Iterator<Integer> com_iter = CommentWordCount.keySet().iterator(); while (com_iter.hasNext()) { int id = com_iter.next(); if (com_id < id) { Set<String> words2; words2 = CommentWordCount.get(id).keySet(); Vector<Integer> vecA = new Vector<Integer>(); Vector<Integer> vecB = new Vector<Integer>(); Iterator<String> w1 = words1.iterator(); Iterator<String> w2 = words2.iterator(); HashSet<String> imp = new HashSet<String>(); while (w1.hasNext()) { String s = w1.next(); imp.add(s); } while (w2.hasNext()) { String s = w2.next(); imp.add(s); } for (String s : imp) { if (CommentWordCount.get(com_id).containsKey(s)) { vecA.add(CommentWordCount.get(com_id).get(s)); } else vecA.add(0); if (CommentWordCount.get(id).containsKey(s)) { vecB.add(CommentWordCount.get(id).get(s)); } else vecB.add(0); } //System.out.println("Size : A"+vecA.size()+" Size: B"+vecB.size()+"maxLen:"+maxlength); double similarity; int product = 0; double sumA = 0; double sumB = 0; for (int i = 0; i < vecA.size(); i++) { product += vecA.elementAt(i) * vecB.elementAt(i); sumA += vecA.elementAt(i) * vecA.elementAt(i); sumB += vecB.elementAt(i) * vecB.elementAt(i); } sumA = Math.sqrt(sumA); sumB = Math.sqrt(sumB); similarity = product / (sumA * sumB); similarity = Math.acos(similarity) * 180 / Math.PI; //System.out.println("Result "+com_id+" "+id+" :"+similarity); if (similarity < 75) { //System.out.println("Result "+com_id+" "+id); if (Topic.containsKey(com_id)) { int val = Topic.get(com_id); val++; Topic.put(com_id, val); } else Topic.put(com_id, 1); if (Topic.containsKey(id)) { int val = Topic.get(id); val++; Topic.put(id, val); } else Topic.put(id, 1); } } } } } }