如何使用 gensim 在 Windows 上运行 word2vec

几年前,我团队的一位前任开发人员编写了以下 Python 代码,调用 word2vec,传入训练文件和输出文件的位置。他在 Linux 上工作。我被要求让它在 Windows 机器上运行。请记住,我几乎不知道 Python,我已经安装了 Gensim,我猜它现在实现了 word2vec,但不知道如何重写代码以使用库而不是在 Windows 上似乎无法编译的可执行文件盒子。有人可以帮我更新此代码吗?


#!/usr/bin/env python3


import os

import csv

import subprocess

import shutil


from gensim.models import word2vec


def train_word2vec(trainFile, output):

    # run word2vec:

    subprocess.run(["word2vec", "-train", trainFile, "-output", output,

                    "-cbow", "0", "-window", "10", "-size", "100"],

                   shell=False)

    # Remove some invalid unicode:

    with open(output, 'rb') as input_,\

         open('%s.new' % output, 'w') as new_output:

        for line in input_:

            try:

                print(line.decode('utf-8'), file=new_output, end='')

            except UnicodeDecodeError:

                print(line)

                pass

    shutil.move('%s.new' % output, output)


def main():

    train_word2vec("c:/temp/wc/test1_BigF.txt", "c:/temp/wc/test1_w2v_model.txt")


if __name__ == '__main__':

    main()


梵蒂冈之花
浏览 187回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python