PageRenderTime 83ms CodeModel.GetById 51ms RepoModel.GetById 7ms app.codeStats 0ms

/swishe/src/lmbSwishFocus.class.php

http://github.com/limb-php-framework/limb
PHP | 133 lines | 99 code | 23 blank | 11 comment | 11 complexity | d706be80bc17e75dbfe071a67e3b3973 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * @package swishe
  11. * @version $Id$
  12. */
  13. class lmbSwishFocus
  14. {
  15. protected $radius;
  16. protected $ellipses;
  17. function __construct($radius, $ellipses = '...')
  18. {
  19. $this->radius = $radius;
  20. $this->ellipses = $ellipses;
  21. }
  22. function process($query, $result)
  23. {
  24. $items = $this->_parseQuery($query);
  25. $split_regex = '\b(' . implode('|', array_map('preg_quote', $items)) . ')';
  26. $split_regex = "~$split_regex~i";
  27. $splitted = preg_split($split_regex, $result, -1, PREG_SPLIT_DELIM_CAPTURE);
  28. $focused = '';
  29. for($i=1;$i<count($splitted);$i=$i+2)
  30. {
  31. if($i == 1)
  32. $left = $this->_gapLeft($splitted[$i-1]);
  33. else
  34. $left = $this->_gap($splitted[$i-1]);
  35. $match = $splitted[$i];
  36. $focused .= ' ' . $left . ' ' . $match;
  37. }
  38. if(isset($splitted[$i-1]))
  39. $focused .= ' ' . $this->_gapRight($splitted[$i-1]);
  40. return trim($focused);
  41. }
  42. protected function _gapRight($content)
  43. {
  44. $words = explode(' ', trim($content));
  45. $total = sizeof($words);
  46. if($total > $this->radius)
  47. {
  48. $tmp = array();
  49. for($i=0;$i<$this->radius;$i++)
  50. $tmp[] = $words[$i];
  51. $tmp[] = $this->ellipses;
  52. return implode(' ', $tmp);
  53. }
  54. else
  55. return implode(' ', $words);
  56. }
  57. protected function _gapLeft($content)
  58. {
  59. $words = explode(' ', trim($content));
  60. $total = sizeof($words);
  61. if($total > $this->radius)
  62. {
  63. $tmp = array();
  64. $tmp[] = $this->ellipses;
  65. for($i=$total-$this->radius;$i<$total;$i++)
  66. $tmp[] = $words[$i];
  67. return implode(' ', $tmp);
  68. }
  69. else
  70. return implode(' ', $words);
  71. }
  72. protected function _gap($content)
  73. {
  74. $words = explode(' ', trim($content));
  75. $total = sizeof($words);
  76. if($total >= $this->radius * 2)
  77. {
  78. $tmp = array();
  79. for($i=0;$i<$this->radius;$i++)
  80. $tmp[] = $words[$i];
  81. $tmp[] = $this->ellipses;
  82. for($i=$total-$this->radius;$i<$total;$i++)
  83. $tmp[] = $words[$i];
  84. return implode(' ', $tmp);
  85. }
  86. else
  87. return implode(' ', $words);
  88. }
  89. protected function _parseQuery($query)
  90. {
  91. $query = strtolower($query);
  92. $query = preg_replace('~\s+~', ' ', $query);
  93. $query = str_replace('(', '', $query);
  94. $query = str_replace(')', '', $query);
  95. $query = str_replace('"', '', $query);
  96. $query = str_replace("'", '', $query);
  97. $query = str_replace('*', '', $query);
  98. $query = str_replace(' and ', ' ', $query);
  99. $query = str_replace(' or ', ' ', $query);
  100. $query = str_replace(' not ', ' ', $query);
  101. $query = str_replace('!', '', $query);
  102. $query = str_replace('&', '', $query);
  103. $query = str_replace('|', '', $query);
  104. $query = trim($query);
  105. $items = array_unique(explode(' ', $query));
  106. arsort($items);
  107. return $items;
  108. }
  109. }