001/** 002 * Copyright (c) 2007-2009, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.feature.syntax; 025 026import java.util.Collections; 027import java.util.List; 028 029import org.apache.uima.jcas.JCas; 030import org.cleartk.ml.Feature; 031import org.cleartk.ml.feature.extractor.CleartkExtractorException; 032import org.cleartk.ml.feature.extractor.FeatureExtractor1; 033import org.cleartk.syntax.constituent.type.TreebankNode; 034import org.apache.uima.fit.util.JCasUtil; 035 036import com.google.common.collect.Lists; 037 038/** 039 * <br> 040 * Copyright (c) 2007-2009, Regents of the University of Colorado <br> 041 * All rights reserved. 042 * 043 * 044 * @author Philipp Wetzler 045 */ 046public class SiblingExtractor implements FeatureExtractor1<TreebankNode> { 047 048 public SiblingExtractor(int offset, FeatureExtractor1<TreebankNode> subExtractor) { 049 this.offset = offset; 050 this.subExtractor = subExtractor; 051 052 if (offset < 0) { 053 if (Math.abs(offset) > 1) 054 this.name = String.format("%dLeftSibling"); 055 else 056 this.name = "LeftSibling"; 057 } else if (offset > 0) { 058 if (Math.abs(offset) > 1) 059 this.name = String.format("%dRightSibling"); 060 else 061 this.name = "RightSibling"; 062 } else { 063 this.name = ""; 064 } 065 } 066 067 068 public List<Feature> extract(JCas jCas, TreebankNode node) 069 throws CleartkExtractorException { 070 TreebankNode parent = node.getParent(); 071 072 if (parent == null) 073 return Collections.emptyList(); 074 075 List<TreebankNode> children = Lists.newArrayList(JCasUtil.select( 076 parent.getChildren(), 077 TreebankNode.class)); 078 int index = children.indexOf(node); 079 int siblingIndex = index + offset; 080 081 if (siblingIndex < 0 || siblingIndex >= children.size()) 082 return Collections.emptyList(); 083 084 TreebankNode sibling = children.get(siblingIndex); 085 086 List<Feature> features = subExtractor.extract(jCas, sibling); 087 for (Feature feature : features) { 088 feature.setName(Feature.createName(name, feature.getName())); 089 } 090 091 return features; 092 } 093 094 private int offset; 095 096 private String name; 097 098 private FeatureExtractor1<TreebankNode> subExtractor; 099 100}