PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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