krickert commented on code in PR #1003:
URL: https://github.com/apache/opennlp/pull/1003#discussion_r3059016190
##########
opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/postag/ConfigurablePOSContextGenerator.java:
##########
@@ -91,28 +102,28 @@ public String[] getContext(int index, String[] tokens,
String[] tags,
}
}
- String cacheKey = index + tagprev + tagprevprev;
- if (contextsCache != null) {
- if (wordsKey == tokens) {
- String[] cachedContexts = contextsCache.get(cacheKey);
+ if (threadState != null) {
+ CacheState state = threadState.get();
+ String cacheKey = index + tagprev + tagprevprev;
+ if (state.wordsKey == tokens) {
+ String[] cachedContexts = state.cache.get(cacheKey);
if (cachedContexts != null) {
return cachedContexts;
}
+ } else {
+ state.cache.clear();
+ state.wordsKey = tokens;
}
- else {
- contextsCache.clear();
- wordsKey = tokens;
- }
+
+ List<String> e = new ArrayList<>();
+ featureGenerator.createFeatures(e, tokens, index, tags);
+ String[] contexts = e.toArray(new String[0]);
+ state.cache.put(cacheKey, contexts);
+ return contexts;
}
List<String> e = new ArrayList<>();
-
featureGenerator.createFeatures(e, tokens, index, tags);
-
- String[] contexts = e.toArray(new String[0]);
- if (contextsCache != null) {
- contextsCache.put(cacheKey, contexts);
- }
- return contexts;
+ return e.toArray(new String[0]);
Review Comment:
Refactored this in 7c1772c: there is a small `createContextFeatures` helper
now, and the no-cache path is an explicit `else` branch.
We still have to actually build contexts when caching is off, so returning
`new String[0]` without calling the feature generator would not be right. The
helper keeps both paths honest without duplicating the List plumbing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]