重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.io.Reader;
成都创新互联公司主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、全网整合营销推广、网站程序开发、HTML5响应式成都网站建设、移动网站建设、微商城、网站托管及网页维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为成都护栏打桩机行业客户提供了网站推广服务。
import 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
你的dic.dat应该是要存在于eclipse中这个工程的debug的目录下,虽然你放在了代码的当前目录下,但是编译的时候并没有把这个文件复制过去
如果你的分词规则是在一个字符串的开头和结尾加上"_",然后两个字符一分的话,代码可以这样写:
import java.util.ArrayList;
import java.util.List;
public class Participle
{
private static final String HEAD_END_STR = "_";
private static final int PARTICIPLE_LENGTH = 2;
public static void main(String[] args)
{
String exampleWord = "计算机";
exampleWord = "_" + exampleWord + "_";
int length = exampleWord.length();
ListString result = new ArrayListString();
for (int i = 0; i length - 1; i++)
{
String str = exampleWord.substring(i, i + PARTICIPLE_LENGTH);
result.add(str);
}
System.out.println(result);
}
}
输出结果:_计, 计算, 算机, 机_