Dictionary.java 18.0 KB
Newer Older
weixin_43283383's avatar
weixin_43283383 已提交
1 2 3
/**
 * IK 中文分词  版本 5.0
 * IK Analyzer release 5.0
4
 *
weixin_43283383's avatar
weixin_43283383 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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
23 24
 *
 *
weixin_43283383's avatar
weixin_43283383 已提交
25 26 27
 */
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
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;

65
	private DictSegment _MainDict;
weixin_43283383's avatar
weixin_43283383 已提交
66

67
	private DictSegment _SurnameDict;
weixin_43283383's avatar
weixin_43283383 已提交
68

69
	private DictSegment _QuantifierDict;
weixin_43283383's avatar
weixin_43283383 已提交
70

71
	private DictSegment _SuffixDict;
weixin_43283383's avatar
weixin_43283383 已提交
72

73 74 75
	private DictSegment _PrepDict;

	private DictSegment _StopWords;
weixin_43283383's avatar
weixin_43283383 已提交
76 77 78 79 80 81


	/**
	 * 配置对象
	 */
	private Configuration configuration;
82 83 84 85 86 87 88 89 90 91 92 93 94 95
	public static ESLogger logger=Loggers.getLogger("ik-analyzer");

	private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);

	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";

	private Dictionary(){

	}
weixin_43283383's avatar
weixin_43283383 已提交
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
		if(singleton == null){
			synchronized(Dictionary.class){
				if(singleton == null){
					singleton = new Dictionary();
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
					singleton.configuration=cfg;
					singleton.loadMainDict();
					singleton.loadSurnameDict();
					singleton.loadQuantifierDict();
					singleton.loadSuffixDict();
					singleton.loadPrepDict();
					singleton.loadStopWordDict();

					//建立监控线程
					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
				}
			}
		}
		return singleton;
	}
133

weixin_43283383's avatar
weixin_43283383 已提交
134 135 136 137 138 139 140 141 142 143
	/**
	 * 获取词典单子实例
	 * @return Dictionary 单例对象
	 */
	public static Dictionary getSingleton(){
		if(singleton == null){
			throw new IllegalStateException("词典尚未初始化,请先调用initial方法");
		}
		return singleton;
	}
144

weixin_43283383's avatar
weixin_43283383 已提交
145 146 147 148 149 150 151 152 153
	/**
	 * 批量加载新词条
	 * @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

weixin_43283383's avatar
weixin_43283383 已提交
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

weixin_43283383's avatar
weixin_43283383 已提交
174 175 176 177 178 179 180
	/**
	 * 检索匹配主词典
	 * @return Hit 匹配结果描述
	 */
	public Hit matchInMainDict(char[] charArray){
		return singleton._MainDict.match(charArray);
	}
181

weixin_43283383's avatar
weixin_43283383 已提交
182 183 184 185 186
	/**
	 * 检索匹配主词典
	 * @return Hit 匹配结果描述
	 */
	public Hit matchInMainDict(char[] charArray , int begin, int length){
187
		return singleton._MainDict.match(charArray, begin, length);
weixin_43283383's avatar
weixin_43283383 已提交
188
	}
189

weixin_43283383's avatar
weixin_43283383 已提交
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


weixin_43283383's avatar
weixin_43283383 已提交
199 200 201 202 203 204 205 206
	/**
	 * 从已匹配的Hit中直接取出DictSegment,继续向下匹配
	 * @return Hit
	 */
	public Hit matchWithHit(char[] charArray , int currentIndex , Hit matchedHit){
		DictSegment ds = matchedHit.getMatchedDictSegment();
		return ds.match(charArray, currentIndex, 1 , matchedHit);
	}
207 208


weixin_43283383's avatar
weixin_43283383 已提交
209 210 211 212
	/**
	 * 判断是否是停止词
	 * @return boolean
	 */
213
	public boolean isStopWord(char[] charArray , int begin, int length){
weixin_43283383's avatar
weixin_43283383 已提交
214
		return singleton._StopWords.match(charArray, begin, length).isMatch();
215 216
	}

