fastText Python Bindings

0.9.3 · active · verified Sat Apr 11

fastText is a library for efficient learning of word representations and sentence classification. Developed by Facebook AI Research, it's particularly good for large-scale text processing tasks. The current version is 0.9.3, with releases focusing on new features, performance, and API stability rather than a fixed cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to train a basic supervised text classification model and make predictions using fastText. The training data must be in the specific fastText format with `__label__` prefixes.

import fasttext
import os

# Create a dummy training file for demonstration
training_data_path = 'train.txt'
with open(training_data_path, 'w') as f:
    f.write('__label__positive This is a good movie.\n')
    f.write('__label__negative This movie was terrible.\n')
    f.write('__label__positive I love this film.\n')

# Train a supervised model
model = fasttext.train_supervised(input=training_data_path)

# Predict a label
text_to_predict = 'This is an excellent film.'
predictions = model.predict(text_to_predict)
print(f"Text: '{text_to_predict}'")
print(f"Prediction: {predictions[0][0]}, Probability: {predictions[1][0]:.4f}")

# Clean up dummy file
os.remove(training_data_path)

view raw JSON →