PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/search/tests/src/Kernel/SearchTokenizerTest.php

https://github.com/drupal/drupal
PHP | 155 lines | 95 code | 20 blank | 40 comment | 4 complexity | bdb22f9f59c80917e04d54c91823e07c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. namespace Drupal\Tests\search\Kernel;
  3. use Drupal\KernelTests\KernelTestBase;
  4. /**
  5. * Tests that CJK tokenizer works as intended.
  6. *
  7. * @group search
  8. */
  9. class SearchTokenizerTest extends KernelTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected static $modules = ['search'];
  14. /**
  15. * Verifies that strings of CJK characters are tokenized.
  16. *
  17. * The search_simplify() function does special things with numbers, symbols,
  18. * and punctuation. So we only test that CJK characters that are not in these
  19. * character classes are tokenized properly. See PREG_CLASS_CKJ for more
  20. * information.
  21. */
  22. public function testTokenizer() {
  23. // Set the minimum word size to 1 (to split all CJK characters) and make
  24. // sure CJK tokenizing is turned on.
  25. $this->config('search.settings')
  26. ->set('index.minimum_word_size', 1)
  27. ->set('index.overlap_cjk', TRUE)
  28. ->save();
  29. // Create a string of CJK characters from various character ranges in
  30. // the Unicode tables.
  31. // Beginnings of the character ranges.
  32. $starts = [
  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 = [
  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 = [];
  79. $charcodes = [];
  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 = mb_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. public 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. $this->config('search.settings')
  106. ->set('index.minimum_word_size', 1)
  107. ->set('index.overlap_cjk', TRUE)
  108. ->save();
  109. $letters = 'abcdefghijklmnopqrstuvwxyz';
  110. $out = trim(search_simplify($letters));
  111. $this->assertEqual($letters, $out, 'Letters are not CJK tokenized');
  112. }
  113. /**
  114. * Like PHP chr() function, but for unicode characters.
  115. *
  116. * Function chr() only works for ASCII characters up to character 255. This
  117. * function converts a number to the corresponding unicode character. Adapted
  118. * from functions supplied in comments on several functions on php.net.
  119. */
  120. public function code2utf($num) {
  121. if ($num < 128) {
  122. return chr($num);
  123. }
  124. if ($num < 2048) {
  125. return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
  126. }
  127. if ($num < 65536) {
  128. return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  129. }
  130. if ($num < 2097152) {
  131. return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  132. }
  133. return '';
  134. }
  135. }