Skip to content

Instantly share code, notes, and snippets.

@dlebech
Last active May 17, 2021 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlebech/3c9fc5b74c4b080622188d409aaef05d to your computer and use it in GitHub Desktop.
Save dlebech/3c9fc5b74c4b080622188d409aaef05d to your computer and use it in GitHub Desktop.
Quick example of language detection with fasttext small model (less than 1MB model)
# Public Domain CC0 license. https://creativecommons.org/publicdomain/zero/1.0/
# Prepare with: pip install fasttext
# Tested with Python 3.9
import urllib.request
import fasttext
# Download small model (917KB)
# Other options: https://fasttext.cc/docs/en/language-identification.html
urllib.request.urlretrieve('https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.ftz', 'model.ft')
# Predict language of "hello world"
m = fasttext.load_model('model.ft')
m.predict('hello world')
# Returns (('__label__en',), array([0.17635809]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment