可以下载一个词库这里用的是搜狗的词库
Segmentation.java
package cn.strong;
import java.util.HashMap;
public class Segmentation {
private String tem = null;
private HashMap mapDic, len;
public Segmentation(HashMap mapDic, HashMap len) {
this.mapDic = mapDic;
this.len = len;
}
//source是输入的中文
//正向最大匹配
public String Fmm(String source) {
String[] targets = new String[source.length()];
String target = "";
//资源的最大长度
int MaxLen = source.length();
//将最大的长度赋给temLen变量
int temLen = MaxLen;
int primarylen = 0;
while (true) {
//判断temLen有没有对应的value值 有的返回true否则返回false
if (len.containsKey(temLen)) {
tem = source.substring(primarylen, temLen);
//判断词库里面有没有tem对应的值 或者只剩下一个单字的时候
if (mapDic.containsKey(tem) || temLen - primarylen == 1) {
primarylen = temLen;
temLen = MaxLen;
if (primarylen == MaxLen)
target = target + tem;
else
target = target + tem + "/";
} else
temLen--;
} else
temLen--;
if (primarylen == MaxLen)
break;
}
return target;
}
//反相最大匹配
public String Bmm(String source) {
String[] targets = new String[source.length()];
String target="";
int MaxLen = source.length();
int temLen = MaxLen;
int primarylen = 0;
int i=0;
while (true) {
if (len.containsKey(temLen)) {
tem = source.substring(primarylen, temLen);
if (mapDic.containsKey(tem)||temLen-primarylen==1) {
if (temLen == MaxLen){
targets[i] = tem;
}
else{
tem = tem+"/";
targets[i] = tem;
}
temLen = primarylen;
primarylen = 0;
i++;
} else
primarylen++;
} else
primarylen++;
if (temLen == 0)
break;
}
for(int j=i-1;j>=0;j--)
target+=targets[j];
return target;
}
}
GenerateDictionary.java
package cn.strong;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
public class GenerateDictionary {
public void GenHashDic(String filename, HashMap hm, HashMap len) throws FileNotFoundException, IOException {
String s = new String();
BufferedReader in = new BufferedReader(new FileReader(filename));
//在此处会循环词库的词数个数次
while ((s = in.readLine()) != null) {
hm.put(s, s.length());
len.put(s.length(), s);
}
}
}
Test.java测试
package cn.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import cn.strong.GenerateDictionary;
import cn.strong.Segmentation;
public class Test {
public static void main(String[] args) throws IOException {
String filename = "sougou.txt";
HashMap hm = new HashMap();
HashMap len = new HashMap();
GenerateDictionary genDic = new GenerateDictionary();
Segmentation seg;
long genStart = System.currentTimeMillis();
genDic.GenHashDic(filename, hm, len);
System.out.println("GenHashDic 消耗时间:"+(System.currentTimeMillis() - genStart));
System.out.println("请输入您需要分解的语句:");
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
String data = "";
data = br.readLine();
seg = new Segmentation(hm, len);
long emmStart = System.currentTimeMillis();
String FmmTarget = seg.Fmm(data);
System.out.println("FMM 算法共花费时间为:"+(System.currentTimeMillis()-emmStart));
long bmmStart = System.currentTimeMillis();
String BmmTarget = seg.Bmm(data);
System.out.println("BMM 算法共花费时间为:"+(System.currentTimeMillis()-bmmStart));
System.out.println("FMM算法统计结果为: " + FmmTarget);
System.out.println("BMM算法统计结果为: " + BmmTarget);
}
}
GenHashDic 消耗时间:1337
请输入您需要分解的语句:
中国四川省成都市青羊区少城路9号人民公园
FMM 算法共花费时间为:0
BMM 算法共花费时间为:0
FMM算法统计结果为: 中国/四川省/成都市/青羊区/少/城/路/9/号/人民/公园
BMM算法统计结果为: 中国/四川省/成都市/青羊区/少/城/路/9/号/人民/公园
相关推荐
Java实现分词(正向最大匹配和逆向最大匹配)两种方法实现
运用正向最大匹配算法进行分析,同时也实现了逆向最大匹配,内有分词词典。
MM算法有三种:一种正向最大匹配,一种逆向最大匹配和双向匹配。本程序实现了正向最大匹配算法。 本程序还可以从我的github上面下载:https://github.com/Zehua-Zeng/Maximum-Matching-Algorithm
中文分词一直都是中文自然语言处理领域的基础研究。目前,网络上流行的很多中文分词软件都可以在付出较少的代价的同时...MM算法有三种:一种正向最大匹配,一种逆向最大匹配和双向匹配。本程序实现了正向最大匹配算法。
里面包含完整代码,有词典,解压后是vs2017的工程文件,可直接运用测试。
使用正向最大匹配FMM分词 以及逆向最大匹配BMM分词 但不是同时使用
MM算法有三种:一种正向最大匹配,一种逆向最大匹配和双向匹配。本程序实现了反向最大匹配算法。 本程序还可以从我的github上面下载:https://github.com/Zehua-Zeng/Reverse-Maximum-Matching-Algorithm
本程序是北京师范大学学生根据一个中文字库对所给的文章进行分词。...采用的算法是正向最大匹配算法和反向最大匹配算法。主要实现屏幕分词和文件分词两项功能。因为对毕业设计有所帮助,所以我要分高一点哈~勿怪偶~
word分词是一个Java实现的中文分词组件,提供了多种基于词典的分词算法,并利用ngram模型来消除歧义。 能准确识别英文、数字,以及日期、时间等数量词,能识别人名、地名、组织机构名等未登录词。 同时提供了Lucene...
逆向最大匹配分词是中文分词基本算法之一,因为是机械切分,所以它也有分词速度快的优点,且逆向最大匹配分词比起正向最大匹配分词更符合人们的语言习惯。逆向最大匹配分词需要在已有词典的基础上,从被处理文档的...
大家都知道,英文的分词由于单词间是以空格进行分隔的,所以分词要相对的容易些,而中文就不同了,中文中一个句子的分隔就是以字为单位的了,而所谓的正向最大匹配和逆向最大匹配便是一种分词匹配的方法,这里以词典...
NULL 博文链接:https://zy3381.iteye.com/blog/1937919
algorithmMenu.add(bmmItem = new JRadioButtonMenuItem("逆向最大匹配", false)); ButtonGroup algorithms = new ButtonGroup(); algorithms.add(fmmItem); algorithms.add(bmmItem); ...
大家都知道,英文的分词由于单词间是以空格进行分隔的,所以分词要相对的容易些,而中文就不同了,中文中一个句子的分隔就是以字为单位的了,而所谓的正向最大匹配和逆向最大匹配便是一种分词匹配的方法,这里以词典...
(2)在中文中,由于偏正结构较多,所以从后向前进行匹配会提高精确度,因此,逆向最大匹配算法比正向最大匹配算法的误差要小。统计结果表明,单纯使用正向最大匹配的错误率为 1/169,单纯使用逆向最大匹配的错误率...
正向最大匹配 # -*- coding:utf-8 -*-\nCODEC='utf-8' def u(s, encoding): 'converted other encoding to unicode encoding' if isinstance(s, unicode): return s else: return unicode(s, encoding) def ...
实现中文信息处理中的汉语分词的功能,采用了正向最大和逆向最大的分词算法。