processor_stopwords.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Processor for removing stopwords from index and search terms.
  4. */
  5. class SearchApiStopWords extends SearchApiAbstractProcessor {
  6. /**
  7. * Holds all words ignored for the last query.
  8. *
  9. * @var array
  10. */
  11. protected $ignored = array();
  12. public function configurationForm() {
  13. $form = parent::configurationForm();
  14. $form += array(
  15. 'help' => array(
  16. '#markup' => '<p>' . t('Provide a stopwords file or enter the words in this form. If you do both, both will be used. Read about !stopwords.', array('!stopwords' => l(t('stop words'), "http://en.wikipedia.org/wiki/Stop_words"))) . '</p>',
  17. ),
  18. 'file' => array(
  19. '#type' => 'textfield',
  20. '#title' => t('Stopwords file URI'),
  21. '#title' => t('Enter the URI of your stopwords.txt file'),
  22. '#description' => t('This must be a stream-type description like <code>public://stopwords/stopwords.txt</code> or <code>http://example.com/stopwords.txt</code> or <code>private://stopwords.txt</code>.'),
  23. ),
  24. 'stopwords' => array(
  25. '#type' => 'textarea',
  26. '#title' => t('Stopwords'),
  27. '#description' => t('Enter a space and/or linebreak separated list of stopwords that will be removed from content before it is indexed and from search terms before searching.'),
  28. '#default_value' => t("but\ndid\nthe this that those\netc"),
  29. ),
  30. );
  31. if (!empty($this->options)) {
  32. $form['file']['#default_value'] = $this->options['file'];
  33. $form['stopwords']['#default_value'] = $this->options['stopwords'];
  34. }
  35. return $form;
  36. }
  37. public function configurationFormValidate(array $form, array &$values, array &$form_state) {
  38. parent::configurationFormValidate($form, $values, $form_state);
  39. $stopwords = trim($values['stopwords']);
  40. $uri = $values['file'];
  41. if (empty($stopwords) && empty($uri)) {
  42. $el = $form['file'];
  43. form_error($el, $el['#title'] . ': ' . t('At stopwords file or words are required.'));
  44. }
  45. if (!empty($uri) && !file_get_contents($uri)) {
  46. $el = $form['file'];
  47. form_error($el, t('Stopwords file') . ': ' . t('The file %uri is not readable or does not exist.', array('%uri' => $uri)));
  48. }
  49. }
  50. public function process(&$value) {
  51. $stopwords = $this->getStopWords();
  52. if (empty($stopwords) && !is_string($value)) {
  53. return;
  54. }
  55. $words = preg_split('/\s+/', $value);
  56. foreach ($words as $sub_key => $sub_value) {
  57. if (isset($stopwords[$sub_value])) {
  58. unset($words[$sub_key]);
  59. $this->ignored[] = $sub_value;
  60. }
  61. }
  62. $value = implode(' ', $words);
  63. }
  64. public function preprocessSearchQuery(SearchApiQuery $query) {
  65. $this->ignored = array();
  66. parent::preprocessSearchQuery($query);
  67. }
  68. public function postprocessSearchResults(array &$response, SearchApiQuery $query) {
  69. if ($this->ignored) {
  70. if (isset($response['ignored'])) {
  71. $response['ignored'] = array_merge($response['ignored'], $this->ignored);
  72. }
  73. else {
  74. $response['ignored'] = $this->ignored;
  75. }
  76. }
  77. }
  78. /**
  79. * @return
  80. * An array whose keys are the stopwords set in either the file or the text
  81. * field.
  82. */
  83. protected function getStopWords() {
  84. if (isset($this->stopwords)) {
  85. return $this->stopwords;
  86. }
  87. $file_words = $form_words = array();
  88. if (!empty($this->options['file']) && $stopwords_file = file_get_contents($this->options['file'])) {
  89. $file_words = preg_split('/\s+/', $stopwords_file);
  90. }
  91. if (!empty($this->options['stopwords'])) {
  92. $form_words = preg_split('/\s+/', $this->options['stopwords']);
  93. }
  94. $this->stopwords = array_flip(array_merge($file_words, $form_words));
  95. return $this->stopwords;
  96. }
  97. }