gstr_similartext.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gstr
  7. // SimilarText calculates the similarity between two strings.
  8. // See http://php.net/manual/en/function.similar-text.php.
  9. func SimilarText(first, second string, percent *float64) int {
  10. var similarText func(string, string, int, int) int
  11. similarText = func(str1, str2 string, len1, len2 int) int {
  12. var sum, max int
  13. pos1, pos2 := 0, 0
  14. // Find the longest segment of the same section in two strings
  15. for i := 0; i < len1; i++ {
  16. for j := 0; j < len2; j++ {
  17. for l := 0; (i+l < len1) && (j+l < len2) && (str1[i+l] == str2[j+l]); l++ {
  18. if l+1 > max {
  19. max = l + 1
  20. pos1 = i
  21. pos2 = j
  22. }
  23. }
  24. }
  25. }
  26. if sum = max; sum > 0 {
  27. if pos1 > 0 && pos2 > 0 {
  28. sum += similarText(str1, str2, pos1, pos2)
  29. }
  30. if (pos1+max < len1) && (pos2+max < len2) {
  31. s1 := []byte(str1)
  32. s2 := []byte(str2)
  33. sum += similarText(string(s1[pos1+max:]), string(s2[pos2+max:]), len1-pos1-max, len2-pos2-max)
  34. }
  35. }
  36. return sum
  37. }
  38. l1, l2 := len(first), len(second)
  39. if l1+l2 == 0 {
  40. return 0
  41. }
  42. sim := similarText(first, second, l1, l2)
  43. if percent != nil {
  44. *percent = float64(sim*200) / float64(l1+l2)
  45. }
  46. return sim
  47. }