Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encodeAsPieces and support commonjs require #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"files.associations": {
"*.tcc": "cpp",
"cstring": "cpp",
"string": "cpp",
"string_view": "cpp",
"fstream": "cpp",
"iomanip": "cpp",
"istream": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"system_error": "cpp",
"typeinfo": "cpp"
}
}
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
# Javascript wrapper for the sentencepiece library

Sentencepiece is compiled to webassembly using emscripten.
## Build

Sentencepiece is compiled to webassembly using emscripten.

To rebuild this project

```bash

yarn

git clone https://github.com/google/sentencepiece.git

yarn build

```

## Use

To use this tool

```js

const { SentencePieceProcessor, cleanText } = require("../dist");
const ROOT = require('app-root-path')

async function main() {

let text = "I am still waiting on my card?"
let cleaned = cleanText(text)

let spp = new SentencePieceProcessor()
await spp.load(`${ROOT}/test/30k-clean.model`)
let ids = spp.encodeIds(cleaned)
console.log(ids)
let str = spp.decodeIds(ids) // list ids->number
console.log(str)

let pieces = spp.encodePieces(cleaned) // list tokens->string
console.log(pieces)
}
main()

```

## Note

- devilyouwei updated this repo to make this module support the js `require` keyword and added the using example.

- 2023-1-10, devilyouwei added `encodePieces`.
100 changes: 59 additions & 41 deletions bindings/sentencepiece.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,75 @@
#include <emscripten.h>
#include "../sentencepiece/src/sentencepiece_processor.h"

