KenLm的环境安装及使用总结
前言
最近,在学习宗庆后的统计自然语言处理中的N元文法求句子概率时,老师介绍了两个模型(KenLM和SRILM)可以从语料库中训练并建模,求出概率。目前使用比较好的有KenLM及SRILM模型,其中KenLM比SRILM晚出来,训练速度也更快,而且支持单机大数据的训练。本文主要对KenLM的分析。
前提配置
- VMware workstation Pro 15
- UbuntuDesktop 18.04(配置尽量高,虚拟机内存尽量在8G,双处理器,双核)
- clone一份虚拟机,以防第一个崩掉,可以及时使用clone的,不用等太长时间再安装
环境配置的过程及步骤
1.python版本修改
由于Ubuntu18.04中的python版本有两个,一个是2.7,一个是3.6,我们可以两个都不用,下载一个最新的版本,以防后续命令由于版本不匹配导致异常。
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
#解压文件
tar zxvf Python-3.7.4.tgz
#进入安装目录
sudo ./configure
#安装make
sudo make
sudo make install
#安装时可能会出现错误,即:ModuleNotFoundError: No module named ‘_ctypes’
解决办法:https://www.jianshu.com/p/d5e8704c9fb7
#默认版本修改,后面数字1代表优先级,数字越大越优先
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 3
sudo update-alternatives --config-python#查看当前的优先级状态
2.修改软件安装源
由于使用Ubuntu官方的下载源速度极其慢,我们可以使用阿里或者清华的镜像源来下载
cd cd /etc/apt/
#备份
sudo cp sources.list sources.list.bak
#修改
vi sources.list
#阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multivers
3.正式安装
#在boost官网下载boost:http://www.boost.org
cd boost
./bootstrap.sh
./b2 install
#安装XZ
wget http://tukaani.org/xz/xz-5.2.2.tar.gz
tar xzvf xz-5.2.2.tar.gz
cd xz-5.2.2
./configure
make
make install
#安装zlib
wget http://zlib.net/zlib-1.2.11.tar.gz
tar xzf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
#安装bzip
wget https://fossies.org/linux/misc/bzip2-1.0.6.tar.gz
tar xzvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6/
make
make install
#安装libbz2-dev
apt-get install libbz2-dev
#安装KenLM
http://kheafield.com/code/kenlm.tar.gz
cd kenlm
mkdir build
cd build
cmake …
make
测试
python
import kenlm
cd kenlm/build/bin/
./lmplz -o 2 --verbose_header --text data/chat_log.txt --arpa result/log.arpa