Dictionary.java 19.9 KB
Newer Older
weixin_43283383's avatar
weixin_43283383 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**
 * IK 中文分词  版本 5.0
 * IK Analyzer release 5.0
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 源代码由林良益(linliangyi2005@gmail.com)提供
 * 版权声明 2012,乌龙茶工作室
 * provided by Linliangyi and copyright 2012 by Oolong studio
 * 
 * 
 */
package org.wltea.analyzer.dic;

A
arron 已提交
28 29 30 31 32 33 34
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
35
import java.nio.file.Path;
A
arron 已提交
36 37 38 39 40 41 42
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

goBD's avatar
goBD 已提交
43 44 45 46 47 48
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
49
import org.elasticsearch.common.io.PathUtils;
weixin_43283383's avatar
weixin_43283383 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.wltea.analyzer.cfg.Configuration;

/**
 * 词典管理类,单子模式
 */
public class Dictionary {


	/*
	 * 词典单子实例
	 */
	private static Dictionary singleton;

    private DictSegment _MainDict;

    private DictSegment _SurnameDict;

    private DictSegment _QuantifierDict;

    private DictSegment _SuffixDict;

    private DictSegment _PrepDict;

    private DictSegment _StopWords;

	
	/**
	 * 配置对象
	 */
	private Configuration configuration;
82
    public static ESLogger logger=Loggers.getLogger("ik-analyzer");
A
arron 已提交
83 84 85
    
    private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
    
weixin_43283383's avatar
weixin_43283383 已提交
86 87 88 89 90 91
    public static final String PATH_DIC_MAIN = "ik/main.dic";
    public static final String PATH_DIC_SURNAME = "ik/surname.dic";
    public static final String PATH_DIC_QUANTIFIER = "ik/quantifier.dic";
    public static final String PATH_DIC_SUFFIX = "ik/suffix.dic";
    public static final String PATH_DIC_PREP = "ik/preposition.dic";
    public static final String PATH_DIC_STOP = "ik/stopword.dic";
A
arron 已提交
92
    
weixin_43283383's avatar
weixin_43283383 已提交
93
    private Dictionary(){
94

weixin_43283383's avatar
weixin_43283383 已提交
95 96 97 98 99 100 101 102 103 104
    }

	/**
	 * 词典初始化
	 * 由于IK Analyzer的词典采用Dictionary类的静态方法进行词典初始化
	 * 只有当Dictionary类被实际调用时,才会开始载入词典,
	 * 这将延长首次分词操作的时间
	 * 该方法提供了一个在应用加载阶段就初始化字典的手段
	 * @return Dictionary
	 */
weixin_43283383's avatar
weixin_43283383 已提交
105
	public static synchronized Dictionary initial(Configuration cfg){
weixin_43283383's avatar
weixin_43283383 已提交
106 107 108 109 110 111 112 113 114 115 116
		if(singleton == null){
			synchronized(Dictionary.class){
				if(singleton == null){
					singleton = new Dictionary();
                    singleton.configuration=cfg;
                    singleton.loadMainDict();
                    singleton.loadSurnameDict();
                    singleton.loadQuantifierDict();
                    singleton.loadSuffixDict();
                    singleton.loadPrepDict();
                    singleton.loadStopWordDict();
goBD's avatar
goBD 已提交
117
                    
A
arron 已提交
118 119 120 121 122 123 124 125 126 127
	                //建立监控线程
	                for(String location:cfg.getRemoteExtDictionarys()){
	                	//10 秒是初始延迟可以修改的  60是间隔时间  单位秒
	            		pool.scheduleAtFixedRate(new Monitor(location), 10, 60, TimeUnit.SECONDS);
	                }
	                for(String location:cfg.getRemoteExtStopWordDictionarys()){
	            		pool.scheduleAtFixedRate(new Monitor(location), 10, 60, TimeUnit.SECONDS);
	                }
	                    
	                return singleton;
weixin_43283383's avatar
weixin_43283383 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
				}
			}
		}
		return singleton;
	}
	
	/**
	 * 获取词典单子实例
	 * @return Dictionary 单例对象
	 */
	public static Dictionary getSingleton(){
		if(singleton == null){
			throw new IllegalStateException("词典尚未初始化,请先调用initial方法");
		}
		return singleton;
	}
	
