轉自:編譯哈工大語言技術平台雲LTP(C++)源碼及LTP4J(Java)源碼
JDK:java version “1.8.0_31”
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
OS:win7 64bit
cmake:V2.8.12/V3.2.2
LTP:V3.2.0
LTP4J:V1.0
Microsoft VS C++:V2010
編譯LTP4J.jar
下載Ant
ltp4j的源碼使用ant進行編譯,首先需要下載ant,點我下載
配置Ant環境變量
我的電腦—>屬性—>高級系統設置—>環境變量
新建ANT_HOME值為:“D:\apache-ant-1.9.4”
編輯PATH,在PATH變量值的末尾添加:“;%ANT_HOME%\bin”
驗證是否配置成功,打開cmd,輸入ant:
C:\Users\TKPad>ant
Buildfile: build.xml does not exist!
Build failed
這樣的話,說明配置成功,因為ant默認運行build.xml文件,該文件需要手動創建。
編譯LTP4J
首先需要下載ltp4j源代碼,下載完成之后,解壓,比如我的地址為:D:\ltp4j-master(推薦重命名為ltp4j),打開cmd,進入d盤ltp4j項目根目錄下,運行ant命令,得到:
D:\ltp4j>ant
Buildfile: D:\ltp4j\build.xml
clean:
[delete] Deleting directory D:\ltp4j\output
compile:
[mkdir] Created dir: D:\ltp4j\output\classes
[javac] D:\ltp4j\build.xml:17: warning: ‘includeantruntime’ was not set, def
aulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 7 source files to D:\ltp4j\output\classes
jar:
[mkdir] Created dir: D:\ltp4j\output\jar
[jar] Building jar: D:\ltp4j\output\jar\ltp4j.jar
main:
BUILD SUCCESSFUL
Total time: 4 seconds
說明編譯成功!查看目錄:D:\ltp4j\output\jar,發現存在ltp4j.jar文件,這時該Jar包就可以使用了。
編譯C++代理程序
安裝CMake
ltp4j使用的C++代理程序使用編譯工具CMake構建項目。 在編譯代理程序之前,你需要首先安裝CMake。 CMake的網站在這里。如果你是Windows用戶,請下載CMake的二進制安裝包;如果你是Linux,Mac OS或Cygwin的用戶,可以通過編譯源碼的方式安裝CMake,當然,你也可以使用Linux的軟件源來安裝。
下載地址點這里,我選擇的是Windows ZIP cmake-3.2.2-win32-x86.zip
下載LTP的C++源碼
下載地址,下載完成之后解壓,我的存放路徑為:D:\ltp-master(推薦重命名為ltp)。
配置LTP的安裝路徑
修改D:\ltp4j目錄下的CMakeLists.txt文件,修改如下部分的斜體加粗部分為你自己的LTP源碼路徑
find_package(JNI)
set (LTP_HOME “D:/ltp” CACHE STRING “Use to specified ltp path”)
set (LIBRARY_OUTPUT_PATH \${PROJECT_SOURCE_DIR}/libs)
set (JNI_SOURCE_DIR \${PROJECT_SOURCE_DIR}/jni)
構建VC Project
在項目文件夾下新建一個名為build的文件夾,使用CMake Gui,在source code中填入項目文件夾,在binaries中填入build文件夾。然后Configure —> Generate。
在點擊configure之后出現如下錯誤:
The C compiler identification is unknown The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project): No CMAKE_C_COMPILER could be found.
CMake Error at CMakeLists.txt:2 (project): No CMAKE_CXX_COMPILER could be found.Configuring incomplete, errors occurred!
See also “D:/ltp4j/build/CMakeFiles/CMakeOutput.log”.
See also “D:/ltp4j/build/CMakeFiles/CMakeError.log”.
那么可能是你的電腦上還沒有C++編譯環境,需要安裝Microsoft Visual Studio C++,VS 2010 express版下載地址為:點我下載VS2010
安裝完成之后,再次編譯,如果出現:
The C compiler “C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/bin/cl.exe” is not able to compile a simple test program.
那么參考文章里面的解決辦法,說簡單點就是以管理員權限運行c1.exe
解決辦法如下:
- Right-Click->Properties on cl.exe in your VS install directory (the exact path appears in the CMake error);
- Choose the Compatibility Tab;
- Check “Run this program as administrator” in the “Privilege Level” box.
之后再次嘗試編譯,出現如下錯誤:
CMake Error at CMakeLists.txt:2 (project):
Cannot enable 64-bit tools with Visual Studio 2010 Express.
Install the Microsoft Windows SDK v7.1 to get 64-bit tools:
http://msdn.microsoft.com/en-us/windows/bb980924.aspx
Configuring incomplete, errors occurred!
主要是因為32位版本不兼容造成的,打開地址,點擊download下載Microsoft Windows SDK for Windows 7 and .NET Framework 4,下載完成之后運行即是在線安裝的形式。如果以cmd命令行的形式運行cmake,需要將cmake的bin目錄路徑加入到操作系統的path變量中。
在configure階段,再次出錯
The C compiler identification is MSVC 16.0.30319.1
The CXX compiler identification is MSVC 16.0.30319.1
Check for working C compiler using: Visual Studio 10
Check for working C compiler using: Visual Studio 10 — broken
CMake Error at D:/cmake-2.8.12.1-win32-x86/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler “C:/Program Files (x86)/Microsoft Visual Studio
10.0/VC/bin/cl.exe” is not able to compile a simple test program.
It fails with the following output:
Change Dir: D:/ltp4j/build/CMakeFiles/CMakeTmp
Run Build Command:C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
cmTryCompileExec1255268599.vcxproj /p:Configuration=Debug
/p:VisualStudioVersion=10.0
Microsoft(R) 生成引擎版本 4.0.30319.17929
[Microsoft .NET Framework 版本 4.0.30319.18063]
版權所有(C) Microsoft Corporation 2007。保留所有權利。
生成啟動時間為 2015/5/6 9:46:05。
節點 1
上的項目“D:\ltp4j\build\CMakeFiles\CMakeTmp\cmTryCompileExec1255268599.vcxproj”(默認目標)。
PrepareForBuild:
正在創建目錄“cmTryCompileExec1255268599.dir\Debug\”。
正在創建目錄“D:\ltp4j\build\CMakeFiles\CMakeTmp\Debug\”。
InitializeBuildStatus: 正在創建“cmTryCompileExec1255268599.dir\Debug\cmTryCompileExec1255268599.unsuccessfulbuild”,因為已指定“AlwaysCreate”。
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\CL.exe /c /Zi /W3 /WX- /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D “CMAKE_INTDIR=\”Debug\”” /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo”cmTryCompileExec1255268599.dir\Debug\“ /Fd”cmTryCompileExec1255268599.dir\Debug\vc100.pdb” /Gd /TC /analyze- /errorReport:queue testCCompiler.c
用於 80x86 的 Microsoft (R) 32 位 C/C++ 優化編譯器 16.00.30319.01 版
版權所有(C) Microsoft Corporation。保留所有權利。
cl /c /Zi /W3 /WX- /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D “CMAKE_INTDIR=\”Debug\”” /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo”cmTryCompileExec1255268599.dir\Debug\“ /Fd”cmTryCompileExec1255268599.dir\Debug\vc100.pdb” /Gd /TC /analyze- /errorReport:queue testCCompiler.c
testCCompiler.c
ManifestResourceCompile:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\rc.exe /nologo /fo”cmTryCompileExec1255268599.dir\Debug\cmTryCompileExec1255268599.exe.embed.manifest.res” cmTryCompileExec1255268599.dir\Debug\cmTryCompileExec1255268599_manifest.rc
Link:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:QUEUE /OUT:”D:\ltp4j\build\CMakeFiles\CMakeTmp\Debug\cmTryCompileExec1255268599.exe” /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /ManifestFile:”cmTryCompileExec1255268599.dir\Debug\cmTryCompileExec1255268599.exe.intermediate.manifest” /MANIFESTUAC:”level=’asInvoker’ uiAccess=’false’” /DEBUG /PDB:”D:/ltp4j/build/CMakeFiles/CMakeTmp/Debug/cmTryCompileExec1255268599.pdb” /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:”D:/ltp4j/build/CMakeFiles/CMakeTmp/Debug/cmTryCompileExec1255268599.lib” /MACHINE:X86 cmTryCompileExec1255268599.dir\Debug\cmTryCompileExec1255268599.exe.embed.manifest.res
cmTryCompileExec1255268599.dir\Debug\testCCompiler.obj /machine:X86 /debug
LINK : fatal error LNK1123: 轉換到 COFF 期間失敗: 文件無效或損壞
[D:\ltp4j\build\CMakeFiles\CMakeTmp\cmTryCompileExec1255268599.vcxproj]
已完成生成項目“D:\ltp4j\build\CMakeFiles\CMakeTmp\cmTryCompileExec1255268599.vcxproj”(默認目標)的操作
失敗。
生成失敗。
“D:\ltp4j\build\CMakeFiles\CMakeTmp\cmTryCompileExec1255268599.vcxproj”(默認目標)
(1) ->
(Link 目標) ->
LINK : fatal error LNK1123: 轉換到 COFF 期間失敗: 文件無效或損壞 [D:\ltp4j\build\CMakeFiles\CMakeTmp\cmTryCompileExec1255268599.vcxproj]
0 個警告
1 個錯誤
已用時間 00:00:00.81
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
Configuring incomplete, errors occurred!
See also “D:/ltp4j/build/CMakeFiles/CMakeOutput.log”.
See also “D:/ltp4j/build/CMakeFiles/CMakeError.log”.
該問題可能是由於.NET Framework 4.5導致,解決辦法是:
CMake doesn’t seem to work if .net 4.5 is installed. If you run into this issue you have to uninstall .net 4.5. Uninstalling .net 4.5 seems to break Visual Studio so then you have to re-install VS2010.
簡單點說就是,卸載.net 4.5,安裝.net 4.0(如果電腦已經安裝,就不必裝了),在卸載.net 4.5會破壞VS2010,所以需要重新再安裝一遍VS2010。參考文章1、參考文章2;安裝完畢之后,重新以管理員身份的權限運行cmake-gui.exe,點擊configure之后,會彈出對話框,如果是VC++2010的話,建議選擇Visual Studio 10,如下圖:cmake-configure
點擊finish->configure,此時進度條開始變為綠色滾動。
The C compiler identification is MSVC 16.0.30319.1
The CXX compiler identification is MSVC 16.0.30319.1
Check for working C compiler using: Visual Studio 10
Check for working C compiler using: Visual Studio 10 — works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working CXX compiler using: Visual Studio 10
Check for working CXX compiler using: Visual Studio 10 — works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Found JNI: D:/Program Files/Java/jdk1.8.0_31/lib/jawt.lib
Configuring done
configure結束之后點擊generate。
如果您依然沒有運行成功,那么建議使用cmake3.x之前的版本,原因是:
Visual Studio 11 Win64 is the wrong generator for Visual Studio 2013. You need to use Visual Studio 12 2013 Win64 if you are using cmake-3.x or Visual Studio 12 Win64 for cmake-2.8.x.
cmake3.x之前的版本下載地址:http://www.cmake.org/files/v2.8/
編譯源碼
構建后得到ALL_BUILD、RUN_TESTS、ZERO_CHECK三個VC Project。使用VS打開ALL_BUILD項目,選擇Release(*)方式構建項目。
(注:boost::multi_array與VS2010不兼容的bug已經在3.1.0中得到修復,3.1.x及以上版本已經可以使用Debug方式構建,但出於效率考慮,仍舊建議使用Release方式構建。)
注意事項
- 該處編譯需要設置Java環境變量JAVA_HOME。
- 需要保持c++編譯器與JDK同是32位或者64位,否則JVM不能加載生成的動態庫
編譯源碼出現
LINK : fatal error LNK1104: cannot open file ‘kernel32.lib’
原因是
Check the VC++ directories, in VS 2010 these can be found in your project properties. Check whether “\$(WindowsSdkDir)\lib” is included in the directories list, if not, manually add it. If you’re building for X64 platform, you should select X64 from the “Platform” ComboBox, and make sure that “$(WindowsSdkDir)\lib\x64” is included in the directories list.
如果X64目錄不包含在庫目錄中,需要手動添加到庫目錄中。如下圖所示添加x64庫
編譯LTP
再次編譯LTP4J出現如下錯誤:
LINK : fatal error LNK1181: cannot open input file ‘postagger.lib’
LINK : fatal error LNK1181: cannot open input file ‘ner.lib’
這是因為在編譯LTP4J之前需要編譯LTP,使用cmake構建LTP,如下圖LTP-x64
之后在LTP的build目錄下會生成VS Project,用VS打開D:\ltp\build\ALL_BUILD.vcxproj,使用Release X64方式進行編譯,如果順利的話,編譯結果見下圖ltp編譯成功
再次編譯LTP4J
在LTP編譯成功之后,將D:\ltp\lib\Release下的所有文件拷貝到D:\ltp4j\build目錄下,然后在VS中打開D:\ltp4j\build\ALL_BUILD.vcxproj,選擇Release X64進行編譯,編譯結果如下:
1>——— 已啟動生成: 項目: ZERO_CHECK, 配置: Release x64 ———
2>——— 已啟動生成: 項目: split_sentence_jni, 配置: Release x64 ———
3>——— 已啟動生成: 項目: segmentor_jni, 配置: Release x64 ———
4>——— 已啟動生成: 項目: srl_jni, 配置: Release x64 ———
5>——— 已啟動生成: 項目: parser_jni, 配置: Release x64 ———
2> Creating library D:/ltp4j/libs/Release/split_sentence_jni.lib and object D:/ltp4j/libs/Release/split_sentence_jni.exp
3> Creating library D:/ltp4j/libs/Release/segmentor_jni.lib and object D:/ltp4j/libs/Release/segmentor_jni.exp
3> segmentor_jni.vcxproj -> D:\ltp4j\libs\Release\segmentor_jni.dll
4> Creating library D:/ltp4j/libs/Release/srl_jni.lib and object D:/ltp4j/libs/Release/srl_jni.exp
5> Creating library D:/ltp4j/libs/Release/parser_jni.lib and object D:/ltp4j/libs/Release/parser_jni.exp
2> split_sentence_jni.vcxproj -> D:\ltp4j\libs\Release\split_sentence_jni.dll
5> parser_jni.vcxproj -> D:\ltp4j\libs\Release\parser_jni.dll
4> srl_jni.vcxproj -> D:\ltp4j\libs\Release\srl_jni.dll
6>——— 已啟動生成: 項目: ner_jni, 配置: Release x64 ———
7>——— 已啟動生成: 項目: postagger_jni, 配置: Release x64 ———
6> Creating library D:/ltp4j/libs/Release/ner_jni.lib and object D:/ltp4j/libs/Release/ner_jni.exp
7> Creating library D:/ltp4j/libs/Release/postagger_jni.lib and object D:/ltp4j/libs/Release/postagger_jni.exp
6> ner_jni.vcxproj -> D:\ltp4j\libs\Release\ner_jni.dll
7> postagger_jni.vcxproj -> D:\ltp4j\libs\Release\postagger_jni.dll
8>——— 已跳過生成: 項目: ALL_BUILD, 配置: Release x64 ———
8>沒有為此解決方案配置選中要生成的項目
========== 生成: 成功 7 個,失敗 0 個,最新 0 個,跳過 1 個 ==========
在Eclipse中使用離線版LTP
配置Eclipse中項目所需資源
在Eclipse中新建Java Project
- 導入ltp4j.jar
- windows下將ltp4j的libs文件夾中生成的所有動態庫、以及原ltp lib文件夾下的splitsnt、segmentor、postagger、ner、parser、srl 6個動態庫拷貝到項目根目錄
- linux下export LD_LIBRARY_PATH=#jni動態庫路徑#
配置完成之后,項目視圖如下所示:項目結構圖
本地接口使用示例
分詞接口
1 |
import java.util.ArrayList; |
詞性標注接口
1 |
import java.util.ArrayList; |
命名實體識別接口
1 |
import java.util.ArrayList; |
依存句法分析接口
1 |
import java.util.ArrayList; |
語義角色標注接口
1 |
import java.util.ArrayList; |
等等,最后一步
注意點
在ltp4j中提供的ltp_data中的model僅僅是作為你測試代碼的正確性,其並非完整的model,如果需要在生產環境中使用,需要到這里下載完整版的model,將下載的完整版ltp_data拷貝到項目中即可。
其他錯誤
Cannot open include file: ‘ammintrin.h’: No such file or directory
https://connect.microsoft.com/VisualStudio/feedback/details/660584/
參考資料:
【1】哈工大LTP官網
【2】LTP4J的github地址
【3】LTP的github地址
【4】LTP3.0參考文檔
【5】LTP4J1.0參考文檔
【6】VS2010與64位系統
【7】安裝 Microsoft Visual C++ 2010 和 Microsoft Windows SDK 7.1
【8】各種標簽詳解