-1

I am a little ashamed that I have to ask this question because I feel like I should know this. I haven't been programming long but I am trying to apply what I learn to a project I'm working on, and that is how I got to this question. Fast Text has a library of word and associated points https://fasttext.cc/docs/en/english-vectors.html . It is used to find the vector of the word. I just want to look a word or two up and see what the result is in order to see if it is useful for my project. They have provided a list of vectors and then a small code chunck. I cannot make heads or tails out of it. some of it I get but i do not see a print function - is it returning the data to a different part of your own code? I also am not sure where the chunk of code opens the data file, usually fname is a handle right? Or are they expecting you to type your file's path there. I also am not familiar with io, I googled the word but didn't find anything useful. Is this something I need to download or is it already a part of python. I know I might be a little out of my league but I learn best by doing, so please don't hate on me.

    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
John B
  • 9
  • 1

1 Answers1

1

Try the following:

my_file_name = 'C:/path/to/file.txt' # Use the path to your file of rows of sentences

my_data = load_vectors(my_file_name) # Function will return data

print(my_data) # To see the output

YScharf
  • 1,638
  • 15
  • 20
  • This is a good example of using the specific loading function provided in the question. But for real work with a set of FastText vectors, you'd be more likely to use the built-in functions for loading existing models in either the official Facebook FastText Python bindings (https://github.com/facebookresearch/fastText/tree/master/python - may not work easily on Windows) or another library that reads those models like Gensim (https://radimrehurek.com/gensim_3.8.3/models/fasttext.html). – gojomo Feb 22 '21 at 18:25