分词java代码 java 分词库
java语言编写一个String的分词程序,功能就是计算输入英文句子的单词个数
import java.io.BufferedReader;
创新互联建站长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为宁波企业提供专业的网站建设、做网站,宁波网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
import java.io.IOException;
import java.io.InputStreamReader;
public class Danci {
public static void main(String[] args){
String str = new String();
System.out.print("请输入一个英文句子:");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//获取键盘输入
str = br.readLine();
}catch(IOException e){
e.printStackTrace();
}
String []s = str.split(" ");//转换成数组
System.out.println("你输入的句子共有单词 "+s.length+" 个");//s.length获取数组长度
}
}
//此程序只能获取一句话的单词个数.
java编个中文分词的程序
import java.io.Reader;
import java.io.StringReader;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis点吸烟 .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
怎样通过java代码实现分词后的text文本生成weka可以处理的arff文件?
先把文件读取出来后放到Instances中,在吧Instances保存成arff文件。保存arff文件代码如下:
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File("c:\test.arff"));
saver.writeBatch();
在线等,比较急!!!我用java版的结巴分词写了一段代码,怎样将它输出到指定的txt文件中?
String str = segmenter.sentenceProcess(s);
System.out.println(str);
BufferedWriter out = new BufferedWriter(new FileWriter("F:\\out.txt"));
out.append(str);
网站栏目:分词java代码 java 分词库
网站链接:http://cdiso.cn/article/ddjpdoe.html