📄 transcriptionmerger.java
字号:
/* * File: TranscriptionMerger.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.imports;import mpi.eudico.client.annotator.util.AnnotationRecreator;import mpi.eudico.client.annotator.util.ClientLogger;import mpi.eudico.client.annotator.util.ProgressListener;import mpi.eudico.server.corpora.clom.TranscriptionStore;import mpi.eudico.server.corpora.clomimpl.abstr.AbstractAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.TierImpl;import mpi.eudico.server.corpora.clomimpl.abstr.TranscriptionImpl;import mpi.eudico.server.corpora.clomimpl.dobes.ACM24TranscriptionStore;import mpi.eudico.server.corpora.clomimpl.type.LinguisticType;import mpi.util.ControlledVocabulary;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import javax.swing.tree.DefaultMutableTreeNode;/** * A class that merges two source transcriptions to a new destination * transcription, in a separate thread. ProgressListeners can register to be * informed about the merging progress. * * @author Han Sloetjes */public class TranscriptionMerger implements ClientLogger { private ArrayList listeners; private File source1File; private File source2File; private File destinationFile; private TranscriptionImpl transcription; private TranscriptionImpl transcription2; private TranscriptionImpl destTranscription; private MergeThread mergeThread; /** an estimated percentage of processing time needed for loading */ private final int LOAD_PERCENTAGE = 15; /** * Creates a new TranscriptionMerger instance. * * @param source1 the path to the first source file * @param source2 the path to the second source file * @param destination the path to the destination file * * @throws IOException thrown if we cannot write to the destination file * @throws NullPointerException thrown when any of the parameters is null * @throws IllegalArgumentException thrown if any of the source files does * not exist */ public TranscriptionMerger(String source1, String source2, String destination) throws IOException { if ((source1 == null) || (source2 == null)) { LOG.warning("Sources for merging cannot be null."); throw new NullPointerException( "Sources for merging cannot be null."); } if (destination == null) { LOG.warning("Destination for merging cannot be null."); throw new NullPointerException( "Destination for merging cannot be null."); } source1File = new File(source1); source2File = new File(source2); if (!source1File.exists() || !source2File.exists()) { LOG.warning("Sources for merging must be existing files."); throw new IllegalArgumentException( "Sources for merging must be existing files."); } destinationFile = new File(destination); if (!destinationFile.exists()) { destinationFile.createNewFile(); } if (!destinationFile.canWrite() || destinationFile.isDirectory()) { LOG.warning("Cannot write to file: " + destinationFile.getAbsolutePath()); throw new IOException("Cannot write to file: " + destinationFile.getAbsolutePath()); } } /** * Creates a new TranscriptionMerger instance.<br> * Note: Merging with an existing transcription is not implemented yet. * * @param transcription the first source as a Transcription object * @param source2 the path to the second source file * @param destination the path to the destination file * * @throws IOException thrown if we cannot write to the destination file * @throws NullPointerException thrown when any of the parameters is null * @throws IllegalArgumentException thrown if any of the source files does * not exist */ public TranscriptionMerger(TranscriptionImpl transcription, String source2, String destination) throws IOException { if ((transcription == null) || (source2 == null)) { LOG.warning("Sources for merging cannot be null."); throw new NullPointerException( "Sources for merging cannot be null."); } if (destination == null) { LOG.warning("Destination for merging cannot be null."); throw new NullPointerException( "Destination for merging cannot be null."); } source2File = new File(source2); if (!source1File.exists() || !source2File.exists()) { LOG.warning("Sources for merging must be existing files."); throw new IllegalArgumentException( "Sources for merging must be existing files."); } destinationFile = new File(destination); if (!destinationFile.exists()) { destinationFile.createNewFile(); } if (!destinationFile.canWrite() || destinationFile.isDirectory()) { LOG.warning("Cannot write to file: " + destinationFile.getAbsolutePath()); throw new IOException("Cannot write to file: " + destinationFile.getAbsolutePath()); } } /** * Starts the merging process. This creates a separate thread that directs * the merging steps. */ public void startMerge() { mergeThread = new MergeThread(TranscriptionMerger.class.getName()); mergeThread.start(); } /** * Creates the first source transcrition. Note: only the construction of a * transcription from a source file is implemented. Duplication or cloning * of an existing, in-memory, transcription is not yet supported. */ private void firstTranscription() { if (transcription != null) { // copy structures from first transcription to dest transcription // to be implemented } else { progressUpdate(5, "Loading first source file..."); try { destTranscription = new TranscriptionImpl(source1File.getAbsolutePath()); destTranscription.setNotifying(false); destTranscription.setChanged(); progressUpdate(LOAD_PERCENTAGE, "First transcription loaded..."); LOG.info("First transcription loaded..."); } catch (Exception rex) { progressInterrupt("Could not load the first source file..."); LOG.warning("Could not load the first source file..."); if (mergeThread != null) { mergeThread.interrupt(); } } } } /** * Creates the second transcription from a source file. */ private void secondTranscription() { progressUpdate(LOAD_PERCENTAGE + 5, "Loading second source file..."); try { transcription2 = new TranscriptionImpl(source2File.getAbsolutePath()); progressUpdate(2 * LOAD_PERCENTAGE, "Second transcription loaded..."); LOG.info("Second transcription loaded..."); } catch (Exception rex) { progressInterrupt("Could not load the second source file..."); LOG.warning("Could not load the second source file..."); if (mergeThread != null) { mergeThread.interrupt(); } } } /** * The actual merging process. Checks what tiers, types and CV's should be * added and then copies the annotations to the destination transcription. */ private void mergeTranscriptions() { progressUpdate(2 * LOAD_PERCENTAGE, "Adding Tiers, LinguisticTypes and ControlledVocabularies..."); try { // extract unique tiers Vector firstTiers = destTranscription.getTiers(); Vector secondTiers = transcription2.getTiers(); Vector tiersToAdd = new Vector(); Hashtable firstTierTable = new Hashtable(); TierImpl t = null; String name = null; for (int i = 0; i < firstTiers.size(); i++) { t = (TierImpl) firstTiers.get(i); name = t.getName(); firstTierTable.put(name, t); } for (int i = 0; i < secondTiers.size(); i++) { t = (TierImpl) secondTiers.get(i); name = t.getName(); if (!firstTierTable.containsKey(name)) { tiersToAdd.add(t); LOG.info("Adding tier to list of tiers: " + t.getName()); } } //sort by hierarchy DefaultMutableTreeNode sortedRootNode = new DefaultMutableTreeNode( "sortRoot"); Hashtable nodes = new Hashtable(); for (int i = 0; i < tiersToAdd.size(); i++) { t = (TierImpl) tiersToAdd.get(i); DefaultMutableTreeNode node = new DefaultMutableTreeNode(t); nodes.put(t, node); } for (int i = 0; i < tiersToAdd.size(); i++) { t = (TierImpl) tiersToAdd.get(i); if ((t.getParentTier() == null) || !tiersToAdd.contains(t.getParentTier())) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.get(t); sortedRootNode.add((DefaultMutableTreeNode) nodes.get(t)); } else { ((DefaultMutableTreeNode) nodes.get(t.getParentTier())).add((DefaultMutableTreeNode) nodes.get( t)); } } // sort the tiers to add tiersToAdd.clear(); Enumeration en = sortedRootNode.breadthFirstEnumeration(); while (en.hasMoreElements()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) en.nextElement(); if (node.getUserObject() instanceof TierImpl) { tiersToAdd.add(node.getUserObject()); } } // first add tiers, linguistic types and CV's addTiersTypesAndCVs(tiersToAdd); // add the sorted tiers, int tierStartProgress = 3 * LOAD_PERCENTAGE; int numTopTiers = Math.max(sortedRootNode.getChildCount(), 1); progressUpdate(tierStartProgress, "Start adding annotations..."); int progressPerIndepTier = (100 - (4 * LOAD_PERCENTAGE)) / numTopTiers; int tierNum = 1; String busy = "..."; // loop over 'top' tiers and add annotations Enumeration topTierEnum = sortedRootNode.children(); while (topTierEnum.hasMoreElements()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) topTierEnum.nextElement(); Object o = node.getUserObject(); if (o instanceof TierImpl) { TierImpl tier = (TierImpl) o; progressUpdate(tierStartProgress, "Merging tier: " + tier.getName()); LOG.info("Merging tier: " + tier.getName()); Vector annotations = tier.getAnnotations(); int numAnn = annotations.size(); if (numAnn > 0) { int ppa = progressPerIndepTier / numAnn; AbstractAnnotation ann = null; DefaultMutableTreeNode recordNode = null; for (int i = 0; i < numAnn; i++) { ann = (AbstractAnnotation) annotations.get(i); recordNode = AnnotationRecreator.createTreeForAnnotation(ann);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -