首页 > 解决方案 > Calculate word embeddings using fasttext

问题描述

I am trying to calculate the word embeddings using fasttext for the following sentence.

a = 'We are pencil in the hands'

I dont have any pretrained model, so how do i go about it?

标签: pythonpython-3.xnlpnltkfasttext

解决方案


您需要一张经过训练的嵌入表。

您可以从FastText 网站下载预训练的嵌入,并使用它们提供的代码来加载嵌入。你甚至不需要为此安装 FastText:

import io

def load_vectors(fname):
    fin = io.open(fname, 'r', encoding='utf-8', newline='\n', errors='ignore')
    n, d = map(int, fin.readline().split())
    data = {}
    for line in fin:
        tokens = line.rstrip().split(' ')
        data[tokens[0]] = map(float, tokens[1:])
    return data

然后,您只需从字典中提取。

或者,您可以按照教程在文本数据上自己训练 fasttext 。用于训练词嵌入的数据集的合理最小值是数十万个词。


推荐阅读