Posts String Similarity
Post
Cancel

String Similarity

Introduction

see instructions in Google Classroom

I have attatched the instruction document below.

View Instructions.

Alternatively, see comments above each method for details about functionality.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.lang.*;
import java.util.*;
import java.lang.Math;


// Nathan Melwani
//string are immutable in my heart


 class StringSimilarity
{
/*
 *    postCondition : return a list with list.get(i).length() == 1 for all i, 0 <= i < list.size()
 *                            All elements of list are contained in String s1 or String s2
 *                            Duplicate elements may exist in list if s1 or s2 contain duplicate elements
 */
   public static List<String> stringUnion( String s1, String s2 )
   {
      List<String> ans = new ArrayList<String>();
      String string2 = s2;
      for(int i = 0; i<s1.length(); i++)
      {
        String temp = "" + s1.charAt(i);
        ans.add(temp);
        string2 = string2.replaceFirst(temp, "");
      }
      
      for(int i = 0; i<string2.length(); i++)
      {
        String temp = "" + string2.charAt(i);
        ans.add(temp);        
      }
      return ans;
   }


/*
 *    postCondition : return a list with list.get(i).length() == 1 for all i, 0 <= i < list.size()
 *                            All elements of list are contained in both String s1 and String s2
 *                            Duplicate elements may exist in list if both s1 and s2 contain duplicate elements
 */
   public static List<String> stringIntersection( String s1, String s2 )
   {
      List<String> ans = new ArrayList<String>();
      String string1 = s1;
      String string2 = s2;
      for(int i = 0; i<s1.length(); i++)
      {
        String temp = "" + s1.charAt(i);
        if(string2.contains(temp))
        {
          ans.add(temp);
          string1 = string1.replaceFirst(temp, "");
          string2 = string2.replaceFirst(temp, "");
        }
      }
      return ans;
   }

   public static double getJaccardIndex(String s1, String s2)
   {
     if(stringIntersection(s1, s2).size() == 0 && stringUnion(s1, s2).size() == 0)
     {
       return 1.0;
     }
     return (double)( stringIntersection(s1, s2).size() ) / (double)(stringUnion(s1, s2).size());
   }
}
This post is licensed under CC BY 4.0