Search in sources :

Example 1 with AnnotationSet

use of gate.AnnotationSet in project gate-core by GateNLP.

the class LuceneDocument method createDocuments.

/**
 * Given an instance of Gate Document, it converts it into the format that
 * lucene can understand and can store in its indexes. This method also stores
 * the tokenStream on the disk in order to retrieve it at the time of
 * searching
 */
public List<Document> createDocuments(String corpusPersistenceID, gate.Document gateDoc, String documentID, List<String> annotSetsToInclude, List<String> annotSetsToExclude, List<String> featuresToInclude, List<String> featuresToExclude, String indexLocation, String baseTokenAnnotationType, Boolean createTokensAutomatically, String indexUnitAnnotationType) {
    if (baseTokenAnnotationType != null)
        baseTokenAnnotationType = baseTokenAnnotationType.trim();
    List<Document> toReturnBack = new ArrayList<Document>();
    List<String> annotSetsToIndex = new ArrayList<String>();
    // about annotation sets to exclude
    if (annotSetsToInclude.size() > 0) {
        annotSetsToIndex = annotSetsToInclude;
    // if there's only one annotation to index, we don't need to
    // create a MergeSet
    // if(annotSetsToIndex.size() == 1) createMergeSet = false;
    } else if (annotSetsToExclude.size() > 0) {
        // if there were no annotation sets to include, check if user has
        // provided any annotation sets to exclude
        // if so, we need to index all annotation sets but provided in the
        // annotationsetstoexclude list
        Set<String> namedAnnotSets = new HashSet<String>();
        if (gateDoc.getNamedAnnotationSets() != null && gateDoc.getNamedAnnotationSets().keySet() != null) {
            namedAnnotSets = gateDoc.getNamedAnnotationSets().keySet();
        }
        for (String setName : namedAnnotSets) {
            if (annotSetsToExclude.contains(setName))
                continue;
            annotSetsToIndex.add(setName);
        }
        if (!annotSetsToExclude.contains(Constants.DEFAULT_ANNOTATION_SET_NAME)) {
            annotSetsToIndex.add(Constants.DEFAULT_ANNOTATION_SET_NAME);
        }
    } else {
        // if both annotation sets to include and annotation sets to
        // exclude are empty
        // we need to index all annotation sets
        Set<String> namedAnnotSets = new HashSet<String>();
        if (gateDoc.getNamedAnnotationSets() != null && gateDoc.getNamedAnnotationSets().keySet() != null) {
            namedAnnotSets = gateDoc.getNamedAnnotationSets().keySet();
        }
        for (String setName : namedAnnotSets) {
            annotSetsToIndex.add(setName);
        }
        annotSetsToIndex.add(Constants.DEFAULT_ANNOTATION_SET_NAME);
    }
    // lets find out the annotation set that contains tokens in it
    AnnotationSet baseTokenAnnotationSet = null;
    // search in annotation sets to find out which of them has the
    // baseTokenAnnotationType annotations
    // initially this is set to false
    boolean searchBaseTokensInAllAnnotationSets = false;
    boolean searchIndexUnitInAllAnnotationSets = false;
    // this variable tells whether we want to create manual tokens or
    // not
    boolean createManualTokens = false;
    // lets check if user's input is setName.basetokenAnnotationType
    int index = -1;
    if (baseTokenAnnotationType != null && baseTokenAnnotationType.length() > 0)
        index = baseTokenAnnotationType.lastIndexOf('.');
    // basetokenAnnotationType
    if (index >= 0) {
        // set name
        String setName = baseTokenAnnotationType.substring(0, index);
        // token type
        baseTokenAnnotationType = baseTokenAnnotationType.substring(index + 1, baseTokenAnnotationType.length());
        // annotation set
        if (setName.equals(Constants.DEFAULT_ANNOTATION_SET_NAME))
            baseTokenAnnotationSet = gateDoc.getAnnotations().get(baseTokenAnnotationType);
        else
            baseTokenAnnotationSet = gateDoc.getAnnotations(setName).get(baseTokenAnnotationType);
        // base token annotation type
        if (baseTokenAnnotationSet == null || baseTokenAnnotationSet.size() == 0) {
            System.err.println("Base Tokens " + baseTokenAnnotationType + " counldn't be found under the specified annotation set " + setName + "\n searching them in other annotation sets");
            searchBaseTokensInAllAnnotationSets = true;
        }
    } else {
        // either baseTokenAnnotation type is null or user hasn't provided
        // any annotaiton set name
        // so we search in all annotation sets
        searchBaseTokensInAllAnnotationSets = true;
    }
    if (baseTokenAnnotationType != null && baseTokenAnnotationType.length() > 0 && searchBaseTokensInAllAnnotationSets) {
        // we set this to true and if we find basetokens in any of the
        // annotationsets to index
        // we will set this to false
        createManualTokens = true;
        for (String aSet : annotSetsToIndex) {
            if (aSet.equals(Constants.DEFAULT_ANNOTATION_SET_NAME)) {
                AnnotationSet tempSet = gateDoc.getAnnotations().get(baseTokenAnnotationType);
                if (tempSet.size() > 0) {
                    baseTokenAnnotationSet = tempSet;
                    // System.out.println("found in default annotation set");
                    createManualTokens = false;
                    break;
                }
            } else {
                AnnotationSet tempSet = gateDoc.getAnnotations(aSet).get(baseTokenAnnotationType);
                if (tempSet.size() > 0) {
                    baseTokenAnnotationSet = tempSet;
                    // System.out.println("found in "+aSet);
                    createManualTokens = false;
                    break;
                }
            }
        }
    }
    // we'll have to create tokens ourselves
    if (baseTokenAnnotationType == null || baseTokenAnnotationType.length() == 0)
        createManualTokens = true;
    // lets check if we have to create ManualTokens
    if (createManualTokens) {
        if (!createTokensAutomatically.booleanValue()) {
            System.out.println("Tokens couldn't be found in the document - Ignoring the document " + gateDoc.getName());
            return null;
        }
        baseTokenAnnotationType = Constants.ANNIC_TOKEN;
        if (baseTokenAnnotationSet == null) {
            baseTokenAnnotationSet = new AnnotationSetImpl(gateDoc);
        }
        if (!createTokens(gateDoc, baseTokenAnnotationSet)) {
            System.out.println("Tokens couldn't be created manually - Ignoring the document " + gateDoc.getName());
            return null;
        }
    }
    // by now, baseTokenAnnotationSet will not be null for sure and we
    // know what's the baseTokenAnnotationType
    // lets find out the annotation set that contains
    // indexUnitAnnotationType in it
    AnnotationSet indexUnitAnnotationSet = null;
    // lets check if user has provided setName.indexUnitAnnotationType
    index = -1;
    if (indexUnitAnnotationType != null && indexUnitAnnotationType.trim().length() > 0)
        index = indexUnitAnnotationType.lastIndexOf('.');
    // indexUnitAnnotationType
    if (index >= 0) {
        // setName
        String setName = indexUnitAnnotationType.substring(0, index);
        // indexUnitAnnotationType
        indexUnitAnnotationType = indexUnitAnnotationType.substring(index + 1, indexUnitAnnotationType.length());
        if (setName.equals(Constants.DEFAULT_ANNOTATION_SET_NAME))
            indexUnitAnnotationSet = gateDoc.getAnnotations().get(indexUnitAnnotationType);
        else
            indexUnitAnnotationSet = gateDoc.getAnnotations(setName).get(indexUnitAnnotationType);
        // if so, we'll have to search other annotation sets
        if (indexUnitAnnotationSet == null || indexUnitAnnotationSet.size() == 0) {
            System.err.println("Index Unit " + indexUnitAnnotationType + " counldn't be found under the specified annotation set " + setName + "\n searching them in other annotation sets");
            searchIndexUnitInAllAnnotationSets = true;
        }
    } else {
        // either indexUnitAnnotationType is null or user hasn't provided
        // the setname
        searchIndexUnitInAllAnnotationSets = true;
    }
    // searching in all annotation set names
    if (indexUnitAnnotationType != null && indexUnitAnnotationType.length() > 0 && searchIndexUnitInAllAnnotationSets) {
        for (String aSet : annotSetsToIndex) {
            if (aSet.equals(Constants.DEFAULT_ANNOTATION_SET_NAME)) {
                AnnotationSet tempSet = gateDoc.getAnnotations().get(indexUnitAnnotationType);
                if (tempSet.size() > 0) {
                    indexUnitAnnotationSet = tempSet;
                    break;
                }
            } else {
                AnnotationSet tempSet = gateDoc.getAnnotations(aSet).get(indexUnitAnnotationType);
                if (tempSet.size() > 0) {
                    indexUnitAnnotationSet = tempSet;
                    break;
                }
            }
        }
    }
    // to null as well
    if (indexUnitAnnotationSet == null) {
        indexUnitAnnotationType = null;
    }
    int j = 0;
    for (String annotSet : annotSetsToIndex) {
        // we need to generate the Token Stream here, and send it to the
        // GateLuceneReader
        AnnotationSet aSetToIndex = annotSet.equals(Constants.DEFAULT_ANNOTATION_SET_NAME) ? gateDoc.getAnnotations() : gateDoc.getAnnotations(annotSet);
        Set<String> indexedFeatures = new HashSet<String>();
        // tempBaseTokenAnnotationSet is not null
        List<Token>[] tokenStreams = getTokens(gateDoc, aSetToIndex, featuresToInclude, featuresToExclude, baseTokenAnnotationType, baseTokenAnnotationSet, indexUnitAnnotationType, indexUnitAnnotationSet, indexedFeatures);
        // tokenStream is set to null
        if (tokenStreams == null)
            return null;
        // this is enabled only if there are more than one annotation sets
        // available to search in
        // if(createMergeSet) {
        // if(mergedSet == null) mergedSet = new AnnotationSetImpl(gateDoc);
        // 
        // // we need to merge all annotations but the
        // // baseTokenAnnotationType
        // for(String aType : aSetToIndex.getAllTypes()) {
        // 
        // if(aType.equals(baseTokenAnnotationType)) {
        // continue;
        // }
        // 
        // if(indexUnitAnnotationType != null
        // && aType.equals(indexUnitAnnotationType)) {
        // continue;
        // }
        // 
        // for(Annotation a : aSetToIndex.get(aType)) {
        // try {
        // mergedSet.add(a.getStartNode().getOffset(), a.getEndNode()
        // .getOffset(), a.getType(), a.getFeatures());
        // }
        // catch(InvalidOffsetException ioe) {
        // throw new GateRuntimeException(ioe);
        // }
        // }
        // 
        // }
        // }
        StringBuffer indexedFeaturesString = new StringBuffer();
        for (String aFeat : indexedFeatures) {
            indexedFeaturesString.append(aFeat + ";");
        }
        Document[] toReturn = new Document[tokenStreams.length];
        for (int i = 0; i < tokenStreams.length; i++, j++) {
            // make a new, empty document
            Document doc = new Document();
            // and then create the document
            LuceneReader reader = new LuceneReader(gateDoc, tokenStreams[i]);
            doc.add(Field.Keyword(Constants.DOCUMENT_ID, documentID));
            doc.add(Field.Keyword(Constants.DOCUMENT_ID_FOR_SERIALIZED_FILE, documentID + "-" + j));
            doc.add(Field.Keyword(Constants.INDEXED_FEATURES, indexedFeaturesString.substring(0, indexedFeaturesString.length() - 1)));
            if (corpusPersistenceID != null)
                doc.add(Field.Keyword(Constants.CORPUS_ID, corpusPersistenceID));
            doc.add(Field.Keyword(Constants.ANNOTATION_SET_ID, annotSet));
            doc.add(Field.Text("contents", reader));
            // here we store token stream on the file system
            try {
                writeOnDisk(tokenStreams[i], documentID, documentID + "-" + j, indexLocation);
            } catch (Exception e) {
                Err.println("\nIgnoring the document : " + gateDoc.getName() + " since its token stream cannot be written on the disk");
                Err.println("Reason: " + e.getMessage());
                return null;
            }
            // return the document
            toReturn[i] = doc;
        }
        toReturnBack.addAll(Arrays.asList(toReturn));
    }
    return toReturnBack;
}
Also used : HashSet(java.util.HashSet) AnnotationSet(gate.AnnotationSet) Set(java.util.Set) ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Document(gate.creole.annic.apache.lucene.document.Document) InvalidOffsetException(gate.util.InvalidOffsetException) GateRuntimeException(gate.util.GateRuntimeException) IOException(java.io.IOException) AnnotationSetImpl(gate.annotation.AnnotationSetImpl) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with AnnotationSet