	/**
	 * 批量加载新词条
	 * @param words Collection<String>词条列表
	 */
	public void addWords(Collection<String> words){
		if(words != null){
			for(String word : words){
				if (word != null) {
					//批量加载词条到主内存词典中
weixin_43283383's avatar
weixin_43283383 已提交
154
					singleton._MainDict.fillSegment(word.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167
				}
			}
		}
	}
	
	/**
	 * 批量移除(屏蔽)词条
	 */
	public void disableWords(Collection<String> words){
		if(words != null){
			for(String word : words){
				if (word != null) {
					//批量屏蔽词条
weixin_43283383's avatar
weixin_43283383 已提交
168
					singleton._MainDict.disableSegment(word.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
				}
			}
		}
	}
	
	/**
	 * 检索匹配主词典
	 * @return Hit 匹配结果描述
	 */
	public Hit matchInMainDict(char[] charArray){
		return singleton._MainDict.match(charArray);
	}
	
	/**
	 * 检索匹配主词典
	 * @return Hit 匹配结果描述
	 */
	public Hit matchInMainDict(char[] charArray , int begin, int length){
weixin_43283383's avatar
weixin_43283383 已提交
187
        return singleton._MainDict.match(charArray, begin, length);
weixin_43283383's avatar
weixin_43283383 已提交
188 189 190 191 192 193 194
	}
	
	/**
	 * 检索匹配量词词典
	 * @return Hit 匹配结果描述
	 */
	public Hit matchInQuantifierDict(char[] charArray , int begin, int length){
weixin_43283383's avatar
weixin_43283383 已提交
195
		return singleton._QuantifierDict.match(charArray, begin, length);
weixin_43283383's avatar
weixin_43283383 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	}
	
	
	/**
	 * 从已匹配的Hit中直接取出DictSegment,继续向下匹配
	 * @return Hit
	 */
	public Hit matchWithHit(char[] charArray , int currentIndex , Hit matchedHit){
		DictSegment ds = matchedHit.getMatchedDictSegment();
		return ds.match(charArray, currentIndex, 1 , matchedHit);
	}
	
	
	/**
	 * 判断是否是停止词
	 * @return boolean
	 */
	public boolean isStopWord(char[] charArray , int begin, int length){			
weixin_43283383's avatar
weixin_43283383 已提交
214
		return singleton._StopWords.match(charArray, begin, length).isMatch();
weixin_43283383's avatar
weixin_43283383 已提交
215 216 217 218 219 220 221 222 223 224
	}	
	
	/**
	 * 加载主词典及扩展词典
	 */
	private void loadMainDict(){
		//建立一个主词典实例
		_MainDict = new DictSegment((char)0);

		//读取主词典文件
225
		Path file = PathUtils.get(configuration.getDictRoot(), Dictionary.PATH_DIC_MAIN);
weixin_43283383's avatar
weixin_43283383 已提交
226 227 228

        InputStream is = null;
        try {
229
            is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
230 231 232 233 234 235 236 237 238 239
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
			String theWord = null;
			do {
				theWord = br.readLine();
				if (theWord != null && !"".equals(theWord.trim())) {
weixin_43283383's avatar
weixin_43283383 已提交
240
					_MainDict.fillSegment(theWord.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
				}
			} while (theWord != null);
			
		} catch (IOException e) {
            logger.error("ik-analyzer",e);

        }finally{
			try {
				if(is != null){
                    is.close();
                    is = null;
				}
			} catch (IOException e) {
                logger.error("ik-analyzer",e);
			}
		}
		//加载扩展词典
		this.loadExtDict();
goBD's avatar
goBD 已提交
259 260
		//加载远程自定义词库
		this.loadRemoteExtDict();
weixin_43283383's avatar
weixin_43283383 已提交
261 262 263 264 265 266 267 268 269 270 271 272
	}	
	
	/**
	 * 加载用户配置的扩展词典到主词库表
	 */
	private void loadExtDict(){
		//加载扩展词典配置
		List<String> extDictFiles  = configuration.getExtDictionarys();
		if(extDictFiles != null){
			InputStream is = null;
			for(String extDictName : extDictFiles){
				//读取扩展词典文件
weixin_43283383's avatar
weixin_43283383 已提交
273
                logger.info("[Dict Loading] " + extDictName);
274
				Path file = PathUtils.get(configuration.getDictRoot(), extDictName);
weixin_43283383's avatar
weixin_43283383 已提交
275
                try {
276
                    is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
                } catch (FileNotFoundException e) {
                    logger.error("ik-analyzer",e);
                }

				//如果找不到扩展的字典,则忽略
				if(is == null){
					continue;
				}
				try {
					BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
					String theWord = null;
					do {
						theWord = br.readLine();
                        if (theWord != null && !"".equals(theWord.trim())) {
							//加载扩展词典数据到主内存词典中
weixin_43283383's avatar
weixin_43283383 已提交
292
							_MainDict.fillSegment(theWord.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
						}
					} while (theWord != null);
					
				} catch (IOException e) {
                    logger.error("ik-analyzer",e);
                }finally{
					try {
						if(is != null){
		                    is.close();
		                    is = null;
						}
					} catch (IOException e) {
                        logger.error("ik-analyzer",e);
                    }
				}
			}
		}		
	}
	
goBD's avatar
goBD 已提交
312 313 314 315 316 317 318
	
	/**
	 * 加载远程扩展词典到主词库表
	 */
	private void loadRemoteExtDict(){
		List<String> remoteExtDictFiles  = configuration.getRemoteExtDictionarys();
		for(String location:remoteExtDictFiles){
weixin_43283383's avatar
weixin_43283383 已提交
319
			logger.info("[Dict Loading] " + location);
goBD's avatar
goBD 已提交
320 321 322
			List<String> lists = getRemoteWords(location);
			//如果找不到扩展的字典,则忽略
			if(lists == null){
weixin_43283383's avatar
weixin_43283383 已提交
323
				logger.error("[Dict Loading] "+location+"加载失败");
goBD's avatar
goBD 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
				continue;
			}
			for(String theWord:lists){
				if (theWord != null && !"".equals(theWord.trim())) {
					//加载扩展词典数据到主内存词典中
					logger.info(theWord);
					_MainDict.fillSegment(theWord.trim().toLowerCase().toCharArray());
				}
			}
		}
		
	}
	
	/**
	 * 从远程服务器上下载自定义词条
	 */
	private static List<String> getRemoteWords(String location){
		
		List<String> buffer = new ArrayList<String>();
		RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10*1000)
				.setConnectTimeout(10*1000).setSocketTimeout(60*1000).build();
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response;
		BufferedReader in;
		HttpGet get = new HttpGet(location);
		get.setConfig(rc);
		try {
			response = httpclient.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				
				String charset = "UTF-8";
				//获取编码,默认为utf-8
				if(response.getEntity().getContentType().getValue().contains("charset=")){
					String contentType=response.getEntity().getContentType().getValue();
					charset=contentType.substring(contentType.lastIndexOf("=")+1);
				}
				in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),charset));
				String line ;
				while((line = in.readLine())!=null){
					buffer.add(line);
				}
				in.close();
				response.close();
				return buffer;
			}
			response.close();
		} catch (ClientProtocolException e) {
371
			logger.error( "getRemoteWords {} error" , e , location);
goBD's avatar
goBD 已提交
372
		} catch (IllegalStateException e) {
373
			logger.error( "getRemoteWords {} error" , e , location );
goBD's avatar
goBD 已提交
374
		} catch (IOException e) {
375
			logger.error( "getRemoteWords {} error" , e , location );
goBD's avatar
goBD 已提交
376 377 378 379 380 381
		}
		return buffer;
	}
	
	
	
