001    /*
002     * The contents of this file are subject to the Mozilla Public
003     * License Version 1.1 (the "License"); you may not use this file
004     * except in compliance with the License. You may obtain a copy of
005     * the License at http://www.mozilla.org/MPL/
006     *
007     * Software distributed under the License is distributed on an "AS
008     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
009     * implied. See the License for the specific language governing
010     * rights and limitations under the License.
011     *
012     * The Original Code is Knowtator.
013     *
014     * The Initial Developer of the Original Code is University of Colorado.  
015     * Copyright (C) 2005-2008.  All Rights Reserved.
016     *
017     * Knowtator was developed by the Center for Computational Pharmacology
018     * (http://compbio.uchcs.edu) at the University of Colorado Health 
019     *  Sciences Center School of Medicine with support from the National 
020     *  Library of Medicine.  
021     *
022     * Current information about Knowtator can be obtained at 
023     * http://knowtator.sourceforge.net/
024     *
025     * Contributor(s):
026     *   Philip V. Ogren <philip@ogren.info> (Original Author)
027     */
028    
029    package edu.uchsc.ccp.iaa.html;
030    
031    import java.io.File;
032    import java.io.IOException;
033    import java.io.PrintStream;
034    import java.text.NumberFormat;
035    import java.util.Map;
036    import java.util.Set;
037    
038    import edu.uchsc.ccp.iaa.Annotation;
039    import edu.uchsc.ccp.iaa.AnnotationSpanIndex;
040    import edu.uchsc.ccp.iaa.IAA;
041    import edu.uchsc.ccp.iaa.matcher.SubclassMatcher;
042    
043    public class SubclassMatcherHTML {
044    
045            public static void printIAA(IAA iaa, SubclassMatcher matcher, File directory, int numberOfDocs,
046                            Map<Annotation, String> annotationTexts, Map<Annotation, String> annotationTextNames) throws IOException {
047                    String fileName = matcher.getName();
048                    PrintStream html = new PrintStream(new File(directory, fileName + ".html"));
049    
050                    NumberFormat percentageFormat = NumberFormat.getPercentInstance();
051                    percentageFormat.setMinimumFractionDigits(2);
052    
053                    html.println(IAA2HTML.initHTML(matcher.getName(), matcher.getDescription()));
054                    html.println("<h2>" + iaa.getSetNames().size() + "-way IAA Results</h2>");
055    
056                    html.println("<p>");
057                    html.println("<table border=1>\n");
058                    html.println("<tr><td><b>IAA</b></td><td><b>matches</b></td><td><b>non-matches</b></td></tr>");
059    
060                    Set<String> sets = iaa.getSetNames();
061    
062                    Map<String, Set<Annotation>> allwayMatches = iaa.getNontrivialAllwayMatches();
063                    Map<String, Set<Annotation>> allwayNonmatches = iaa.getNontrivialAllwayNonmatches();
064    
065                    Set<Annotation> allwayMatchesSingleSet = IAA2HTML.getSingleSet(allwayMatches);
066                    Set<Annotation> allwayNonmatchesSingleSet = IAA2HTML.getSingleSet(allwayNonmatches);
067    
068                    int totalAllwayMatches = allwayMatchesSingleSet.size();
069                    int totalAllwayNonmatches = allwayNonmatchesSingleSet.size();
070    
071                    double iaaScore = (double) totalAllwayMatches / ((double) totalAllwayMatches + (double) totalAllwayNonmatches);
072    
073                    html.println("<tr><td>" + percentageFormat.format(iaaScore) + "</td>" + "<td>" + totalAllwayMatches + "</td>"
074                                    + "<td>" + totalAllwayNonmatches + "</td></tr>");
075                    html.println("</table>");
076    
077                    html.println("<br>IAA = matches / matches + non-matches");
078                    html.println("<br>IAA calculated on " + numberOfDocs + " documents.");
079                    html.println("<br>There are " + (totalAllwayMatches + totalAllwayNonmatches)
080                                    + " annotations with the class or subclass of " + matcher.getIAAClass());
081    
082                    Map<String, Set<Annotation>> nonmatches = iaa.getAllwayNonmatches();
083                    Set<Annotation> nonmatchesSingleSet = IAA2HTML.getSingleSet(nonmatches);
084                    AnnotationSpanIndex spanIndex = new AnnotationSpanIndex(nonmatchesSingleSet);
085    
086                    IAA2HTML.printMatchData(html, sets, fileName, directory, allwayMatches, annotationTexts, annotationTextNames,
087                                    matcher.getSubclasses(), iaa);
088    
089                    IAA2HTML.printNonmatchData(html, sets, fileName, directory, allwayNonmatches, spanIndex, annotationTexts,
090                                    annotationTextNames, matcher.getSubclasses(), iaa);
091    
092                    Map<String, Map<String, Set<Annotation>>> pairwiseMatches = iaa.getNontrivialPairwiseMatches();
093                    Map<String, Map<String, Set<Annotation>>> pairwiseNonmatches = iaa.getNontrivialPairwiseNonmatches();
094    
095                    IAA2HTML.printPairwiseAgreement(html, sets, pairwiseMatches, pairwiseNonmatches, percentageFormat);
096    
097                    html.flush();
098                    html.close();
099    
100            }
101    
102    }