fasttext-modern 0.10.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fasttext_modern-0.10.0/CHANGELOG.md +28 -0
- fasttext_modern-0.10.0/CONTRIBUTING.md +28 -0
- fasttext_modern-0.10.0/FORK.md +34 -0
- fasttext_modern-0.10.0/LICENSE +21 -0
- fasttext_modern-0.10.0/MANIFEST.in +8 -0
- fasttext_modern-0.10.0/PKG-INFO +379 -0
- fasttext_modern-0.10.0/README.md +363 -0
- fasttext_modern-0.10.0/SECURITY.md +16 -0
- fasttext_modern-0.10.0/pyproject.toml +19 -0
- fasttext_modern-0.10.0/python/README.md +332 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/FastText.py +623 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/__init__.py +23 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/_version.py +4 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/pybind/fasttext_pybind.cc +528 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/tests/__init__.py +14 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/tests/test_configurations.py +239 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/tests/test_script.py +629 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/util/__init__.py +15 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext/util/util.py +209 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext_modern.egg-info/PKG-INFO +379 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext_modern.egg-info/SOURCES.txt +56 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext_modern.egg-info/dependency_links.txt +1 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext_modern.egg-info/not-zip-safe +1 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext_modern.egg-info/requires.txt +1 -0
- fasttext_modern-0.10.0/python/fasttext_module/fasttext_modern.egg-info/top_level.txt +2 -0
- fasttext_modern-0.10.0/setup.cfg +7 -0
- fasttext_modern-0.10.0/setup.py +187 -0
- fasttext_modern-0.10.0/src/aligned.h +98 -0
- fasttext_modern-0.10.0/src/args.cc +494 -0
- fasttext_modern-0.10.0/src/args.h +96 -0
- fasttext_modern-0.10.0/src/autotune.cc +477 -0
- fasttext_modern-0.10.0/src/autotune.h +89 -0
- fasttext_modern-0.10.0/src/densematrix.cc +265 -0
- fasttext_modern-0.10.0/src/densematrix.h +85 -0
- fasttext_modern-0.10.0/src/dictionary.cc +590 -0
- fasttext_modern-0.10.0/src/dictionary.h +114 -0
- fasttext_modern-0.10.0/src/fasttext.cc +832 -0
- fasttext_modern-0.10.0/src/fasttext.h +170 -0
- fasttext_modern-0.10.0/src/loss.cc +346 -0
- fasttext_modern-0.10.0/src/loss.h +163 -0
- fasttext_modern-0.10.0/src/main.cc +454 -0
- fasttext_modern-0.10.0/src/matrix.cc +25 -0
- fasttext_modern-0.10.0/src/matrix.h +45 -0
- fasttext_modern-0.10.0/src/meter.cc +214 -0
- fasttext_modern-0.10.0/src/meter.h +91 -0
- fasttext_modern-0.10.0/src/model.cc +93 -0
- fasttext_modern-0.10.0/src/model.h +79 -0
- fasttext_modern-0.10.0/src/productquantizer.cc +251 -0
- fasttext_modern-0.10.0/src/productquantizer.h +63 -0
- fasttext_modern-0.10.0/src/quantmatrix.cc +125 -0
- fasttext_modern-0.10.0/src/quantmatrix.h +61 -0
- fasttext_modern-0.10.0/src/real.h +14 -0
- fasttext_modern-0.10.0/src/utils.cc +53 -0
- fasttext_modern-0.10.0/src/utils.h +72 -0
- fasttext_modern-0.10.0/src/vector.cc +96 -0
- fasttext_modern-0.10.0/src/vector.h +62 -0
- fasttext_modern-0.10.0/tests/test_numpy_compatibility.py +86 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to fastText Modern are documented here.
|
|
4
|
+
|
|
5
|
+
## [0.10.0] - 2026-09-02
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Prebuilt CPython 3.10 through 3.14 wheels for Linux x86-64, Windows x86-64,
|
|
10
|
+
macOS x86-64, and macOS Apple Silicon.
|
|
11
|
+
- Cross-platform native CMake validation and packaging tests.
|
|
12
|
+
- Trusted Publishing release automation for PyPI.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- Renamed the Python distribution to `fasttext-modern`; the import remains
|
|
17
|
+
`fasttext`.
|
|
18
|
+
- Raised the native compiler baseline to C++17 and CMake 3.16.
|
|
19
|
+
- Made host-specific CPU optimization opt-in for portable binaries.
|
|
20
|
+
- Declared Python 3.10 through 3.14 support and pybind11 3.x builds.
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
|
|
24
|
+
- NumPy 2 compatibility for prediction and evaluation arrays.
|
|
25
|
+
- Windows shared/static library and Python binding portability issues.
|
|
26
|
+
- Default thread selection on single-core systems.
|
|
27
|
+
|
|
28
|
+
[0.10.0]: https://github.com/Naviden/fastText/releases/tag/v0.10.0
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Contributing to fastText Modern
|
|
2
|
+
We want to make contributing to this project as easy and transparent as possible.
|
|
3
|
+
|
|
4
|
+
## Issues
|
|
5
|
+
We use GitHub issues to track public bugs. Please ensure your description is clear and has sufficient instructions to be able to reproduce the issue.
|
|
6
|
+
|
|
7
|
+
### Reproducing issues
|
|
8
|
+
Please make sure that the issue you mention is not a result of one of the existing third-party libraries. For example, please do not post an issue if you encountered an error within a third-party Python library. We can only help you with errors which can be directly reproduced either with our C++ code or the corresponding Python bindings. If you do find an error, please post detailed steps to reproduce it. If we can't reproduce your error, we can't help you fix it.
|
|
9
|
+
|
|
10
|
+
## Pull Requests
|
|
11
|
+
For substantial changes, please open an issue before submitting a pull request.
|
|
12
|
+
Small, well-scoped fixes may be submitted directly.
|
|
13
|
+
|
|
14
|
+
To create a pull request:
|
|
15
|
+
|
|
16
|
+
1. Fork the repo and create your branch from `main`.
|
|
17
|
+
2. If you've added code that should be tested, add tests.
|
|
18
|
+
3. If you've changed APIs, update the documentation.
|
|
19
|
+
4. Ensure the test suite passes.
|
|
20
|
+
5. Make sure your code is formatted consistently with the surrounding code.
|
|
21
|
+
|
|
22
|
+
## Tests
|
|
23
|
+
First, you will need to make sure you have the required data. For that, please have a look at the fetch_test_data.sh script under tests. Next run the tests using the runtests.py script passing a path to the directory containing the datasets.
|
|
24
|
+
|
|
25
|
+
## License
|
|
26
|
+
By contributing to fastText Modern, you agree that your contributions will be
|
|
27
|
+
licensed under the repository's MIT License. No separate contributor license
|
|
28
|
+
agreement is required by this fork.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# About this fork
|
|
2
|
+
|
|
3
|
+
fastText Modern is an independently maintained fork of
|
|
4
|
+
[facebookresearch/fastText](https://github.com/facebookresearch/fastText). It
|
|
5
|
+
focuses on keeping the native library and Python bindings buildable and easy to
|
|
6
|
+
install on current platforms.
|
|
7
|
+
|
|
8
|
+
## Identity and compatibility
|
|
9
|
+
|
|
10
|
+
- The Python distribution is named `fasttext-modern`.
|
|
11
|
+
- The Python import remains `fasttext` for source compatibility.
|
|
12
|
+
- This project is not affiliated with or endorsed by Meta.
|
|
13
|
+
- fastText and Meta names and marks belong to their respective owners. Their
|
|
14
|
+
use here identifies the upstream project and compatibility target.
|
|
15
|
+
|
|
16
|
+
## License and attribution
|
|
17
|
+
|
|
18
|
+
The upstream source is licensed under the MIT License. The original
|
|
19
|
+
[LICENSE](LICENSE), copyright notices, and Git history are preserved. New
|
|
20
|
+
contributions to this repository are made under the same MIT License.
|
|
21
|
+
|
|
22
|
+
## Models and datasets
|
|
23
|
+
|
|
24
|
+
This repository and its Python distributions contain source code only. Linked
|
|
25
|
+
pretrained models and datasets are provided by third parties or the upstream
|
|
26
|
+
project and may have separate terms. They are not relicensed or redistributed
|
|
27
|
+
by fastText Modern.
|
|
28
|
+
|
|
29
|
+
## Maintenance policy
|
|
30
|
+
|
|
31
|
+
The fork follows upstream bug fixes when they remain compatible with the goals
|
|
32
|
+
of this project. Fork releases use their own version numbers and changelog, and
|
|
33
|
+
changes are tested on the supported Python and native-platform matrix before
|
|
34
|
+
release.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present, Facebook, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fasttext-modern
|
|
3
|
+
Version: 0.10.0
|
|
4
|
+
Summary: Community-maintained fastText Python bindings
|
|
5
|
+
Home-page: https://github.com/Naviden/fastText
|
|
6
|
+
Author: Onur Celebi and fastText contributors
|
|
7
|
+
Maintainer: Naviden
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Source, https://github.com/Naviden/fastText
|
|
10
|
+
Project-URL: Issues, https://github.com/Naviden/fastText/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/Naviden/fastText/blob/main/CHANGELOG.md
|
|
12
|
+
Project-URL: Upstream, https://github.com/facebookresearch/fastText
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Programming Language :: C++
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
25
|
+
Classifier: Topic :: Software Development
|
|
26
|
+
Classifier: Topic :: Scientific/Engineering
|
|
27
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
28
|
+
Classifier: Operating System :: POSIX
|
|
29
|
+
Classifier: Operating System :: Unix
|
|
30
|
+
Classifier: Operating System :: MacOS
|
|
31
|
+
Requires-Python: >=3.10
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Requires-Dist: numpy
|
|
35
|
+
Dynamic: author
|
|
36
|
+
Dynamic: classifier
|
|
37
|
+
Dynamic: description
|
|
38
|
+
Dynamic: description-content-type
|
|
39
|
+
Dynamic: home-page
|
|
40
|
+
Dynamic: license
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
Dynamic: maintainer
|
|
43
|
+
Dynamic: project-url
|
|
44
|
+
Dynamic: requires-dist
|
|
45
|
+
Dynamic: requires-python
|
|
46
|
+
Dynamic: summary
|
|
47
|
+
|
|
48
|
+
# fastText Modern Python bindings
|
|
49
|
+
|
|
50
|
+
> This is the Python guide for the independently maintained fastText Modern
|
|
51
|
+
> fork. It is not affiliated with or endorsed by Meta. The import remains
|
|
52
|
+
> compatible with upstream fastText. See the
|
|
53
|
+
> [fork policy](https://github.com/Naviden/fastText/blob/main/FORK.md) for
|
|
54
|
+
> attribution and scope.
|
|
55
|
+
|
|
56
|
+
[fastText](https://fasttext.cc/) is a library for efficient learning of word representations and sentence classification.
|
|
57
|
+
|
|
58
|
+
In this document we present how to use fastText in python.
|
|
59
|
+
|
|
60
|
+
## Table of contents
|
|
61
|
+
|
|
62
|
+
* [Requirements](#requirements)
|
|
63
|
+
* [Installation](#installation)
|
|
64
|
+
* [Usage overview](#usage-overview)
|
|
65
|
+
* [Word representation model](#word-representation-model)
|
|
66
|
+
* [Text classification model](#text-classification-model)
|
|
67
|
+
* [IMPORTANT: Preprocessing data / encoding conventions](#important-preprocessing-data-encoding-conventions)
|
|
68
|
+
* [More examples](#more-examples)
|
|
69
|
+
* [API](#api)
|
|
70
|
+
* [`train_unsupervised` parameters](#train_unsupervised-parameters)
|
|
71
|
+
* [`train_supervised` parameters](#train_supervised-parameters)
|
|
72
|
+
* [`model` object](#model-object)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# Requirements
|
|
76
|
+
|
|
77
|
+
[fastText](https://fasttext.cc/) builds on Linux, macOS, and Windows.
|
|
78
|
+
It requires a compiler with C\++17 support, [Python](https://www.python.org/)
|
|
79
|
+
3.10 or newer, [NumPy](https://numpy.org/), and
|
|
80
|
+
[pybind11](https://github.com/pybind/pybind11).
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# Installation
|
|
84
|
+
|
|
85
|
+
To install the latest release, you can do :
|
|
86
|
+
```bash
|
|
87
|
+
$ python -m pip install fasttext-modern
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
or, to get the latest development version of fasttext, you can install from our github repository :
|
|
91
|
+
```bash
|
|
92
|
+
$ git clone https://github.com/Naviden/fastText.git
|
|
93
|
+
$ cd fastText
|
|
94
|
+
$ python -m pip install .
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
# Usage overview
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
## Word representation model
|
|
101
|
+
|
|
102
|
+
In order to learn word vectors, as [described here](https://fasttext.cc/docs/en/references.html#enriching-word-vectors-with-subword-information), we can use `fasttext.train_unsupervised` function like this:
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
```py
|
|
106
|
+
import fasttext
|
|
107
|
+
|
|
108
|
+
# Skipgram model :
|
|
109
|
+
model = fasttext.train_unsupervised('data.txt', model='skipgram')
|
|
110
|
+
|
|
111
|
+
# or, cbow model :
|
|
112
|
+
model = fasttext.train_unsupervised('data.txt', model='cbow')
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
where `data.txt` is a training file containing utf-8 encoded text.
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
The returned `model` object represents your learned model, and you can use it to retrieve information.
|
|
120
|
+
|
|
121
|
+
```py
|
|
122
|
+
print(model.words) # list of words in dictionary
|
|
123
|
+
print(model['king']) # get the vector of the word 'king'
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
### Saving and loading a model object
|
|
128
|
+
|
|
129
|
+
You can save your trained model object by calling the function `save_model`.
|
|
130
|
+
```py
|
|
131
|
+
model.save_model("model_filename.bin")
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
and retrieve it later thanks to the function `load_model` :
|
|
135
|
+
```py
|
|
136
|
+
model = fasttext.load_model("model_filename.bin")
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
For more information about word representation usage of fasttext, you can refer to our [word representations tutorial](https://fasttext.cc/docs/en/unsupervised-tutorial.html).
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
## Text classification model
|
|
143
|
+
|
|
144
|
+
In order to train a text classifier using the method [described here](https://fasttext.cc/docs/en/references.html#bag-of-tricks-for-efficient-text-classification), we can use `fasttext.train_supervised` function like this:
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
```py
|
|
148
|
+
import fasttext
|
|
149
|
+
|
|
150
|
+
model = fasttext.train_supervised('data.train.txt')
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
where `data.train.txt` is a text file containing a training sentence per line along with the labels. By default, we assume that labels are words that are prefixed by the string `__label__`
|
|
154
|
+
|
|
155
|
+
Once the model is trained, we can retrieve the list of words and labels:
|
|
156
|
+
|
|
157
|
+
```py
|
|
158
|
+
print(model.words)
|
|
159
|
+
print(model.labels)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
To evaluate our model by computing the precision at 1 (P@1) and the recall on a test set, we use the `test` function:
|
|
163
|
+
|
|
164
|
+
```py
|
|
165
|
+
def print_results(N, p, r):
|
|
166
|
+
print("N\t" + str(N))
|
|
167
|
+
print("P@{}\t{:.3f}".format(1, p))
|
|
168
|
+
print("R@{}\t{:.3f}".format(1, r))
|
|
169
|
+
|
|
170
|
+
print_results(*model.test('test.txt'))
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
We can also predict labels for a specific text :
|
|
174
|
+
|
|
175
|
+
```py
|
|
176
|
+
model.predict("Which baking dish is best to bake a banana bread ?")
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
By default, `predict` returns only one label : the one with the highest probability. You can also predict more than one label by specifying the parameter `k`:
|
|
180
|
+
```py
|
|
181
|
+
model.predict("Which baking dish is best to bake a banana bread ?", k=3)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
If you want to predict more than one sentence you can pass an array of strings :
|
|
185
|
+
|
|
186
|
+
```py
|
|
187
|
+
model.predict(["Which baking dish is best to bake a banana bread ?", "Why not put knives in the dishwasher?"], k=3)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
Of course, you can also save and load a model to/from a file as [in the word representation usage](#saving-and-loading-a-model-object).
|
|
192
|
+
|
|
193
|
+
For more information about text classification usage of fasttext, you can refer to our [text classification tutorial](https://fasttext.cc/docs/en/supervised-tutorial.html).
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
### Compress model files with quantization
|
|
199
|
+
|
|
200
|
+
When you want to save a supervised model file, fastText can compress it in order to have a much smaller model file by sacrificing only a little bit performance.
|
|
201
|
+
|
|
202
|
+
```py
|
|
203
|
+
# with the previously trained `model` object, call :
|
|
204
|
+
model.quantize(input='data.train.txt', retrain=True)
|
|
205
|
+
|
|
206
|
+
# then display results and save the new model :
|
|
207
|
+
print_results(*model.test(valid_data))
|
|
208
|
+
model.save_model("model_filename.ftz")
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
`model_filename.ftz` will have a much smaller size than `model_filename.bin`.
|
|
212
|
+
|
|
213
|
+
For further reading on quantization, you can refer to [this paragraph from our blog post](https://fasttext.cc/blog/2017/10/02/blog-post.html#model-compression).
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
## IMPORTANT: Preprocessing data / encoding conventions
|
|
217
|
+
|
|
218
|
+
In general it is important to properly preprocess your data. In particular our example scripts in the [root folder](https://github.com/Naviden/fastText) do this.
|
|
219
|
+
|
|
220
|
+
fastText assumes UTF-8 encoded text. All text must be Python `str` values. The
|
|
221
|
+
passed text is [encoded as UTF-8 by pybind11](https://pybind11.readthedocs.io/en/stable/advanced/cast/strings.html)
|
|
222
|
+
before it is passed to the fastText C++ library. This means it is important to
|
|
223
|
+
use UTF-8 encoded text when building a model. On Unix-like systems you can
|
|
224
|
+
convert text using [iconv](https://en.wikipedia.org/wiki/Iconv).
|
|
225
|
+
|
|
226
|
+
fastText will tokenize (split text into pieces) based on the following ASCII characters (bytes). In particular, it is not aware of UTF-8 whitespace. We advice the user to convert UTF-8 whitespace / word boundaries into one of the following symbols as appropiate.
|
|
227
|
+
|
|
228
|
+
* space
|
|
229
|
+
* tab
|
|
230
|
+
* vertical tab
|
|
231
|
+
* carriage return
|
|
232
|
+
* formfeed
|
|
233
|
+
* the null character
|
|
234
|
+
|
|
235
|
+
The newline character is used to delimit lines of text. In particular, the EOS token is appended to a line of text if a newline character is encountered. The only exception is if the number of tokens exceeds the MAX\_LINE\_SIZE constant as defined in the [Dictionary header](https://github.com/Naviden/fastText/blob/main/src/dictionary.h). This means if you have text that is not separate by newlines, such as the [fil9 dataset](http://mattmahoney.net/dc/textdata), it will be broken into chunks with MAX\_LINE\_SIZE of tokens and the EOS token is not appended.
|
|
236
|
+
|
|
237
|
+
The length of a token is the number of UTF-8 characters by considering the [leading two bits of a byte](https://en.wikipedia.org/wiki/UTF-8#Description) to identify [subsequent bytes of a multi-byte sequence](https://github.com/Naviden/fastText/blob/main/src/dictionary.cc). Knowing this is especially important when choosing the minimum and maximum length of subwords. Further, the EOS token (as specified in the [Dictionary header](https://github.com/Naviden/fastText/blob/main/src/dictionary.h)) is considered a character and will not be broken into subwords.
|
|
238
|
+
|
|
239
|
+
## More examples
|
|
240
|
+
|
|
241
|
+
In order to have a better knowledge of fastText models, please consider the main [README](https://github.com/Naviden/fastText/blob/main/README.md) and the [upstream tutorials](https://fasttext.cc/docs/en/supervised-tutorial.html).
|
|
242
|
+
|
|
243
|
+
You can find further python examples in [the doc folder](https://github.com/Naviden/fastText/tree/main/python/doc/examples).
|
|
244
|
+
|
|
245
|
+
As with any package you can get help on any Python function using the help function.
|
|
246
|
+
|
|
247
|
+
For example
|
|
248
|
+
|
|
249
|
+
```
|
|
250
|
+
+>>> import fasttext
|
|
251
|
+
+>>> help(fasttext.FastText)
|
|
252
|
+
|
|
253
|
+
Help on module fasttext.FastText in fasttext:
|
|
254
|
+
|
|
255
|
+
NAME
|
|
256
|
+
fasttext.FastText
|
|
257
|
+
|
|
258
|
+
DESCRIPTION
|
|
259
|
+
# Copyright (c) 2017-present, Facebook, Inc.
|
|
260
|
+
# All rights reserved.
|
|
261
|
+
#
|
|
262
|
+
# This source code is licensed under the MIT license found in the
|
|
263
|
+
# LICENSE file in the root directory of this source tree.
|
|
264
|
+
|
|
265
|
+
FUNCTIONS
|
|
266
|
+
load_model(path)
|
|
267
|
+
Load a model given a filepath and return a model object.
|
|
268
|
+
|
|
269
|
+
tokenize(text)
|
|
270
|
+
Given a string of text, tokenize it and return a list of tokens
|
|
271
|
+
[...]
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
# API
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
## `train_unsupervised` parameters
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
input # training file path (required)
|
|
282
|
+
model # unsupervised fasttext model {cbow, skipgram} [skipgram]
|
|
283
|
+
lr # learning rate [0.05]
|
|
284
|
+
dim # size of word vectors [100]
|
|
285
|
+
ws # size of the context window [5]
|
|
286
|
+
epoch # number of epochs [5]
|
|
287
|
+
minCount # minimal number of word occurences [5]
|
|
288
|
+
minn # min length of char ngram [3]
|
|
289
|
+
maxn # max length of char ngram [6]
|
|
290
|
+
neg # number of negatives sampled [5]
|
|
291
|
+
wordNgrams # max length of word ngram [1]
|
|
292
|
+
loss # loss function {ns, hs, softmax, ova} [ns]
|
|
293
|
+
bucket # number of buckets [2000000]
|
|
294
|
+
thread # number of threads [number of cpus]
|
|
295
|
+
lrUpdateRate # change the rate of updates for the learning rate [100]
|
|
296
|
+
t # sampling threshold [0.0001]
|
|
297
|
+
verbose # verbose [2]
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## `train_supervised` parameters
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
input # training file path (required)
|
|
304
|
+
lr # learning rate [0.1]
|
|
305
|
+
dim # size of word vectors [100]
|
|
306
|
+
ws # size of the context window [5]
|
|
307
|
+
epoch # number of epochs [5]
|
|
308
|
+
minCount # minimal number of word occurences [1]
|
|
309
|
+
minCountLabel # minimal number of label occurences [1]
|
|
310
|
+
minn # min length of char ngram [0]
|
|
311
|
+
maxn # max length of char ngram [0]
|
|
312
|
+
neg # number of negatives sampled [5]
|
|
313
|
+
wordNgrams # max length of word ngram [1]
|
|
314
|
+
loss # loss function {ns, hs, softmax, ova} [softmax]
|
|
315
|
+
bucket # number of buckets [2000000]
|
|
316
|
+
thread # number of threads [number of cpus]
|
|
317
|
+
lrUpdateRate # change the rate of updates for the learning rate [100]
|
|
318
|
+
t # sampling threshold [0.0001]
|
|
319
|
+
label # label prefix ['__label__']
|
|
320
|
+
verbose # verbose [2]
|
|
321
|
+
pretrainedVectors # pretrained word vectors (.vec file) for supervised learning []
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
## `model` object
|
|
325
|
+
|
|
326
|
+
`train_supervised`, `train_unsupervised` and `load_model` functions return an instance of `_FastText` class, that we generaly name `model` object.
|
|
327
|
+
|
|
328
|
+
This object exposes those training arguments as properties : `lr`, `dim`, `ws`, `epoch`, `minCount`, `minCountLabel`, `minn`, `maxn`, `neg`, `wordNgrams`, `loss`, `bucket`, `thread`, `lrUpdateRate`, `t`, `label`, `verbose`, `pretrainedVectors`. So `model.wordNgrams` will give you the max length of word ngram used for training this model.
|
|
329
|
+
|
|
330
|
+
In addition, the object exposes several functions :
|
|
331
|
+
|
|
332
|
+
```python
|
|
333
|
+
get_dimension # Get the dimension (size) of a lookup vector (hidden layer).
|
|
334
|
+
# This is equivalent to `dim` property.
|
|
335
|
+
get_input_vector # Given an index, get the corresponding vector of the Input Matrix.
|
|
336
|
+
get_input_matrix # Get a copy of the full input matrix of a Model.
|
|
337
|
+
get_labels # Get the entire list of labels of the dictionary
|
|
338
|
+
# This is equivalent to `labels` property.
|
|
339
|
+
get_line # Split a line of text into words and labels.
|
|
340
|
+
get_output_matrix # Get a copy of the full output matrix of a Model.
|
|
341
|
+
get_sentence_vector # Given a string, get a single vector represenation. This function
|
|
342
|
+
# assumes to be given a single line of text. We split words on
|
|
343
|
+
# whitespace (space, newline, tab, vertical tab) and the control
|
|
344
|
+
# characters carriage return, formfeed and the null character.
|
|
345
|
+
get_subword_id # Given a subword, return the index (within input matrix) it hashes to.
|
|
346
|
+
get_subwords # Given a word, get the subwords and their indicies.
|
|
347
|
+
get_word_id # Given a word, get the word id within the dictionary.
|
|
348
|
+
get_word_vector # Get the vector representation of word.
|
|
349
|
+
get_words # Get the entire list of words of the dictionary
|
|
350
|
+
# This is equivalent to `words` property.
|
|
351
|
+
is_quantized # whether the model has been quantized
|
|
352
|
+
predict # Given a string, get a list of labels and a list of corresponding probabilities.
|
|
353
|
+
quantize # Quantize the model reducing the size of the model and it's memory footprint.
|
|
354
|
+
save_model # Save the model to the given path
|
|
355
|
+
test # Evaluate supervised model using file given by path
|
|
356
|
+
test_label # Return the precision and recall score for each label.
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
The properties `words`, `labels` return the words and labels from the dictionary :
|
|
360
|
+
```py
|
|
361
|
+
model.words # equivalent to model.get_words()
|
|
362
|
+
model.labels # equivalent to model.get_labels()
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
The object overrides `__getitem__` and `__contains__` functions in order to return the representation of a word and to check if a word is in the vocabulary.
|
|
366
|
+
|
|
367
|
+
```py
|
|
368
|
+
model['king'] # equivalent to model.get_word_vector('king')
|
|
369
|
+
'king' in model # equivalent to `'king' in model.get_words()`
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
Support and community
|
|
374
|
+
---------------------
|
|
375
|
+
|
|
376
|
+
- [fastText Modern issues](https://github.com/Naviden/fastText/issues)
|
|
377
|
+
- [Upstream fastText](https://github.com/facebookresearch/fastText)
|
|
378
|
+
- [Stack Overflow](https://stackoverflow.com/questions/tagged/fasttext)
|
|
379
|
+
- [Upstream Google group](https://groups.google.com/forum/#!forum/fasttext-library)
|