Search.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Ustring;
  36. /**
  37. * Class \Hoa\Ustring\Search.
  38. *
  39. * Some algorithms about search in strings.
  40. *
  41. * @copyright Copyright © 2007-2017 Hoa community
  42. * @license New BSD License
  43. */
  44. class Search
  45. {
  46. /**
  47. * Search by approximated patterns, with k differences based upon the
  48. * principle diagonal monotony.
  49. *
  50. * @param string $y Haystack.
  51. * @param string $x Needle.
  52. * @param int $k Number of differences.
  53. * @return array
  54. */
  55. public static function approximated($y, $x, $k)
  56. {
  57. $x = (string) $x;
  58. $y = (string) $y;
  59. $m = strlen($x);
  60. $n = strlen($y);
  61. $offset = [];
  62. $L = [-1 => array_fill(-1, $n - $m + $k + 3, -2)];
  63. for ($q = 0, $max = $k - 1; $q <= $max; ++$q) {
  64. $L[$q][-$q - 1] = $L[$q][-$q - 2] = $q - 1;
  65. }
  66. for ($q = 0; $q <= $k; ++$q) {
  67. for ($d = -$q, $max = $n - $m + $k - $q; $d <= $max; ++$d) {
  68. $l = min(
  69. max(
  70. $L[$q - 1][$d - 1],
  71. $L[$q - 1][$d ] + 1,
  72. $L[$q - 1][$d + 1] + 1
  73. ),
  74. $m - 1
  75. );
  76. $a = substr($x, $l + 1, $m - $l);
  77. $b = substr($y, $l + 1 + $d, $n - $l - $d);
  78. $L[$q][$d] = $l + static::lcp($a, $b);
  79. if ($L[$q][$d] == $m - 1 ||
  80. $d + $L[$q][$d] == $n - 1) {
  81. $j = $m + $d;
  82. $i = max(0, $j - $m);
  83. $offset[$q][] = ['i' => $i, 'j' => $j, 'l' => $j - $i];
  84. }
  85. }
  86. }
  87. return empty($offset) ? $offset : $offset[$k];
  88. }
  89. /**
  90. * Length of the longest common prefixes.
  91. *
  92. * @param string $x Word.
  93. * @param string $y Word.
  94. * @return int
  95. */
  96. public static function lcp($x, $y)
  97. {
  98. $max = min(strlen($x), strlen($y));
  99. $i = 0;
  100. while ($i < $max && $x[$i] == $y[$i]) {
  101. ++$i;
  102. }
  103. return $i;
  104. }
  105. }