Search code examples
python-3.xnlpfasttext

FastText: TypeError: loadModel(): incompatible function arguments


Edit: Working on Windows

I just want do load an already downloaded fasttext embedding model but got an error (see at the bottom) I can't find a solution to. This is the code:

import fasttext
from pathlib import Path

base_path = Path("..")
fasttext_model = base_path / "models" / "cc.de.300.bin"

class EmbeddingVectorizer:
    def __init__(self):

        self.embedding_model = fasttext.load_model(fasttext_model)

    def __call__(self, doc):
        """
        Convert address to embedding vectors
        :param address: The address to convert
        :return: The embeddings vectors
        """
        embeddings = []
        for word in doc:
            embeddings.append(self.embedding_model[word])
        return embeddings

embedding_model = EmbeddingVectorizer()

This is the error:

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2152/702628572.py in <module>
     15         return embeddings
     16 
---> 17 embedding_model = EmbeddingVectorizer()

~\AppData\Local\Temp/ipykernel_2152/702628572.py in __init__(self)
      2     def __init__(self):
      3 
----> 4         self.embedding_model = fasttext.load_model(fasttext_model)
      5 
      6     def __call__(self, doc):

~\Anaconda3\envs\project-relation-skill-extraction-master-thesis\lib\site-packages\fasttext\FastText.py in load_model(path)
    439     """Load a model given a filepath and return a model object."""
    440     eprint("Warning : `load_model` does not return WordVectorModel or SupervisedModel any more, but a `FastText` object which is very similar.")
--> 441     return _FastText(model_path=path)
    442 
    443 

~\Anaconda3\envs\project-relation-skill-extraction-master-thesis\lib\site-packages\fasttext\FastText.py in __init__(self, model_path, args)
     96         self.f = fasttext.fasttext()
     97         if model_path is not None:
---> 98             self.f.loadModel(model_path)
     99         self._words = None
    100         self._labels = None

TypeError: loadModel(): incompatible function arguments. The following argument types are supported:
    1. (self: fasttext_pybind.fasttext, arg0: str) -> None

Invoked with: <fasttext_pybind.fasttext object at 0x0000016C630257B0>, WindowsPath('../models/cc.de.300.bin')

The documentation of fasttext doesn't give me a clue what might could be wrong. Any guesses? Thank you!


Solution

  • Supply a string, not a Path object.

    Per the error...

    TypeError: loadModel(): incompatible function arguments. The following argument types are supported:
        1. (self: fasttext_pybind.fasttext, arg0: str) -> None
    
    Invoked with: <fasttext_pybind.fasttext object at 0x0000016C630257B0>, WindowsPath('../models/cc.de.300.bin')
    

    ...the arg0 (1st positional argument) should be a str, and it's seeing a WindowsPath object instead.

    It will probably be enough to just use str(facebook_model), instead of just facebook_model, as your argument to fasttext.load_model().

    But if there's any further confusion about where you're actually pointing the fasttext code, you could also look at, and try, str(facebook_model.resolve()), so that you're sure to see the absolute full path to your file.