Coder Social home page Coder Social logo

jieba-wasm's Introduction

jieba-wasm

jieba-rs 的 wasm binding

编译成 WASM 摆脱编译 Node Addon 的烦恼

Usage

Node.js

const {
  cut,
  cut_all,
  cut_for_search,
  tokenize,
  add_word,
} = require("jieba-wasm");
cut("中华人民共和国武汉市长江大桥", true);
// [ '中华人民共和国', '武汉市', '长江大桥' ]
cut_all("中华人民共和国武汉市长江大桥", true);
/*
[
  '中',         '中华',
  '中华人民',   '中华人民共和国',
  '华',         '华人',
  '人',         '人民',
  '人民共和国', '民',
  '共',         '共和',
  '共和国',     '和',
  '国',         '武',
  '武汉',       '武汉市',
  '汉',         '市',
  '市长',       '长',
  '长江',       '长江大桥',
  '江',         '大',
  '大桥',       '桥'
]
*/
cut_for_search("中华人民共和国武汉市长江大桥", true);
/*
[
  '中华',     '华人',
  '人民',     '共和',
  '共和国',   '中华人民共和国',
  '武汉',     '武汉市',
  '长江',     '大桥',
  '长江大桥'
]
*/
tokenize("中华人民共和国武汉市长江大桥", "default", true);
/*
[
  { word: '中华人民共和国', start: 0, end: 7 },
  { word: '武汉市', start: 7, end: 10 },
  { word: '长江大桥', start: 10, end: 14 }
]
*/
tokenize("中华人民共和国武汉市长江大桥", "search", true);
/*
[
  { word: '中华', start: 0, end: 2 },
  { word: '华人', start: 1, end: 3 },
  { word: '人民', start: 2, end: 4 },
  { word: '共和', start: 4, end: 6 },
  { word: '共和国', start: 4, end: 7 },
  { word: '中华人民共和国', start: 0, end: 7 },
  { word: '武汉', start: 7, end: 9 },
  { word: '武汉市', start: 7, end: 10 },
  { word: '长江', start: 10, end: 12 },
  { word: '大桥', start: 12, end: 14 },
  { word: '长江大桥', start: 10, end: 14 }
]
*/

cut("桥大江长市汉武的省北湖国和共民人华中");
/*
[
  '桥', '大江', '长',
  '市', '汉',   '武',
  '的', '省',   '北湖',
  '国', '和',   '共',
  '民', '人',   '华中'
]
*/
["桥大江长", "市汉武", "省北湖", "国和共民人华中"].map((word) => {
  add_word(word);
});
cut("桥大江长市汉武的省北湖国和共民人华中");
// ["桥大江长", "市汉武", "的", "省北湖", "国和共民人华中"];

Browser

import init, { cut } from 'jieba-wasm';

// 重要:使用前必须初始化
await init();

cut("中华人民共和国武汉市长江大桥", true);
// [ '中华人民共和国', '武汉市', '长江大桥' ]

示例 Demo

前期准备

首先保证存在 rust 环境,然后运行以下命令

npm run build:cargo
npm run build

运行浏览器端示例

cd demo/web
npm install
npm run dev

Piror Art

https://github.com/messense/jieba-rs

jieba-wasm's People

Contributors

fengkx avatar thulof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jieba-wasm's Issues

Deno support request

import { cut } from "https://cdn.skypack.dev/jieba-wasm?dts";
console.log(await cut("我來到北京清華大學"));

save it as jieba-wasm.deno.js, and run deno --unstable run -A jieba-wasm.deno.js

returns

Download https://cdn.skypack.dev/jieba-wasm?dts
Download https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=imports/optimized/jieba-wasm.js
Download https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=types/index.d.ts
error: Uncaught TypeError: Cannot read properties of undefined (reading '__wbindgen_add_to_stack_pointer')
    wasm.__wbindgen_add_to_stack_pointer(16);
         ^
    at cut (https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=imports/optimized/jieba-wasm.js:173:10)
    at file:///C:/jieba-wasm.deno.js:2:19

Note: the CDN "skypack.dev" used in the example converts npm package into a Deno-compatible format (or at least they claim they can)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.