segment.js 2.4 KB

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