segment.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. let nodeRsJiebaLoaded = false;
  2. let segmentit;
  3. let hansIntlSegmenter;
  4. export function segment(hans, segment) {
  5. try {
  6. if (segment === "@node-rs/jieba") {
  7. const { load, cut } = require("@node-rs/jieba");
  8. if (!nodeRsJiebaLoaded) {
  9. nodeRsJiebaLoaded = true;
  10. load();
  11. }
  12. return cut(hans, false);
  13. }
  14. if (segment === "segmentit") {
  15. const { Segment, useDefault } = require("segmentit");
  16. if (!segmentit) {
  17. segmentit = useDefault(new Segment());
  18. }
  19. return segmentit.doSegment(hans, {
  20. simple: true,
  21. });
  22. }
  23. if (segment === "Intl.Segmenter") {
  24. if (typeof Intl?.Segmenter === "function") {
  25. if (!hansIntlSegmenter) {
  26. hansIntlSegmenter = new Intl.Segmenter("zh-Hans-CN", {
  27. granularity: "word",
  28. });
  29. }
  30. return [...hansIntlSegmenter.segment(hans)].map((s) => s.segment);
  31. }
  32. }
  33. const nodejieba = require("nodejieba");
  34. return nodejieba.cutSmall(hans, 4);
  35. }
  36. catch (ex) {
  37. return [hans];
  38. }
  39. }
  40. //# sourceMappingURL=segment.js.map