fork download
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <math.h>
  5. #include <iterator>
  6. #include <set>
  7. #include <string>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. /*
  13. * dice coefficient = bigram overlap * 2 / bigrams in a + bigrams in b
  14. * (C) 2007 Francis Tyers
  15. * Modifications made by Stefan Koshiw 2010
  16. * Now it outputs values [0..1]
  17. * Released under the terms of the GNU GPL.
  18. */
  19.  
  20.  
  21. float dice_coefficient(wstring string1, wstring string2);
  22.  
  23.  
  24. int main()
  25. {
  26. float dice = 0;
  27. wstring str1(L"save");
  28. wstring str2(L"gave");
  29.  
  30. dice = dice_coefficient(str1, str2);
  31. cout << dice;
  32. }
  33.  
  34.  
  35. float dice_coefficient(wstring string1, wstring string2)
  36. {
  37.  
  38. set<wstring> string1_bigrams;
  39. set<wstring> string2_bigrams;
  40.  
  41. //base case
  42. if (string1.length() == 0 || string2.length() == 0)
  43. {
  44. return 0;
  45. }
  46.  
  47. for (unsigned int i = 0; i < (string1.length() - 1); i++) { // extract character bigrams from string1
  48. string1_bigrams.insert(string1.substr(i, 2));
  49. }
  50. for (unsigned int i = 0; i < (string2.length() - 1); i++) { // extract character bigrams from string2
  51. string2_bigrams.insert(string2.substr(i, 2));
  52. }
  53.  
  54. int intersection = 0;
  55.  
  56. // find the intersection between the two sets
  57.  
  58. for (set<wstring>::iterator IT = string2_bigrams.begin();
  59. IT != string2_bigrams.end();
  60. IT++)
  61. {
  62. intersection += string1_bigrams.count((*IT));
  63. }
  64.  
  65. // calculate dice coefficient
  66. int total = string1_bigrams.size() + string2_bigrams.size();
  67. float dice = (float)(intersection * 2) / (float)total;
  68.  
  69. return dice;
  70. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
0.666667