Inverse maximum matching algorithm for Chinese Word Segmentation and Chinese Word Segmentation Matching Algorithm
Package org. wltea. analyzer. core;
Import java. io. IOException;
Import java. io. Reader;
Import java. util. HashMap;
Import java. util. HashSet;
Import java. util. Collections list;
Import java. util. Map;
Import java. util. Set;
Import org. wltea. analyzer. cfg. Configuration;
Import org. wltea. analyzer. dic. Dictionary;
/**
* Context of word splitting
* @ Author TongXueQiang
* @ Date 2016/01/22
* @ Since 1.7
*/
Class AnalyzeContext {
Private char [] segmentBuff;
Private int [] charTypes;
Private int buffOffset;
Private int cursor;
Private int available;
Private Set <String> buffLocker;
Private QuickSortSet orgLexemes;
Private Map <Integer, LexemePath> pathMap;
Private grouping list <Lexeme> results;
Private Configuration cfg;
Private Integer moveIndex;
Public AnalyzeContext (Configuration cfg ){
This. cfg = cfg;
This. segmentBuff = new char [2, 4096];
This. charTypes = new int [4096];
This. buffLocker = new HashSet <String> ();
This. orgLexemes = new QuickSortSet ();
This. pathMap = new HashMap <Integer, LexemePath> ();
This. results = new rule list <Lexeme> ();
}
Int getCursor (){
Return this. cursor;
}
Char [] getSegmentBuff (){
Return this. segmentBuff;
}
Char getCurrentChar (){
Return this. segmentBuff [this. cursor];
}
Int getCurrentCharType (){
Return this. charTypes [this. cursor];
}
Int getBufferOffset (){
Return this. buffOffset;
}
/**
* Fill the buffer with characters
* @ Param reader
* @ Return
* @ Throws IOException
*/
Int fillBuffer (Reader reader) throws IOException {
Int readCount = 0;
If (this. buffOffset = 0 ){
ReadCount = reader. read (this. segmentBuff );
} Else {
Int offset = this. available-this. cursor;
If (offset> 0 ){
System. arraycopy (this. segmentBuff, this. cursor,
This. segmentBuff, 0, offset );
ReadCount = offset;
}
ReadCount + = reader. read (this. segmentBuff, offset,-offset );
}
This. available = readCount;
This. cursor = 0;
Return readCount;
}
Void initCursor (){
This. cursor = this. available-1;
// Canonicalized characters
This. segmentBuff [this. cursor] = CharacterUtil
. Regularize (this. segmentBuff [this. cursor]);
// Specifies the character type, such as Arabic numerals and English letters.
This. charTypes [this. cursor] = CharacterUtil
. IdentifyCharType (this. segmentBuff [this. cursor]);
}
Boolean moveCursor (){
If (this. cursor-moveIndex)> 0 ){
This. cursor-= (moveIndex + 1 );
// System. out. println ("cursor position after the pointer is moved:" + cursor );
// Normalize the current character after moving the pointer
This. segmentBuff [this. cursor] = CharacterUtil
. Regularize (this. segmentBuff [this. cursor]);
// Specify the type of the Current Character
This. charTypes [this. cursor] = CharacterUtil
. IdentifyCharType (this. segmentBuff [this. cursor]);
Return true;
}
Return false;
}
Void lockBuffer (String segmenterName ){
This. buffLocker. add (segmenterName );
}
Void unlockBuffer (String segmenterName ){
This. buffLocker. remove (segmenterName );
}
Boolean isBufferLocked (){
Return (this. buffLocker. size ()> 0 );
}
Boolean isBufferConsumed (){
Return (this. cursor = this. available-1 );
}
Boolean needRefillBuffer (){
Return (this. available = 4096) & (this. cursor <this. available-1)
& (This. cursor> this. available-100 )&&(! (IsBufferLocked ())));
}
Void markBufferOffset (){
This. buffOffset + = this. cursor;
}
Void addLexeme (Lexeme lexeme ){
This. orgLexemes. addLexeme (lexeme );
}
Void addLexemePath (LexemePath path ){
If (path! = Null)
This. pathMap. put (Integer. valueOf (path. getPathBegin (), path );
}
QuickSortSet getOrgLexemes (){
Return this. orgLexemes;
}
/**
* Output result set
*/
Void outputToResult (){
Int index = 0;
While (index <= this. cursor ){
LexemePath path = (LexemePath) this. pathMap. get (Integer
. ValueOf (index ));
If (path! = Null ){
Lexeme l = path. pollFirst ();
If (l! = Null ){
This. results. add (l );
Index = l. getBegin () + l. getLength ();
This. cursor = index;
}
} Else {
OutputSingleCJK (index );
++ Index;
}
}
This. pathMap. clear ();
}
Private void outputSingleCJK (int index ){
Lexeme singleCharLexeme;
If (4 = this. charTypes [index]) {
SingleCharLexeme = new Lexeme (this. buffOffset, index, 1, 64 );
This. results. add (singleCharLexeme );
} Else if (8 = this. charTypes [index]) {
SingleCharLexeme = new Lexeme (this. buffOffset, index, 1, 8 );
This. results. add (singleCharLexeme );
}
}
/**
* Retrieve the word dollar and assign a value to the word dollar
* @ Return
*/
Lexeme getNextLexeme (){
Lexeme result = (Lexeme) this. results. pollFirst ();
While (result! = Null ){
Compound (result); // merge quantifiers
// Filter out deprecated words
If (Dictionary. getSingleton (). isStopWord (this. segmentBuff,
Result. getBegin (), result. getLength ())){
// System. out. println (Dictionary. getSingleton (). isStopWord (this. segmentBuff,
// Result. getBegin (), result. getLength ()));
Result = (Lexeme) this. results. pollFirst ();
} Else {
// Assign a value to Lexeme
Result. setLexemeText (String. valueOf (this. segmentBuff,
Result. getBegin (), result. getLength ()));
Break;
}
}
Return result;
}
Void reset (){
This. buffLocker. clear ();
This. orgLexemes = new QuickSortSet ();
This. available = 0;
This. buffOffset = 0;
This. charTypes = new int [4096];
This. cursor = 0;
This. results. clear ();
This. segmentBuff = new char [2, 4096];
This. pathMap. clear ();
}
/**
* Merge quantifiers
* @ Param result
*/
Private void compound (Lexeme result ){
If (! (This. cfg. useSmart ())){
Return;
}
If (this. results. isEmpty ())
Return;
Lexeme nextLexeme;
Boolean appendOk;
If (2 = result. getLexemeType ()){
NextLexeme = (Lexeme) this. results. peekFirst ();
AppendOk = false;
If (16 = nextLexeme. getLexemeType ()){
AppendOk = result. append (nextLexeme, 16 );
} Else if (32 = nextLexeme. getLexemeType ()){
AppendOk = result. append (nextLexeme, 48 );
}
If (appendOk ){
This. results. pollFirst ();
}
}
If (16 = result. getLexemeType ())&&(! (This. results. isEmpty ()))){
NextLexeme = (Lexeme) this. results. peekFirst ();
AppendOk = false;
If (32 = nextLexeme. getLexemeType ()){
AppendOk = result. append (nextLexeme, 48 );
}
If (! (AppendOk ))
Return;
This. results. pollFirst ();
}
}
Public void setMoveIndex (Integer moveIndex ){
This. moveIndex = moveIndex;
}
}
The following is the reverse maximum matching algorithm of CJK:
Package org. wltea. analyzer. core;
Import org. wltea. analyzer. dic. Dictionary;
Import org. wltea. analyzer. dic. Hit;
/**
* China-Japan-Korea word divider and reverse maximum Matching Algorithm
*
* @ Author TongXueQiang
* @ Date 2016/01/20
* @ Since 1.7
*/
Class CJKSegmenter implements ISegmenter {
Static final String SEGMENTER_NAME = "CJK_SEGMENTER ";
Static Integer MATCH_LEN = 7;
Static Integer moveIndex = MATCH_LEN-1;
CJKSegmenter (){
}
/*
* Reverse maximum Matching Algorithm
*
* @ See org. wltea. analyzer. core. ISegmenter # analyze (org. wltea. analyzer. core.
* AnalyzeContext)
*/
Public void analyze (AnalyzeContext context ){
If (context. getCursor () <moveIndex ){
MoveIndex = context. getCursor ();
MATCH_LEN = context. getCursor () + 1;
}
Hit singleCharHit = Dictionary. getSingleton (). matchInMainDict (
Context. getSegmentBuff (), context. getCursor ()-moveIndex,
MATCH_LEN );
If (singleCharHit. isMatch () | MATCH_LEN = 1 ){
Lexeme newLexeme = new Lexeme (context. getBufferOffset (),
Context. getCursor ()-moveIndex, MATCH_LEN, 4 );
Context. addLexeme (newLexeme );
Context. setMoveIndex (moveIndex );
Init ();
} Else {
If (! SingleCharHit. isUnmatch () | singleCharHit. isUnmatch ()){
-- MoveIndex;
-- MATCH_LEN;
Analyze (context );
}
}
}
Private void init (){
MoveIndex = 6;
MATCH_LEN = 7;
}
@ Override
Public void reset (){
}
}