|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Algorithms.Strings.Similarity; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// <para> |
| 8 | +/// Jaccard similarity is a statistic that measures how similar two sets of data are. It is calculated by dividing |
| 9 | +/// the number of common elements in both sets by the number of elements in either set. More formally, it is the |
| 10 | +/// quotient of the division of the size of the size of the intersection divided by the size of the union of two sets. |
| 11 | +/// </para> |
| 12 | +/// <para> |
| 13 | +/// The result is a value between 0 and 1, where 0 means no similarity and 1 means perfect similarity. |
| 14 | +/// </para> |
| 15 | +/// <para> |
| 16 | +/// For example, suppose we have two sets of words: |
| 17 | +/// <list type="bullet"> |
| 18 | +/// <item> |
| 19 | +/// A = {apple, banana, cherry, date} |
| 20 | +/// </item> |
| 21 | +/// <item> |
| 22 | +/// B = {banana, cherry, elderberry, fig} |
| 23 | +/// </item> |
| 24 | +/// </list> |
| 25 | +/// </para> |
| 26 | +/// <para> |
| 27 | +/// The number of common elements in both sets is 2 (banana and cherry). The number of elements in either set is 6 |
| 28 | +/// (apple, banana, cherry, date, elderberry, fig). |
| 29 | +/// </para> |
| 30 | +/// <para> |
| 31 | +/// The Jaccard similarity coefficient is 2 / 6 = 0.333333 or 33.333% similarity. |
| 32 | +/// </para> |
| 33 | +/// </summary> |
| 34 | +public class JaccardSimilarity |
| 35 | +{ |
| 36 | + /// <summary> |
| 37 | + /// Calculates the Jaccard similarity coefficient between two strings. |
| 38 | + /// </summary> |
| 39 | + /// <param name="left">The first string to compare.</param> |
| 40 | + /// <param name="right">The second string to compare.</param> |
| 41 | + /// <returns>A double value between 0 and 1 that represents the similarity of the two strings.</returns> |
| 42 | + /// <exception cref="ArgumentNullException">Thrown when either the input is null.</exception> |
| 43 | + /// <remarks> |
| 44 | + /// This method uses a HashSet to represent the sets of characters in the input strings. |
| 45 | + /// </remarks> |
| 46 | + public double Calculate(string left, string right) |
| 47 | + { |
| 48 | + // Validate the input strings before proceeding. |
| 49 | + ValidateInput(left, right); |
| 50 | + |
| 51 | + // Get the lengths of the input strings. |
| 52 | + var leftLength = left.Length; |
| 53 | + var rightLength = right.Length; |
| 54 | + |
| 55 | + // If both strings are empty, return 1.0 as the similarity coefficient. |
| 56 | + if (leftLength == 0 && rightLength == 0) |
| 57 | + { |
| 58 | + return 1.0d; |
| 59 | + } |
| 60 | + |
| 61 | + // If either string is empty, return 0.0 as the similarity coefficient. |
| 62 | + if (leftLength == 0 || rightLength == 0) |
| 63 | + { |
| 64 | + return 0.0d; |
| 65 | + } |
| 66 | + |
| 67 | + // Get the unique characters in each string. |
| 68 | + var leftSet = new HashSet<char>(left); |
| 69 | + var rightSet = new HashSet<char>(right); |
| 70 | + |
| 71 | + // Get the union of the two strings. |
| 72 | + var unionSet = new HashSet<char>(leftSet); |
| 73 | + foreach (var c in rightSet) |
| 74 | + { |
| 75 | + unionSet.Add(c); |
| 76 | + } |
| 77 | + |
| 78 | + // Calculate the intersection size of the two strings. |
| 79 | + var intersectionSize = leftSet.Count + rightSet.Count - unionSet.Count; |
| 80 | + |
| 81 | + // Return the Jaccard similarity coefficient as the ratio of intersection to union. |
| 82 | + return 1.0d * intersectionSize / unionSet.Count; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Validates the input strings and throws an exception if either is null. |
| 87 | + /// </summary> |
| 88 | + /// <param name="left">The first string to validate.</param> |
| 89 | + /// <param name="right">The second string to validate.</param> |
| 90 | + private void ValidateInput(string left, string right) |
| 91 | + { |
| 92 | + if (left == null || right == null) |
| 93 | + { |
| 94 | + var paramName = left == null ? nameof(left) : nameof(right); |
| 95 | + throw new ArgumentNullException(paramName, "Input cannot be null"); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments