• 大小: 1KB
    文件类型: .gz
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: 其他
  • 标签: 神经网络  

资源简介

通过wiki生成word2vec模型的例子,使用的中文 wiki资料

资源截图

代码片段和文件信息

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function

import logging
import os.path
import six
import sys

from gensim.corpora import WikiCorpus

if __name__ == ‘__main__‘:
    program = os.path.basename(sys.argv[0])
    logger = logging.getLogger(program)

    logging.basicConfig(format=‘%(asctime)s: %(levelname)s: %(message)s‘)
    logging.root.setLevel(level=logging.INFO)
    logger.info(“running %s“ % ‘ ‘.join(sys.argv))

    # check and process input arguments
    if len(sys.argv) != 3:
        print(“Using: python process_wiki.py enwiki.xxx.xml.bz2 wiki.en.text“)
        sys.exit(1)
    inp outp = sys.argv[1:3]
    space = “ “
    i = 0

    output = open(outp ‘w‘)
    wiki = WikiCorpus(inp lemmatize=False dictionary={})
    for text in wiki.get_texts():
        if six.PY3:
            output.write(bytes(‘ ‘.join(text) ‘utf-8‘).decode(‘utf-8‘) + ‘\n‘)
        #   ###another method###
        #    output.write(
        #            space.join(map(lambda x:x.decode(“utf-8“) text)) + ‘\n‘)
        else:
            output.write(space.join(text) + “\n“)
        i = i + 1
        if (i % 10000 == 0):
            logger.info(“Saved “ + str(i) + “ articles“)

    output.close()
    logger.info(“Finished Saved “ + str(i) + “ articles“)

评论

共有 条评论