use of gate.AnnotationSet in project gate-core by GateNLP.

the class CorefEditor method initGUI.

/**
 * This method intiates the GUI for co-reference editor
 */
@Override
protected void initGUI() {
    // get a pointer to the textual view used for highlights
    Iterator<DocumentView> centralViewsIter = owner.getCentralViews().iterator();
    while (textView == null && centralViewsIter.hasNext()) {
        DocumentView aView = centralViewsIter.next();
        if (aView instanceof TextualDocumentView)
            textView = (TextualDocumentView) aView;
    }
    textPane = (JTextArea) ((JScrollPane) textView.getGUI()).getViewport().getView();
    highlighter = textPane.getHighlighter();
    chainToolTipAction = new ChainToolTipAction();
    chainToolTipTimer = new javax.swing.Timer(500, chainToolTipAction);
    chainToolTipTimer.setRepeats(false);
    newCorefAction = new NewCorefAction();
    newCorefActionTimer = new javax.swing.Timer(500, newCorefAction);
    newCorefActionTimer.setRepeats(false);
    colorGenerator = new ColorGenerator();
    // main Panel
    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    // topPanel
    topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    // subPanel
    subPanel = new JPanel();
    subPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    // showAnnotations Button
    showAnnotations = new JToggleButton("Show");
    showAnnotations.addActionListener(this);
    // annotSets
    annotSets = new JComboBox<String>();
    annotSets.addActionListener(this);
    // get all the annotationSets
    Map<String, AnnotationSet> annotSetsMap = document.getNamedAnnotationSets();
    annotSetsModel = new DefaultComboBoxModel<String>();
    if (annotSetsMap != null) {
        String[] array = annotSetsMap.keySet().toArray(new String[annotSetsMap.keySet().size()]);
        for (int i = 0; i < array.length; i++) {
            annotSetsMap.get(array[i]).addAnnotationSetListener(this);
        }
        annotSetsModel = new DefaultComboBoxModel<String>(array);
    }
    document.getAnnotations().addAnnotationSetListener(this);
    annotSetsModel.insertElementAt(DEFAULT_ANNOTSET_NAME, 0);
    annotSets.setModel(annotSetsModel);
    // annotTypes
    annotTypesModel = new DefaultComboBoxModel<String>();
    annotTypes = new JComboBox<String>(annotTypesModel);
    annotTypes.addActionListener(this);
    subPanel.add(new JLabel("Sets : "));
    subPanel.add(annotSets);
    JPanel tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    tempPanel.add(new JLabel("Types : "));
    tempPanel.add(annotTypes);
    tempPanel.add(showAnnotations);
    // intialises the Data
    initData();
    // and creating the tree
    corefTree = new JTree(rootNode);
    corefTree.putClientProperty("JTree.lineStyle", "None");
    corefTree.setRowHeight(corefTree.getRowHeight() * 2);
    corefTree.setLargeModel(true);
    corefTree.setAutoscrolls(true);
    // corefTree.setRootVisible(false);
    // corefTree.setShowsRootHandles(false);
    corefTree.addMouseListener(new CorefTreeMouseListener());
    corefTree.setCellRenderer(new CorefTreeCellRenderer());
    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(new JScrollPane(corefTree), BorderLayout.CENTER);
    topPanel.add(subPanel, BorderLayout.CENTER);
    topPanel.add(tempPanel, BorderLayout.SOUTH);
    // get the highlighter
    textPaneMouseListener = new TextPaneMouseListener();
    annotSets.setSelectedIndex(0);
    // finally show the tree
    // annotSetSelectionChanged();
    document.addDocumentListener(this);
    document.getFeatures().addFeatureMapListener(this);
}
Also used : JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) AnnotationSet(gate.AnnotationSet) BorderLayout(java.awt.BorderLayout) JToggleButton(javax.swing.JToggleButton) JScrollPane(javax.swing.JScrollPane) ColorGenerator(gate.swing.ColorGenerator) JLabel(javax.swing.JLabel) Point(java.awt.Point) JTree(javax.swing.JTree)

Example 3 with AnnotationSet

use of gate.AnnotationSet in project gate-core by GateNLP.

the class CorpusQualityAssurance method readSetsTypesFeatures.

/**
 * Update set lists.
 * @param documentStart first document to read in the corpus,
 * the first document of the corpus is 0.
 */
protected void readSetsTypesFeatures(final int documentStart) {
    if (!isShowing()) {
        return;
    }
    corpusChanged = false;
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            progressBar.setMaximum(corpus.size() - 1);
            progressBar.setString("Read sets, types, features");
            reloadCacheAction.setEnabled(false);
        }
    });
    CorpusQualityAssurance.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (docsSetsTypesFeatures.size() != corpus.getDocumentNames().size() || !docsSetsTypesFeatures.keySet().containsAll(corpus.getDocumentNames())) {
                if (documentStart == 0) {
                    docsSetsTypesFeatures.clear();
                }
                TreeMap<String, TreeMap<String, TreeSet<String>>> setsTypesFeatures;
                TreeMap<String, TreeSet<String>> typesFeatures;
                TreeSet<String> features;
                for (int i = documentStart; i < corpus.size(); i++) {
                    // fill in the lists of document, set, type and feature names
                    boolean documentWasLoaded = corpus.isDocumentLoaded(i);
                    Document document = corpus.get(i);
                    if (document != null && document.getAnnotationSetNames() != null) {
                        setsTypesFeatures = new TreeMap<String, TreeMap<String, TreeSet<String>>>(collator);
                        HashSet<String> setNames = new HashSet<String>(document.getAnnotationSetNames());
                        setNames.add("");
                        for (String set : setNames) {
                            typesFeatures = new TreeMap<String, TreeSet<String>>(collator);
                            AnnotationSet annotations = document.getAnnotations(set);
                            for (String type : annotations.getAllTypes()) {
                                features = new TreeSet<String>(collator);
                                for (Annotation annotation : annotations.get(type)) {
                                    for (Object featureKey : annotation.getFeatures().keySet()) {
                                        features.add((String) featureKey);
                                    }
                                }
                                typesFeatures.put(type, features);
                            }
                            setsTypesFeatures.put(set, typesFeatures);
                        }
                        docsSetsTypesFeatures.put(document.getName(), setsTypesFeatures);
                    }
                    if (!documentWasLoaded) {
                        corpus.unloadDocument(document);
                        Factory.deleteResource(document);
                    }
                    final int progressValue = i + 1;
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            progressBar.setValue(progressValue);
                            if ((progressValue + 1) % 5 == 0) {
                                // update the set list every 5 documents read
                                updateSetList();
                            }
                        }
                    });
                    if (Thread.interrupted()) {
                        return;
                    }
                }
            }
            updateSetList();
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    progressBar.setValue(progressBar.getMinimum());
                    progressBar.setString("");
                    CorpusQualityAssurance.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    reloadCacheAction.setEnabled(true);
                }
            });
        }
    };
    readSetsTypesFeaturesThread = new Thread(runnable, "readSetsTypesFeatures");
    readSetsTypesFeaturesThread.setPriority(Thread.MIN_PRIORITY);
    readSetsTypesFeaturesThread.start();
}
Also used : AnnotationSet(gate.AnnotationSet) TreeMap(java.util.TreeMap) Document(gate.Document) Annotation(gate.Annotation) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet)