weixin_43283383's avatar
weixin_43283383 已提交
382 383 384 385 386 387 388 389
	/**
	 * 加载用户扩展的停止词词典
	 */
	private void loadStopWordDict(){
		//建立主词典实例
        _StopWords = new DictSegment((char)0);

        //读取主词典文件
390
		Path file = PathUtils.get(configuration.getDictRoot(), Dictionary.PATH_DIC_STOP);
weixin_43283383's avatar
weixin_43283383 已提交
391 392 393

        InputStream is = null;
        try {
394
            is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
395 396 397 398 399 400 401 402 403 404
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
            String theWord = null;
            do {
                theWord = br.readLine();
                if (theWord != null && !"".equals(theWord.trim())) {
weixin_43283383's avatar
weixin_43283383 已提交
405
                    _StopWords.fillSegment(theWord.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
                }
            } while (theWord != null);

        } catch (IOException e) {
            logger.error("ik-analyzer",e);

        }finally{
            try {
                if(is != null){
                    is.close();
                    is = null;
                }
            } catch (IOException e) {
                logger.error("ik-analyzer",e);
            }
        }


		//加载扩展停止词典
		List<String> extStopWordDictFiles  = configuration.getExtStopWordDictionarys();
		if(extStopWordDictFiles != null){
			is = null;
			for(String extStopWordDictName : extStopWordDictFiles){
weixin_43283383's avatar
weixin_43283383 已提交
429
                logger.info("[Dict Loading] " + extStopWordDictName);
weixin_43283383's avatar
weixin_43283383 已提交
430 431

                //读取扩展词典文件
432
                file=PathUtils.get(configuration.getDictRoot(), extStopWordDictName);
weixin_43283383's avatar
weixin_43283383 已提交
433
                try {
434
                    is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448
                } catch (FileNotFoundException e) {
                    logger.error("ik-analyzer",e);
                }
				//如果找不到扩展的字典,则忽略
				if(is == null){
					continue;
				}
				try {
					BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
					String theWord = null;
					do {
						theWord = br.readLine();
						if (theWord != null && !"".equals(theWord.trim())) {
							//加载扩展停止词典数据到内存中
weixin_43283383's avatar
weixin_43283383 已提交
449
                            _StopWords.fillSegment(theWord.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
						}
					} while (theWord != null);
					
				} catch (IOException e) {
                    logger.error("ik-analyzer",e);
					
				}finally{
					try {
						if(is != null){
		                    is.close();
		                    is = null;
						}
					} catch (IOException e) {
                        logger.error("ik-analyzer",e);
					}
				}
			}
goBD's avatar
goBD 已提交
467 468 469 470 471
		}
		
		//加载远程停用词典
		List<String> remoteExtStopWordDictFiles  = configuration.getRemoteExtStopWordDictionarys();
		for(String location:remoteExtStopWordDictFiles){
weixin_43283383's avatar
weixin_43283383 已提交
472
			logger.info("[Dict Loading] " + location);
goBD's avatar
goBD 已提交
473 474 475
			List<String> lists = getRemoteWords(location);
			//如果找不到扩展的字典,则忽略
			if(lists == null){
weixin_43283383's avatar
weixin_43283383 已提交
476
				logger.error("[Dict Loading] "+location+"加载失败");
goBD's avatar
goBD 已提交
477 478 479 480 481 482 483 484 485 486 487 488
				continue;
			}
			for(String theWord:lists){
				if (theWord != null && !"".equals(theWord.trim())) {
					//加载远程词典数据到主内存中
					logger.info(theWord);
					_StopWords.fillSegment(theWord.trim().toLowerCase().toCharArray());
				}
			}
		}
		
		
weixin_43283383's avatar
weixin_43283383 已提交
489 490 491 492 493 494 495 496 497
	}
	
	/**
	 * 加载量词词典
	 */
	private void loadQuantifierDict(){
		//建立一个量词典实例
		_QuantifierDict = new DictSegment((char)0);
		//读取量词词典文件
498
		Path file = PathUtils.get(configuration.getDictRoot(), Dictionary.PATH_DIC_QUANTIFIER);
weixin_43283383's avatar
weixin_43283383 已提交
499 500
        InputStream is = null;
        try {
501
            is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
502 503 504 505 506 507 508 509 510
        } catch (FileNotFoundException e) {
            logger.error("ik-analyzer",e);
        }
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
			String theWord = null;
			do {
				theWord = br.readLine();
				if (theWord != null && !"".equals(theWord.trim())) {
weixin_43283383's avatar
weixin_43283383 已提交
511
					_QuantifierDict.fillSegment(theWord.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
				}
			} while (theWord != null);
			
		} catch (IOException ioe) {
			logger.error("Quantifier Dictionary loading exception.");
			
		}finally{
			try {
				if(is != null){
                    is.close();
                    is = null;
				}
			} catch (IOException e) {
                logger.error("ik-analyzer",e);
			}
		}
	}


    private void loadSurnameDict(){

        _SurnameDict = new DictSegment((char)0);
534
		Path file = PathUtils.get(configuration.getDictRoot(), Dictionary.PATH_DIC_SURNAME);
weixin_43283383's avatar
weixin_43283383 已提交
535 536
        InputStream is = null;
        try {
537
            is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
        } catch (FileNotFoundException e) {
            logger.error("ik-analyzer",e);
        }
        if(is == null){
            throw new RuntimeException("Surname Dictionary not found!!!");
        }
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
            String theWord;
            do {
                theWord = br.readLine();
                if (theWord != null && !"".equals(theWord.trim())) {
                    _SurnameDict.fillSegment(theWord.trim().toCharArray());
                }
            } while (theWord != null);
        } catch (IOException e) {
            logger.error("ik-analyzer",e);
        }finally{
            try {
                if(is != null){
                    is.close();
                    is = null;
                }
            } catch (IOException e) {
                logger.error("ik-analyzer",e);
            }
        }
    }


    private void loadSuffixDict(){

        _SuffixDict = new DictSegment((char)0);
571
		Path file = PathUtils.get(configuration.getDictRoot(), Dictionary.PATH_DIC_SUFFIX);
weixin_43283383's avatar
weixin_43283383 已提交
572 573
        InputStream is = null;
        try {
574
            is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
        } catch (FileNotFoundException e) {
            logger.error("ik-analyzer",e);
        }
        if(is == null){
            throw new RuntimeException("Suffix Dictionary not found!!!");
        }
        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
            String theWord;
            do {
                theWord = br.readLine();
                if (theWord != null && !"".equals(theWord.trim())) {
                    _SuffixDict.fillSegment(theWord.trim().toCharArray());
                }
            } while (theWord != null);
        } catch (IOException e) {
            logger.error("ik-analyzer",e);
        }finally{
            try {
                is.close();
                is = null;
            } catch (IOException e) {
                logger.error("ik-analyzer",e);
            }
        }
    }


    private void loadPrepDict(){

        _PrepDict = new DictSegment((char)0);
607
		Path file = PathUtils.get(configuration.getDictRoot(), Dictionary.PATH_DIC_PREP);
weixin_43283383's avatar
weixin_43283383 已提交
608 609
        InputStream is = null;
        try {
610
            is = new FileInputStream(file.toFile());
weixin_43283383's avatar
weixin_43283383 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
        } catch (FileNotFoundException e) {
            logger.error("ik-analyzer",e);
        }
        if(is == null){
            throw new RuntimeException("Preposition Dictionary not found!!!");
        }
        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
            String theWord;
            do {
                theWord = br.readLine();
                if (theWord != null && !"".equals(theWord.trim())) {

                    _PrepDict.fillSegment(theWord.trim().toCharArray());
                }
            } while (theWord != null);
        } catch (IOException e) {
            logger.error("ik-analyzer",e);
        }finally{
            try {
                is.close();
                is = null;
            } catch (IOException e) {
                logger.error("ik-analyzer",e);
            }
        }
    }
goBD's avatar
goBD 已提交
639 640 641
    
    public void reLoadMainDict(){
    	logger.info("重新加载词典...");
R
rockybean 已提交
642 643
		// 新开一个实例加载词典,减少加载过程对当前词典使用的影响
		Dictionary tmpDict = new Dictionary();
644
		tmpDict.configuration = getSingleton().configuration;
R
rockybean 已提交
645 646 647 648 649
		tmpDict.loadMainDict();
		tmpDict.loadStopWordDict();
		_MainDict = tmpDict._MainDict;
		_StopWords = tmpDict._StopWords;
		logger.info("重新加载词典完毕...");
goBD's avatar
goBD 已提交
650 651
    }
    
weixin_43283383's avatar
weixin_43283383 已提交
652
}