📄 annotationrecreator.java
字号:
/* * File: AnnotationRecreator.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * 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 mpi.eudico.client.annotator.util;import mpi.eudico.client.annotator.svg.SVGAnnotationDataRecord;import mpi.eudico.server.corpora.clom.Annotation;import mpi.eudico.server.corpora.clom.Transcription;import mpi.eudico.server.corpora.clomimpl.abstr.AbstractAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.AlignableAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.RefAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.SVGAlignableAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.TierImpl;import mpi.eudico.server.corpora.clomimpl.type.Constraint;import mpi.eudico.server.corpora.clomimpl.type.LinguisticType;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import java.util.Vector;import java.util.logging.Logger;import javax.swing.tree.DefaultMutableTreeNode;/** * This class provides methods for storing annotations' state and recreation of * (deleted) annotations.<br> * It can: <br> * - create a dependency tree for annotations and store relevant data from * each annotation involved<br> * - recreate annotations (including dependent annotations) given the stored * information - handle conversion of annotations after a modification of * attributes of a Linguistic Type Note: preliminary * * @author Han Sloetjes * @version july 2004 */public class AnnotationRecreator { /** a logger */ private static final Logger LOG = Logger.getLogger(AnnotationRecreator.class.getName()); /** * Recreates annotations on Tiers with the specified LinguisticType.<br> * This operation can be neccessary when an other kind of Annotation type * is needed after a modification in the type, e.g. conversion of * AlignableAnnotations into SVGAlignableAnnotations when the * LinguisticType has been changed to allow graphic references.<br> * Be sure to lock the whole transcription while this operation is going * on! * * @param trans the Transcription * @param type the modified LinguisticType */ public static void convertAnnotations(Transcription trans, LinguisticType type) { if ((trans == null) || (type == null)) { return; } ArrayList convertedTiers = new ArrayList(); Vector tiersToConvert = null; tiersToConvert = trans.getTiersWithLinguisticType(type.getLinguisticTypeName()); Vector rootTiers = new Vector(); // put all root tiers at the beginning of the vector for (int i = 0; i < tiersToConvert.size(); i++) { TierImpl t = (TierImpl) tiersToConvert.get(i); if (!t.hasParentTier()) { rootTiers.add(t); } } for (int i = 0; i < tiersToConvert.size(); i++) { TierImpl t = (TierImpl) tiersToConvert.get(i); if (!rootTiers.contains(t)) { rootTiers.add(t); } } tiersToConvert = rootTiers; // convert all annotations on these tiers Iterator convIt = tiersToConvert.iterator(); TierImpl curTier = null; while (convIt.hasNext()) { curTier = (TierImpl) convIt.next(); if (!convertedTiers.contains(curTier)) { AnnotationRecreator.convertAnnotations(trans, curTier); convertedTiers.add(curTier); Vector depTiers = curTier.getDependentTiers(); for (int i = 0; i < depTiers.size(); i++) { if (!convertedTiers.contains(depTiers.get(i))) { convertedTiers.add(depTiers.get(i)); } } } } } /** * Recreates annotations on the specified Tier.<br> * This operation can be necessary when an other kind of Annotation type * is needed after a modification in the type, e.g. conversion of * AlignableAnnotations into SVGAlignableAnnotations when the * LinguisticType has been changed to allow graphic references.<br> * Be sure to lock the whole transcription while this operation is going * on! * * @param trans the Transcription * @param tier the modified LinguisticType */ public static void convertAnnotations(Transcription trans, TierImpl tier) { if ((trans == null) || (tier == null)) { return; } Vector annotations = null; AbstractAnnotation absAnn = null; DefaultMutableTreeNode root = null; annotations = tier.getAnnotations(); Iterator annIt = annotations.iterator(); ArrayList annTreeList = new ArrayList(annotations.size()); // step 1: create data structures while (annIt.hasNext()) { absAnn = (AbstractAnnotation) annIt.next(); root = createTreeForAnnotation(absAnn); annTreeList.add(root); } annIt = annotations.iterator(); // step 2: delete annotations while (annIt.hasNext()) { absAnn = (AbstractAnnotation) annIt.next(); tier.removeAnnotation(absAnn); } annIt = annTreeList.iterator(); // step 3: recreate annotations while (annIt.hasNext()) { root = (DefaultMutableTreeNode) annIt.next(); AnnotationRecreator.createAnnotationFromTree(trans, root); } } /** * Creates a tree structure from one annotation.<br> * The specified annotation will be the root of the tree. UNFINISHED! * * @param aa the annotation * * @return the root of the created tree */ public static DefaultMutableTreeNode createTreeForAnnotation( AbstractAnnotation aa) { DefaultMutableTreeNode root = null; if (aa instanceof SVGAlignableAnnotation) { root = new DefaultMutableTreeNode(new SVGAnnotationDataRecord( (SVGAlignableAnnotation) aa)); } else { root = new DefaultMutableTreeNode(new AnnotationDataRecord(aa)); } ArrayList children = null; AbstractAnnotation next = null; AbstractAnnotation parent = null; DefaultMutableTreeNode nextNode = null; DefaultMutableTreeNode parentNode = root; TierImpl tier = null; String tierName = null; DefaultMutableTreeNode tempNode = null; AnnotationDataRecord dataRecord = null; children = aa.getParentListeners(); if (children.size() > 0) {downloop: for (int i = 0; i < children.size(); i++) { next = (AbstractAnnotation) children.get(i); if (next instanceof SVGAlignableAnnotation) { nextNode = new DefaultMutableTreeNode(new SVGAnnotationDataRecord( (SVGAlignableAnnotation) next)); } else { nextNode = new DefaultMutableTreeNode(new AnnotationDataRecord( next)); } // children can come in any order tier = (TierImpl) next.getTier(); if (parentNode.getChildCount() == 0) { parentNode.add(nextNode); } else { long bt = next.getBeginTimeBoundary(); for (int k = 0; k < parentNode.getChildCount(); k++) { tempNode = (DefaultMutableTreeNode) parentNode.getChildAt(k); dataRecord = (AnnotationDataRecord) tempNode.getUserObject(); tierName = next.getTier().getName(); if ((dataRecord.getBeginTime() > bt) && (tierName != null) && dataRecord.getTierName().equals(tierName)) { parentNode.insert(nextNode, k); break; } else if (k == (parentNode.getChildCount() - 1)) { parentNode.add(nextNode); } } } if (next.getParentListeners().size() > 0) { children = next.getParentListeners(); parentNode = nextNode; i = -1; continue downloop; } if (i == (children.size() - 1)) {uploop: while (true) { parent = (AbstractAnnotation) next.getParentAnnotation(); if (parent != null) { parentNode = (DefaultMutableTreeNode) nextNode.getParent(); children = parent.getParentListeners(); int j = children.indexOf(next); if (j == (children.size() - 1)) { if (parent == aa) { break downloop; } next = parent; nextNode = parentNode; continue uploop; } else { i = j; continue downloop; } } else { break downloop; } } } } } /* Enumeration en = root.depthFirstEnumeration(); System.out.println("Depth First:\n"); while (en.hasMoreElements()){ DefaultMutableTreeNode nextnode = (DefaultMutableTreeNode)en.nextElement(); AnnotationDataRecord rec = (AnnotationDataRecord) nextnode.getUserObject(); System.out.println("Level: " + nextnode.getLevel() + " -- Tier: " + rec.getTierName() + " anndata: " + rec.getValue()); } System.out.println("\n"); System.out.println("Breadth First:\n"); en = root.breadthFirstEnumeration(); while (en.hasMoreElements()){ DefaultMutableTreeNode nextnode = (DefaultMutableTreeNode)en.nextElement(); AnnotationDataRecord rec = (AnnotationDataRecord) nextnode.getUserObject(); System.out.println("Level: " + nextnode.getLevel() + " -- Tier: " + rec.getTierName() + " anndata: " + rec.getValue()); } System.out.println("\n"); System.out.println("Post Order:\n"); en = root.postorderEnumeration(); while (en.hasMoreElements()){ DefaultMutableTreeNode nextnode = (DefaultMutableTreeNode)en.nextElement(); AnnotationDataRecord rec = (AnnotationDataRecord) nextnode.getUserObject(); System.out.println("Level: " + nextnode.getLevel() + " -- Tier: " + rec.getTierName() + " anndata: " + rec.getValue()); } System.out.println("\n"); System.out.println("Pre Order:\n"); en = root.preorderEnumeration(); while (en.hasMoreElements()){ DefaultMutableTreeNode nextnode = (DefaultMutableTreeNode)en.nextElement(); AnnotationDataRecord rec = (AnnotationDataRecord) nextnode.getUserObject(); System.out.println("Level: " + nextnode.getLevel() + " -- Tier: " + rec.getTierName() + " anndata: " + rec.getValue()); } System.out.println("\n"); */ return root; } /** * Creates a treenode without children from one annotation.<br> * * @param aa the annotation * * @return the root of the created treenode */ public static DefaultMutableTreeNode createNodeForAnnotation( AbstractAnnotation aa) { DefaultMutableTreeNode node = null; if (aa instanceof SVGAlignableAnnotation) { node = new DefaultMutableTreeNode(new SVGAnnotationDataRecord( (SVGAlignableAnnotation) aa)); } else { node = new DefaultMutableTreeNode(new AnnotationDataRecord(aa)); } return node; } /** * (Re)creates an annotation with all depending annotations from the * information contained in the specified Node. <br> * Suitable for annotations on a root tier or any other tier where only * one annotation has to be recreated. * * @param trans the Transcription to work on * @param root the rootnode containing the data objects for the annotations * * @return the created (root) annotation or null */ public static AbstractAnnotation createAnnotationFromTree( Transcription trans, DefaultMutableTreeNode root) { if ((trans == null) || (root == null)) { return null; } AbstractAnnotation annotation = null; DefaultMutableTreeNode node; AlignableAnnotation aa = null; RefAnnotation ra = null; Annotation an = null; /* create new annotations * iterate twice over the depending annotations: on the first * pass only Annotations with a time alignable begin time are * created, on the second pass the rest is done. */ Enumeration en = root.breadthFirstEnumeration(); AnnotationDataRecord annData = null; TierImpl tier = null; long begin; long end; int linStereoType = -1; while (en.hasMoreElements()) { aa = null; //reset
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -