Advertisement
atcasanova

Jaro-Winkler Distance Bash Implementation

Dec 19th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.62 KB | None | 0 0
  1. ####################################
  2. # jaro-winkler bash implementation #
  3. #     by atcasanova@gmail.com      #
  4. ####################################
  5. # not properly working yet, but br #
  6. # ings acceptable results when com #
  7. # aring strings. Feel free to corr #
  8. # ect                              #
  9. ####################################
  10.  
  11. #!/bin/bash
  12. str1=`echo $1 | sed 's/\.//g' | sed 's/\;//g' | sed 's/\_//g' | sed 's/\-//g'`
  13. str2=`echo $2 | sed 's/\.//g' | sed 's/\;//g' | sed 's/\_//g' | sed 's/\-//g'`
  14. lenstr1=`echo $str1 | wc -c`
  15. let lenstr1--
  16. lenstr2=`echo $str2 | wc -c`
  17. let lenstr2--
  18.  
  19. if [ "$str1" == "$str2" ]
  20. then
  21.     jaro=1;
  22.     echo $jaro
  23. else
  24.     if [ $lenstr2 -gt $lenstr1 ]
  25.     then
  26.             tmp=$str1
  27.             str1=$str2
  28.             str2=$tmp
  29.             tmp=$lenstr1
  30.             lenstr1=$lenstr2
  31.             lenstr2=$tmp
  32.     fi
  33.  
  34.  
  35.     for (( k=0; k<=$lenstr1; k++ ))
  36.     do
  37.         found1[$k]=0;
  38.     done
  39.  
  40.     for (( q=0; q<=$lenstr2; q++ ))
  41.     do
  42.         found2[$q]=0;
  43.     done;
  44.  
  45.  
  46.     md=`echo $lenstr1/2-1 | bc`
  47.     m=0
  48.     t=0
  49.  
  50.     for (( i=1; i<=$lenstr1; i++ ))
  51.     do
  52.             start=`expr $i - $md`
  53.             end=`expr $i + $md`
  54.             for (( j=$start; j<=$end; j++ ))
  55.             do
  56.                     char1=`echo $str1 | cut -c$i`
  57.                     if [ $j -le `expr $i + $md` ]
  58.                     then
  59.                             if [ $j -gt 0 ]
  60.                             then
  61.                                     echo ""
  62.                                     char2=`echo $str2 | cut -c$j`
  63.                                     echo -n "Checking $char1 ($i) vs $char2 ($j)"
  64.                             else
  65.                                     char2=naoehchar
  66.                             fi
  67.                             if [ "$char1" == "$char2" ] && [ ${found2[$j]} -eq 0 ] && [ ${found1[$i]} -eq 0 ]
  68.                             then
  69.                                     let m++
  70.                                     echo -n " [match] "
  71.                     found2[$j]=1
  72.                     found1[$i]=1
  73.                                     if [ $i -ne $j ]
  74.                                     then
  75.                                             echo -n " [transposition] "
  76.                                             let t++
  77.                                     fi
  78.                             fi
  79.                     fi
  80.             done
  81.     done
  82.     echo ""
  83.     echo Transpositions: $t
  84.     let t=$t/2
  85.     echo matching distance: $md
  86.     var=`expr $m - $t`
  87.     echo matching characters: $m
  88.     p1=.333
  89.     p2=`echo "scale=3; $m/$lenstr1" | bc`
  90.     p3=`echo "scale=3; $m/$lenstr2" | bc`
  91.     if [ $m -lt 1 ]
  92.     then
  93.         jaro=0;
  94.     else
  95.         p4=`echo "scale=3; ($m-$t)/$m" | bc`
  96.         jaro=`echo "scale=3; $p1*($p2+$p3+$p4)" | bc`
  97.     fi
  98.     echo Jaro-Winkler Distance for $1 and $2 is $jaro
  99. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement