FastText: TypeError: loadModel(): incompatible function arguments

FastText: TypeError: loadModel(): incompatible function arguments

编辑:正在处理 Windows

我只想加载一个已下载的 fasttext 嵌入模型,但出现错误(见底部)我找不到解决方案。这是代码:

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()

这是错误:

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')

fasttext 的文档没有给我任何可能出错的线索。有什么猜测吗?谢谢!

提供字符串,而不是 Path 对象。

根据错误...

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')

...arg0(第一个位置参数)应该是 str,它看到的是 WindowsPath 对象。

可能只使用 str(facebook_model) 而不是 facebook_model 作为 fasttext.load_model().

的参数就足够了

但是如果您对 fasttext 代码实际指向的位置有任何进一步的混淆,您也可以查看并尝试 str(facebook_model.resolve()),这样您一定会看到绝对文件的完整路径。