Skip to content

Instantly share code, notes, and snippets.

@monkeymars
Created October 19, 2013 03:07
Show Gist options
  • Save monkeymars/7051244 to your computer and use it in GitHub Desktop.
Save monkeymars/7051244 to your computer and use it in GitHub Desktop.

Revisions

  1. @riqswe riqswe revised this gist Aug 20, 2010. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -8,26 +8,26 @@
    *
    * $tags1 = "code, php, jaccard, test, items";
    * $tags2 = "test, code";
    * echo getSimilarityCoefficient($tags1, $tags2); // print 0.28
    * echo getSimilarityCoefficient( $tags1, $tags2 ); // 0.28
    *
    * $str1 = "similarity coefficient of two items";
    * $str2 = "two items are cool";
    * echo getSimilarityCoefficient($str1, $str2, " "); // print 0.44
    * echo getSimilarityCoefficient( $str1, $str2, " " ); // 0.44
    *
    * @param string $item1
    * @param string $item2
    * @param string $separator
    * @return float
    * @author Henrique Hohmann
    * @version 0.1
    * @version 0.1
    */
    function getSimilarityCoefficient( $item1, $item2, $separator = "," ) {

    $item1 = explode( $separator, $item1 );
    $item2 = explode( $separator, $item2 );
    $arr_intersection = array_intersect($item2, $item2);
    $arr_union = array_merge($item1, $item2);
    $coefficient = count($arr_intersection) / count($arr_union);
    $arr_intersection = array_intersect( $item2, $item2 );
    $arr_union = array_merge( $item1, $item2 );
    $coefficient = count( $arr_intersection ) / count( $arr_union );

    return $coefficient;
    }
  2. @riqswe riqswe created this gist Aug 20, 2010.
    35 changes: 35 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php

    /**
    * Return the coefficient of two items based on Jaccard index
    * http://en.wikipedia.org/wiki/Jaccard_index
    *
    * Example:
    *
    * $tags1 = "code, php, jaccard, test, items";
    * $tags2 = "test, code";
    * echo getSimilarityCoefficient($tags1, $tags2); // print 0.28
    *
    * $str1 = "similarity coefficient of two items";
    * $str2 = "two items are cool";
    * echo getSimilarityCoefficient($str1, $str2, " "); // print 0.44
    *
    * @param string $item1
    * @param string $item2
    * @param string $separator
    * @return float
    * @author Henrique Hohmann
    * @version 0.1
    */
    function getSimilarityCoefficient( $item1, $item2, $separator = "," ) {

    $item1 = explode( $separator, $item1 );
    $item2 = explode( $separator, $item2 );
    $arr_intersection = array_intersect($item2, $item2);
    $arr_union = array_merge($item1, $item2);
    $coefficient = count($arr_intersection) / count($arr_union);

    return $coefficient;
    }

    ?>