import { Thread } from "../thread/Thread" ; import { SensitiveWordConfig } from "./SensitiveWordConfig" ; /**
 * 敏感字词库  初始化工具
 */ export class SensitiveWordInit extends Thread { /**
     * 不让new
     */ private constructor ( ) { super ( ) ; } param sensitiveWords return public static initKeyWord ( sensitiveWords , callback ) { let init = new SensitiveWordInit ( ) ; init . on ( "onParseEnd" , ( map ) => { init . close ( ) ; callback ( map ) ; } ) ; init . start ( sensitiveWords , SensitiveWordConfig . END_MARK , SensitiveWordConfig . WORD_NO_END , SensitiveWordConfig . WORD_END ) ; } param sensitiveWords param END_MARK param WORD_NO_END param WORD_END returns run ( sensitiveWords , END_MARK , WORD_NO_END , WORD_END ) { this . emit ( "onParseStart" ) ; if ( sensitiveWords == null ) { this . emit ( "onParseEnd" ) ; return null ; } let keyWordArr = new Set ( ) ; for ( let i = 0 ; i < sensitiveWords . length ; i ++ ) { let s = sensitiveWords [ i ] ; keyWordArr . add ( s . trim ( ) ) ; } let map = this . addSensitiveWordToHashMap ( keyWordArr , END_MARK , WORD_NO_END , WORD_END ) ; this . emit ( "onParseEnd" , map ) ; } param keyWordSet private addSensitiveWordToHashMap ( keyWordArr , END_MARK , WORD_NO_END , WORD_END ) { let map = { } ; let nowMap ; let newWordMap ; keyWordArr . forEach ( key => { nowMap = map ; for ( let i = 0 ; i < key . length ; i ++ ) { let keyChar = key . charAt ( i ) ; let wordMap = nowMap [ keyChar ] ; if ( wordMap ) { nowMap = wordMap ; } else { newWordMap = { } ; newWordMap [ END_MARK ] = WORD_NO_END ; nowMap [ keyChar ] = newWordMap ; nowMap = newWordMap ; } if ( i == key . length - 1 ) { nowMap [ END_MARK ] = WORD_END ; } } } ) ; return map ; } }