weixin_43283383's avatar
weixin_43283383 已提交
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 229 230 231 232 233
		InputStream is = null;
		try {
			is = new FileInputStream(file.toFile());
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage(), e);
		}

weixin_43283383's avatar
weixin_43283383 已提交
234 235 236 237 238 239
		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
				}
			} while (theWord != null);
243

weixin_43283383's avatar
weixin_43283383 已提交
244
		} catch (IOException e) {
245
			logger.error("ik-analyzer",e);
weixin_43283383's avatar
weixin_43283383 已提交
246

247
		}finally{
weixin_43283383's avatar
weixin_43283383 已提交
248 249
			try {
				if(is != null){
250 251
					is.close();
					is = null;
weixin_43283383's avatar
weixin_43283383 已提交
252 253
				}
			} catch (IOException e) {
254
				logger.error("ik-analyzer",e);
weixin_43283383's avatar
weixin_43283383 已提交
255 256 257 258
			}
		}
		//加载扩展词典
		this.loadExtDict();
goBD's avatar
goBD 已提交
259 260
		//加载远程自定义词库
		this.loadRemoteExtDict();
261 262
	}

weixin_43283383's avatar
weixin_43283383 已提交
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){
				//读取扩展词典文件
273
				logger.info("[Dict Loading] " + extDictName);
274
				Path file = PathUtils.get(configuration.getDictRoot(), extDictName);
275 276 277 278 279
				try {
					is = new FileInputStream(file.toFile());
				} catch (FileNotFoundException e) {
					logger.error("ik-analyzer",e);
				}
weixin_43283383's avatar
weixin_43283383 已提交
280 281 282 283 284 285 286 287 288 289

				//如果找不到扩展的字典,则忽略
				if(is == null){
					continue;
				}
				try {
					BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
					String theWord = null;
					do {
						theWord = br.readLine();
290
						if (theWord != null && !"".equals(theWord.trim())) {
weixin_43283383's avatar
weixin_43283383 已提交
291
							//加载扩展词典数据到主内存词典中
weixin_43283383's avatar
weixin_43283383 已提交
292
							_MainDict.fillSegment(theWord.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
293 294
						}
					} while (theWord != null);
295

weixin_43283383's avatar
weixin_43283383 已提交
296
				} catch (IOException e) {
297 298
					logger.error("ik-analyzer",e);
				}finally{
weixin_43283383's avatar
weixin_43283383 已提交
299 300
					try {
						if(is != null){
301 302
							is.close();
							is = null;
weixin_43283383's avatar
weixin_43283383 已提交
303 304
						}
					} catch (IOException e) {
305 306
						logger.error("ik-analyzer",e);
					}
weixin_43283383's avatar
weixin_43283383 已提交
307 308
				}
			}
309
		}
weixin_43283383's avatar
weixin_43283383 已提交
310
	}
311 312


goBD's avatar
goBD 已提交
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
				continue;
			}
			for(String theWord:lists){
				if (theWord != null && !"".equals(theWord.trim())) {
					//加载扩展词典数据到主内存词典中
					logger.info(theWord);
					_MainDict.fillSegment(theWord.trim().toLowerCase().toCharArray());
				}
			}
		}
334

goBD's avatar
goBD 已提交
335
	}
336

goBD's avatar
goBD 已提交
337 338 339 340
	/**
	 * 从远程服务器上下载自定义词条
	 */
	private static List<String> getRemoteWords(String location){
341

goBD's avatar
goBD 已提交
342 343 344 345 346 347 348 349 350 351 352
		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){
353

goBD's avatar
goBD 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
				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
		}
		return buffer;
	}
379 380 381



weixin_43283383's avatar
weixin_43283383 已提交
382 383 384 385 386
	/**
	 * 加载用户扩展的停止词词典
	 */
	private void loadStopWordDict(){
		//建立主词典实例
387
		_StopWords = new DictSegment((char)0);
weixin_43283383's avatar
weixin_43283383 已提交
388

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

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
		InputStream is = null;
		try {
			is = new FileInputStream(file.toFile());
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage(), 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())) {
					_StopWords.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);
			}
		}