class StringView {
private:
std::string str;
absl::string_view view;
public:
StringView(const std::string &input) {
str = std::string(input);
view = absl::string_view(str.data(),str.length());
}
absl::string_view get_view() {
return this->view;
}
using namespace emscripten;
using namespace std;
using namespace absl;

class StringView
{
private:
string str;
string_view view;

public:
StringView(const string &input)
{
str = string(input);
view = string_view(str.data(), str.length());
}
string_view get_view()
{
return this->view;
}
};

template <typename T> emscripten::val vecToView(std::vector<T> vec) {
return emscripten::val(emscripten::typed_memory_view(vec.size(),vec.data()));
/*
template <typename T>
val vecToIntArray(vector<T> vec)
{
return val(typed_memory_view(vec.size(), vec.data()));
}

template <typename T>
val vecToStringArray(vector<T> vec)
{
return val(vec);
}
*/

EMSCRIPTEN_BINDINGS(sentencepiece) {
emscripten::register_vector<std::string>("VectorString");
emscripten::register_vector<int>("VectorInt");
EMSCRIPTEN_BINDINGS(sentencepiece)
{
register_vector<string>("VectorString");
register_vector<int>("VectorInt");

emscripten::function("vecToView",emscripten::select_overload<emscripten::val(std::vector<int>)>(&vecToView));
emscripten::function("vecFromJSArray",emscripten::select_overload<std::vector<int>(const emscripten::val &)>(&emscripten::vecFromJSArray));
/*
emscripten::function("vecToIntArray", select_overload<val(vector<int>)>(&vecToIntArray));
emscripten::function("vecToStringArray", select_overload<val(vector<string>)>(&vecToStringArray));
*/
emscripten::function("vecFromJSArray", select_overload<vector<int>(const val &)>(&vecFromJSArray));

emscripten::class_<sentencepiece::util::Status>("Status")
.constructor()
.function("ToString",&sentencepiece::util::Status::ToString)
;
.constructor()
.function("ToString", &sentencepiece::util::Status::ToString);

emscripten::class_<absl::string_view>("AbslStringView")
.constructor<const std::string &>()
;
emscripten::class_<string_view>("AbslStringView")
.constructor<const string &>();

emscripten::class_<StringView>("StringView")
.constructor<const std::string &>()
.function("getView",&StringView::get_view)
;
.constructor<const string &>()
.function("getView", &StringView::get_view);

emscripten::class_<sentencepiece::SentencePieceProcessor>("SentencePieceProcessor")
.smart_ptr_constructor("SentencePieceProcessor", &std::make_shared<sentencepiece::SentencePieceProcessor>)
.function("Load", emscripten::select_overload<sentencepiece::util::Status(absl::string_view)>(&sentencepiece::SentencePieceProcessor::Load))
.function("status", &sentencepiece::SentencePieceProcessor::status)
.function("SetEncodeExtraOptions", &sentencepiece::SentencePieceProcessor::SetEncodeExtraOptions)
.function("SetDecodeExtraOptions", &sentencepiece::SentencePieceProcessor::SetDecodeExtraOptions)
.function("SetVocabulary", &sentencepiece::SentencePieceProcessor::SetVocabulary)
.function("ResetVocabulary", &sentencepiece::SentencePieceProcessor::ResetVocabulary)
.function("LoadVocabulary", &sentencepiece::SentencePieceProcessor::LoadVocabulary)
.function("EncodeAsPieces", &sentencepiece::SentencePieceProcessor::EncodeAsPieces)
.function("EncodeAsIds", &sentencepiece::SentencePieceProcessor::EncodeAsIds)
.function("PieceToId", &sentencepiece::SentencePieceProcessor::PieceToId)
.function("DecodeIds", &sentencepiece::SentencePieceProcessor::DecodeIds)
;
.smart_ptr_constructor("SentencePieceProcessor", &std::make_shared<sentencepiece::SentencePieceProcessor>)
.function("Load", emscripten::select_overload<sentencepiece::util::Status(string_view)>(&sentencepiece::SentencePieceProcessor::Load))
.function("status", &sentencepiece::SentencePieceProcessor::status)
.function("SetEncodeExtraOptions", &sentencepiece::SentencePieceProcessor::SetEncodeExtraOptions)
.function("SetDecodeExtraOptions", &sentencepiece::SentencePieceProcessor::SetDecodeExtraOptions)
.function("SetVocabulary", &sentencepiece::SentencePieceProcessor::SetVocabulary)
.function("ResetVocabulary", &sentencepiece::SentencePieceProcessor::ResetVocabulary)
.function("LoadVocabulary", &sentencepiece::SentencePieceProcessor::LoadVocabulary)
.function("EncodeAsPieces", &sentencepiece::SentencePieceProcessor::EncodeAsPieces)
.function("EncodeAsIds", &sentencepiece::SentencePieceProcessor::EncodeAsIds)
.function("PieceToId", &sentencepiece::SentencePieceProcessor::PieceToId)
.function("DecodeIds", &sentencepiece::SentencePieceProcessor::DecodeIds);
}
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "@weblab-notebook/sentencepiece",
"version": "0.1.3",
"description": "Sentencepiece tokenization for natural language processing.",
"name": "sentencepiece-js",
"version": "1.1.0",
"description": "Sentencepiece tokenization for natural language processing, JS version.",
"main": "dist/index.js",
"type": "module",
"exports": {
"imports": "./dist/index.js",
"default": "./dist/index.js"
Expand All @@ -24,14 +23,15 @@
"keywords": [
"machine_learning",
"albert",
"nlp"
"nlp",
"sentencepiece"
],
"author": "Jan Kaul",
"author": "devilyouwei, Jan Kaul",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/JanKaul/sentencepiece/issues"
},
"homepage": "https://github.com/JanKaul/sentencepiece#readme",
"homepage": "https://github.com/devilyouwei/sentencepiece",
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
"@rollup/plugin-commonjs": "^20.0.0",
Expand All @@ -43,5 +43,8 @@
"rollup": "^2.56.2",
"tslib": "^2.3.1",
"typescript": "^4.4.3"
},
"dependencies": {
"app-root-path": "^3.1.0"
}
}
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
input: 'src/index.ts',
output: {
dir: 'dist',
format: 'es'
format: 'cjs'
},
plugins: [nodeResolve({ browser: true }), commonjs(), wasm(), typescript({ target: "es2017", downlevelIteration: true })]
plugins: [nodeResolve({ browser: true }), commonjs(), wasm(), typescript({ target: "es5", downlevelIteration: true })]
};
2 changes: 1 addition & 1 deletion sentencepiece
Submodule sentencepiece updated 82 files
+26 −0 .github/workflows/cifuzz.yml
+77 −0 .github/workflows/cmake.yml
+136 −0 .github/workflows/wheel.yml
+0 −98 .travis.yml
+37 −2 CMakeLists.txt
+20 −9 README.md
+1 −1 VERSION.txt
+0 −27 appveyor.yml
+1,020 −0 cmake/ios.toolchain.cmake
+16,908 −0 data/nfkd.tsv
+13 −9 doc/api.md
+1 −1 doc/experiments.md
+1 −1 doc/normalization.md
+56 −46 doc/options.md
+69 −99 python/README.md
+0 −1 python/VERSION.txt
+1 −1 python/add_new_vocab.ipynb
+14 −12 python/build_bundled.sh
+0 −73 python/make_py_wheel.sh
+0 −88 python/make_py_wheel_mac.sh
+0 −157 python/once.h
+19 −25 python/sentencepiece_python_module_example.ipynb
+1 −1 python/setup.cfg
+51 −18 python/setup.py
+655 −93 python/src/sentencepiece/__init__.py
+1 −0 python/src/sentencepiece/_version.py
+1,147 −106 python/src/sentencepiece/sentencepiece.i
+77 −49 python/src/sentencepiece/sentencepiece_model_pb2.py
+3,989 −940 python/src/sentencepiece/sentencepiece_wrap.cxx
+482 −170 python/test/sentencepiece_test.py
+2 −2 sentencepiece.pc.in
+18 −2 src/CMakeLists.txt
+3 −3 src/bpe_model.cc
+13 −11 src/bpe_model_trainer.h
+33 −11 src/builder.cc
+4 −1 src/builder.h
+193 −114 src/builtin_pb/sentencepiece_model.pb.cc
+213 −84 src/builtin_pb/sentencepiece_model.pb.h
+2 −28 src/common.h
+8 −2 src/compile_charsmap_main.cc
+5 −7 src/error.cc
+8 −1 src/freelist.h
+8 −5 src/freelist_test.cc
+15 −0 src/init.h
+6 −24 src/model_interface.h
+2 −17 src/model_interface_test.cc
+3 −4 src/normalizer.cc
+0 −1 src/normalizer.h
+11 −0 src/sentencepiece_model.proto
+276 −65 src/sentencepiece_processor.cc
+297 −106 src/sentencepiece_processor.h
+234 −17 src/sentencepiece_processor_test.cc
+4 −4 src/sentencepiece_trainer.h
+14 −8 src/spec_parser.h
+1 −0 src/spm_decode_main.cc
+12 −11 src/spm_encode_main.cc
+3 −3 src/spm_export_vocab_main.cc
+1 −0 src/spm_normalize_main.cc
+17 −0 src/spm_train_main.cc
+1 −0 src/test_main.cc
+80 −1 src/trainer_interface.cc
+4 −4 src/trainer_interface.h
+4 −0 src/trainer_interface_test.cc
+1 −1 src/unicode_script.h
+120 −45 src/unigram_model.cc
+15 −0 src/unigram_model.h
+69 −68 src/unigram_model_test.cc
+11 −9 src/unigram_model_trainer.cc
+3 −1 src/unigram_model_trainer.h
+116 −2 src/unigram_model_trainer_test.cc
+14 −31 src/util.cc
+4 −13 src/util.h
+0 −5 tensorflow/.gitignore
+0 −40 tensorflow/README.md
+0 −38 test.bat
+0 −140 test.sh
+16 −5 third_party/absl/flags/flag.cc
+8 −2 third_party/absl/flags/flag.h
+31 −0 third_party/absl/random/distributions.h
+33 −0 third_party/absl/random/random.h
+0 −267 third_party/absl/strings/string_view.cc
+0 −508 third_party/absl/strings/string_view.h
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sentencePieceProcessor, cleanText } from "./sentencePieceProcessor";
import { SentencePieceProcessor, cleanText } from "./sentencePieceProcessor";

export { sentencePieceProcessor, cleanText }
export default { sentencePieceProcessor, cleanText }
export { SentencePieceProcessor, cleanText }
export default { SentencePieceProcessor, cleanText }
23 changes: 0 additions & 23 deletions src/sentencePieceProcessor.test.js

This file was deleted.

Loading