philschmid HF staff edugp commited on
Commit
1040b0b
0 Parent(s):

Duplicate from edugp/kenlm

Browse files

Co-authored-by: Eduardo Gonzalez Ponferrada <[email protected]>

This view is limited to 50 files because it contains too many changes.   See raw diff
.gitattributes ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
28
+ *sp.model filter=lfs diff=lfs merge=lfs -text
29
+ *sp.vocab filter=lfs diff=lfs merge=lfs -text
30
+ *.arpa filter=lfs diff=lfs merge=lfs -text
LICENSE ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2021-2022 Eduardo González Ponferrada
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
README.md ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - es
4
+ - af
5
+ - ar
6
+ - arz
7
+ - as
8
+ - bn
9
+ - fr
10
+ - sw
11
+ - eu
12
+ - ca
13
+ - zh
14
+ - en
15
+ - hi
16
+ - ur
17
+ - id
18
+ - pt
19
+ - vi
20
+ - gu
21
+ - kn
22
+ - ml
23
+ - mr
24
+ - ta
25
+ - te
26
+ - yo
27
+ tags:
28
+ - kenlm
29
+ - perplexity
30
+ - n-gram
31
+ - kneser-ney
32
+ - bigscience
33
+ license: mit
34
+ datasets:
35
+ - wikipedia
36
+ - oscar
37
+ duplicated_from: edugp/kenlm
38
+ ---
39
+
40
+ # KenLM models
41
+ This repo contains several KenLM models trained on different tokenized datasets and languages.
42
+ KenLM models are probabilistic n-gram languge models that models. One use case of these models consist on fast perplexity estimation for [filtering or sampling large datasets](https://huggingface.co/bertin-project/bertin-roberta-base-spanish). For example, one could use a KenLM model trained on French Wikipedia to run inference on a large dataset and filter out samples that are very unlike to appear on Wikipedia (high perplexity), or very simple non-informative sentences that could appear repeatedly (low perplexity).
43
+
44
+ At the root of this repo you will find different directories named after the dataset models were trained on (e.g. `wikipedia`, `oscar`). Within each directory, you will find several models trained on different language subsets of the dataset (e.g. `en (English)`, `es (Spanish)`, `fr (French)`). For each language you will find three different files
45
+ * `{language}.arpa.bin`: The trained KenLM model binary
46
+ * `{language}.sp.model`: The trained SentencePiece model used for tokenization
47
+ * `{language}.sp.vocab`: The vocabulary file for the SentencePiece model
48
+
49
+ The models have been trained using some of the preprocessing steps from [cc_net](https://github.com/facebookresearch/cc_net), in particular replacing numbers with zeros and normalizing punctuation. So, it is important to keep the default values for the parameters: `lower_case`, `remove_accents`, `normalize_numbers` and `punctuation` when using the pre-trained models in order to replicate the same pre-processing steps at inference time.
50
+
51
+ # Dependencies
52
+ * KenLM: `pip install https://github.com/kpu/kenlm/archive/master.zip`
53
+ * SentencePiece: `pip install sentencepiece`
54
+
55
+ # Example:
56
+ ```
57
+ from model import KenlmModel
58
+
59
+
60
+ # Load model trained on English wikipedia
61
+ model = KenlmModel.from_pretrained("wikipedia", "en")
62
+
63
+ # Get perplexity
64
+ model.get_perplexity("I am very perplexed")
65
+ # 341.3 (low perplexity, since sentence style is formal and with no grammar mistakes)
66
+
67
+ model.get_perplexity("im hella trippin")
68
+ # 46793.5 (high perplexity, since the sentence is colloquial and contains grammar mistakes)
69
+ ```
70
+ In the example above we see that, since Wikipedia is a collection of encyclopedic articles, a KenLM model trained on it will naturally give lower perplexity scores to sentences with formal language and no grammar mistakes than colloquial sentences with grammar mistakes.
mc4/ig.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bf7c27f49a2368b603b363f093417352e36a7227cef7a214631050efbd3c04b0
3
+ size 2025000654
mc4/ig.arpa.trie.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:557f588644ccca523169495129fa258851a552885ebf400a2a2b9c3d9a26d476
3
+ size 952564451
mc4/ig.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c987d7b1646ba45136dc06d509a1e667a9337f197db54e0472cc09f4ee4f0986
3
+ size 885958
mc4/ig.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca68f0396db1795beac9ca8aa8ed14782fb1cc01c52de7e97b35c5c7b0f0ee91
3
+ size 683760
mc4/ny.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d6dbd24886c53dda31f687f0e929473f378d396f97b8cf05bf8e73bdc49e2e2
3
+ size 3556104547
mc4/ny.arpa.trie.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c385e996bd99aacabcc6479616e8ec8c0eac73c8c1064e68649bc0fc283741cb
3
+ size 1694641098
mc4/ny.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d45d61f40c2df9f20a85749f43724ad2f475b6ba2fb09d86db2983719cfc2b25
3
+ size 902895
mc4/ny.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4367a6be4ca21743c2c465c731d0b3f8c6bfe4bbd2355f79ff1ea64ed5515988
3
+ size 700958
mc4/sn.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:039060e9ac7db9039c1141c6ba231b4e1cf97a5aee63aeb1f4641042fc08bd4c
3
+ size 4802972178
mc4/sn.arpa.trie.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:82bc0994e6cd2783dda3a23e257727aa28e833f72cdc95be2406b102a2edea25
3
+ size 2331081939
mc4/sn.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:47d52abd2c26da133b6901ef3bfda12d032e7c7e90b668c6c41ab2a179dc66aa
3
+ size 900223
mc4/sn.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:98ee54644a596b0d5f476eaf68726ee442834d9bdcef97716bb7aeb8d758130e
3
+ size 698093
mc4/st.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1aa917b1f02c4fb2b6927ce2dc46e50d5f8171dc375d333207ddb06c0dbd6d6
3
+ size 2103573048
mc4/st.arpa.trie.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:611c4bc26db22fbf4abdc396dd25f9138da577c6ace9997e12a172387d58270f
3
+ size 984849770
mc4/st.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3a8e2d5442162fa975ef8bfbdbd2201eaf98bec71ab614bd74036a287974b538
3
+ size 922202
mc4/st.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:987599705afc4ad40b02282871783a40464991c9d2cabd529217b049d6371446
3
+ size 720086
mc4/xh.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b2c5cb3fee548ce0629849d47747d141efff1b2de7c9dc5e2be166f946b072b8
3
+ size 1917154691
mc4/xh.arpa.trie.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:013bbccd45e9c775e511870e163b7e03a18bd1e2092c9e283b817b5b47149ba9
3
+ size 928523775
mc4/xh.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8d4278c5447178104c6b689b6e7d85b59cbfe8a9e9054eb5ee47267f9ca86b91
3
+ size 899309
mc4/xh.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1a1ef95b95c11a58262d7975acaa3bfc8aaaa23baf731b7c535ec4fa97b35c79
3
+ size 697154
mc4/zu.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1e1393f05347e426060fa98d5b8a763834b35b513579d32ff9937446855283bf
3
+ size 6377741974
mc4/zu.arpa.trie.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:03d9ec365a667f6061a96f8592cacde990d8dda18783b847dfb4ad7b650de21c
3
+ size 3105856237
mc4/zu.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e8242c97953d7f31ad00a36fd4b3ea347eb056a5c2a97ceeae983273a8430adb
3
+ size 868576
mc4/zu.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0230bbd8d49a103701575ee218420589ee52a644c172084017522fc0733a74f6
3
+ size 666409
model.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import unicodedata
4
+ from typing import Dict
5
+
6
+ import kenlm
7
+ import sentencepiece
8
+ from huggingface_hub import cached_download, hf_hub_url
9
+
10
+
11
+ class SentencePiece:
12
+ def __init__(
13
+ self,
14
+ model: str,
15
+ ):
16
+ super().__init__()
17
+ self.sp = sentencepiece.SentencePieceProcessor()
18
+ self.sp.load(str(model))
19
+
20
+ def do(self, text: dict) -> dict:
21
+ tokenized = self.sp.encode_as_pieces(text)
22
+ return " ".join(tokenized)
23
+
24
+
25
+ class KenlmModel:
26
+ digit_re: re.Pattern = re.compile(r"\d")
27
+ unicode_punct: Dict[str, str] = {
28
+ ",": ",",
29
+ "。": ".",
30
+ "、": ",",
31
+ "„": '"',
32
+ "”": '"',
33
+ "“": '"',
34
+ "«": '"',
35
+ "»": '"',
36
+ "1": '"',
37
+ "」": '"',
38
+ "「": '"',
39
+ "《": '"',
40
+ "》": '"',
41
+ "´": "'",
42
+ "∶": ":",
43
+ ":": ":",
44
+ "?": "?",
45
+ "!": "!",
46
+ "(": "(",
47
+ ")": ")",
48
+ ";": ";",
49
+ "–": "-",
50
+ "—": " - ",
51
+ ".": ". ",
52
+ "~": "~",
53
+ "’": "'",
54
+ "…": "...",
55
+ "━": "-",
56
+ "〈": "<",
57
+ "〉": ">",
58
+ "【": "[",
59
+ "】": "]",
60
+ "%": "%",
61
+ "►": "-",
62
+ }
63
+ unicode_punct_re = re.compile(f"[{''.join(unicode_punct.keys())}]")
64
+ non_printing_chars_re = re.compile(
65
+ f"[{''.join(map(chr, list(range(0,32)) + list(range(127,160))))}]"
66
+ )
67
+ kenlm_model_dir = None
68
+ sentence_piece_model_dir = None
69
+
70
+ def __init__(
71
+ self,
72
+ model_dataset: str,
73
+ language: str,
74
+ lower_case: bool = False,
75
+ remove_accents: bool = False,
76
+ normalize_numbers: bool = True,
77
+ punctuation: int = 1,
78
+ ):
79
+ self.model = kenlm.Model(os.path.join(model_dataset, f"{language}.arpa.bin"))
80
+ self.tokenizer = SentencePiece(os.path.join(model_dataset, f"{language}.sp.model"))
81
+ self.accent = remove_accents
82
+ self.case = lower_case
83
+ self.numbers = normalize_numbers
84
+ self.punct = punctuation
85
+
86
+ @classmethod
87
+ def from_pretrained(
88
+ cls,
89
+ model_dataset: str,
90
+ language: str,
91
+ ):
92
+ return cls(
93
+ model_dataset,
94
+ language,
95
+ False,
96
+ False,
97
+ True,
98
+ 1,
99
+ )
100
+
101
+ def pp(self, log_score, length):
102
+ return 10.0 ** (-log_score / length)
103
+
104
+ def get_perplexity(self, doc: str, normalize_cc_net: bool = True):
105
+ if normalize_cc_net:
106
+ doc = self.normalize(
107
+ doc,
108
+ accent=self.accent,
109
+ case=self.case,
110
+ numbers=self.numbers,
111
+ punct=self.punct,
112
+ )
113
+ # Tokenize (after normalizing): See https://github.com/facebookresearch/cc_net/blob/bda555bd1cf1ee2e0b925363e62a61cd46c8b60d/cc_net/mine.py#L352 for full pipeline
114
+ doc = self.tokenizer.do(doc)
115
+ doc_log_score, doc_length = 0, 0
116
+ for line in doc.split("\n"):
117
+ log_score = self.model.score(line)
118
+ length = len(line.split()) + 1
119
+ doc_log_score += log_score
120
+ doc_length += length
121
+ return round(self.pp(doc_log_score, doc_length), 1)
122
+
123
+ def normalize(
124
+ self,
125
+ line: str,
126
+ accent: bool = True,
127
+ case: bool = True,
128
+ numbers: bool = True,
129
+ punct: int = 1,
130
+ ) -> str:
131
+ line = line.strip()
132
+ if not line:
133
+ return line
134
+ if case:
135
+ line = line.lower()
136
+ if accent:
137
+ line = self.strip_accents(line)
138
+ if numbers:
139
+ line = self.digit_re.sub("0", line)
140
+ if punct == 1:
141
+ line = self.replace_unicode_punct(line)
142
+ elif punct == 2:
143
+ line = self.remove_unicode_punct(line)
144
+ line = self.remove_non_printing_char(line)
145
+ return line
146
+
147
+ def strip_accents(self, line: str) -> str:
148
+ """Strips accents from a piece of text."""
149
+ nfd = unicodedata.normalize("NFD", line)
150
+ output = [c for c in nfd if unicodedata.category(c) != "Mn"]
151
+ if len(output) == line:
152
+ return line
153
+ return "".join(output)
154
+
155
+ def replace_unicode_punct(self, text: str) -> str:
156
+ return "".join(self.unicode_punct.get(c, c) for c in text)
157
+
158
+ def remove_unicode_punct(self, text: str) -> str:
159
+ """More aggressive version of replace_unicode_punct but also faster."""
160
+ return self.unicode_punct_re.sub("", text)
161
+
162
+ def remove_non_printing_char(self, text: str) -> str:
163
+ return self.non_printing_chars_re.sub("", text)
oscar/af.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ad1c7a15e7dc4552fbe331387cf1d2aa9b2995354ff58743969d943e4fbc9a2b
3
+ size 1699310488
oscar/af.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a3249527d2b6fff0db32f3feeadf4acc806034c58aa2877b7804c1dbd079faf0
3
+ size 965654
oscar/af.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:141388a76a9e49b671152fe2962f8114e2aab1fef540751f385a10e6dd0fe3a6
3
+ size 763598
oscar/ar.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8e845ca65266408136026abc25279a7e9df45d6d4c48a16f15fcbcacb48ffd43
3
+ size 22880497746
oscar/ar.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:88b367b53707607953aeb7ca5e28d6904aedae50278cdd799fd15e5d5088bd4b
3
+ size 1073054
oscar/ar.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fd019d3c9e386bdbaeb1e37a6faeb2457b761142a852f2a9e97ef79dffbb6254
3
+ size 870884
oscar/arz.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8d84ceb2b604df092a6862acc780f13d294ffcee482a6fdda418e0f2b0c83b87
3
+ size 288231441
oscar/arz.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b98bc5055ff7f03c83c37da78933399ab5d13e26fa9ae634d95a8a4c1360e895
3
+ size 1063144
oscar/arz.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e05eb13200da58a46dc4feec9d2daac5c167f11caf5aed860aca503c4122e77
3
+ size 861047
oscar/as.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f6f0bef94b578617b8bd942b108938496a1c21e9be8b645db256bc3e54fedb54
3
+ size 410830531
oscar/as.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e5d588d095646ba80f0cfd15e83d418852fde6d97861bc6c4ba655f4b06d476
3
+ size 1274635
oscar/as.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2c3e4bc59c25b48174798a378e433b2d966fdfc3ac84ded0519348d953bc4ece
3
+ size 1072886
oscar/bn.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:50fe651938e0286e9b0be63d212658573dd5ed3f1e6061d09f006bc5393a114a
3
+ size 16813119142
oscar/bn.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fd007f17eb669e546d0564896b514a8816cf1805b7ef1ab53879fcf467f49410
3
+ size 1367929
oscar/bn.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6c51b211e9e184239c53e9bca1b44e56e728c55802adc6854d8ef9c9ba0a74c9
3
+ size 1165756
oscar/ca.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:db1f9b6be02b3df19df0ba55ff697607ef7b4ca3a5045a4211f2c4f6ca9ee788
3
+ size 12565197725
oscar/ca.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3b8ac5a125f25995996995f4ac14acfcfdf8125657e22ab0b6897b39044d22b9
3
+ size 960913
oscar/ca.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e30e0486b473439e2c96eae51689b220529e40de7e6eee8f586ffc09afb89d3b
3
+ size 758607
oscar/en.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c85daa6f6a85d4e4ec14b1695d82bdc47ca6fd8f8034ecc4947e383aadf0d6f8
3
+ size 34132973800
oscar/en.sp.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d81c313df13d5194f65a7ef37d583934f4c151091c68c70a72625f98232c7223
3
+ size 936812
oscar/en.sp.vocab ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6a08d8e45267868c780d86c7ae2829bd00dec3621bf991bb99ed4deafb36c88d
3
+ size 734613
oscar/es.arpa.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7665a9e1a610104827a3c4e6c4b86a3a6c4a9507f7fd74a2e06673134d39bf62
3
+ size 21752241307