PageRenderTime 75ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/search/src/Tests/SearchTokenizerTest.php

https://github.com/build2be/drupal
PHP | 153 lines | 95 code | 17 blank | 41 comment | 4 complexity | f373c897272931d7df6546f8c78ef606 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\search\Tests\SearchTokenizerTest.
  5. */
  6. namespace Drupal\search\Tests;
  7. /**
  8. * Tests that CJK tokenizer works as intended.
  9. *
  10. * @group search
  11. */
  12. class SearchTokenizerTest extends SearchTestBase {
  13. /**
  14. * Verifies that strings of CJK characters are tokenized.
  15. *
  16. * The search_simplify() function does special things with numbers, symbols,
  17. * and punctuation. So we only test that CJK characters that are not in these
  18. * character classes are tokenized properly. See PREG_CLASS_CKJ for more
  19. * information.
  20. */
  21. function testTokenizer() {
  22. // Set the minimum word size to 1 (to split all CJK characters) and make
  23. // sure CJK tokenizing is turned on.
  24. \Drupal::config('search.settings')
  25. ->set('index.minimum_word_size', 1)
  26. ->set('index.overlap_cjk', TRUE)
  27. ->save();
  28. $this->refreshVariables();
  29. // Create a string of CJK characters from various character ranges in
  30. // the Unicode tables.
  31. // Beginnings of the character ranges.
  32. $starts = array(
  33. 'CJK unified' => 0x4e00,
  34. 'CJK Ext A' => 0x3400,
  35. 'CJK Compat' => 0xf900,
  36. 'Hangul Jamo' => 0x1100,
  37. 'Hangul Ext A' => 0xa960,
  38. 'Hangul Ext B' => 0xd7b0,
  39. 'Hangul Compat' => 0x3131,
  40. 'Half non-punct 1' => 0xff21,
  41. 'Half non-punct 2' => 0xff41,
  42. 'Half non-punct 3' => 0xff66,
  43. 'Hangul Syllables' => 0xac00,
  44. 'Hiragana' => 0x3040,
  45. 'Katakana' => 0x30a1,
  46. 'Katakana Ext' => 0x31f0,
  47. 'CJK Reserve 1' => 0x20000,
  48. 'CJK Reserve 2' => 0x30000,
  49. 'Bomofo' => 0x3100,
  50. 'Bomofo Ext' => 0x31a0,
  51. 'Lisu' => 0xa4d0,
  52. 'Yi' => 0xa000,
  53. );
  54. // Ends of the character ranges.
  55. $ends = array(
  56. 'CJK unified' => 0x9fcf,
  57. 'CJK Ext A' => 0x4dbf,
  58. 'CJK Compat' => 0xfaff,
  59. 'Hangul Jamo' => 0x11ff,
  60. 'Hangul Ext A' => 0xa97f,
  61. 'Hangul Ext B' => 0xd7ff,
  62. 'Hangul Compat' => 0x318e,
  63. 'Half non-punct 1' => 0xff3a,
  64. 'Half non-punct 2' => 0xff5a,
  65. 'Half non-punct 3' => 0xffdc,
  66. 'Hangul Syllables' => 0xd7af,
  67. 'Hiragana' => 0x309f,
  68. 'Katakana' => 0x30ff,
  69. 'Katakana Ext' => 0x31ff,
  70. 'CJK Reserve 1' => 0x2fffd,
  71. 'CJK Reserve 2' => 0x3fffd,
  72. 'Bomofo' => 0x312f,
  73. 'Bomofo Ext' => 0x31b7,
  74. 'Lisu' => 0xa4fd,
  75. 'Yi' => 0xa48f,
  76. );
  77. // Generate characters consisting of starts, midpoints, and ends.
  78. $chars = array();
  79. $charcodes = array();
  80. foreach ($starts as $key => $value) {
  81. $charcodes[] = $starts[$key];
  82. $chars[] = $this->code2utf($starts[$key]);
  83. $mid = round(0.5 * ($starts[$key] + $ends[$key]));
  84. $charcodes[] = $mid;
  85. $chars[] = $this->code2utf($mid);
  86. $charcodes[] = $ends[$key];
  87. $chars[] = $this->code2utf($ends[$key]);
  88. }
  89. // Merge into a string and tokenize.
  90. $string = implode('', $chars);
  91. $out = trim(search_simplify($string));
  92. $expected = drupal_strtolower(implode(' ', $chars));
  93. // Verify that the output matches what we expect.
  94. $this->assertEqual($out, $expected, 'CJK tokenizer worked on all supplied CJK characters');
  95. }
  96. /**
  97. * Verifies that strings of non-CJK characters are not tokenized.
  98. *
  99. * This is just a sanity check - it verifies that strings of letters are
  100. * not tokenized.
  101. */
  102. function testNoTokenizer() {
  103. // Set the minimum word size to 1 (to split all CJK characters) and make
  104. // sure CJK tokenizing is turned on.
  105. \Drupal::config('search.settings')
  106. ->set('minimum_word_size', 1)
  107. ->set('overlap_cjk', TRUE)
  108. ->save();
  109. $this->refreshVariables();
  110. $letters = 'abcdefghijklmnopqrstuvwxyz';
  111. $out = trim(search_simplify($letters));
  112. $this->assertEqual($letters, $out, 'Letters are not CJK tokenized');
  113. }
  114. /**
  115. * Like PHP chr() function, but for unicode characters.
  116. *
  117. * chr() only works for ASCII characters up to character 255. This function
  118. * converts a number to the corresponding unicode character. Adapted from
  119. * functions supplied in comments on several functions on php.net.
  120. */
  121. function code2utf($num) {
  122. if ($num < 128) {
  123. return chr($num);
  124. }
  125. if ($num < 2048) {
  126. return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
  127. }
  128. if ($num < 65536) {
  129. return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  130. }
  131. if ($num < 2097152) {
  132. return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  133. }
  134. return '';
  135. }
  136. }