Java实现正向最大匹配算法的分词算法
package com.demo; import java.io.*; import java.util.ArrayList; /** * 正向最大匹配算法的分词算法. */ public class MM { String Maxlen; ArrayList<String> dict; String s; String subs; String result = ""; public MM() { dict = new ArrayList<String>(); dict.add("山西"); dict.add("大同"); dict.add("云冈"); dict.add("校西街"); dict.add("小区"); } //最大正向匹配算法 public String MMwork(int len, String sen) { while (sen.length() != 1) { while (true) { if (len < sen.length()) { s = sen.substring(0, len); } else { len = sen.length(); s = sen.substring(0, len); } if (dict.contains(s) || len == 1) { result = result + s + "/"; break; } else { len = len - 1; } } sen = sen.substring(len); len = 3; } result = result + sen + "/"; return result; } public static void main(String[] args) { MM mm = new MM(); System.out.println(mm.MMwork(3,"山西省大同市云冈区校西街201号")); } }
参考链接:https://blog.csdn.net/yin5627/article/details/78231914
抱歉,暂停评论。