给sunpinyin加速

因为sunpinyin词库一大就会卡,因此需要自己添加一个脚本给sunpinyin加速。

加速的原理就是把词库添加到内存,现在内存都这么大,根本不在乎这么几兆,当然输入体验更重要啦~

首先先建一个脚本实现把词库放到内存中的功能,脚本就取名为sunpinyin_speed_up吧。

#!/bin/sh

# Capture the exit signal, make sure it is the FIRST uncommented line.
trap "do_exit" SIGHUP SIGINT SIGQUIT SIGTERM

SUN_DIR="/home/xuzhenan/.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"
  #notify-send 'Pinyin dict' 'done'
  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_speed_up

 

之后将其设置为开机启动就好了。

因为我们用systemd的方式,所以需要再写一个开机启动的服务放到/etc/systemd/system/中。

[Unit]
Description=Sunpinyin dict cache

[Service]
ExecStart=/home/xuzhenan/mysh/sunpinyin_speed_up

[Install]
WantedBy=multi-user.target 
sunpinyin_speed_up.service

之后运行

sudo systemctl enable sunpinyin_speed_up.service 

将其设置为开机自启动就好了。

posted @ 2017-07-01 20:57  BBBob  阅读(227)  评论(0编辑  收藏  举报