配置--NLP常用第三放依赖的安装和使用

jieba分词

参考: https://www.jianshu.com/p/8d455e70f724

ubuntu下安装jieba

pip install jieba

使用

import jieba
import jieba.posseg as psg

content = "张三非常喜欢打篮球,他认为打篮球和有趣."

print("精确分词模式结果:")
segs_1 = jieba.cut(content, cut_all=False)
print("/".join(segs_1))

print("全模式分词模式结果:")
segs_3 = jieba.cut(content, cut_all = True)
print("/".join(segs_3))

print("搜索引擎模式结果:")
segs_4 = jieba.cut_for_search(content)
print("/".join(segs_4))

print("获取分词后的list:")
segs_5 = jieba.lcut(content)
print(segs_5)

print("获取词性")
print([(x.word, x.flag) for x in psg.lcut(content)])

gensim

利用gensim得到对应单词的词向量

利用gensim和jieba生成中文词向量

https://blog.csdn.net/shuihupo/article/details/85162237

针对中文文档生成分词得到的每个单词对应的词向量。

import os
import jieba
import jieba.analyse
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence

class ChineseW2V:
    def __init__(self, filename, outputPath="./output/", size=300, window=10, min_count=1, hs=1):
        self.filename = filename # 文件路径
        self.outputPath = outputPath # 文件输出路径
        self.size = size
        self.window = window
        self.min_count = min_count
        self.hs = hs
        self.jieba_cut_outfile = None

    def readFile(self):
        with open(self.filename, encoding="utf-8") as f:
            document = f.read()
            document_cut = jieba.cut(document)
            result = ' '.join(document_cut)

            if not os.path.exists(self.outputPath):
                os.mkdir(self.outputPath)

            with open(self.outputPath+"jieba_cut.txt", "w", encoding="utf-8") as outf:
                outf.write(result)
        self.jieba_cut_outfile = self.outputPath+"jieba_cut.txt"

    def genEmbedding(self):
        self.readFile()
        sentences = LineSentence(self.jieba_cut_outfile)
        model = Word2Vec(sentences, hs=self.hs, min_count=self.min_count, window=self.window, size=self.size)
        model.save("./output/Chinese_word2vec.model")
        print("=="*3+"Done"+"=="*3)


if __name__ == '__main__':
    filename = './data/三体txt.txt'
    genmodel = ChineseW2V(filename)
    genmodel.genEmbedding()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值