前言:
第一篇文章講完了Linux環境下安裝scws分詞工具php插件的前期工作,接下來我們將開始正式用phpize編譯安裝Php的SCWS插件了!!
用phpize編譯安裝Php的SCWS插件
如果上述環境都沒有任何問題,那么接下來你可以正式開始你的SCWS分詞工具安裝了:
-
- 進入scws-1.2.3安裝源碼的目錄 phpext/
cd ~/scws-1.2.3/phpext/
-
- 在/phpext/目錄中執行PHP安裝目錄下的bin目錄下的phpize
安裝命令如下(Php5.5):
# /opt/remi/php55/root/bin/phpize
Php7.0的安裝方式就用如下命令:
# /opt/remi/php70/root/bin/phpize
返回如下:
Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212 - 在/phpext/目錄中執行PHP安裝目錄下的bin目錄下的phpize
如果你是Php7.0的話用就用如下命令:
# /opt/remi/php70/root/bin/phpize
返回如下:
Configuring for:
PHP Api Version: 20151012
Zend Module Api No: 20151012
Zend Extension Api No: 320151012
- 就在上面的當前目錄執行
# ./configure --with-scws=/usr/local/scws
結果有提示如下錯誤:
configure: error: Cannot find php-config. Please use --with-php-config=PAT
看來我們需要指定文件php-config的具體位置了,改成如下的形式:
命令如下(Php5.5):
# ./configure --with-scws=/usr/local/scws--with-php-config=/opt/remi/php55/root/bin/php-config
如果你是Php7.0的話用就用如下命令:
# ./configure --with-scws=/usr/local/scws --with-php-config=/opt/remi/php70/root/bin/php-config
返回了一堆的信息,那么恭喜你,估計你應該離成功不遠了。
-
執行
make
返回如下信息:Libraries have been installed in:
/root/scsws-1.2.3/phpext/modulesIf you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the
LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to theLD_RUN_PATH' environment variable during linking - use the
-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
Build complete.
Don't forget to run 'make test'. 然后用 root 身份執行
make install
返回如下信息的話證明SCWS分詞插件安裝成功!
PHP5.5環境返回信息
Installing shared extensions: /opt/remi/php55/root/usr/lib64/php/modules/
PHP7.0環境返回信息
Installing shared extensions: /opt/remi/php70/root/usr/lib64/php/modules/
- 最后一步是在 php.ini 文件中加入以下幾行
PHP5.5的路徑
/opt/remi/php55/root/etc/php.ini
PHP7.0的路徑跟Php5.5的路徑略有不同
/etc/opt/remi/php70/php.ini
[scws]
;
; 注意請檢查 php.ini 中的 extension_dir 的設定值是否正確, 否則請將 extension_dir 設為空,
; 再把 extension = scws.so 指定絕對路徑。
;
extension = scws.so
scws.default.charset = gbk
scws.default.fpath = /usr/local/scws/etc
注:上文的
scws.default.charset = gbk
可按你的需要改為scws.default.charset = UTF-8
設定完畢保存,再重啟php服務
# systemctl restart php55-php-fpm.service
才能使新的 php.ini 生效,查看phpinfo信息如下圖:
測試分詞效果:
Linux系的服務器測試代碼test.php如下
<?php
$msg = '大家好,我的名字叫李白,很高興認識大家!';
$shfc = scws_open();
scws_set_dict($shfc, '/usr/local/scws/etc/dict.utf8.xdb');
scws_set_rule($shfc, '/usr/local/scws/etc/rules.utf8.ini');
scws_set_ignore($shfc,true);
scws_send_text($shfc, $msg);
$Amsg2 = scws_get_result($shfc);
scws_close($shfc);
foreach($Amsg2 as $value)
{
foreach($value as $key => $value2)
{
echo $value2 .',';
}
echo '<br />';
}
?>
如果是windows系的服務器的話,只需要把上面的詞庫地址的參數更改為相應的地址就行。
scws_set_dict($shfc, ini_get("scws.default.fpath").'\dict.utf8.xdb', SCWS_XDICT_XDB);
或者是
scws_set_dict($shfc,'c:\program Files\scws\dict.utf8.xdb', SCWS_XDICT_XDB);
運行結果見下圖。
至此SCWS已成功安裝完畢,接下來的使用就不在本文的討論范圍了,下面主要說一下SCWS-1.2.3的自定義詞庫,另外開篇吧!
Tag:Php分詞, SCWS, 分詞技術
發布時間:2015年10月22日
博客被黑,挪窩簡書安家……