jieba分词 cache问题

问题描述
jieba 在 pyinstaller -F app.py打包之后换其他电脑无法正常使用,经定位后发现是jeiba需要缓存temp目录存在jieba.cache文件
解决方案
参考文章 Pyinstaller打包用spec添加资源文件,亲测可用
spec写入资源jieba.cache
binaries=[("jieba.cache",".")]
通过move移动资源【获取到当前脚本运行的位置 然后jieba在里面 直接拼接绝对位置然后移动到指定位置】
shutil.move(os.path.abspath(os.path.dirname(sys.path[0]))+"\\jieba.cache", os.getenv("TEMP")+"\\jieba.cache")
在这里插入图片描述

# 词频统计.py
import os
import sys
import jieba
import shutil

shutil.move(os.path.abspath(os.path.dirname(sys.path[0]))+"\\jieba.cache", os.getenv("TEMP")+"\\jieba.cache")

txt = open("词频.txt", "r",
           encoding='utf8', errors='ignore').read()
words = jieba.lcut(txt)

counts = {}
for word in words:
    if len(word) == 1:
        continue
    counts[word] = counts.get(word, 0) + 1

items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
file = open(r'result.txt', 'w')
for i in range(len(items)):
    word, count = items[i]
    if count > 3 and not("-" in word):
        result = "{0:<10}{1:>5}".format(word, count)
        file.write(result+'\n')
        print(result)
file.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值