Example 4 with AnnotationSet

use of gate.AnnotationSet in project gate-core by GateNLP.

the class CorpusQualityAssurance method compareAnnotation.

protected void compareAnnotation() {
    int progressValuePrevious = -1;
    if (readSetsTypesFeaturesThread != null && readSetsTypesFeaturesThread.isAlive()) {
        // stop the thread that reads the sets, types and features
        progressValuePrevious = progressBar.getValue();
        readSetsTypesFeaturesThread.interrupt();
    }
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            progressBar.setMaximum(corpus.size() - 1);
            progressBar.setString("Compare annotations");
            setList.setEnabled(false);
            setCheck.setEnabled(false);
            typeList.setEnabled(false);
            typeCheck.setEnabled(false);
            featureList.setEnabled(false);
            featureCheck.setEnabled(false);
            optionsButton.setEnabled(false);
            measureTabbedPane.setEnabled(false);
            measureList.setEnabled(false);
            exportToHtmlAction.setEnabled(false);
            reloadCacheAction.setEnabled(false);
        }
    });
    boolean useBdm = false;
    if (measuresType == FSCORE_MEASURES) {
        differsByDocThenType.clear();
        documentNames.clear();
        for (Object measure : measureList.getSelectedValues()) {
            if (((String) measure).contains("BDM")) {
                useBdm = true;
                break;
            }
        }
    }
    List<ClassificationMeasures> classificationMeasuresList = new ArrayList<ClassificationMeasures>();
    List<OntologyMeasures> documentOntologyMeasuresList = new ArrayList<OntologyMeasures>();
    List<OntologyMeasures> annotationOntologyMeasuresList = new ArrayList<OntologyMeasures>();
    // for each document
    for (int row = 0; row < corpus.size(); row++) {
        boolean documentWasLoaded = corpus.isDocumentLoaded(row);
        Document document = corpus.get(row);
        documentNames.add(document.getName());
        Set<Annotation> keys = new HashSet<Annotation>();
        Set<Annotation> responses = new HashSet<Annotation>();
        // get annotations from selected annotation sets
        if (keySetName.equals("[Default set]")) {
            keys = document.getAnnotations();
        } else if (document.getAnnotationSetNames() != null && document.getAnnotationSetNames().contains(keySetName)) {
            keys = document.getAnnotations(keySetName);
        }
        if (responseSetName.equals("[Default set]")) {
            responses = document.getAnnotations();
        } else if (document.getAnnotationSetNames() != null && document.getAnnotationSetNames().contains(responseSetName)) {
            responses = document.getAnnotations(responseSetName);
        }
        if (!documentWasLoaded) {
            // in case of datastore
            corpus.unloadDocument(document);
            Factory.deleteResource(document);
        }
        // add data to the fscore document table
        if (measuresType == FSCORE_MEASURES) {
            types.clear();
            for (Object type : typeList.getSelectedValues()) {
                types.add((String) type);
            }
            if (typeList.isSelectionEmpty()) {
                for (int i = 0; i < typeList.getModel().getSize(); i++) {
                    types.add((String) typeList.getModel().getElementAt(i));
                }
            }
            Set<String> featureSet = new HashSet<String>();
            for (Object feature : featureList.getSelectedValues()) {
                featureSet.add((String) feature);
            }
            HashMap<String, AnnotationDiffer> differsByType = new HashMap<String, AnnotationDiffer>();
            AnnotationDiffer differ;
            Set<Annotation> keysIter = new HashSet<Annotation>();
            Set<Annotation> responsesIter = new HashSet<Annotation>();
            for (String type : types) {
                if (!keys.isEmpty() && !types.isEmpty()) {
                    keysIter = ((AnnotationSet) keys).get(type);
                }
                if (!responses.isEmpty() && !types.isEmpty()) {
                    responsesIter = ((AnnotationSet) responses).get(type);
                }
                differ = new AnnotationDiffer();
                differ.setSignificantFeaturesSet(featureSet);
                // compare
                differ.calculateDiff(keysIter, responsesIter);
                differsByType.put(type, differ);
            }
            differsByDocThenType.add(differsByType);
            differ = new AnnotationDiffer(differsByType.values());
            List<String> measuresRow;
            if (useBdm) {
                OntologyMeasures ontologyMeasures = new OntologyMeasures();
                ontologyMeasures.setBdmFile(bdmFileUrl);
                ontologyMeasures.calculateBdm(differsByType.values());
                documentOntologyMeasuresList.add(ontologyMeasures);
                measuresRow = ontologyMeasures.getMeasuresRow(measureList.getSelectedValues(), documentNames.get(documentNames.size() - 1));
            } else {
                measuresRow = differ.getMeasuresRow(measureList.getSelectedValues(), documentNames.get(documentNames.size() - 1));
            }
            documentTableModel.addRow(measuresRow.toArray());
        // add data to the classification document table
        } else if (measuresType == CLASSIFICATION_MEASURES && !keys.isEmpty() && !responses.isEmpty()) {
            ClassificationMeasures classificationMeasures = new ClassificationMeasures();
            classificationMeasures.calculateConfusionMatrix((AnnotationSet) keys, (AnnotationSet) responses, (String) typeList.getSelectedValue(), (String) featureList.getSelectedValue(), verboseOptionCheckBox.isSelected());
            classificationMeasuresList.add(classificationMeasures);
            List<String> measuresRow = classificationMeasures.getMeasuresRow(measure2List.getSelectedValues(), documentNames.get(documentNames.size() - 1));
            document2TableModel.addRow(measuresRow.toArray());
            List<List<String>> matrix = classificationMeasures.getConfusionMatrix(documentNames.get(documentNames.size() - 1));
            for (List<String> matrixRow : matrix) {
                while (confusionTableModel.getColumnCount() < matrix.size()) {
                    confusionTableModel.addColumn(" ");
                }
                confusionTableModel.addRow(matrixRow.toArray());
            }
        }
        final int progressValue = row + 1;
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                progressBar.setValue(progressValue);
            }
        });
    }
    // add data to the fscore annotation table
    if (measuresType == FSCORE_MEASURES) {
        for (String type : types) {
            ArrayList<AnnotationDiffer> differs = new ArrayList<AnnotationDiffer>();
            for (HashMap<String, AnnotationDiffer> differsByType : differsByDocThenType) {
                differs.add(differsByType.get(type));
            }
            List<String> measuresRow;
            if (useBdm) {
                OntologyMeasures ontologyMeasures = new OntologyMeasures();
                ontologyMeasures.setBdmFile(bdmFileUrl);
                ontologyMeasures.calculateBdm(differs);
                annotationOntologyMeasuresList.add(ontologyMeasures);
                measuresRow = ontologyMeasures.getMeasuresRow(measureList.getSelectedValues(), type);
            } else {
                AnnotationDiffer differ = new AnnotationDiffer(differs);
                measuresRow = differ.getMeasuresRow(measureList.getSelectedValues(), type);
            }
            annotationTableModel.addRow(measuresRow.toArray());
        }
    }
    // add summary rows to the fscore tables
    if (measuresType == FSCORE_MEASURES) {
        if (useBdm) {
            OntologyMeasures ontologyMeasures = new OntologyMeasures(documentOntologyMeasuresList);
            printSummary(ontologyMeasures, documentTableModel, 5, documentTableModel.getRowCount(), measureList.getSelectedValues());
            ontologyMeasures = new OntologyMeasures(annotationOntologyMeasuresList);
            printSummary(ontologyMeasures, annotationTableModel, 5, annotationTableModel.getRowCount(), measureList.getSelectedValues());
        } else {
            List<AnnotationDiffer> differs = new ArrayList<AnnotationDiffer>();
            for (Map<String, AnnotationDiffer> differsByType : differsByDocThenType) {
                differs.addAll(differsByType.values());
            }
            AnnotationDiffer differ = new AnnotationDiffer(differs);
            printSummary(differ, documentTableModel, 5, documentTableModel.getRowCount(), measureList.getSelectedValues());
            printSummary(differ, annotationTableModel, 5, annotationTableModel.getRowCount(), measureList.getSelectedValues());
        }
    // add summary rows to the classification tables
    } else if (measuresType == CLASSIFICATION_MEASURES) {
        ClassificationMeasures classificationMeasures = new ClassificationMeasures(classificationMeasuresList);
        printSummary(classificationMeasures, document2TableModel, 3, document2TableModel.getRowCount(), measure2List.getSelectedValues());
        List<List<String>> matrix = classificationMeasures.getConfusionMatrix("Whole corpus");
        int insertionRow = 0;
        for (List<String> row : matrix) {
            while (confusionTableModel.getColumnCount() < matrix.size()) {
                confusionTableModel.addColumn(" ");
            }
            confusionTableModel.insertRow(insertionRow++, row.toArray());
        }
    }
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            progressBar.setValue(progressBar.getMinimum());
            progressBar.setString("");
            setList.setEnabled(true);
            setCheck.setEnabled(true);
            typeList.setEnabled(true);
            typeCheck.setEnabled(true);
            featureList.setEnabled(true);
            featureCheck.setEnabled(true);
            optionsButton.setEnabled(true);
            measureTabbedPane.setEnabled(true);
            measureList.setEnabled(true);
            exportToHtmlAction.setEnabled(true);
            reloadCacheAction.setEnabled(true);
        }
    });
    if (progressValuePrevious > -1) {
        // restart the thread where it was interrupted
        readSetsTypesFeatures(progressValuePrevious);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Document(gate.Document) ArrayList(java.util.ArrayList) List(java.util.List) JList(javax.swing.JList) AnnotationDiffer(gate.util.AnnotationDiffer) OntologyMeasures(gate.util.OntologyMeasures) HashSet(java.util.HashSet) Annotation(gate.Annotation) ClassificationMeasures(gate.util.ClassificationMeasures)

Example 5 with AnnotationSet

use of gate.AnnotationSet in project gate-core by GateNLP.

the class RelationSetView method setTarget.

@Override
public void setTarget(Object target) {
    if (doc != null) {
        doc.removeDocumentListener(this);
        doc.getAnnotations().removeAnnotationSetListener(this);
        doc.getAnnotations().getRelations().removeRelationSetListener(this);
        for (String name : doc.getAnnotationSetNames()) {
            AnnotationSet as = doc.getAnnotations(name);
            as.removeAnnotationSetListener(this);
            as.getRelations().removeRelationSetListener(this);
        }
    }
    doc = (Document) target;
    doc.addDocumentListener(this);
    doc.getAnnotations().addAnnotationSetListener(this);
    doc.getAnnotations().getRelations().addRelationSetListener(this);
    for (String name : doc.getAnnotationSetNames()) {
        AnnotationSet as = doc.getAnnotations(name);
        as.addAnnotationSetListener(this);
        as.getRelations().addRelationSetListener(this);
    }
    refresh();
}
Also used : AnnotationSet(gate.AnnotationSet)

Aggregations

AnnotationSet (gate.AnnotationSet)43 Annotation (gate.Annotation)27 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 Document (gate.Document)9 List (java.util.List)8 FeatureMap (gate.FeatureMap)7 InvalidOffsetException (gate.util.InvalidOffsetException)6 AnnotationSetImpl (gate.annotation.AnnotationSetImpl)5 Set (java.util.Set)5 StatusListener (gate.event.StatusListener)4 GateRuntimeException (gate.util.GateRuntimeException)4 Point (java.awt.Point)4 IOException (java.io.IOException)4 URL (java.net.URL)4 Map (java.util.Map)4 Color (java.awt.Color)3 TreeSet (java.util.TreeSet)3 TestDocument (gate.corpora.TestDocument)2