de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.morpho.POSNgram.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.morpho.POSNgram.java

Source

/*
 * Copyright 2016
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universitt Darmstadt
 *
 * Licensed 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 de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.morpho;

import de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.AbstractUnitSentenceFeatureGenerator;
import de.tudarmstadt.ukp.dkpro.core.api.frequency.util.FrequencyDistribution;
import de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;
import de.tudarmstadt.ukp.dkpro.core.ngrams.util.NGramStringListIterable;
import de.tudarmstadt.ukp.dkpro.tc.api.exception.TextClassificationException;
import de.tudarmstadt.ukp.dkpro.tc.api.features.Feature;
import de.tudarmstadt.ukp.dkpro.tc.features.ngram.util.NGramUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;

import java.util.ArrayList;
import java.util.List;

/**
 * Extract POS-n-grams for classification units. Does not treat them binary, but emits the
 * actual counts.
 *
 * @author Ivan Habernal
 */
public class POSNgram extends AbstractUnitSentenceFeatureGenerator {

    public static final String FEATURE_NAME = "_pos_ngram_";

    @Override
    protected List<Feature> extract(JCas jCas, Sentence sentence, String sentencePrefix)
            throws TextClassificationException {
        List<Feature> result = new ArrayList<>();
        // extract post n-grams
        FrequencyDistribution<String> documentPOSNGrams = getSentencePosNGrams(jCas, 1, 3, true, sentence);

        for (String posNGram : documentPOSNGrams.getKeys()) {
            // we use sparse vectors here
            result.add(new Feature(sentencePrefix + FEATURE_NAME + posNGram, documentPOSNGrams.getCount(posNGram)));
        }
        return result;
    }

    public static FrequencyDistribution<String> getSentencePosNGrams(JCas jcas, int minN, int maxN,
            boolean useCanonical, Sentence Sentence) {
        FrequencyDistribution<String> result = new FrequencyDistribution<>();

        List<String> posTagString = new ArrayList<>();
        for (POS p : JCasUtil.selectCovered(jcas, POS.class, Sentence)) {
            if (useCanonical) {
                posTagString.add(p.getClass().getSimpleName());
            } else {
                posTagString.add(p.getPosValue());
            }
        }
        String[] posAsArray = posTagString.toArray(new String[posTagString.size()]);

        for (List<String> nGram : new NGramStringListIterable(posAsArray, minN, maxN)) {
            result.inc(StringUtils.join(nGram, NGramUtils.NGRAM_GLUE));

        }

        return result;
    }

}