Skip to content

Commit b5cb336

Browse files
committed
committing StopWords class
1 parent 108cb68 commit b5cb336

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.logicaldoc.core.i18n;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.StringReader;
6+
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.HashSet;
9+
import java.util.Locale;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import com.logicaldoc.util.LocaleUtil;
17+
import com.logicaldoc.util.io.ResourceUtil;
18+
19+
/**
20+
* Class for retrieving the words from the stopwords files
21+
*
22+
* @author Giuseppe Desiato - LogicalDOC
23+
* @since 9.1.1
24+
*/
25+
public abstract class StopWords {
26+
27+
protected static Logger log = LoggerFactory.getLogger(StopWords.class);
28+
29+
private static final Map<Locale, Set<String>> cacheMap = new HashMap<>();
30+
31+
private StopWords() {
32+
// do nothing
33+
}
34+
35+
public static Set<String> getStopWords(String locale) {
36+
return cacheMap.computeIfAbsent(LocaleUtil.toLocale(locale), StopWords::loadStopWords);
37+
}
38+
39+
public static Set<String> getStopWords(Locale locale) {
40+
return cacheMap.computeIfAbsent(locale, StopWords::loadStopWords);
41+
}
42+
43+
private static Set<String> loadStopWords(Locale locale) {
44+
String resource = "/stopwords/stopwords_" + locale + ".txt";
45+
if (!ResourceUtil.existsResource(resource))
46+
resource = "/stopwords/stopwords_" + locale.getLanguage() + ".txt";
47+
if (!ResourceUtil.existsResource(resource))
48+
resource = "/stopwords/stopwords_en.txt";
49+
log.debug("Loading stopwords from: {}", resource);
50+
try (BufferedReader reader = new BufferedReader(new StringReader(ResourceUtil.readAsString(resource)));) {
51+
String line = "";
52+
Set<String> words = new HashSet<>();
53+
while ((line = reader.readLine()) != null)
54+
words.add(line.trim());
55+
return words;
56+
} catch (IOException e) {
57+
log.warn(e.getMessage(), e);
58+
return Collections.emptySet();
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)