python2.7安装使用thulac库时遇到的一些问题

本文介绍了一个关于THULAC Python版本在Windows10环境下出现MemoryError的问题及解决方案。通过修改CBModel.py文件中的特定代码段并引入array模块,成功解决了因内存分配问题导致的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:Windows10、python2.7.14、thulac-0.1.1
提示错误:

Traceback (most recent call last):
File "", line 1, in 
File "C:\Python27\lib\site-packages\thulac_init_.py", line 58, in init
self.__tagging_decoder.init((self.__prefix+"model_c_model.bin"),(self.__prefix+"model_c_dat.bin"),(self.__prefix+"model_c_label.txt"))
File "C:\Python27\lib\site-packages\thulac\character\CBTaggingDecoder.py", line 36, in init
self.model = CBModel(modelFile)
File "C:\Python27\lib\site-packages\thulac\character\CBModel.py", line 58, in init
self.fl_weights = struct.unpack("<"+str(self.f_size * self.l_size)+"i", temp)
MemoryError

查阅了资料后,找到了修改方法
编辑CBModel.py,找到__init__方法,找到下面这个代码:

temp = inputfile.read(4 * self.l_size * self.l_size)
self.ll_weights = struct.unpack("<"+str(self.l_size * self.l_size)+"i", temp)
self.ll_weights = tuple(self.ll_weights)

temp = inputfile.read(4 * self.f_size * self.l_size)
self.fl_weights = struct.unpack("<"+str(self.f_size * self.l_size)+"i", temp)

改成

temp = inputfile.read(4 * self.l_size * self.l_size)
self.ll_weights = array.array('i')
self.ll_weights.fromstring(temp)
self.ll_weights = tuple(self.ll_weights)

temp = inputfile.read(4 * self.f_size * self.l_size)
self.fl_weights = array.array('i')
self.fl_weights.fromstring(temp)

再加上import

import array

python2.7适用,3.6有这个API,也能兼任

转自:https://github.com/thunlp/THULAC-Python/issues/25

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值