lunr.th.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*!
  2. * Lunr languages, `Thai` language
  3. * https://github.com/MihaiValentin/lunr-languages
  4. *
  5. * Copyright 2017, Keerati Thiwanruk
  6. * http://www.mozilla.org/MPL/
  7. */
  8. /*!
  9. * based on
  10. * Snowball JavaScript Library v0.3
  11. * http://code.google.com/p/urim/
  12. * http://snowball.tartarus.org/
  13. *
  14. * Copyright 2010, Oleg Mazko
  15. * http://www.mozilla.org/MPL/
  16. */
  17. /**
  18. * export the module via AMD, CommonJS or as a browser global
  19. * Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
  20. */
  21. ;
  22. (function(root, factory) {
  23. if (typeof define === 'function' && define.amd) {
  24. // AMD. Register as an anonymous module.
  25. define(factory)
  26. } else if (typeof exports === 'object') {
  27. /**
  28. * Node. Does not work with strict CommonJS, but
  29. * only CommonJS-like environments that support module.exports,
  30. * like Node.
  31. */
  32. module.exports = factory()
  33. } else {
  34. // Browser globals (root is window)
  35. factory()(root.lunr);
  36. }
  37. }(this, function() {
  38. /**
  39. * Just return a value to define the module export.
  40. * This example returns an object, but the module
  41. * can return a function as the exported value.
  42. */
  43. return function(lunr) {
  44. /* throw error if lunr is not yet included */
  45. if ('undefined' === typeof lunr) {
  46. throw new Error('Lunr is not present. Please include / require Lunr before this script.');
  47. }
  48. /* throw error if lunr stemmer support is not yet included */
  49. if ('undefined' === typeof lunr.stemmerSupport) {
  50. throw new Error('Lunr stemmer support is not present. Please include / require Lunr stemmer support before this script.');
  51. }
  52. /*
  53. Thai tokenization is the same to Japanense, which does not take into account spaces.
  54. So, it uses the same logic to assign tokenization function due to different Lunr versions.
  55. */
  56. var isLunr2 = lunr.version[0] == "2";
  57. /* register specific locale function */
  58. lunr.th = function() {
  59. this.pipeline.reset();
  60. this.pipeline.add(
  61. /*lunr.th.stopWordFilter,*/
  62. lunr.th.trimmer
  63. );
  64. if (isLunr2) { // for lunr version 2.0.0
  65. this.tokenizer = lunr.th.tokenizer;
  66. } else {
  67. if (lunr.tokenizer) { // for lunr version 0.6.0
  68. lunr.tokenizer = lunr.th.tokenizer;
  69. }
  70. if (this.tokenizerFn) { // for lunr version 0.7.0 -> 1.0.0
  71. this.tokenizerFn = lunr.th.tokenizer;
  72. }
  73. }
  74. };
  75. /* lunr trimmer function */
  76. lunr.th.wordCharacters = "[\u0e00-\u0e7f]";
  77. lunr.th.trimmer = lunr.trimmerSupport.generateTrimmer(lunr.th.wordCharacters);
  78. lunr.Pipeline.registerFunction(lunr.th.trimmer, 'trimmer-th');
  79. var segmenter = lunr.wordcut;
  80. segmenter.init();
  81. lunr.th.tokenizer = function (obj) {
  82. //console.log(obj);
  83. if (!arguments.length || obj == null || obj == undefined) return []
  84. if (Array.isArray(obj)) return obj.map(function (t) { return isLunr2 ? new lunr.Token(t) : t })
  85. var str = obj.toString().replace(/^\s+/, '');
  86. return segmenter.cut(str).split('|');
  87. }
  88. };
  89. }))