Search in sources :

Example 1 with SimilarityException

use of dkpro.similarity.algorithms.api.SimilarityException in project dkpro-tc by dkpro.

the class SimilarityPairFeatureExtractor method extract.

@Override
public Set<Feature> extract(JCas view1, JCas view2) throws TextClassificationException {
    try {
        double similarity;
        switch(textSimilarityResource.getMode()) {
            case text:
                similarity = textSimilarityResource.getSimilarity(view1.getDocumentText(), view2.getDocumentText());
                break;
            case jcas:
                similarity = ((JCasTextSimilarityMeasure) textSimilarityResource).getSimilarity(view1, view2);
                break;
            default:
                List<String> f1 = getItems(view1);
                List<String> f2 = getItems(view2);
                // Remove "_" tokens
                for (int i = f1.size() - 1; i >= 0; i--) {
                    if (f1.get(i) == null || f1.get(i).equals("_")) {
                        f1.remove(i);
                    }
                }
                for (int i = f2.size() - 1; i >= 0; i--) {
                    if (f2.get(i) == null || f2.get(i).equals("_")) {
                        f2.remove(i);
                    }
                }
                similarity = textSimilarityResource.getSimilarity(f1, f2);
        }
        return new Feature("Similarity" + textSimilarityResource.getName(), similarity, FeatureType.NUMERIC).asSet();
    } catch (FeaturePathException e) {
        throw new TextClassificationException(e);
    } catch (SimilarityException e) {
        throw new TextClassificationException(e);
    }
}
Also used : TextClassificationException(org.dkpro.tc.api.exception.TextClassificationException) FeaturePathException(de.tudarmstadt.ukp.dkpro.core.api.featurepath.FeaturePathException) SimilarityException(dkpro.similarity.algorithms.api.SimilarityException) Feature(org.dkpro.tc.api.features.Feature)

Example 2 with SimilarityException

use of dkpro.similarity.algorithms.api.SimilarityException in project dkpro-tc by dkpro.

the class CosineFeatureExtractor method extract.

@Override
public Set<Feature> extract(JCas view1, JCas view2) throws TextClassificationException {
    try {
        TextClassificationTarget aTarget1 = JCasUtil.selectSingle(view1, TextClassificationTarget.class);
        TextClassificationTarget aTarget2 = JCasUtil.selectSingle(view2, TextClassificationTarget.class);
        // Note: getSimilarity(String, String) is *not* a convenience
        // method for getSimilarity(Collection<String>, Collection<String>).
        Set<String> text1 = NGramUtils.getDocumentNgrams(view1, aTarget1, true, false, 1, 1, stopwords, ngramAnnotationType).getKeys();
        Set<String> text2 = NGramUtils.getDocumentNgrams(view2, aTarget2, true, false, 1, 1, stopwords, ngramAnnotationType).getKeys();
        double similarity = measure.getSimilarity(text1, text2);
        // Temporary fix for DKPro Similarity Issue 30
        if (Double.isNaN(similarity)) {
            similarity = 0.0;
        }
        return new Feature("Similarity" + measure.getName(), similarity, FeatureType.NUMERIC).asSet();
    } catch (SimilarityException e) {
        throw new TextClassificationException(e);
    }
}
Also used : TextClassificationException(org.dkpro.tc.api.exception.TextClassificationException) TextClassificationTarget(org.dkpro.tc.api.type.TextClassificationTarget) SimilarityException(dkpro.similarity.algorithms.api.SimilarityException) Feature(org.dkpro.tc.api.features.Feature)

Aggregations

SimilarityException (dkpro.similarity.algorithms.api.SimilarityException)2 TextClassificationException (org.dkpro.tc.api.exception.TextClassificationException)2 Feature (org.dkpro.tc.api.features.Feature)2 FeaturePathException (de.tudarmstadt.ukp.dkpro.core.api.featurepath.FeaturePathException)1 TextClassificationTarget (org.dkpro.tc.api.type.TextClassificationTarget)1