Skip to content

Commit

Permalink
fix[en]: run expensive initalizations only once in UpperCaseNgramRule (
Browse files Browse the repository at this point in the history
…#9545)

* fix[en]: run expensive initalizations only once in UpperCaseNgramRule

* fix: initialization of UpperCaseNgramRule
  • Loading branch information
fabrichter authored Oct 26, 2023
1 parent 5c777a4 commit 5a9de2a
Showing 1 changed file with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
public class UpperCaseNgramRule extends Rule {

public static final int THRESHOLD = 50;
private static MorfologikAmericanSpellerRule spellerRule;
private static MorfologikAmericanSpellerRule spellerRule = null;
private static LinguServices linguServices = null;
private static final Set<String> exceptions = new HashSet<>(Arrays.asList(
"Bin", "Spot", // names
"Go", // common usage, as in "Go/No Go decision"
"French", "Roman", "Hawking", "Square", "Japan", "Premier", "Allied"
));
private static final AhoCorasickDoubleArrayTrie<String> exceptionTrie = new AhoCorasickDoubleArrayTrie<>();
private static AhoCorasickDoubleArrayTrie<String> exceptionTrie = null;
private static final List<List<PatternToken>> ANTI_PATTERNS = Arrays.asList(
Arrays.asList(
token("Hugs"), token("and"), token("Kisses")
Expand Down Expand Up @@ -516,31 +516,43 @@ public UpperCaseNgramRule(ResourceBundle messages, LanguageModel lm, Language la
Example.fixed("This <marker>prototype</marker> was developed by Miller et al."));
antiPatterns = cacheAntiPatterns(lang, ANTI_PATTERNS);

initTrie();
initSpeller();
if (userConfig != null && linguServices == null) {
linguServices = userConfig.getLinguServices();
initTrie();
}
}

private void initSpeller() {
if (spellerRule == null) {
initTrie();
try {
spellerRule = new MorfologikAmericanSpellerRule(messages, lang);
} catch (IOException e) {
throw new RuntimeException(e);
synchronized (UpperCaseNgramRule.class) {
if (spellerRule == null) {
try {
spellerRule = new MorfologikAmericanSpellerRule(messages, lang);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}

private void initTrie() {
CachingWordListLoader cachingWordListLoader = new CachingWordListLoader();
List<String> words = new ArrayList<>();
words.addAll(cachingWordListLoader.loadWords("en/specific_case.txt"));
words.addAll(cachingWordListLoader.loadWords("spelling_global.txt"));
Map<String,String> map = new HashMap<>();
for (String word : words) {
map.put(word, word);
}
synchronized (exceptionTrie) {
exceptionTrie.build(map);
if (exceptionTrie == null) {
synchronized (UpperCaseNgramRule.class) {
if (exceptionTrie == null) {
exceptionTrie = new AhoCorasickDoubleArrayTrie<>();
CachingWordListLoader cachingWordListLoader = new CachingWordListLoader();
List<String> words = new ArrayList<>();
words.addAll(cachingWordListLoader.loadWords("en/specific_case.txt"));
words.addAll(cachingWordListLoader.loadWords("spelling_global.txt"));
Map<String,String> map = new HashMap<>();
for (String word : words) {
map.put(word, word);
}
exceptionTrie.build(map);
}
}
}
}

Expand Down

0 comments on commit 5a9de2a

Please sign in to comment.