sentencepiece 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +176 -0
- data/README.md +66 -0
- data/ext/sentencepiece/extconf.rb +14 -0
- data/ext/sentencepiece/sentencepiece.cpp +25 -0
- data/ext/sentencepiece/sentencepiece.hpp +353 -0
- data/lib/sentencepiece/version.rb +5 -0
- data/lib/sentencepiece.rb +4 -0
- data/sig/sentencepiece.rbs +4 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5fea5ff13a05fa657877f461944ccb7f5f4b8b6568d25c7581f795ad19514d9a
|
4
|
+
data.tar.gz: ecfb8603781a0b9129ea68d94272739c558d07a1aaff8f35b5b76c1bcbb4ee87
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fe928e1d7810a77f34ccce58b4feea2754ba533fbe1a3e4a0f8ccfbbfb0bdb5b0366550ce5844e6df50ad68a261fe8e9a6c047dde415683bb072d91c39f91868
|
7
|
+
data.tar.gz: 5247986788a8a7c6678b65d23d235647518e25f32e1d888246255d556b20e82668331de0b7a8d71998be06163b1cded08be51af7a2b23719499f2aac79e6cd72
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at yoshoku@outlook.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# sentencepiece.rb
|
2
|
+
|
3
|
+
[![License](https://img.shields.io/badge/License-Apache%202.0-yellowgreen.svg)](https://github.com/yoshoku/sentencepiece.rb/blob/main/LICENSE.txt)
|
4
|
+
|
5
|
+
sentencepiece.rb provides Ruby bindings for the [SentencePiece](https://github.com/google/sentencepiece),
|
6
|
+
an unsupervised text tokenizer and detokenizer for neural network-based text generation.
|
7
|
+
|
8
|
+
It is still under development and may undergo many changes in the future.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Install SentencePiece using your OS package manager;
|
13
|
+
|
14
|
+
macOS:
|
15
|
+
|
16
|
+
$ brew install sentencepiece
|
17
|
+
|
18
|
+
Ubuntu:
|
19
|
+
|
20
|
+
$ sudo apt-get install sentencepiece libsentencepiece-dev
|
21
|
+
|
22
|
+
Install the gem and add to the application's Gemfile by executing:
|
23
|
+
|
24
|
+
$ bundle add setencepiece
|
25
|
+
|
26
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
27
|
+
|
28
|
+
$ gem install sentencepiece
|
29
|
+
|
30
|
+
If you use homebrew on Apple M1/M2 mac, specify the homebrew installation directory:
|
31
|
+
|
32
|
+
$ gem install sentencepiece -- --with-opt-dir=/opt/homebrew
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'sentencepiece'
|
38
|
+
|
39
|
+
sp = SentencePiece::SentencePieceProcessor.new(model_file: '/path/to/model_file.model')
|
40
|
+
|
41
|
+
sp.encode('This is a test')
|
42
|
+
# => [17522, 2852, 29, 2002]
|
43
|
+
|
44
|
+
sp.encode(['This is a test', 'Hello world'])
|
45
|
+
# => [[17522, 2852, 29, 2002], [9770, 18905]]
|
46
|
+
|
47
|
+
sp.encode('This is a test', out_type: 'str')
|
48
|
+
# => ["▁This", "▁is", "▁a", "▁test"]
|
49
|
+
|
50
|
+
sp.decode([17522, 2852, 29, 2002])
|
51
|
+
# => "This is a test"
|
52
|
+
|
53
|
+
sp.decode([[17522, 2852, 29, 2002], [9770, 18905]])
|
54
|
+
# => ["This is a test", "Hello world"]
|
55
|
+
```
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/sentencepiece.rb/.
|
60
|
+
This project is intended to be a safe, welcoming space for collaboration,
|
61
|
+
and contributors are expected to adhere to the [code of conduct](https://github.com/yoshoku/sentencepiece.rb/blob/main/CODE_OF_CONDUCT.md).
|
62
|
+
|
63
|
+
## Code of Conduct
|
64
|
+
|
65
|
+
Everyone interacting in the SentencePiece project's codebases, issue trackers,
|
66
|
+
chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yoshoku/sentencepiece.rb/blob/main/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mkmf'
|
4
|
+
|
5
|
+
abort 'libstdc++ is not found.' unless have_library('stdc++')
|
6
|
+
abort 'libsentencepiece is not found.' unless have_library('sentencepiece')
|
7
|
+
abort 'libsentencepiece_train is not found.' unless have_library('sentencepiece_train')
|
8
|
+
|
9
|
+
# abort 'sentencepiece_processor.h is not found.' unless have_header('sentencepiece_processor.h')
|
10
|
+
# abort 'sentencepiece_trainer.h is not found.' unless have_header('sentencepiece_trainer.h')
|
11
|
+
|
12
|
+
$CXXFLAGS << ' -std=c++11'
|
13
|
+
|
14
|
+
create_makefile('sentencepiece/sentencepiece')
|
@@ -0,0 +1,25 @@
|
|
1
|
+
/**
|
2
|
+
* sentencepiece.rb provides Ruby bindings for the SentencePiece.
|
3
|
+
*
|
4
|
+
* Copyright (c) 2023 Atsushi Tatsuma
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
|
19
|
+
#include "sentencepiece.hpp"
|
20
|
+
|
21
|
+
extern "C" void Init_sentencepiece(void) {
|
22
|
+
rb_mSentencePiece = rb_define_module("SentencePiece");
|
23
|
+
rb_eSentencePieceError = rb_define_class_under(rb_mSentencePiece, "Error", rb_eRuntimeError);
|
24
|
+
RbSentencePieceProcessor::define_class(rb_mSentencePiece);
|
25
|
+
}
|
@@ -0,0 +1,353 @@
|
|
1
|
+
/**
|
2
|
+
* sentencepiece.rb provides Ruby bindings for the SentencePiece.
|
3
|
+
*
|
4
|
+
* Copyright (c) 2023 Atsushi Tatsuma
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
|
19
|
+
#ifndef SENTENCEPIECE_HPP
|
20
|
+
#define SENTENCEPIECE_HPP 1
|
21
|
+
|
22
|
+
#include <sentencepiece_processor.h>
|
23
|
+
#include <sentencepiece_trainer.h>
|
24
|
+
|
25
|
+
#include <ruby.h>
|
26
|
+
|
27
|
+
VALUE rb_mSentencePiece;
|
28
|
+
VALUE rb_eSentencePieceError;
|
29
|
+
VALUE rb_cSentencePieceProcessor;
|
30
|
+
|
31
|
+
class RbSentencePieceProcessor {
|
32
|
+
public:
|
33
|
+
static VALUE sentencepiece_processor_alloc(VALUE self) {
|
34
|
+
sentencepiece::SentencePieceProcessor* ptr = (sentencepiece::SentencePieceProcessor*)ruby_xmalloc(sizeof(sentencepiece::SentencePieceProcessor));
|
35
|
+
new (ptr) sentencepiece::SentencePieceProcessor();
|
36
|
+
return TypedData_Wrap_Struct(self, &sentencepiece_processor_type, ptr);
|
37
|
+
};
|
38
|
+
|
39
|
+
static void sentencepiece_processor_free(void* ptr) {
|
40
|
+
((sentencepiece::SentencePieceProcessor*)ptr)->~SentencePieceProcessor();
|
41
|
+
ruby_xfree(ptr);
|
42
|
+
};
|
43
|
+
|
44
|
+
static size_t sentencepiece_processor_size(const void* ptr) {
|
45
|
+
return sizeof(*((sentencepiece::SentencePieceProcessor*)ptr));
|
46
|
+
};
|
47
|
+
|
48
|
+
static sentencepiece::SentencePieceProcessor* get_sentencepiece_processor(VALUE self) {
|
49
|
+
sentencepiece::SentencePieceProcessor* ptr;
|
50
|
+
TypedData_Get_Struct(self, sentencepiece::SentencePieceProcessor, &sentencepiece_processor_type, ptr);
|
51
|
+
return ptr;
|
52
|
+
};
|
53
|
+
|
54
|
+
static VALUE define_class(VALUE outer) {
|
55
|
+
rb_cSentencePieceProcessor = rb_define_class_under(outer, "SentencePieceProcessor", rb_cObject);
|
56
|
+
rb_define_alloc_func(rb_cSentencePieceProcessor, sentencepiece_processor_alloc);
|
57
|
+
rb_define_method(rb_cSentencePieceProcessor, "initialize", RUBY_METHOD_FUNC(_sentencepiece_processor_init), -1);
|
58
|
+
rb_define_method(rb_cSentencePieceProcessor, "load", RUBY_METHOD_FUNC(_sentencepiece_processor_load), 1);
|
59
|
+
rb_define_method(rb_cSentencePieceProcessor, "encode", RUBY_METHOD_FUNC(_sentencepiece_processor_encode), -1);
|
60
|
+
rb_define_method(rb_cSentencePieceProcessor, "decode", RUBY_METHOD_FUNC(_sentencepiece_processor_decode), -1);
|
61
|
+
rb_define_method(rb_cSentencePieceProcessor, "piece_size", RUBY_METHOD_FUNC(_sentencepiece_processor_piece_size), 0);
|
62
|
+
rb_define_method(rb_cSentencePieceProcessor, "piece_to_id", RUBY_METHOD_FUNC(_sentencepiece_processor_piece_to_id), 1);
|
63
|
+
rb_define_method(rb_cSentencePieceProcessor, "id_to_piece", RUBY_METHOD_FUNC(_sentencepiece_processor_id_to_piece), 1);
|
64
|
+
return rb_cSentencePieceProcessor;
|
65
|
+
};
|
66
|
+
|
67
|
+
private:
|
68
|
+
static const rb_data_type_t sentencepiece_processor_type;
|
69
|
+
|
70
|
+
static VALUE _sentencepiece_processor_init(int argc, VALUE* argv, VALUE self) {
|
71
|
+
sentencepiece::SentencePieceProcessor* ptr = get_sentencepiece_processor(self);
|
72
|
+
new (ptr) sentencepiece::SentencePieceProcessor();
|
73
|
+
|
74
|
+
VALUE kw_args = Qnil;
|
75
|
+
ID kw_table[1] = { rb_intern("model_file") };
|
76
|
+
VALUE kw_values[1] = { Qundef };
|
77
|
+
rb_scan_args(argc, argv, "0:", &kw_args);
|
78
|
+
rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values);
|
79
|
+
VALUE model_file = kw_values[0] != Qundef ? kw_values[0] : Qnil;
|
80
|
+
|
81
|
+
if (!NIL_P(model_file)) {
|
82
|
+
if (!RB_TYPE_P(model_file, T_STRING)) {
|
83
|
+
rb_raise(rb_eArgError, "expected model_file to be a String");
|
84
|
+
return Qnil;
|
85
|
+
}
|
86
|
+
sentencepiece::util::Status status = ptr->Load(StringValueCStr(model_file));
|
87
|
+
if (!status.ok()) {
|
88
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
89
|
+
return Qnil;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
RB_GC_GUARD(model_file);
|
94
|
+
return Qnil;
|
95
|
+
};
|
96
|
+
|
97
|
+
static VALUE _sentencepiece_processor_load(VALUE self, VALUE model_path) {
|
98
|
+
sentencepiece::SentencePieceProcessor* ptr = get_sentencepiece_processor(self);
|
99
|
+
sentencepiece::util::Status status = ptr->Load(StringValueCStr(model_path));
|
100
|
+
if (!status.ok()) {
|
101
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
102
|
+
return Qfalse;
|
103
|
+
}
|
104
|
+
RB_GC_GUARD(model_path);
|
105
|
+
return Qtrue;
|
106
|
+
};
|
107
|
+
|
108
|
+
static VALUE _sentencepiece_processor_encode(int argc, VALUE* argv, VALUE self) {
|
109
|
+
VALUE kw_args = Qnil;
|
110
|
+
ID kw_table[1] = { rb_intern("out_type") };
|
111
|
+
VALUE kw_values[1] = { Qundef };
|
112
|
+
|
113
|
+
VALUE text = Qnil;
|
114
|
+
rb_scan_args(argc, argv, "1:", &text, &kw_args);
|
115
|
+
rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values);
|
116
|
+
VALUE out_type = kw_values[0] != Qundef ? kw_values[0] : rb_str_new_cstr("int");
|
117
|
+
|
118
|
+
if (!RB_TYPE_P(out_type, T_STRING)) {
|
119
|
+
rb_raise(rb_eArgError, "expected out_type to be a String");
|
120
|
+
return Qnil;
|
121
|
+
}
|
122
|
+
if (strcmp(StringValueCStr(out_type), "str") != 0 && strcmp(StringValueCStr(out_type), "int") != 0) {
|
123
|
+
rb_raise(rb_eArgError, "expected out_type to be 'str' or 'id'");
|
124
|
+
return Qnil;
|
125
|
+
}
|
126
|
+
if (!RB_TYPE_P(text, T_STRING) && !RB_TYPE_P(text, T_ARRAY)) {
|
127
|
+
rb_raise(rb_eArgError, "expected text to be a String or an Array");
|
128
|
+
return Qnil;
|
129
|
+
}
|
130
|
+
|
131
|
+
VALUE output = rb_ary_new();
|
132
|
+
sentencepiece::SentencePieceProcessor* ptr = get_sentencepiece_processor(self);
|
133
|
+
if (RB_TYPE_P(text, T_STRING)) {
|
134
|
+
if (strcmp(StringValueCStr(out_type), "str") == 0) {
|
135
|
+
std::vector<std::string> pieces;
|
136
|
+
sentencepiece::util::Status status = ptr->Encode(StringValueCStr(text), &pieces);
|
137
|
+
if (!status.ok()) {
|
138
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
139
|
+
return Qnil;
|
140
|
+
}
|
141
|
+
for (const std::string& token : pieces) {
|
142
|
+
rb_ary_push(output, rb_utf8_str_new_cstr(token.c_str()));
|
143
|
+
}
|
144
|
+
} else {
|
145
|
+
std::vector<int> ids;
|
146
|
+
sentencepiece::util::Status status = ptr->Encode(StringValueCStr(text), &ids);
|
147
|
+
if (!status.ok()) {
|
148
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
149
|
+
return Qnil;
|
150
|
+
}
|
151
|
+
for (const int idx : ids) {
|
152
|
+
rb_ary_push(output, INT2NUM(idx));
|
153
|
+
}
|
154
|
+
}
|
155
|
+
} else {
|
156
|
+
const size_t n_text = RARRAY_LEN(text);
|
157
|
+
if (strcmp(StringValueCStr(out_type), "str") == 0) {
|
158
|
+
for (size_t i = 0; i < n_text; i++) {
|
159
|
+
VALUE sub_text = rb_ary_entry(text, i);
|
160
|
+
std::vector<std::string> pieces;
|
161
|
+
sentencepiece::util::Status status = ptr->Encode(StringValueCStr(sub_text), &pieces);
|
162
|
+
if (!status.ok()) {
|
163
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
164
|
+
return Qnil;
|
165
|
+
}
|
166
|
+
VALUE sub_output = rb_ary_new();
|
167
|
+
for (const std::string& token : pieces) {
|
168
|
+
rb_ary_push(sub_output, rb_utf8_str_new_cstr(token.c_str()));
|
169
|
+
}
|
170
|
+
rb_ary_push(output, sub_output);
|
171
|
+
}
|
172
|
+
} else {
|
173
|
+
for (size_t i = 0; i < n_text; i++) {
|
174
|
+
VALUE sub_text = rb_ary_entry(text, i);
|
175
|
+
std::vector<int> ids;
|
176
|
+
sentencepiece::util::Status status = ptr->Encode(StringValueCStr(sub_text), &ids);
|
177
|
+
if (!status.ok()) {
|
178
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
179
|
+
return Qnil;
|
180
|
+
}
|
181
|
+
VALUE sub_output = rb_ary_new();
|
182
|
+
for (const int idx : ids) {
|
183
|
+
rb_ary_push(sub_output, INT2NUM(idx));
|
184
|
+
}
|
185
|
+
rb_ary_push(output, sub_output);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
RB_GC_GUARD(out_type);
|
191
|
+
RB_GC_GUARD(text);
|
192
|
+
|
193
|
+
return output;
|
194
|
+
};
|
195
|
+
|
196
|
+
static VALUE _sentencepiece_processor_decode(int argc, VALUE* argv, VALUE self) {
|
197
|
+
VALUE kw_args = Qnil;
|
198
|
+
ID kw_table[1] = { rb_intern("out_type") };
|
199
|
+
VALUE kw_values[1] = { Qundef };
|
200
|
+
|
201
|
+
VALUE pieces = Qnil;
|
202
|
+
rb_scan_args(argc, argv, "1:", &pieces, &kw_args);
|
203
|
+
rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values);
|
204
|
+
VALUE out_type = kw_values[0] != Qundef ? kw_values[0] : rb_str_new_cstr("int");
|
205
|
+
|
206
|
+
if (!RB_TYPE_P(pieces, T_ARRAY)) {
|
207
|
+
rb_raise(rb_eArgError, "expected out_type to be an Array");
|
208
|
+
return Qnil;
|
209
|
+
}
|
210
|
+
if (strcmp(StringValueCStr(out_type), "str") != 0 && strcmp(StringValueCStr(out_type), "int") != 0) {
|
211
|
+
rb_raise(rb_eArgError, "expected out_type to be 'str' or 'id'");
|
212
|
+
return Qnil;
|
213
|
+
}
|
214
|
+
|
215
|
+
const size_t n_pieces = RARRAY_LEN(pieces);
|
216
|
+
if (n_pieces == 0) return rb_str_new_cstr("");
|
217
|
+
|
218
|
+
VALUE output = Qnil;
|
219
|
+
sentencepiece::SentencePieceProcessor* ptr = get_sentencepiece_processor(self);
|
220
|
+
const size_t n_dimensions = RB_TYPE_P(rb_ary_entry(pieces, 0), T_ARRAY) ? 2 : 1;
|
221
|
+
|
222
|
+
if (strcmp(StringValueCStr(out_type), "str") == 0) {
|
223
|
+
if (n_dimensions == 1) {
|
224
|
+
std::vector<std::string> pcs;
|
225
|
+
for (size_t i = 0; i < n_pieces; i++) {
|
226
|
+
VALUE et = rb_ary_entry(pieces, i);
|
227
|
+
pcs.push_back(StringValueCStr(et));
|
228
|
+
}
|
229
|
+
std::string text;
|
230
|
+
sentencepiece::util::Status status = ptr->Decode(pcs, &text);
|
231
|
+
if (!status.ok()) {
|
232
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
233
|
+
return Qfalse;
|
234
|
+
}
|
235
|
+
output = rb_str_new_cstr(text.c_str());
|
236
|
+
} else {
|
237
|
+
output = rb_ary_new();
|
238
|
+
for (size_t i = 0; i < n_pieces; i++) {
|
239
|
+
VALUE sub_pieces = rb_ary_entry(pieces, i);
|
240
|
+
const size_t n_sub_pieces = RARRAY_LEN(sub_pieces);
|
241
|
+
std::vector<std::string> pcs;
|
242
|
+
for (size_t j = 0; j < n_sub_pieces; j++) {
|
243
|
+
VALUE et = rb_ary_entry(sub_pieces, j);
|
244
|
+
pcs.push_back(StringValueCStr(et));
|
245
|
+
}
|
246
|
+
std::string text;
|
247
|
+
sentencepiece::util::Status status = ptr->Decode(pcs, &text);
|
248
|
+
if (!status.ok()) {
|
249
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
250
|
+
return Qfalse;
|
251
|
+
}
|
252
|
+
rb_ary_push(output, rb_str_new_cstr(text.c_str()));
|
253
|
+
}
|
254
|
+
}
|
255
|
+
} else {
|
256
|
+
if (n_dimensions == 1) {
|
257
|
+
std::vector<int> pcs;
|
258
|
+
for (size_t i = 0; i < n_pieces; i++) {
|
259
|
+
VALUE et = rb_ary_entry(pieces, i);
|
260
|
+
pcs.push_back(NUM2INT(et));
|
261
|
+
}
|
262
|
+
std::string text;
|
263
|
+
sentencepiece::util::Status status = ptr->Decode(pcs, &text);
|
264
|
+
if (!status.ok()) {
|
265
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
266
|
+
return Qfalse;
|
267
|
+
}
|
268
|
+
output = rb_str_new_cstr(text.c_str());
|
269
|
+
} else {
|
270
|
+
output = rb_ary_new();
|
271
|
+
for (size_t i = 0; i < n_pieces; i++) {
|
272
|
+
VALUE sub_pieces = rb_ary_entry(pieces, i);
|
273
|
+
const size_t n_sub_pieces = RARRAY_LEN(sub_pieces);
|
274
|
+
std::vector<int> pcs;
|
275
|
+
for (size_t j = 0; j < n_sub_pieces; j++) {
|
276
|
+
VALUE et = rb_ary_entry(sub_pieces, j);
|
277
|
+
pcs.push_back(NUM2INT(et));
|
278
|
+
}
|
279
|
+
std::string text;
|
280
|
+
sentencepiece::util::Status status = ptr->Decode(pcs, &text);
|
281
|
+
if (!status.ok()) {
|
282
|
+
rb_raise(rb_eSentencePieceError, "%s", status.message());
|
283
|
+
return Qfalse;
|
284
|
+
}
|
285
|
+
rb_ary_push(output, rb_str_new_cstr(text.c_str()));
|
286
|
+
}
|
287
|
+
}
|
288
|
+
}
|
289
|
+
|
290
|
+
return output;
|
291
|
+
};
|
292
|
+
|
293
|
+
static VALUE _sentencepiece_processor_piece_size(VALUE self) {
|
294
|
+
sentencepiece::SentencePieceProcessor* ptr = get_sentencepiece_processor(self);
|
295
|
+
return INT2NUM(ptr->GetPieceSize());
|
296
|
+
};
|
297
|
+
|
298
|
+
static VALUE _sentencepiece_processor_piece_to_id(VALUE self, VALUE piece) {
|
299
|
+
if (!RB_TYPE_P(piece, T_STRING) && !RB_TYPE_P(piece, T_ARRAY)) {
|
300
|
+
rb_raise(rb_eArgError, "expected piece to be a String or an Array");
|
301
|
+
return Qnil;
|
302
|
+
}
|
303
|
+
VALUE output = Qnil;
|
304
|
+
sentencepiece::SentencePieceProcessor* ptr = get_sentencepiece_processor(self);
|
305
|
+
if (RB_TYPE_P(piece, T_STRING)) {
|
306
|
+
const int id = ptr->PieceToId(StringValueCStr(piece));
|
307
|
+
output = INT2NUM(id);
|
308
|
+
} else {
|
309
|
+
const size_t n_pieces = RARRAY_LEN(piece);
|
310
|
+
output = rb_ary_new();
|
311
|
+
for (size_t i = 0; i < n_pieces; i++) {
|
312
|
+
VALUE et = rb_ary_entry(piece, i);
|
313
|
+
const int id = ptr->PieceToId(StringValueCStr(et));
|
314
|
+
rb_ary_push(output, INT2NUM(id));
|
315
|
+
}
|
316
|
+
}
|
317
|
+
return output;
|
318
|
+
};
|
319
|
+
|
320
|
+
static VALUE _sentencepiece_processor_id_to_piece(VALUE self, VALUE ids) {
|
321
|
+
if (!RB_INTEGER_TYPE_P(ids) && !RB_TYPE_P(ids, T_ARRAY)) {
|
322
|
+
rb_raise(rb_eArgError, "expected piece to be an Integer or an Array");
|
323
|
+
return Qnil;
|
324
|
+
}
|
325
|
+
VALUE output = Qnil;
|
326
|
+
sentencepiece::SentencePieceProcessor* ptr = get_sentencepiece_processor(self);
|
327
|
+
if (RB_INTEGER_TYPE_P(ids)) {
|
328
|
+
const std::string piece = ptr->IdToPiece(NUM2INT(ids));
|
329
|
+
output = rb_utf8_str_new_cstr(piece.c_str());
|
330
|
+
} else {
|
331
|
+
const size_t n_ids = RARRAY_LEN(ids);
|
332
|
+
output = rb_ary_new();
|
333
|
+
for (size_t i = 0; i < n_ids; i++) {
|
334
|
+
VALUE et = rb_ary_entry(ids, i);
|
335
|
+
const std::string piece = ptr->IdToPiece(NUM2INT(et));
|
336
|
+
rb_ary_push(output, rb_utf8_str_new_cstr(piece.c_str()));
|
337
|
+
}
|
338
|
+
}
|
339
|
+
return output;
|
340
|
+
};
|
341
|
+
};
|
342
|
+
|
343
|
+
const rb_data_type_t RbSentencePieceProcessor::sentencepiece_processor_type = {
|
344
|
+
"RbSentencePieceProcessor",
|
345
|
+
{ NULL,
|
346
|
+
RbSentencePieceProcessor::sentencepiece_processor_free,
|
347
|
+
RbSentencePieceProcessor::sentencepiece_processor_size },
|
348
|
+
NULL,
|
349
|
+
NULL,
|
350
|
+
RUBY_TYPED_FREE_IMMEDIATELY
|
351
|
+
};
|
352
|
+
|
353
|
+
#endif /* SENTENCEPIECE_HPP */
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sentencepiece
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yoshoku
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
sentencepiece.rb provides Ruby bindings for the SentencePiece,
|
15
|
+
an unsupervised text tokenizer and detokenizer for neural network-based text generation.
|
16
|
+
email:
|
17
|
+
- yoshoku@outlook.com
|
18
|
+
executables: []
|
19
|
+
extensions:
|
20
|
+
- ext/sentencepiece/extconf.rb
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- CHANGELOG.md
|
24
|
+
- CODE_OF_CONDUCT.md
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- ext/sentencepiece/extconf.rb
|
28
|
+
- ext/sentencepiece/sentencepiece.cpp
|
29
|
+
- ext/sentencepiece/sentencepiece.hpp
|
30
|
+
- lib/sentencepiece.rb
|
31
|
+
- lib/sentencepiece/version.rb
|
32
|
+
- sig/sentencepiece.rbs
|
33
|
+
homepage: https://github.com/yoshoku/sentencepiece.rb
|
34
|
+
licenses: []
|
35
|
+
metadata:
|
36
|
+
homepage_uri: https://github.com/yoshoku/sentencepiece.rb
|
37
|
+
source_code_uri: https://github.com/yoshoku/sentencepiece.rb
|
38
|
+
changelog_uri: https://github.com/yoshoku/sentencepiece.rb/blob/main/CHANGELOG.md
|
39
|
+
rubygems_mfa_required: 'true'
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.3.26
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Ruby bindings for the SentencePiece
|
59
|
+
test_files: []
|