sunpinyin_Sunpinyin加速脚本 – 内存缓存词库

UPDATE2:感谢“据说要有”的建议,已修改脚本。

UPDATE1:脚本在异常退出(比如收到KILL信号,系统断电等)后再次运行时会有严重的BUG,导致用户词库丢失。现已修正,强烈推荐各位使用者更新。如果已经有过异常退出的用户,请退出脚本后至~/.sunpinyin/目录下检查(ls -l),若userdict为符号链接,请删除之,sunpinyin将重建新的用户词库。对因此造成的数据损失表示抱歉。

之前在一篇文章里,我提到了在Ubuntu下使用fcitx + sunpinyin配合hubertstar的大词库实现媲美搜狗的流畅输入,但是仍然有一个大问题——当词库过大的时候输入中会出现停顿现象,严重影响用户体验(因此我当时也只推荐使用small版词库)。

这是因为fcitx和sunpinyin的词库都是使用SQLITE,在输入时需要反复查询,由于词库的特殊性(前后两个查询的词经常离得很远),SQLITE的缓存机制很难起效,因而只能反复读硬盘,导致速度很慢。

不过,在大宝的启发下,我参考了Ubuntu中文论坛里hubertstar的帖子,以及大宝的博客,我写了一个供Ubuntu使用的sunpinyin加速脚本,效果非常不错,拿上来和大家分享。

原理非常暴力——直接将整个sunpinyin的userdict放入/dev/shm(即存在于内存中的tmpfs),然后符号链接回来,相当于手动暴力cache整个词库,大幅提高了速度。这样做法的缺点就是内存消耗较大——我现在使用的大词库有100+MB,即需要100+MB的内存,不过,当下基本每个人都有2G+的内存,本来也就很难用完,这样正好提高了内存的利用率。

使用方式:将这个脚本放在方便的地方,通过chmod +x赋予可执行权利,在“系统 -> 首选项 -> 启动应用程序”中添加这个脚本。

相比于hubertstar和大宝的脚本,这个脚本有如下优点:

使用方便,只需要在GNOME会话登陆后自动运行即可;

自动将用户词库变更备份回原目录,不会导致自造词丢失;

卸载方便,脚本在退出时(包括关机或登出)会自动恢复用户词库,因而如果没有启动这个脚本也不影响运行sunpinyin;

完全运行在用户空间,不需要使用服务或者cron。

#!/bin/bash

#sunpinyin_speed_up.sh

# Sunpinyin Speed Up Script for Ubuntu (by memory caching, and no data loss)

# You can run this script in background on GNOME logging in.

# Originally written Hubert Star, modified by Bob Robot (http://robotshell.org/).

# Capture the exit signal, make sure it is the FIRST uncommented line.

trap "do_exit" SIGHUP SIGINT SIGQUIT SIGTERM

SUN_DIR="${HOME}/.sunpinyin"

SHM_USERDICT="/dev/shm/sunpinyin_userdict.sh0"

# Backup the userdict and restore all changes made by this script on exit.

do_exit() {

cp -f "${SHM_USERDICT}" "${SUN_DIR}/userdict.real"

rm -f "${SHM_USERDICT}"

mv -f "${SUN_DIR}/userdict.real" "${SUN_DIR}/userdict"

exit 0

}

# Work around for abnormal quit.

if [ -e "${SUN_DIR}/userdict.real" ]

then

rm -f "${SHM_USERDICT}"

mv -f "${SUN_DIR}/userdict.real" "${SUN_DIR}/userdict"

fi

# Rename the real userdict, copy it to RAM and make a symblic link back.

# From now on the modification and query on userdict takes place in RAM.

mv -f "${SUN_DIR}/userdict" "${SUN_DIR}/userdict.real"

cp -f "${SUN_DIR}/userdict.real" "${SHM_USERDICT}"

ln -sf "${SHM_USERDICT}" "${SUN_DIR}/userdict"

# Automatically backup the userdict, make sure not losing the modification.

p_count=0

while [ true ]

do

p_count=$(($p_count+1))

sleep 1800

if [ $p_count == 4 ]

then

p_count=0

cp -f "${SHM_USERDICT}" "${SUN_DIR}/userdict.real"

fi

p_size_shm=$(ls -l "${SHM_USERDICT}" | awk '{print $5}')

p_size_real_t=$(ls -l "${SUN_DIR}/userdict.real" | awk '{print $5}')

p_size_real=$(($p_size_real_t+512))

if [ $p_size_shm -ge $p_size_real ]

then

cp -f "${SHM_USERDICT}" "${SUN_DIR}/userdict.real"

fi

done

经测试,不论多大的词库(只要你内存无压力)都可以顺畅输入。赶紧把你的sunpinyin词库换成超大词库,来体验更流畅的Ubuntu拼音输入吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值