NLP中的用N-gram語言模型做英語完型填空的環境搭建



本文是對github上fork的xing_NLP中的用N-gram語言模型做完型填空這樣一個NLP項目環境搭建的一個說明,本來寫在README.md中。第一次用github中的wiki,想想嘗試一下也不錯,然而格式非常的混亂,自己都不滿意,所以先在博客園記錄一下,等github博客搭建成功了再說。

1. 操作系統:

作為programer,linux自然是首先選擇,ubuntu,centos等等都可以。我用的是CentOS7.3,之前用Centos6.5各種報錯,建議裝最新版的linux系統,何為最新版?2016年以后出的linxu系統。
相關問題,后續給出。

2. 環境搭建:

以下操作建議用root用戶進行。

2.1 anaconda(python2.7版)

這里給出清華大學開源鏡像下載鏈接:
[anacondapython2.7最新版清華鏈接](https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda2-4.4.0-Linux-x86_64.sh)
安裝方法:
bash Anaconda2-4.4.0-Linux-x86_64.sh

2.2 安裝NLTK安裝方法:

pip install NLTK
安裝完成后,要在NLTK里下載punkt這個包。
[root@xiaolyu12 ~]# ipython
Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, Dec 20 2016, 23:09:15)
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import nltk

In [2]: nltk.download()
NLTK Downloader
---------------------------------------------------------------------------
d) Download l) List u) Update c) Config h) Help q) Quit
---------------------------------------------------------------------------
Downloader> d

Download which package (l=list; x=cancel)?
Identifier> punkt
Downloading package punkt to /root/nltk_data...
Package punkt is already up-to-date!

---------------------------------------------------------------------------
d) Download l) List u) Update c) Config h) Help q) Quit
---------------------------------------------------------------------------
因為我這里安裝過,所以出現已經是最新的說明。

2.3 安裝Kenlm

這個是本文的重點,異常的復雜:
下面這個鏈接是官網關於依賴包的安裝說明,可以看懂的,照官網來,看不懂的繼續往下看:
[kenlm官網關於依賴包的安裝說明:](https://kheafield.com/code/kenlm/dependencies/)
說明:安裝kenlm之前一定要看一下gcc 的版本(gcc -v)版本一定要>=4.8.否則會報下列錯誤:
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I. -I/root/anaconda2/include/python2.7 -c util/float_to_string.cc -o build/temp.linux-x86_64-2.7/util/float_to_string.o -O3 -DNDEBUG -DKENLM_MAX_ORDER=6 -std=c++11 -DHAVE_ZLIB
cc1plus: 警告:命令行選項“-Wstrict-prototypes”對 Ada/C/ObjC 是有效的,但對 C++ 無效
cc1plus: 錯誤:無法識別的命令行選項“-std=c++11”
error: command 'gcc' failed with exit status 1

----------------------------------------
Command "/root/anaconda2/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-NDhcKC-build/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-GP5mEP-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-NDhcKC-build/`

2.3.1 依賴包的安裝:

cmake , xz, zlib , bzip2, boost (boost一定要放在最后安裝,它要依賴前面的包)
(1)cmake的安裝:因為我是Centos,這個我之前就有,采用yum安裝的,編譯安裝也可以。(官網下載tar.gz,或者tar.bz)
yum install cmake
(2)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
(3)zlib的安裝:
wget http://zlib.net/zlib-1.2.8.tar.gz
tar xzf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install`
(4) bzip2的安裝:
wget http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz
tar xzvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6/
make
make install
(5)boost的安裝:
wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.bz2
tar xjf boost_1_64_0.tar.bz2
./bootstrap.sh
./b2 install

說明不建議編譯安裝,太耗時間,大約30min+, 沒必要。
我用最簡單的方法,yum安裝:

yum install -y boost boost-devel boost-doc

ubuntu環境用下面這個命令進行安裝:

sudo apt-get install libboost-all-dev

2.3.2 kenlm的安裝:

前面的依賴包安裝成功,那么這一步就水到渠成了:
weget http://kheafield.com/code/kenlm.tar.gz
cd kenlm
mkdir build
cd build
cmake ..
make

安裝到這一步還是不行的,import kenlm 導入模塊的時候,會報錯,找不到kenlm

cd kenlm

python setup.py install 執行一下。

搞定!

上圖來看一下安裝完成后的效果:


最后:切記:要將kenlm/build/bin目錄配置的環境變量中:

在文件.bashrc中添加這樣一句話:

export PATH=/root/kenlm/build/bin:$PATH

 kenlm/build//bin下面的這些可執行文件非常重要,會在后續的博客中給出:

 

參考博客:

http://thegrandjanitor.com/2015/12/28/using-arpa-lm-with-python/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM