Python implementation Word2vec Training results bin file to TXT file

Source: Internet
Author: User

The manager asked me to convert the bin file from Word2vec training to a TXT file, and I don't know what the TXT file is for. In fact, Word2vec training corpus can choose the training department out of the bin file or txt file, but the process is too long to train the bin file, I am afraid to directly train the TXT file is also slow, so I still try to do this thing.

I used the Gensim, this need to install their own, my computer installed this is quite troublesome.

#-*-coding:utf-8-*-ImportGensimImportCodecsdefMain (): Path_to_model='Result.bin'output_file='file.txt'bin2txt (Path_to_model, output_file)defbin2txt (Path_to_model, output_file): Output= Codecs.open (Output_file,'W','Utf-8') Model= Gensim.models.KeyedVectors.load_word2vec_format (Path_to_model, binary=True)Print('Done loading word2vec!') Vocab=Model.vocab forIteminchVocab:vector=list () forDimensioninchModel[item]: Vector.append (str (dimension)) Vector_str=",". Join (vector) line= Item +"\ t"+vector_str Output.writelines ( line+"\ n") #本来用的是write () method, but the result is not a newline effect.    Changed to Writelines () method has not been tried. Output.close ()if __name__=="__main__": Main ()

Python implementation Word2vec Training results bin file to TXT file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.