📄 elansearchengine.java
字号:
/* * File: ElanSearchEngine.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.search.model;import mpi.eudico.client.annotator.search.result.model.ElanMatch;import mpi.eudico.server.corpora.clom.Annotation;import mpi.eudico.server.corpora.clom.Transcription;import mpi.eudico.server.corpora.clomimpl.abstr.TierImpl;import mpi.eudico.util.TimeRelation;import mpi.search.SearchLocale;import mpi.search.content.model.CorpusType;import mpi.search.content.query.model.*;import mpi.search.model.SearchEngine;import mpi.search.model.SearchListener;import mpi.search.query.model.Query;import java.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.List;import java.util.logging.*;import java.util.regex.*;/** * The SearchEngine performs the actual search in ELAN * * @author Alexander Klassmann * @version Aug 2005 Identity removed */public class ElanSearchEngine implements SearchEngine { /** Holds value of property DOCUMENT ME! */ private static final Logger logger = Logger.getLogger(ElanSearchEngine.class.getName()); private Hashtable annotationHash = new Hashtable(); private Hashtable patternHash = new Hashtable(); private Hashtable relationshipHash = new Hashtable(); private Hashtable unitTierHash = new Hashtable(); private Transcription transcription; /** * constructor * * @param transcription */ public ElanSearchEngine(SearchListener searchTool, Transcription transcription) { this.transcription = transcription; logger.setLevel(Level.ALL); } /** * Performs search * * @param query * * @throws PatternSyntaxException DOCUMENT ME! * @throws QueryFormulationException DOCUMENT ME! * @throws NullPointerException DOCUMENT ME! */ public void executeThread(ContentQuery query) throws PatternSyntaxException, QueryFormulationException, NullPointerException { //set unlimited size since search is done only within one transcription query.getResult().setPageSize(Integer.MAX_VALUE); initHashtables(query); AnchorConstraint anchorConstraint = query.getAnchorConstraint(); String[] tierNames = anchorConstraint.getTierNames(); if (tierNames[0].equals(Constraint.ALL_TIERS)) { tierNames = (String[]) annotationHash.keySet().toArray(new String[0]); } for (int i = 0; i < tierNames.length; i++) { List anchorAnnotations = (List) annotationHash.get(tierNames[i]); List anchorMatches; if (!(anchorConstraint instanceof RestrictedAnchorConstraint)) { int[] range = getAnnotationIndicesInScope(anchorAnnotations, anchorConstraint.getLowerBoundary(), anchorConstraint.getUpperBoundary(), anchorConstraint.getUnit()); anchorMatches = getMatches(null, (Pattern) patternHash.get(anchorConstraint), anchorConstraint.getId(), anchorAnnotations, range); } else { anchorMatches = ((RestrictedAnchorConstraint) anchorConstraint).getResult() .getMatches(tierNames[i]); } filterDependentConstraints(anchorMatches, anchorConstraint); for (int j = 0; j < anchorMatches.size(); j++) { query.getResult().addMatch((ElanMatch) anchorMatches.get(j)); } } } /** * DOCUMENT ME! * * @param query DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public void performSearch(Query query) throws Exception { executeThread((ContentQuery) query); } /** * same as getAnnotationIndicesInScope(...) with distance 0 * * @param annotationList * @param intervalBegin * @param intervalEnd * @param timeComparisonMode * * @return int[] */ private static int[] getAnnotationIndicesInScope(List annotationList, long intervalBegin, long intervalEnd, String timeComparisonMode) { return getAnnotationIndicesInScope(annotationList, intervalBegin, intervalEnd, 0L, timeComparisonMode); } /** * returns indices of annotations that fulfull the time constraint the * parameter "distance" is used only for particular timeComparisonModes * * @param annotationList * @param intervalBegin * @param intervalEnd * @param distance * @param timeComparisonMode * * @return int[] */ private static int[] getAnnotationIndicesInScope(List annotationList, long intervalBegin, long intervalEnd, long distance, String timeComparisonMode) { int[] annotationsInInterval = new int[annotationList.size()]; int index = 0; for (int i = 0; i < annotationList.size(); i++) { Annotation annotation = (Annotation) annotationList.get(i); boolean constraintFulfilled = false; if (Constraint.OVERLAP.equals(timeComparisonMode)) { constraintFulfilled = TimeRelation.overlaps(annotation, intervalBegin, intervalEnd); } else if (Constraint.IS_INSIDE.equals(timeComparisonMode)) { constraintFulfilled = TimeRelation.isInside(annotation, intervalBegin, intervalEnd); } else if (Constraint.NO_OVERLAP.equals(timeComparisonMode)) { constraintFulfilled = TimeRelation.doesNotOverlap(annotation, intervalBegin, intervalEnd); } else if (Constraint.NOT_INSIDE.equals(timeComparisonMode)) { constraintFulfilled = TimeRelation.isNotInside(annotation, intervalBegin, intervalEnd); } else if (Constraint.LEFT_OVERLAP.equals(timeComparisonMode)) { constraintFulfilled = TimeRelation.overlapsOnLeftSide(annotation, intervalBegin, intervalEnd); } else if (Constraint.RIGHT_OVERLAP.equals(timeComparisonMode)) { constraintFulfilled = TimeRelation.overlapsOnRightSide(annotation, intervalBegin, intervalEnd); } else if (Constraint.WITHIN_OVERALL_DISTANCE.equals( timeComparisonMode)) { constraintFulfilled = TimeRelation.isWithinDistance(annotation, intervalBegin, intervalEnd, distance); } else if (Constraint.WITHIN_DISTANCE_TO_LEFT_BOUNDARY.equals( timeComparisonMode)) { constraintFulfilled = TimeRelation.isWithinLeftDistance(annotation, intervalBegin, distance); } else if (Constraint.WITHIN_DISTANCE_TO_RIGHT_BOUNDARY.equals( timeComparisonMode)) { constraintFulfilled = TimeRelation.isWithinRightDistance(annotation, intervalEnd, distance); } else if (Constraint.BEFORE_LEFT_DISTANCE.equals( timeComparisonMode)) { constraintFulfilled = TimeRelation.isBeforeLeftDistance(annotation, intervalBegin, distance); } else if (Constraint.AFTER_RIGHT_DISTANCE.equals( timeComparisonMode)) { constraintFulfilled = TimeRelation.isAfterRightDistance(annotation, intervalEnd, distance); } if (constraintFulfilled) { annotationsInInterval[index++] = i; } } int[] range = new int[index]; System.arraycopy(annotationsInInterval, 0, range, 0, index); return range; } /** * Returns list with the annotations (not their indices!) in constraint * tier within specified range * * @param lowerBoundary * @param upperBoundary * @param unitTier * @param unitAnnotations * @param relationship * @param centralAnnotation * * @return List * * @throws NullPointerException DOCUMENT ME! */ private static List getAnnotationsInScope(long lowerBoundary, long upperBoundary, TierImpl unitTier, List unitAnnotations, TierImpl[] relationship, Annotation centralAnnotation) throws NullPointerException { List annotationsInScope = new ArrayList(); Annotation centralUnitAnnotation = centralAnnotation; while ((centralUnitAnnotation.getTier() != unitTier) && (centralUnitAnnotation != null)) { centralUnitAnnotation = centralUnitAnnotation.getParentAnnotation(); } if (centralUnitAnnotation == null) { throw new NullPointerException(); } int unitAnnotationIndex = unitAnnotations.indexOf(centralUnitAnnotation); int[] unitAnnotationIndicesInScope = getRangeForTier(unitTier, lowerBoundary, upperBoundary, unitAnnotationIndex); Annotation rootOfCentralAnnotation = centralUnitAnnotation; while (rootOfCentralAnnotation.hasParentAnnotation()) { rootOfCentralAnnotation = rootOfCentralAnnotation.getParentAnnotation(); } logger.log(Level.FINE, "Unit annotation " + centralUnitAnnotation.getValue()); Annotation unitAnnotation; for (int k = 0; k < unitAnnotationIndicesInScope.length; k++) { unitAnnotation = (Annotation) unitAnnotations.get(unitAnnotationIndicesInScope[k]); boolean haveSameRoot = true; if (unitAnnotation.hasParentAnnotation()) { Annotation rootOfUnitAnnotation = unitAnnotation; while (rootOfUnitAnnotation.hasParentAnnotation()) { rootOfUnitAnnotation = rootOfUnitAnnotation.getParentAnnotation(); } haveSameRoot = rootOfUnitAnnotation == rootOfCentralAnnotation; } if (haveSameRoot) { annotationsInScope.addAll(getDescAnnotations(unitAnnotation, relationship)); } } return annotationsInScope; } /** * gets all descendant annotations (e.g. children of children etc.) * * @param ancestorAnnotation * @param relationship *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -