43
43
import java .util .concurrent .ScheduledExecutorService ;
44
44
import java .util .concurrent .TimeUnit ;
45
45
46
+ import org .apache .http .Header ;
47
+ import org .apache .http .HttpEntity ;
46
48
import org .apache .http .client .ClientProtocolException ;
47
49
import org .apache .http .client .config .RequestConfig ;
48
50
import org .apache .http .client .methods .CloseableHttpResponse ;
@@ -69,14 +71,8 @@ public class Dictionary {
69
71
70
72
private DictSegment _MainDict ;
71
73
72
- private DictSegment _SurnameDict ;
73
-
74
74
private DictSegment _QuantifierDict ;
75
75
76
- private DictSegment _SuffixDict ;
77
-
78
- private DictSegment _PrepDict ;
79
-
80
76
private DictSegment _StopWords ;
81
77
82
78
/**
@@ -88,12 +84,12 @@ public class Dictionary {
88
84
89
85
private static ScheduledExecutorService pool = Executors .newScheduledThreadPool (1 );
90
86
91
- public static final String PATH_DIC_MAIN = "main.dic" ;
92
- public static final String PATH_DIC_SURNAME = "surname.dic" ;
93
- public static final String PATH_DIC_QUANTIFIER = "quantifier.dic" ;
94
- public static final String PATH_DIC_SUFFIX = "suffix.dic" ;
95
- public static final String PATH_DIC_PREP = "preposition.dic" ;
96
- public static final String PATH_DIC_STOP = "stopword.dic" ;
87
+ private static final String PATH_DIC_MAIN = "main.dic" ;
88
+ private static final String PATH_DIC_SURNAME = "surname.dic" ;
89
+ private static final String PATH_DIC_QUANTIFIER = "quantifier.dic" ;
90
+ private static final String PATH_DIC_SUFFIX = "suffix.dic" ;
91
+ private static final String PATH_DIC_PREP = "preposition.dic" ;
92
+ private static final String PATH_DIC_STOP = "stopword.dic" ;
97
93
98
94
private final static String FILE_NAME = "IKAnalyzer.cfg.xml" ;
99
95
private final static String EXT_DICT = "ext_dict" ;
@@ -128,15 +124,13 @@ private Dictionary(Configuration cfg) {
128
124
if (input != null ) {
129
125
try {
130
126
props .loadFromXML (input );
131
- } catch (InvalidPropertiesFormatException e ) {
132
- logger .error ("ik-analyzer" , e );
133
127
} catch (IOException e ) {
134
128
logger .error ("ik-analyzer" , e );
135
129
}
136
130
}
137
131
}
138
132
139
- public String getProperty (String key ){
133
+ private String getProperty (String key ){
140
134
if (props !=null ){
141
135
return props .getProperty (key );
142
136
}
@@ -148,7 +142,7 @@ public String getProperty(String key){
148
142
*
149
143
* @return Dictionary
150
144
*/
151
- public static synchronized Dictionary initial (Configuration cfg ) {
145
+ public static synchronized void initial (Configuration cfg ) {
152
146
if (singleton == null ) {
153
147
synchronized (Dictionary .class ) {
154
148
if (singleton == null ) {
@@ -172,14 +166,12 @@ public static synchronized Dictionary initial(Configuration cfg) {
172
166
}
173
167
}
174
168
175
- return singleton ;
176
169
}
177
170
}
178
171
}
179
- return singleton ;
180
172
}
181
173
182
- private List < String > walkFileTree (List <String > files , Path path ) {
174
+ private void walkFileTree (List <String > files , Path path ) {
183
175
if (Files .isRegularFile (path )) {
184
176
files .add (path .toString ());
185
177
} else if (Files .isDirectory (path )) try {
@@ -200,7 +192,6 @@ public FileVisitResult visitFileFailed(Path file, IOException e) {
200
192
} else {
201
193
logger .warn ("[Ext Loading] file not found: " + path );
202
194
}
203
- return files ;
204
195
}
205
196
206
197
private void loadDictFile (DictSegment dict , Path file , boolean critical , String name ) {
@@ -225,7 +216,7 @@ private void loadDictFile(DictSegment dict, Path file, boolean critical, String
225
216
}
226
217
}
227
218
228
- public List <String > getExtDictionarys () {
219
+ private List <String > getExtDictionarys () {
229
220
List <String > extDictFiles = new ArrayList <String >(2 );
230
221
String extDictCfg = getProperty (EXT_DICT );
231
222
if (extDictCfg != null ) {
@@ -242,7 +233,7 @@ public List<String> getExtDictionarys() {
242
233
return extDictFiles ;
243
234
}
244
235
245
- public List <String > getRemoteExtDictionarys () {
236
+ private List <String > getRemoteExtDictionarys () {
246
237
List <String > remoteExtDictFiles = new ArrayList <String >(2 );
247
238
String remoteExtDictCfg = getProperty (REMOTE_EXT_DICT );
248
239
if (remoteExtDictCfg != null ) {
@@ -258,7 +249,7 @@ public List<String> getRemoteExtDictionarys() {
258
249
return remoteExtDictFiles ;
259
250
}
260
251
261
- public List <String > getExtStopWordDictionarys () {
252
+ private List <String > getExtStopWordDictionarys () {
262
253
List <String > extStopWordDictFiles = new ArrayList <String >(2 );
263
254
String extStopWordDictCfg = getProperty (EXT_STOP );
264
255
if (extStopWordDictCfg != null ) {
@@ -275,7 +266,7 @@ public List<String> getExtStopWordDictionarys() {
275
266
return extStopWordDictFiles ;
276
267
}
277
268
278
- public List <String > getRemoteExtStopWordDictionarys () {
269
+ private List <String > getRemoteExtStopWordDictionarys () {
279
270
List <String > remoteExtStopWordDictFiles = new ArrayList <String >(2 );
280
271
String remoteExtStopWordDictCfg = getProperty (REMOTE_EXT_STOP );
281
272
if (remoteExtStopWordDictCfg != null ) {
@@ -291,7 +282,7 @@ public List<String> getRemoteExtStopWordDictionarys() {
291
282
return remoteExtStopWordDictFiles ;
292
283
}
293
284
294
- public String getDictRoot () {
285
+ private String getDictRoot () {
295
286
return conf_dir .toAbsolutePath ().toString ();
296
287
}
297
288
@@ -468,25 +459,30 @@ private static List<String> getRemoteWordsUnprivileged(String location) {
468
459
469
460
String charset = "UTF-8" ;
470
461
// 获取编码,默认为utf-8
471
- if (response .getEntity ().getContentType ().getValue ().contains ("charset=" )) {
472
- String contentType = response .getEntity ().getContentType ().getValue ();
473
- charset = contentType .substring (contentType .lastIndexOf ("=" ) + 1 );
474
- }
475
- in = new BufferedReader (new InputStreamReader (response .getEntity ().getContent (), charset ));
476
- String line ;
477
- while ((line = in .readLine ()) != null ) {
478
- buffer .add (line );
479
- }
480
- in .close ();
481
- response .close ();
482
- return buffer ;
462
+ HttpEntity entity = response .getEntity ();
463
+ if (entity !=null ){
464
+ Header contentType = entity .getContentType ();
465
+ if (contentType !=null &&contentType .getValue ()!=null ){
466
+ String typeValue = contentType .getValue ();
467
+ if (typeValue !=null &&typeValue .contains ("charset=" )){
468
+ charset = typeValue .substring (typeValue .lastIndexOf ("=" ) + 1 );
469
+ }
470
+ }
471
+
472
+ if (entity .getContentLength () > 0 ) {
473
+ in = new BufferedReader (new InputStreamReader (entity .getContent (), charset ));
474
+ String line ;
475
+ while ((line = in .readLine ()) != null ) {
476
+ buffer .add (line );
477
+ }
478
+ in .close ();
479
+ response .close ();
480
+ return buffer ;
481
+ }
482
+ }
483
483
}
484
484
response .close ();
485
- } catch (ClientProtocolException e ) {
486
- logger .error ("getRemoteWords {} error" , e , location );
487
- } catch (IllegalStateException e ) {
488
- logger .error ("getRemoteWords {} error" , e , location );
489
- } catch (IOException e ) {
485
+ } catch (IllegalStateException | IOException e ) {
490
486
logger .error ("getRemoteWords {} error" , e , location );
491
487
}
492
488
return buffer ;
@@ -548,24 +544,24 @@ private void loadQuantifierDict() {
548
544
}
549
545
550
546
private void loadSurnameDict () {
551
- _SurnameDict = new DictSegment ((char ) 0 );
547
+ DictSegment _SurnameDict = new DictSegment ((char ) 0 );
552
548
Path file = PathUtils .get (getDictRoot (), Dictionary .PATH_DIC_SURNAME );
553
549
loadDictFile (_SurnameDict , file , true , "Surname" );
554
550
}
555
551
556
552
private void loadSuffixDict () {
557
- _SuffixDict = new DictSegment ((char ) 0 );
553
+ DictSegment _SuffixDict = new DictSegment ((char ) 0 );
558
554
Path file = PathUtils .get (getDictRoot (), Dictionary .PATH_DIC_SUFFIX );
559
555
loadDictFile (_SuffixDict , file , true , "Suffix" );
560
556
}
561
557
562
558
private void loadPrepDict () {
563
- _PrepDict = new DictSegment ((char ) 0 );
559
+ DictSegment _PrepDict = new DictSegment ((char ) 0 );
564
560
Path file = PathUtils .get (getDictRoot (), Dictionary .PATH_DIC_PREP );
565
561
loadDictFile (_PrepDict , file , true , "Preposition" );
566
562
}
567
563
568
- public void reLoadMainDict () {
564
+ void reLoadMainDict () {
569
565
logger .info ("重新加载词典..." );
570
566
// 新开一个实例加载词典,减少加载过程对当前词典使用的影响
571
567
Dictionary tmpDict = new Dictionary (configuration );
0 commit comments