weixin_43283383's avatar
weixin_43283383 已提交
422 423 424 425 426 427 428


		//加载扩展停止词典
		List<String> extStopWordDictFiles  = configuration.getExtStopWordDictionarys();
		if(extStopWordDictFiles != null){
			is = null;
			for(String extStopWordDictName : extStopWordDictFiles){
429 430 431 432 433 434 435 436 437
				logger.info("[Dict Loading] " + extStopWordDictName);

				//读取扩展词典文件
				file=PathUtils.get(configuration.getDictRoot(), extStopWordDictName);
				try {
					is = new FileInputStream(file.toFile());
				} catch (FileNotFoundException e) {
					logger.error("ik-analyzer",e);
				}
weixin_43283383's avatar
weixin_43283383 已提交
438 439 440 441 442 443 444 445 446 447 448
				//如果找不到扩展的字典,则忽略
				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())) {
							//加载扩展停止词典数据到内存中
449
							_StopWords.fillSegment(theWord.trim().toCharArray());
weixin_43283383's avatar
weixin_43283383 已提交
450 451
						}
					} while (theWord != null);
452

weixin_43283383's avatar
weixin_43283383 已提交
453
				} catch (IOException e) {
454 455
					logger.error("ik-analyzer",e);

weixin_43283383's avatar
weixin_43283383 已提交
456 457 458
				}finally{
					try {
						if(is != null){
459 460
							is.close();
							is = null;
weixin_43283383's avatar
weixin_43283383 已提交
461 462
						}
					} catch (IOException e) {
463
						logger.error("ik-analyzer",e);
weixin_43283383's avatar
weixin_43283383 已提交
464 465 466
					}
				}
			}
goBD's avatar
goBD 已提交
467
		}
468

goBD's avatar
goBD 已提交
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
				continue;
			}
			for(String theWord:lists){
				if (theWord != null && !"".equals(theWord.trim())) {
					//加载远程词典数据到主内存中
					logger.info(theWord);
					_StopWords.fillSegment(theWord.trim().toLowerCase().toCharArray());
				}
			}
		}
487 488


weixin_43283383's avatar
weixin_43283383 已提交
489
	}
490

weixin_43283383's avatar
weixin_43283383 已提交
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);
499 500 501 502 503 504
		InputStream is = null;
		try {
			is = new FileInputStream(file.toFile());
		} catch (FileNotFoundException e) {
			logger.error("ik-analyzer",e);
		}
weixin_43283383's avatar
weixin_43283383 已提交
505 506 507 508 509 510
		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
				}
			} while (theWord != null);
514

weixin_43283383's avatar
weixin_43283383 已提交
515 516
		} catch (IOException ioe) {
			logger.error("Quantifier Dictionary loading exception.");
517

weixin_43283383's avatar
weixin_43283383 已提交
518 519 520
		}finally{
			try {
				if(is != null){
521 522
					is.close();
					is = null;
weixin_43283383's avatar
weixin_43283383 已提交
523 524
				}
			} catch (IOException e) {
525
				logger.error("ik-analyzer",e);
weixin_43283383's avatar
weixin_43283383 已提交
526 527 528 529 530
			}
		}
	}


531
	private void loadSurnameDict(){
weixin_43283383's avatar
weixin_43283383 已提交
532

533
		_SurnameDict = new DictSegment((char)0);
534
		Path file = PathUtils.get(configuration.getDictRoot(), Dictionary.PATH_DIC_SURNAME);
535 536 537 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
		InputStream is = null;
		try {
			is = new FileInputStream(file.toFile());
		} 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);
572 573 574 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
		InputStream is = null;
		try {
			is = new FileInputStream(file.toFile());
		} 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);
608 609 610 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 639 640 641
		InputStream is = null;
		try {
			is = new FileInputStream(file.toFile());
		} 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);
			}
		}
	}

	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("重新加载词典完毕...");
650 651 652
	}

}