/*
* Copyright (C) 2015 Artificial Intelligence
* Laboratory @ University of Udine.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package it.uniud.ailab.dcore.utils;
import it.uniud.ailab.dcore.annotation.annotations.FeatureAnnotation;
import it.uniud.ailab.dcore.annotation.annotations.InferenceAnnotation;
import it.uniud.ailab.dcore.annotation.annotators.WikipediaInferenceAnnotator;
import it.uniud.ailab.dcore.Blackboard;
import it.uniud.ailab.dcore.annotation.Annotation;
import it.uniud.ailab.dcore.annotation.annotators.GenericEvaluatorAnnotator;
import it.uniud.ailab.dcore.persistence.Gram;
import it.uniud.ailab.dcore.persistence.Keyphrase;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Some utilities for operations on the blackboard.
*
* @author Marco
*/
public class BlackboardUtils {
/**
* Prints the scores of an extraction to stdout.
*
* @param b the annotated blackboard
* @param printDetails you can set it to true to print some more information
*/
public static void printScores(Blackboard b, boolean printDetails) {
Collection<Gram> grams = b.getKeyphrases();
System.out.println("");
System.out.println(String.format(
"Extraction completed with a grand total of %d grams distilled.",
grams.size()));
System.out.println("");
System.out.println("** SCORES **");
Map<Keyphrase, Double> scoredGrams = new HashMap<>();
for (Gram g : grams) {
Keyphrase k = (Keyphrase)g;
scoredGrams.put(k, k.getFeature(GenericEvaluatorAnnotator.SCORE));
}
Stream<Map.Entry<Keyphrase, Double>> ordered
= scoredGrams.entrySet().stream().sorted(
Collections.reverseOrder(Map.Entry.comparingByValue())).limit(20);
for (Map.Entry<Keyphrase, Double> scoredGram : ordered.collect(Collectors.toList())) {
System.out.print(String.format("%-24s", scoredGram.getKey().getIdentifier()));
for (FeatureAnnotation f : scoredGram.getKey().getFeatures()) {
System.out.print(String.format("%-12s:%8.3f ; ", f.getAnnotator(), f.getScore()));
}
if (printDetails) {
System.out.println();
System.out.print(String.format("%-24s", "--POS pattern:"));
System.out.print(scoredGram.getKey().getTokens().get(0).getPoS());
for (int i = 1;
i < scoredGram.getKey().getTokens().size();
i++)
{
System.out.print("/" +
scoredGram.getKey().getTokens().get(i).getPoS());
}
}
Annotation[] anns = scoredGram.getKey().getAnnotations();
if (printDetails && anns.length > 0) {
System.out.println();
System.out.print(String.format("%-24s", "--Annotations:"));
for (Annotation a : anns) {
System.out.print(a + " ; ");
}
}
System.out.println();
}
}
/**
* Prints the scores of an extraction to stdout.
*
* @param b the annotated blackboard
*/
public static void printScores(Blackboard b) {
printScores(b,false);
}
/**
* Print the hypernyms generated by an extraction.
*
* @param b the annotated blackboard.
*/
public static void printHypernyms(Blackboard b) {
System.out.println("** HYPERNYMS **");
List<InferenceAnnotation> inferences = new ArrayList<>();
b.getAnnotations(WikipediaInferenceAnnotator.HYPERNYMS)
.stream().forEach(a -> {
inferences.add((InferenceAnnotation) a);
});
Map<String, Double> inferenceMap = new HashMap<>();
for (InferenceAnnotation a : inferences) {
inferenceMap.put(a.getConcept(), a.getScore());
}
// sort the entries
List<Map.Entry<String, Double>> ordered
= inferenceMap.entrySet().stream()
.sorted(
Collections.reverseOrder(Map.Entry.comparingByValue()))
.limit(20)
.collect(Collectors.toList());
for (Map.Entry<String,Double> e : ordered) {
System.out.println(String.format("%-32s:\t%8.3f ; ",
e.getKey(),e.getValue()));
}
}
/**
* Print the related concepts generated by an extraction.
*
* @param b the annotated blackboard.
*/
public static void printRelated(Blackboard b) {
System.out.println("** RELATED CONCEPTS **");
List<InferenceAnnotation> inferences = new ArrayList<>();
b.getAnnotations(WikipediaInferenceAnnotator.RELATED)
.stream().forEach(a -> {
inferences.add((InferenceAnnotation)a);
});
Map<String,Double> inferenceMap = new HashMap<>();
for (InferenceAnnotation a : inferences) {
inferenceMap.put(a.getConcept(),a.getScore());
}
// sort the entries
List<Map.Entry<String, Double>> ordered
= inferenceMap.entrySet().stream()
.sorted(
Collections.reverseOrder(Map.Entry.comparingByValue()))
.limit(20)
.collect(Collectors.toList());
for (Map.Entry<String,Double> e : ordered) {
System.out.println(String.format("%-32s:\t%8.3f ; ",
e.getKey(),e.getValue()));
}
}
/**
* Print the concept inferred by an extraction.
*
* @param b the annotated blackboard.
*/
public static void printInference(Blackboard b) {
printHypernyms(b);
printRelated(b);
}
}