接着是正向最大匹配的实现类MaxMatchSegmentProcessor:
最后是反向最大匹配的实现类ReverseMaxMatchSegmentProcessor:
java 代码
- /*
- * @作者:Hades , 创建日期:2006-11-17
- *
- * 汕头大学03计算机本科
- *
- */
- package edu.stu.cn.segment.matching.processor;
- import java.util.LinkedList;
- import java.util.StringTokenizer;
- import edu.stu.cn.segment.matching.dictionary.DictionaryImpl;
- /**
- * @author Hades Guan 正向最大匹配分词法操作类
- */
- public class MaxMatchSegmentProcessor extends MatchSegmentProcessor
- {
- /**
- * 字符串分隔器
- */
- private StringTokenizer tokenizer = null;
- /**
- * 默认构造函数
- */
- public MaxMatchSegmentProcessor()
- {
- this.initSeperator();
- }
- /**
- * 以一个词典操作类实例为参数的构造函数
- *
- * @param dic
- * 词典操作类实例
- */
- public MaxMatchSegmentProcessor(DictionaryImpl dic)
- {
- this.dic = dic;
- this.initSeperator();
- }
- /**
- * 对text文本进行分词,把结果保存为字符串链表
- *
- * @param text
- * 待分词的文本
- * @return 分词结果
- */
- public LinkedList<string> textProcess(String text) </string>
- {
- if (text == null)
- return null;
- // 初始化结果链表
- LinkedList<string> result = new LinkedList<string>(); </string></string>
- // 对待分词文本进行分隔
- this.tokenizer = new StringTokenizer(text, this.seperator);
- while (tokenizer.hasMoreTokens())
- {
- String token = tokenizer.nextToken();
- // 判断分隔文本是否为null
- if (token == null)
- continue;
- // 初始化位置标签
- int pos = 0;
- // 当前分隔文本长度
- int len = token.length();
- // 结尾位置
- int end = len;
- // 循环匹配
- while (pos < len)
- {
- while (end > pos)
- {
- // 判断end处字符是否为数字或英文字母
- if (end > 0
- && CHAR_AND_NUM.indexOf(token.charAt(end - 1)) >= 0)
- {
- // 记录英语字母开始位置、英语字母结束位置
- int englishEnd = end, englishStart = end;
- while (englishStart > 0
- && CHAR_AND_NUM.indexOf(token
- .charAt(englishStart - 1)) >= 0)
- englishStart--;
- // 判断当位置标签指向当前英文串首地址时将结果插入分词结果集
- if (pos == englishStart)
- {
- result.add(token
- .substring(englishStart, englishEnd));
- pos = end;
- end = len;
- }
- }
- // end of 判断end处字符是否为数字或英文字母
- // 判断分段是否已分析完毕
- if (end > pos)
- {
- // 汉字处理
- String word = token.substring(pos, end);
- if (dic.match(word))
- {
- result.add(word);
- pos = end;
- end = len;
- }
- else
- {
- // 当判断到剩余单字时,将词加入到词库中
- if (word.length() == 1)
- {
- result.add(word);
- pos = end;
- end = len;
- }
- else
- end--;
- }
- // end of match
- }
- // end of if(end>pos)
- }
- // end of while (end > pos)
- }
- // end of while (pos < len)
- }
- // end of while (tokenizer.hasMoreTokens())
- return result;
- }
- }
最后是反向最大匹配的实现类ReverseMaxMatchSegmentProcessor:
java 代码
- /*
- * @作者:Hades , 创建日期:2006-11-17
- *
- * 汕头大学03计算机本科
- *
- */
- package edu.stu.cn.segment.matching.processor;
- import java.util.LinkedList;
- import java.util.StringTokenizer;
- import edu.stu.cn.segment.matching.dictionary.DictionaryImpl;
- /**
- * @author Hades Guan 反向最大匹配分词法操作类
- */
- public class ReverseMaxMatchSegmentProcessor extends MatchSegmentProcessor
- {
- /**
- * 字符串分隔器
- */
- private StringTokenizer tokenizer = null;
- /**
- * 默认构造函数
- */
- public ReverseMaxMatchSegmentProcessor()
- {
- this.initSeperator();
- }
- /**
- * 以一个词典操作类实例为参数的构造函数
- *
- * @param dic
- * 词典操作类实例
- */
- public ReverseMaxMatchSegmentProcessor(DictionaryImpl dic)
- {
- this.dic = dic;
- this.initSeperator();
- }
- /**
- * 对text文本进行分词,把结果保存为字符串链表
- *
- * @param text
- * 待分词的文本
- * @return 分词结果
- */
- public LinkedList<string> textProcess(String text) </string>
- {
- if (text == null)
- return null;
- // 初始化分词结果集
- LinkedList<string> result = new LinkedList<string>(); </string></string>
- // 初始化分隔器
- this.tokenizer = new StringTokenizer(text, this.seperator);
- // 分隔文本
- while (tokenizer.hasMoreTokens())
- {
- // 获取分隔文本
- String token = tokenizer.nextToken();
- // 如果分隔文本为空则开始下一个循环
- if (token == null)
- continue;
- // 初始化变量
- int len = token.length(), start = 0, pos = len;
- int wordCount = 0;
- // 循环匹配
- while (pos > 0)
- {
- while (start < pos)
- {
- // 判断start处字符是否为数字或英文字母
- if (start < len
- && CHAR_AND_NUM.indexOf(token.charAt(start)) >= 0)
- {
- // 记录英语字母开始位置、英语字母结束位置
- int englishEnd = start, englishStart = start;
- while (englishEnd < len
- && CHAR_AND_NUM.indexOf(token
- .charAt(englishEnd)) >= 0)
- englishEnd++;
- // 判断当位置标签指向当前英文串首地址时将结果插入分词结果集
- if (englishEnd == pos)
- {
- result.add(result.size() - wordCount, token
- .substring(englishStart, englishEnd));
- wordCount++;
- pos = start;
- start = 0;
- }
- }
- // end of if(CHAR_AND_NUM.indexOf(token.charAt(start))>=0)
- // 判断分段是否已分析完毕
- if (start < pos)
- {
- String word = token.substring(start, pos);
- if (dic.match(word))
- {
- result.add(result.size() - wordCount, word);
- wordCount++;
- pos = start;
- start = 0;
- }
- else
- {
- // 当判断到剩余单字时,将词加入到词库中
- if (word.length() == 1)
- {
- result.add(result.size() - wordCount, word);
- wordCount++;
- pos = start;
- start = 0;
- }
- else
- start++;
- }
- // end of match
- }
- // end of if(start
- }
- // end of while (start < pos)
- }
- // end of while (pos > 0)
- }
- // end of while (tokenizer.hasMoreTokens())
- return result;
- }
- }