To get a better overview and understanding of the need for this project, I would recommend to read my article on the same.
The objective of this script is to be able to use our custom fasttext embeddings with the Flair.
The integration is very simple:
- Copy the
fasttext_custom_embeddings_with_flair.py
file to your project. - Install the packages from
requirements.txt
. - Import the
FastTextEmbeddings
fromfasttext_custom_embeddings_with_flair.py
file. - Instantiate an object of
FastTextEmbeddings
by passing either the local path or the remote http(s) url to the constructor. - Use it like you use any other embedding object in Flair.
from fasttext_custom_embeddings_with_flair import FastTextEmbeddings
from flair.data import Sentence
ft_embeddings = FastTextEmbeddings('/path/to/custom_fasttext_embeddings.bin', use_local=True)
sentence = Sentence('The quick brown fox jumps over a lazy dog.', use_tokenizer=True)
ft_embeddings.embed(sentence)
- Initialize the constructor by passing the local path to embeddings
.bin
file. - Set the boolean
use_local
toTrue
.
from fasttext_custom_embeddings_with_flair import FastTextEmbeddings
from flair.data import Sentence
ft_embeddings = FastTextEmbeddings('/url/to/custom_fattext_embeddings.bin', use_local=False)
sentence = Sentence('The quick brown fox jumps over a lazy dog.', use_tokenizer=True)
ft_embeddings.embed(sentence)
- Initialize the constructor by passing the remote url to embeddings
.bin
file. (I have tested this with my embeddings in S3 with an https url.) - Set the boolean
use_local
toFalse
. (Mandatory step if you are using a remote url.)
from fasttext_custom_embeddings_with_flair import FastTextEmbeddings
from flair.embeddings import WordEmbeddings, StackedEmbeddings
from flair.data import Sentence
ft_embeddings = FastTextEmbeddings('/url/to/custom_fattext_embeddings.bin', use_local=False)
glove_embeddings = WordEmbeddings('glove')
sentence = Sentence('The quick brown fox jumps over a lazy dog.', use_tokenizer=True)
stacked_embeddings = StackedEmbeddings([ft_embeddings, glove_embeddings])
stacked_embeddings.embed(sentence)
from fasttext_custom_embeddings_with_flair import FastTextEmbeddings
from flair.embeddings import WordEmbeddings, DocumentRNNEmbeddings
from flair.data import Sentence
ft_embeddings = FastTextEmbeddings('/url/to/custom_fattext_embeddings.bin', use_local=True)
glove_embeddings = WordEmbeddings('glove')
sentence = Sentence('The quick brown fox jumps over a lazy dog.', use_tokenizer=True)
document_rnn_embeddings = DocumentRNNEmbeddings([ft_embeddings, glove_embeddings])
document_rnn_embeddings.embed(sentence)
In case of any doubts, get in touch.
Refernces:
This repository is licensed under the Apache 2.0 License.