📄 chinesesegmenter.java
字号:
boolean result = true;
for (int i = 0; i < testword.length(); i++) {
if (Character.UnicodeBlock.of(testword.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
result = false;
break;
}
}
return result;
}
public String segmentLine(String cline, String separator) {
StringBuffer currentword = new StringBuffer();
StringBuffer outline = new StringBuffer();
StringBuffer notword = new StringBuffer();
StringBuffer english = new StringBuffer();
int i, clength, biaoji=1;
char currentchar;
notword.append("#");
clength = cline.length();
for (i = 0; i < clength; i++) {
currentchar = cline.charAt(i);
if (Character.UnicodeBlock.of(currentchar) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| isNumber(cline.substring(i, i + 1)) == true) {
if (english.length() != 0 ){
outline.append(english.toString());
outline.append(separator);
english.setLength(0);
}
if (currentword.length() == 0) { // start looking for next
// word
currentword.append(currentchar);
} else {
if (zhwords.containsKey(new String(currentword.toString()
+ currentchar).intern()) == true
&& ((String) (zhwords.get(new String(currentword
.toString()
+ currentchar).intern()))).equals("1") == true) {
// word is in lexicon
currentword.append(currentchar);
if (notword.length() !=1 ){
outline.append(notword.toString());
if (Character.isWhitespace(currentchar) == false ) {
outline.append(separator);
}
notword.setLength(0);
notword.append("#");
}
} else if (isAllForeign(currentword.toString())
&& cforeign.contains(new String(
new char[] { currentchar }).intern())
&& i + 2 < clength
&& (zhwords.containsKey(cline.substring(i, i + 2)
.intern()) == false)) {
// Possible a transliteration of a foreign name
currentword.append(currentchar);
if (notword.length() !=1 ){
outline.append(notword.toString());
if (Character.isWhitespace(currentchar) == false ) {
outline.append(separator);
}
notword.setLength(0);
notword.append("#");
}
} else if (isNumber(currentword.toString())
&& cnumbers.contains(new String(
new char[] { currentchar }).intern())
/*
* && (i + 2 < clength) &&
* (zhwords.containsKey(cline.substring(i, i+2).intern()) ==
* false)
*/) {
// Put all consecutive number characters together
currentword.append(currentchar);
} else if ((zhwords.containsKey(new String(currentword
.toString()
+ currentchar).intern()))
&& (((String) (zhwords.get(new String(currentword
.toString()
+ currentchar).intern()))).equals("2") == true)
&& i + 1 < clength
&& (zhwords.containsKey(new String(currentword
.toString()
+ currentchar + cline.charAt(i + 1))
.intern()) == true)) {
// Starts a word in the lexicon
currentword.append(currentchar);
if (notword.length() !=1 ){
outline.append(notword.toString());
if (Character.isWhitespace(currentchar) == false ) {
outline.append(separator);
}
notword.setLength(0);
notword.append("#");
}
} else { // Start anew
if (currentword.length()==1){
if (Character.isWhitespace(currentchar) == false ) {
notword.append(currentword.toString());
}
}
else
{
outline.append(currentword.toString());
if (Character.isWhitespace(currentchar) == false ) {
outline.append(separator);
}
}
currentword.setLength(0);
currentword.append(currentchar);
}
}
} else { // Not chinese character
if (currentword.length() == 1) {
outline.append("#");
}
if (currentword.length() > 0) {
outline.append(currentword.toString());
outline.append(separator);
currentword.setLength(0);
biaoji=0;
}
if (notword.length() !=1 ){
outline.append(notword.toString());
outline.append(separator);
notword.setLength(0);
notword.append("#");
biaoji=0;
}
if (!isSign(cline.substring(i, i + 1))){
english.append(currentchar);
biaoji=1;
}
else {
if ( biaoji == 1 ){
english.append(separator);
biaoji=0 ;
}
}
}
}
outline.append(currentword.toString());
return outline.toString();
}
public static void main(String[] args) throws Exception {
ChineseSegmenter seg = ChineseSegmenter.getGBSegmenter();
System.out.println(seg.segmentLine("在当今的信息化社会,面对互联网上不断增加的海量信息,人们对信息的处理需求日益增多。大家在面对网络上面五花八门的信息感到兴奋的同时,也发现了寻找到适合自己口味的东西就像在大海中捞针一样困难。解决这个难题的方法之一就是搜索引擎。人们希望借助搜索引擎以最快的速度定位到自己感兴趣的东西。然而,众多的搜索引擎也面临着如下的难题,所以很难满足用户需要:1. 网上的信息多数为自然语言,大部分的搜索引擎对自然语言的处理不到位,因而很难正确的判定用户的需求,也就很难正确的对结果进行细致的分类。2. 搜索引擎找到的信息数量多,篇幅长,很难有效的呈现给用户。如何很好解决搜索引擎的缺点,尽量满足用户的需要呢?最重要的一点就是将信息进行压缩,概括出反应主旨的几句话,这样用户就可在最短的时间内浏览尽量多的信息,并判断那些文章需要进一步的阅读。要做到这一点,搜索引擎就需要自然语言处理中的自动文摘技术,来对自然语言的各种信息进行关键信息的提取或生成。搜索引擎使用人工方式对海量的信息生成摘要是不现实的,Internet上的自动文摘系统就已经成为了紧迫的社会需求,并具有极大的科学意义和实际的应用价值。", "\n"));
FileOutputStream fos = new FileOutputStream ( "text.txt" ) ;
OutputStreamWriter osw = new OutputStreamWriter ( fos );
BufferedWriter bw = new BufferedWriter ( osw );
bw.write( seg.segmentLine("本公司及董事会全体成员保证信息披露的内容真实、准确、完整,没有虚假记载、误导性陈述或重大遗漏。一、预计的本期业绩情况1.业绩预告期间: 2006 年1 月1 日至2006 年12 月31 日2.业绩预告情况:同向大幅上升公司预计2006 年1 月1 日至2006 年12 月31 日的净利润与上年同期相比将增加50%-1003.业绩预告是否经过注册会计师预审计: 否二、上年同期业绩公司2005 年度净利润42,903,999.94 元,每股收益0.138 元。三、业绩变动原因说明公司大力发展主营业务,围绕名牌战略,努力开拓市场,狠抓经营管理,在保证产品质量的前提下,增加产量,扩大销量,由于措施有力,使公司的现代化管理水平进一步提高,激励机制更加完善科学,全体员工积极性得到充分调动,市场份额不断扩大,销售网络、销售渠道进一步完善,公司及品牌的知名度进一步提高,产品成本及各项费用降低,从而使公司保持了良好的发展势头,公司经营业绩稳定提高.", "\r\n"));
bw.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -