2.从nlpir官网下载相关资源,这里下载的是2013版本。下载地址:http://ictclas.nlpir.org/upload/20140324095815_ICTCLAS2014.rar
3.将下载文件解压缩,找到其中的Data文件夹、bin文件夹中的NLPIR.dll文件、include文件夹中的NLPIR.h文件、lib文件夹
中的NLPIR.lib文件,将这四个东西复制到刚刚的text-segmentation文件夹中的text-segmentation文件夹下,如下
效果:
4.点击text-segmentation文件夹下的text-segmentation.sln,在vs中打开这个项目。按照如下方式将NLPIR.h文件加入到头液李文件中。
5.点击project ->properties(快捷键ALT +
F7),按照下图所示的进行设置。别问我为什么这么设置,我也不懂,摸索了半天才摸索出来的,不这么圆埋宴设置就是搞不定。
6.在source里面新建程序新建c++源程序
7.接下来就可以在这里写代码了,下面附上一段我的代码作为例子。最关键的是前面的函数头部分,可以直接抄过去。
#ifndef OS_LINUX
#include
#pragma comment(lib, "NLPIR.lib")
#endif
#include
#include
#include
#include
#include
#include "NLPIR.h"
#include
using namespace std
void IfInit()//判断是否已经成功初始化
void IfExit()//是否成功退出
int main()
{
IfInit()//初始化程序
NLPIR_SetPOSmap(ICT_POS_MAP_SECOND)
int nitems//
nitems=NLPIR_ImportUserDict("H:\\sentence\\UserDict_manual.txt")
NLPIR_SaveTheUsrDic()
cout<<"import "<<nitems<<"
words"<<endl
//NLPIR_AddUserWord("A股")
//NLPIR_AddUserWord("沪指")
//int i
//char s[100]
//char s2[100]
//ifstream infile
////infile.open("H:\\sentence\\files to break
word.txt")
//infile.open("C:\\Users\\pc\\Desktop\\temp\\file_to.txt")
//ifstream infile2
////infile2.open("H:\\sentence\\files after break
word.txt")
//infile2.open("C:\\Users\\pc\\Desktop\\temp\\file_after.txt")
//for(i=0i<717696i++)//1798
//橘银{
// if (i<350000)
// {
// infile.getline(s,100)
// infile2.getline(s2,100)
// continue
// }
// infile.getline(s,100)
// infile2.getline(s2,100)
// NLPIR_FileProcess(s,s2,0)
// cout<<i+1<<endl
//}
NLPIR_FileProcess("try.txt","try_result.txt",0)
IfExit()//退出
system("pause")
return 0
}
void IfInit()//判断是否已经成功初始化
{
if(!NLPIR_Init(0,UTF8_CODE))
{
cout<<"Init fails"<<endl
exit(EXIT_FAILURE)
}
else
{
cout<<"Init ok"<<endl
}
}
void IfExit()//是否成功退出
{
if(!NLPIR_Exit())
{
cout<<"Exit fails"<<endl
}
else
{
cout<<"Exit ok"<<endl
}
}
8.若函数报错,可以去text-segmentation文件夹中的text-segmentation文件夹下查看log文件。常见的错误是Initial
Fail,这很可能是因为license过期了,解决方法可见http://blog.csdn.net/heyu158/article/details/17525753
。
import java.io.Readerimport java.io.StringReader
import org.apache.lucene.analysis.*
import org.apache.lucene.analysis.cjk.CJKAnalyzer
import org.apache.lucene.analysis.cn.ChineseAnalyzer
import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.mira.lucene.analysis.MIK_CAnalyzer
public class JeAnalyzer {
public static void testStandard(String testString) {
try {
Analyzer analyzer = new StandardAnalyzer()
Reader r = new StringReader(testString)
StopFilter sf = (StopFilter) analyzer.tokenStream("", r)
System.err.println("=====standard analyzer====")
Token t
while ((t = sf.next()) != null) {
System.out.println(t.termText())
}
} catch (Exception e) {
e.printStackTrace()
}
}
public static void testCJK(String testString) {
try {
Analyzer analyzer = new CJKAnalyzer()
旅和 Reader r = new StringReader(testString)
StopFilter sf = (StopFilter) analyzer.tokenStream("", r)
System.err.println("=====cjk analyzer====")
Token t
while ((t = sf.next()) != null) {
System.out.println(t.termText())
拆腊盯 }
} catch (Exception e) {
e.printStackTrace()
}
}
public static void testChiniese(String testString) {
try {
Analyzer analyzer = new ChineseAnalyzer()
Reader r = new StringReader(testString)
TokenFilter tf = (TokenFilter) analyzer.tokenStream("", r)
System.err.println("=====chinese analyzer====")
Token t
while ((t = tf.next()) != null) {
System.out.println(t.termText())
}
} catch (Exception e) {
e.printStackTrace()
局仔 }
}
public static String transJe(String testString, String c1, String c2) {
String result = ""
try {
Analyzer analyzer = new MIK_CAnalyzer()
Reader r = new StringReader(testString)
TokenStream ts = (TokenStream) analyzer.tokenStream("", r)
Token t
while ((t = ts.next()) != null) {
result += t.termText() + ","
}
} catch (Exception e) {
e.printStackTrace()
}
return result
}
public static void main(String[] args) {
try {
String testString = "中文分词的方法其实不局限于中文应用,也被应用到英文处理,如手写识别,单词之间的空格就很清楚,中文分词方法可以帮助判别英文单词的边界"
System.out.println("测试的语句 "+testString)
String sResult[] = transJe(testString, "gb2312", "utf-8").split(",")
for (int i = 0 i < sResult.length i++) {
System.out.println(sResult[i])
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
jar包
lucene-analyzers-2.4.1.jar
lucene-core-2.4.1.jar
IKAnalyzer2.0.2OBF.jar
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)