segment.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var __read = (this && this.__read) || function (o, n) {
  2. var m = typeof Symbol === "function" && o[Symbol.iterator];
  3. if (!m) return o;
  4. var i = m.call(o), r, ar = [], e;
  5. try {
  6. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  7. }
  8. catch (error) { e = { error: error }; }
  9. finally {
  10. try {
  11. if (r && !r.done && (m = i["return"])) m.call(i);
  12. }
  13. finally { if (e) throw e.error; }
  14. }
  15. return ar;
  16. };
  17. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  18. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  19. if (ar || !(i in from)) {
  20. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  21. ar[i] = from[i];
  22. }
  23. }
  24. return to.concat(ar || Array.prototype.slice.call(from));
  25. };
  26. define(["require", "exports"], function (require, exports) {
  27. "use strict";
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.segment = void 0;
  30. var nodeRsJiebaLoaded = false;
  31. var segmentit;
  32. var hansIntlSegmenter;
  33. function segment(hans, segment) {
  34. try {
  35. if (segment === "@node-rs/jieba") {
  36. var _a = require("@node-rs/jieba"), load = _a.load, cut = _a.cut;
  37. if (!nodeRsJiebaLoaded) {
  38. nodeRsJiebaLoaded = true;
  39. load();
  40. }
  41. return cut(hans, false);
  42. }
  43. if (segment === "segmentit") {
  44. var _b = require("segmentit"), Segment = _b.Segment, useDefault = _b.useDefault;
  45. if (!segmentit) {
  46. segmentit = useDefault(new Segment());
  47. }
  48. return segmentit.doSegment(hans, {
  49. simple: true,
  50. });
  51. }
  52. if (segment === "Intl.Segmenter") {
  53. if (typeof (Intl === null || Intl === void 0 ? void 0 : Intl.Segmenter) === "function") {
  54. if (!hansIntlSegmenter) {
  55. hansIntlSegmenter = new Intl.Segmenter("zh-Hans-CN", {
  56. granularity: "word",
  57. });
  58. }
  59. return __spreadArray([], __read(hansIntlSegmenter.segment(hans)), false).map(function (s) { return s.segment; });
  60. }
  61. }
  62. var nodejieba = require("nodejieba");
  63. return nodejieba.cutSmall(hans, 4);
  64. }
  65. catch (ex) {
  66. return [hans];
  67. }
  68. }
  69. exports.segment = segment;
  70. });
  71. //# sourceMappingURL=segment.js.map