{"id":32556315,"url":"https://github.com/olegtarasov/fasttext.netwrapper","last_synced_at":"2025-10-28T22:54:54.139Z","repository":{"id":69236547,"uuid":"118227887","full_name":"olegtarasov/FastText.NetWrapper","owner":"olegtarasov","description":".NET Standard wrapper for fastText library. Now works on Windows, Linux and MacOs!","archived":false,"fork":false,"pushed_at":"2024-09-30T11:46:10.000Z","size":2390,"stargazers_count":76,"open_issues_count":6,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-08-24T08:39:43.432Z","etag":null,"topics":["csharp","fasttext","machine-learning","net","nlp"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/olegtarasov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-20T09:17:00.000Z","updated_at":"2025-07-09T22:30:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd262361-9dc2-4844-baae-570fa9f935fb","html_url":"https://github.com/olegtarasov/FastText.NetWrapper","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/olegtarasov/FastText.NetWrapper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olegtarasov%2FFastText.NetWrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olegtarasov%2FFastText.NetWrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olegtarasov%2FFastText.NetWrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olegtarasov%2FFastText.NetWrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/olegtarasov","download_url":"https://codeload.github.com/olegtarasov/FastText.NetWrapper/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olegtarasov%2FFastText.NetWrapper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281527382,"owners_count":26516845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-28T02:00:06.022Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","fasttext","machine-learning","net","nlp"],"created_at":"2025-10-28T22:54:52.863Z","updated_at":"2025-10-28T22:54:54.132Z","avatar_url":"https://github.com/olegtarasov.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build status](https://github.com/olegtarasov/FastText.NetWrapper/actions/workflows/BuildAndPublish.yml/badge.svg)](https://github.com/olegtarasov/FastText.NetWrapper/actions)\n[![Nuget](https://img.shields.io/nuget/v/FastText.NetWrapper?style=flat-square)](https://www.nuget.org/packages/FastText.NetWrapper)\n[![Donwloads](https://img.shields.io/nuget/dt/FastText.NetWrapper?label=Nuget\u0026style=flat-square)](https://www.nuget.org/packages/FastText.NetWrapper)\n\n# FastText.NetWrapper\n\nThis is a cross-platform .NET Standard wrapper for Facebook's [FastText](https://github.com/facebookresearch/fastText) library.\nThe wrapper comes with bundled precompiled native binaries for all three platforms: Windows, Linux and MacOs.\n\nJust add it to your project and start using it! No additional setup required. This library will unpack and call appropriate native\nbinary depending on target platform.\n\n## Is this project dead or abandoned?\n\nOf course not! It's just complete :) There are no major updates for fastText, and most bugs in this repository are fixed. All features\nshould work and if something doesn't — just ping me with an issue and I will try to get back to you.\n\n## Usage\n\nLibrary API closely follows fastText command-line interface, so you can jump right in.\n\n### Supervised model training\n\nThe simplest use case is to train a supervised model with default parameters. We create a `FastTextWrapper` and call `Supervised()`.\n\n```c#\nvar fastText = new FastTextWrapper();\nfastText.Supervised(\"cooking.train.txt\",  \"cooking\");\n```\n\nNote the arguments:\n\n1. We specify an input file with one labeled example per line. Here we use Stack Overflow cooking dataset from Facebook:\nhttps://dl.fbaipublicfiles.com/fasttext/data/cooking.stackexchange.tar.gz. You can find extracted files split into training\nand validation sets in `UnitTests` directory in this repository.\n2. Your model will be saved to `cooking.bin` and `cooking.vec` with pretrained vectors will be placed if the same directory.\n3. Here we use `Supervised()` overload with 2 arguments. This means that training will be done with default parameters.\nIt's a good starting point and is the same as calling fastText this way:\n\n```bash\n./fasttext supervised -input cooking.train.txt -output cooking\n```\n\n### Loading models\n\nCall `LoadModel()` and specify path to the `.bin` model file:\n\n```c#\nvar fastText = new FastTextWrapper();\nfastText.LoadModel(\"model.bin\");\n```\n\n### Using pretrained vectors\n\nTo use pretrained vectors for your supervised model, create an instance of `SupervisedArgs` and customize it:\n\n**❗ Important ❗** It doesn't say this anywhere in the original documentation, but you must use preterained vectors in **text** format\n(`.vec` file extension), and not in binary format. If you try to use binary vectors, you will get an error about your vectors having\nthe dimension 0.\n\n```c#\nvar fastText = new FastTextWrapper();\n\nvar args = new SupervisedArgs\n{\n    PretrainedVectors = \"cooking.unsup.300.vec\",\n    dim = 300\n};\n\nfastText.Supervised(\"cooking.train.txt\", \"cooking\", args);\n```\n\nHere we get default training arguments, supply a path to pretrained vectors file and adjust vector dimension accordingly.\n\n**❗ Important ❗** Be sure to always check the dimension of your pretrained vectors! Many vectors on the internet have dimension `300`,\nbut default dimension for fastText supervised model training is `100`.\n\n### Testing the model\n\nNow you can easily test a supervised model against a validation set. You can specify different values for `k` and `threshlod` as well.\n\n```c#\nvar result = fastText.Test(\"cooking.valid.txt\");\n```\n\nYou will get an instance of `TestResult` where you can find aggregated or per-label metrics:\n\n```c#\nConsole.WriteLine($\"Results:\\n\\tPrecision: {result.GlobalMetrics.GetPrecision()}\" +\n                            $\"\\n\\tRecall: {result.GlobalMetrics.GetRecall()}\" +\n                            $\"\\n\\tF1: {result.GlobalMetrics.GetF1()}\");\n```\n\nYou can even get a precision-recall curve (aggregated or per-label)! Here is an example of exporting an SVG plot with cross-platform\n[OxyPlot library](https://oxyplot.github.io):\n\n```c#\nvar result = fastText.Test(\"cooking.valid.txt\");\nvar curve = result.GetPrecisionRecallCurve();\n\nvar series = new LineSeries {StrokeThickness = 1};\nseries.Points.AddRange(curve.Select(x =\u003e new DataPoint(x.recall, x.precision)).OrderBy(x =\u003e x.X));\n\nvar plotModel = new PlotModel\n{\n    Series = { series },\n    Axes =\n    {\n        new LinearAxis {Position = AxisPosition.Bottom, Title = \"Recall\"},\n        new LinearAxis {Position = AxisPosition.Left, Title = \"Precision\"}\n    }\n};\n\nusing (var stream = new FileStream(\"precision-recall.svg\", FileMode.Create, FileAccess.Write))\n{\n    SvgExporter.Export(plotModel, stream, 600, 600, false);\n}\n```\n\n![](docs/prec-rec.png)\n\n### Supervised model quantization\n\nYou can train a new supervised model and quantize it immediatly by replacing `SupervisedArgs` with `QuantizedSupervisedArgs`:\n\n```c#\nvar fastText = new FastTextWrapper();\nfastText.Supervised(\"cooking.train.txt\", \"cooking\", new QuantizedSupervisedArgs());\n```\n\nYou can also load an existing model and quantize it:\n\n```c#\nvar fastText = new FastTextWrapper();\nfastText.LoadModel(\"model.bin\");\nfastText.Quantize();\n```\n\n### Training unsupervised models\n\nUse `Unsupervised()` method specifying model type: Skipgram or Cbow:\n\n```c#\nvar fastText = new FastTextWrapper();\nfastText.Unsupervised(UnsupervisedModel.SkipGram, \"cooking.train.nolabels.txt\",  \"cooking\");\n```\n\nYou can use an optional `UnsupervisedArgs` argument to customize training.\n\n### Automatic hyperparameter tuning\n\nYou can use fastText autotune to do an automatic hyperparameter search.\n\nRefer to https://github.com/facebookresearch/fastText/blob/master/docs/autotune.md for complete parameter reference.\n\nUse `AutotuneArgs` to control tuning:\n\n```c#\nvar fastText = new FastTextWrapper();\n\nvar autotuneArgs = new AutotuneArgs\n{\n    Duration = 30, // in seconds\n    Metric = \"precisionAtRecall:30\", // supports custom metrics\n    Predictions = 2, // Supports @k predictions\n    ModelSize = \"10M\", // Set this to train a quantized model and do an\n                       // additional quantization hyperparameter search. Requires QuantizedSupervisedArgs.\n    ValidationFile = \"cooking.valid.txt\" // REQUIRED: path to a validation file\n};\n\nfastText.Supervised(\"cooking.train.txt\",  \"cooking\", new QuantizedSupervisedArgs(), autotuneArgs);\n```\n\n### Progress callbacks\n\nYou can get progress callbacks from the native library. To do so, add a handler to `(Un)SupervisedArgs.TrainProgressCallback` for\nsimple training, or to `AutotuneArgs.AutotuneProgressCallback` for hyperparameter tuning.\n\nSee `ConsoleTest` project for an example of using training callbacks with `ShellProgressBar` library:\n\n```c#\nusing (var pBar = new ProgressBar(100, \"Training\"))\n{\n    var ftArgs = new SupervisedArgs\n    {\n        // ... Other args\n        verbose = 0,\n        TrainProgressCallback = (progress, loss, wst, lr, eta) =\u003e\n        {\n            pBar.Tick((int)Math.Ceiling(progress * 100), $\"Loss: {loss}, words/thread/sec: {wst}, LR: {lr}, ETA: {eta}\");\n        }\n    };\n\n    fastText.Supervised(\"cooking.train.txt\", outPath, ftArgs);\n}\n```\n\n![](docs/progress.gif)\n\n### Stopping `stderr` output\n\nNative FastText library reports training progress to `stderr` by default. You can turn off this output by setting\n`(Un)SupervisedArgs.verbose = 0` for simple training and `AutotuneArgs.Verbose = 0` for hyperparameter tuning.\n\n### Getting logs from the wrapper\n\n`FastTextWrapper` can produce a small amount of logs mostly concerning native library management. You can turn logging on by providing an\ninstance of `Microsoft.Extensions.Logging.ILoggerFactory`. In this example we use Serilog with console sink.\n\nYou can also inject your standard `IloggerFactory` through .NET Core DI.\n\n```c#\n// Add the following Nuget packages to your project:\n// * Serilog.Sinks.Console\n// * Serilog.Extensions.Logging\n\nLog.Logger = new LoggerConfiguration()\n                .MinimumLevel.Debug()\n                .WriteTo.Console(theme: ConsoleTheme.None)\n                .CreateLogger();\n\nvar fastText = new FastTextWrapper(loggerFactory: new SerilogLoggerFactory());\n```\n\n### Handling native exceptions\n\nIn version `1.1` I've added much better native error handling. Now in case of most native errors you will get a nice\n`NativeLibraryException` which you can inspect for detailed error description.\n\n## Windows Requirements\n\nSince this wrapper uses native C++ binaries under the hood, you will need to have Visual C++ Runtime Version 140 installed when\nrunning under Windows. Visit the MS Downloads page (https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads)\nand select the appropriate redistributable.\n\n## FastText C-style API\n\nIf you are interested in using FastText with C-style API, here is my fork of the official library: https://github.com/olegtarasov/fastText.\n\n## Changelog\n\n### `1.3.1`\n\n* Updated fastText binaries with latest improvements from the Facebook repo.\n\n### `1.3.0`\n\n* Native libraries are now explicitly included in target project and copied to output directory. Hopefully,\nthis solves a couple of problems with the previous approach of dynamically extracting libraries from\nresources.\n\n### `1.2.5`\n\n* Fixed progress callbacks for unsupervised model training.\n\n### `1.2.4`\n\n* Added progress callbacks for model training and autotuning.\n\n### `1.2.3`\n\n* Added supervised model quantization with `Quantize` method.\n* Stable version released! 🎉\n\n### `1.2.2-preview`\n\n* Merged #20 with new `GetWordVector` method.\n\n### `1.2.1-preview`\n\n* Added model autotuning with quantization support.\n* Fixed a horrible bug with `bool` marshalling.\n\n### `1.2.0-preview`\n\nVersion 1.2.0 introduces a few breaking changes to library API. If you are not ready to migrate, use v. `1.1.2`.\n\n* **❗️Breaking change:️** Removed both deprecated `Train()` methods.\n* **❗️Breaking change:️** Removed deprecated `SupervisedArgs` class.\n* **❗️Breaking change:️** Removed `FastTextArgs.SupervisedDefaults()` in favor of new `SupervisedArgs` with default constructor.\n* **❗️Breaking change:️** `FastTextArgs` class can't be constructed directly, use new `SupervisedArgs` and `UnsupervisedArgs` classes.\n* Added an `Unsupervised()` method to train Skipgram or Cbow models.\n\n### `1.1.2`\n\n* Fixed a horrible bug with `bool` marshalling on a `1.1.*` branch.\n\n### `1.1.0`, `1.1.1`\n\n* Added new `Supervised()` method as part of streamlining the API.\n* Added new `Test()` method for testing supervised model.\n* Deprecated both `Train()` methods. They will be removed in v. `1.2.0`.\n\n### `1.0.38`\n\n* Fixed a horrible bug with `bool` marshalling on a `1.0.*` branch.\n\n## Version `1.2.0` migration guide\n\n* Instead of old `Train()` methods use `Supervised()` and `Unsupervised()` methods.\n* Instead of `FastTextArgs.SupervisedDefaults()` use `SupervisedArgs` or `Supervised()` overload with 2 arguments.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folegtarasov%2Ffasttext.netwrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folegtarasov%2Ffasttext.netwrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folegtarasov%2Ffasttext.netwrapper/lists"}