krickert commented on code in PR #1177: URL: https://github.com/apache/opennlp/pull/1177#discussion_r3621744515
########## opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/sentiment/EmojiSentimentContextGenerator.java: ########## @@ -0,0 +1,82 @@ +/* + * 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. + */ + +package opennlp.tools.sentiment; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import opennlp.tools.util.normalizer.EmojiAnnotator; + +/** + * A {@link SentimentContextGenerator} that adds emoji annotation features to the default + * token context: for every annotated token the project-authored coarse sentiment score + * ({@code emojiSentiment=2}), the entity type ({@code emojiType=HEART}), the category + * ({@code emojiCategory=SMILEYS_AND_EMOTION}), and for flags the region ({@code emojiRegion=DE}). + * A crying-face or heart pictograph thereby becomes direct evidence for the sentiment model + * instead of an opaque token. + * + * <p>Strictly opt-in through the {@link SentimentFactory} seam: train with + * {@link EmojiSentimentFactory} (or any factory whose + * {@link SentimentFactory#createContextGenerator()} returns this class) and the same context is + * regenerated at prediction time; the default factory and existing models are unchanged.</p> + * + * @see EmojiSentimentFactory + * @see EmojiAnnotator + */ +public class EmojiSentimentContextGenerator extends SentimentContextGenerator { + + private final EmojiAnnotator annotator; + + /** + * Instantiates a context generator over the bundled and derived annotation layers. + */ + public EmojiSentimentContextGenerator() { + this(new EmojiAnnotator()); + } + + /** + * Instantiates a context generator over a configured annotator, for example one with a + * gazetteer join. + * + * @param annotator The annotator to use. Must not be {@code null}. + * @throws IllegalArgumentException if {@code annotator} is {@code null}. + */ + public EmojiSentimentContextGenerator(EmojiAnnotator annotator) { + if (annotator == null) { + throw new IllegalArgumentException("Annotator must not be null"); + } + this.annotator = annotator; + } + + /** + * {@inheritDoc} + * + * <p>Appends the emoji annotation features of every annotated token to the default token + * context.</p> + */ + @Override + public String[] getContext(String[] text) { + final List<String> context = new ArrayList<>(text.length); Review Comment: done ########## opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/featuregen/EmojiAnnotationFeatureGenerator.java: ########## @@ -0,0 +1,75 @@ +/* + * 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. + */ + +package opennlp.tools.util.featuregen; + +import java.util.List; + +import opennlp.tools.util.normalizer.EmojiAnnotator; + +/** + * Generates features from the emoji annotation layer for the token at the given index: the + * project-authored coarse sentiment score ({@code emojiSentiment=2}), the entity type + * ({@code emojiType=HEART}), the document category ({@code emojiCategory=SMILEYS_AND_EMOTION}), + * and for flags the ISO 3166 region ({@code emojiRegion=DE}). For the name finder the entity + * type behaves like gazetteer evidence: a flag token carries {@code emojiType=FLAG} and its + * region without any dictionary. + * + * <p>Strictly opt-in: this generator only runs when a feature generation descriptor names + * {@link EmojiAnnotationFeatureGeneratorFactory}, so default descriptors and existing models are + * unchanged. A token that is not an annotated symbol contributes no features.</p> + * + * <p>Unlike most feature generators this one is stateless and thread-safe (when its annotator's + * join is): it keeps no adaptive data.</p> + * + * @see EmojiAnnotator + */ +public class EmojiAnnotationFeatureGenerator implements AdaptiveFeatureGenerator { + + private final EmojiAnnotator annotator; + + /** + * Instantiates a generator over the bundled and derived annotation layers. + */ + public EmojiAnnotationFeatureGenerator() { + this(new EmojiAnnotator()); + } + + /** + * Instantiates a generator over a configured annotator, for example one with a gazetteer join. + * + * @param annotator The annotator to use. Must not be {@code null}. + * @throws IllegalArgumentException if {@code annotator} is {@code null}. + */ + public EmojiAnnotationFeatureGenerator(EmojiAnnotator annotator) { + if (annotator == null) { + throw new IllegalArgumentException("Annotator must not be null"); + } + this.annotator = annotator; + } + + /** + * {@inheritDoc} + * + * <p>Adds one feature per present annotation attribute of the token at {@code index}.</p> + */ + @Override + public void createFeatures(List<String> features, String[] tokens, int index, + String[] previousOutcomes) { + annotator.collectFeatures(tokens[index], features); Review Comment: done -- 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]
