📄 tiercopier.java
字号:
/* * File: TierCopier.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.tier;import mpi.eudico.client.annotator.svg.SVGAnnotationDataRecord;import mpi.eudico.client.annotator.util.AnnotationDataRecord;import mpi.eudico.client.annotator.util.AnnotationRecreator;import mpi.eudico.client.annotator.util.ClientLogger;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.HashMap;import java.util.Vector;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.TreeNode;/** * A class to create copies of annotations on copies of tiers. Two main issues: <br> * - the tier names that have been stored in AnnotationDataRecords have to be * mapped to the names of the copies of the tiers <br> * - if the toplevel tier of the copies is not an independent tier a suitable * parent annotation has to be found for each annotation copy. */public class TierCopier implements ClientLogger { /** a constant for copying to a tier of the same stereotype */ public static final int SAME = 0; /** transition from tier of any stereotype to root */ public static final int ANY_TO_ROOT = 1; /** transition from root tier to time subdivision tier */ public static final int ROOT_TO_TIMESUB = 2; /** transition from root tier to symbolic subdivision tier */ public static final int ROOT_TO_SYMSUB = 3; /** transition from root tier to symbolic association tier */ public static final int ROOT_TO_ASSOC = 4; /** transition from time subdivision tier to symbolic subdivision tier */ public static final int TIMESUB_TO_SYMSUB = 5; /** transition from time subdivision tier to symbolic association tier! */ public static final int TIMESUB_TO_ASSOC = 6; /** transition from symbolic subdivision tier to time subdivision tier */ public static final int SYMSUB_TO_TIMESUB = 7; /** transition from symbolic subdivision tier to symbolic association tier */ public static final int SYMSUB_TO_ASSOC = 8; /** transition from symbolic association tier to time subdivision tier */ public static final int ASSOC_TO_TIMESUB = 9; /** transition from symbolic association tier to symbolic subdivision tier */ public static final int ASSOC_TO_SYMSUB = 10; /** Holds value of property DOCUMENT ME! */ public static final int ROOT_TO_INCLUDED_IN = 11; /** Holds value of property DOCUMENT ME! */ public static final int TIMESUB_TO_INCLUDED_IN = 12; /** Holds value of property DOCUMENT ME! */ public static final int SYMSUB_TO_INCLUDED_IN = 13; /** Holds value of property DOCUMENT ME! */ public static final int ASSOC_TO_INCLUDED_IN = 14; /** Holds value of property DOCUMENT ME! */ public static final int INCLUDED_IN_TO_TIMESUB = 15; /** Holds value of property DOCUMENT ME! */ public static final int INCLUDED_IN_TO_SYMSUB = 16; /** Holds value of property DOCUMENT ME! */ public static final int INCLUDED_IN_TO_ASSOC = 17; /** * Creates a new TierCopier instance */ public TierCopier() { } /** * Suitable for recreation of annotations on root tiers or timesubdivision * tiers. * * @param trans the transcription * @param root the 'root' node * @param tierMapping old names to new names mapping * * @return the annotation for 'rootNode' */ public AbstractAnnotation createAnnotationFromTree(Transcription trans, DefaultMutableTreeNode root, HashMap tierMapping) { if ((trans == null) || (root == null) || (tierMapping == null)) { return null; } AbstractAnnotation annotation = null; DefaultMutableTreeNode node; AnnotationDataRecord annData = null; String tierName = null; TierImpl tier = null; AlignableAnnotation aa = null; RefAnnotation ra = null; Annotation an = null; long begin; long end; int linStereoType = -1; long[] timeBounds = new long[] { 0, 0 }; // // find a parent annotation if the top level copy-tier is not a root annData = (AnnotationDataRecord) root.getUserObject(); tierName = (String) tierMapping.get(annData.getTierName()); tier = (TierImpl) trans.getTierWithId(tierName); if (tier == null) { LOG.warning( "Cannot recreate annotations: tier copy does not exist: " + tierName); return null; } if (tier.hasParentTier()) { Vector overlap = ((TierImpl) tier.getParentTier()).getOverlappingAnnotations(annData.getBeginTime(), annData.getEndTime()); if (overlap.size() > 0) { long overl = 0; int index = 0; for (int i = 0; i < overlap.size(); i++) { an = (Annotation) overlap.get(i); long ol = 0; if (an.getBeginTimeBoundary() > annData.getBeginTime()) { if (an.getEndTimeBoundary() > annData.getEndTime()) { ol = annData.getEndTime() - an.getBeginTimeBoundary(); } else { ol = an.getEndTimeBoundary() - an.getBeginTimeBoundary(); } } else { if (an.getEndTimeBoundary() > annData.getEndTime()) { ol = annData.getEndTime() - annData.getBeginTime(); } else { ol = an.getEndTimeBoundary() - annData.getBeginTime(); } } if (ol > overl) { overl = ol; index = i; } /* if (tier.getOverlappingAnnotations( an.getBeginTimeBoundary(), an.getEndTimeBoundary()).size() == 0) { timeBounds[0] = an.getBeginTimeBoundary(); timeBounds[1] = an.getEndTimeBoundary(); break; }*/ } an = (Annotation) overlap.get(index); timeBounds[0] = an.getBeginTimeBoundary(); timeBounds[1] = an.getEndTimeBoundary(); if ((timeBounds[0] == 0) && (timeBounds[1] == 0)) { return null; } } else { return null; // no overlap, no annotation } } // Enumeration en = root.breadthFirstEnumeration(); while (en.hasMoreElements()) { aa = null; //reset node = (DefaultMutableTreeNode) en.nextElement(); annData = (AnnotationDataRecord) node.getUserObject(); tierName = (String) tierMapping.get(annData.getTierName()); tier = (TierImpl) trans.getTierWithId(tierName); if (tier == null) { LOG.warning("Cannot recreate annotations: tier does not exist."); continue; } if (tier.isTimeAlignable()) { if (annData.isBeginTimeAligned()) { begin = annData.getBeginTime(); end = annData.getEndTime(); // correct to fit in the parentbounds if (begin < timeBounds[0]) { begin = timeBounds[0]; } if (end > timeBounds[1]) { end = timeBounds[1]; } // should nor happen anymore; sometimes an annotation can have the same begin and 'virtual' // end time on a time-subdivision tier if (!annData.isEndTimeAligned() && (end == begin)) { end++; } aa = (AlignableAnnotation) tier.createAnnotation(begin, end); if (node == root) { annotation = aa; } if (aa != null) { aa.setValue(annData.getValue()); if (aa instanceof SVGAlignableAnnotation && annData instanceof SVGAnnotationDataRecord) { SVGAnnotationDataRecord svgRec = (SVGAnnotationDataRecord) annData; if (svgRec.getShape() != null) { ((SVGAlignableAnnotation) aa).setShape(svgRec.getShape()); } if (svgRec.getSvgElementId() != null) { ((SVGAlignableAnnotation) aa).setSVGElementID(svgRec.getSvgElementId()); } } } else { LOG.warning( "Alignable annotation could not be recreated: " + annData.getValue() + " bt: " + annData.getBeginTime() + " et: " + annData.getEndTime()); } } } else { // non-alignable in second run } } // second run en = root.breadthFirstEnumeration(); // for re-creation of unaligned annotation on Alignable (Time-Subdivision) tiers Annotation prevAnn = null; while (en.hasMoreElements()) { aa = null; //reset an = null; ra = null; node = (DefaultMutableTreeNode) en.nextElement(); annData = (AnnotationDataRecord) node.getUserObject(); tierName = (String) tierMapping.get(annData.getTierName()); tier = (TierImpl) trans.getTierWithId(tierName); if (tier == null) { LOG.warning("Cannot recreate annotations: tier does not exist."); continue; } if (tier.isTimeAlignable()) { if (!annData.isBeginTimeAligned()) { if ((prevAnn != null) && (!prevAnn.getTier().getName().equals((String) tierMapping.get( annData.getTierName())) || (prevAnn.getEndTimeBoundary() <= annData.getBeginTime()))) { // reset previous annotation field prevAnn = null; } if (prevAnn == null) { begin = annData.getBeginTime(); end = annData.getEndTime(); // correct to fit in the parentbounds if (begin < timeBounds[0]) { begin = timeBounds[0]; } if (end > timeBounds[1]) { end = timeBounds[1]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -