001/** 
002 * Copyright (c) 2007-2008, 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.ArrayList;
027import java.util.List;
028import java.util.ListIterator;
029
030import org.apache.uima.jcas.JCas;
031import org.cleartk.ml.Feature;
032import org.cleartk.ml.feature.extractor.CleartkExtractorException;
033import org.cleartk.ml.feature.extractor.FeatureExtractor1;
034import org.cleartk.ml.feature.extractor.FeatureExtractor2;
035import org.cleartk.syntax.constituent.type.TreebankNode;
036import org.cleartk.syntax.constituent.type.TreebankNodeUtil;
037
038/**
039 * <br>
040 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
041 * All rights reserved.
042 * 
043 * 
044 * @author Philipp Wetzler
045 * 
046 */
047public class SyntacticPathExtractor implements FeatureExtractor2<TreebankNode, TreebankNode> {
048  protected final String UP_SEPARATOR = "::";
049
050  protected final String DOWN_SEPARATOR = ";;";
051
052  protected FeatureExtractor1<TreebankNode> pathMemberExtractor;
053
054  protected String name;
055
056  protected boolean isPartial;
057
058  /**
059   * 
060   * @param pathMemberExtractor
061   *          this extractor will be used to get a feature for every node on the path, which will
062   *          then be combined to form a single string. The extractor should preferably generate
063   *          exactly one <tt>StringFeature</tt>, but must generate at least one
064   *          <tt>StringFeature</tt>, <tt>LongFeature</tt>, <tt>DoubleFeature</tt>, or
065   *          <tt>BooleanFeature</tt>. Only the first feature will then be used and naively
066   *          converted to a string.
067   * @param partial
068   *          if true, generate a partial path only, i.e. from the first node up to the lowest
069   *          common ancestor of the two
070   */
071  public SyntacticPathExtractor(FeatureExtractor1<TreebankNode> pathMemberExtractor, boolean partial) {
072    this.pathMemberExtractor = pathMemberExtractor;
073    this.isPartial = partial;
074  }
075
076  /**
077   * This constructor defaults to a full rather than a partial path.
078   */
079  public SyntacticPathExtractor(FeatureExtractor1<TreebankNode> pathMemberExtractor) {
080    this(pathMemberExtractor, false);
081  }
082
083  /**
084   * Extract a string representation of a path feature.
085   * 
086   * @param leftConstituent
087   *          the first node of the path
088   * 
089   * @param rightConstituent
090   *          the last node of the path
091   * 
092   * @return List of one <em>StringFeature</em>, which contains a string representation of the path
093   *         between the two nodes.
094   * 
095   */
096  public List<Feature> extract(JCas view, TreebankNode leftConstituent, TreebankNode rightConstituent)
097      throws CleartkExtractorException {
098
099    List<TreebankNode> fromStart = TreebankNodeUtil.getPathToRoot(leftConstituent);
100    List<TreebankNode> fromEnd = TreebankNodeUtil.getPathToRoot(rightConstituent);
101    String pathFeatureName = null;
102    String lengthFeatureName = null;
103
104    fromEnd.remove(fromEnd.size() - 1);
105    while (fromStart.size() > 1 && fromEnd.size() > 0
106        && fromStart.get(fromStart.size() - 2) == fromEnd.get(fromEnd.size() - 1)) {
107      fromStart.remove(fromStart.size() - 1);
108      fromEnd.remove(fromEnd.size() - 1);
109    }
110
111    int length = fromStart.size();
112    if (!isPartial)
113      length += fromEnd.size();
114
115    try {
116      ListIterator<TreebankNode> it = fromStart.listIterator();
117      StringBuffer pathBuffer = new StringBuffer();
118      boolean first = true;
119      while (it.hasNext()) {
120        Feature feature = this.pathMemberExtractor.extract(view, it.next()).get(0);
121        if (first) {
122          String s = feature.getName();
123          if (isPartial) {
124            pathFeatureName = Feature.createName(name, "PartialSyntacticPath(" + s + ")");
125            lengthFeatureName = Feature.createName(name, "PartialSyntacticPath", "Length");
126          } else {
127            pathFeatureName = Feature.createName(name, "SyntacticPath(" + s + ")");
128            lengthFeatureName = Feature.createName(name, "SyntacticPath", "Length");
129          }
130          first = false;
131        } else {
132          pathBuffer.append(this.UP_SEPARATOR);
133        }
134        pathBuffer.append(feature.getValue().toString());
135      }
136
137      if (!isPartial) {
138        it = fromEnd.listIterator(fromEnd.size());
139        while (it.hasPrevious()) {
140          Feature feature = this.pathMemberExtractor.extract(view, it.previous()).get(0);
141          pathBuffer.append(this.DOWN_SEPARATOR);
142          pathBuffer.append(feature.getValue().toString());
143        }
144      }
145
146      List<Feature> features = new ArrayList<Feature>(2);
147      features.add(new Feature(pathFeatureName, pathBuffer.toString()));
148      features.add(new Feature(lengthFeatureName, (long) length));
149
150      return features;
151
152    } catch (IndexOutOfBoundsException e) {
153      return new ArrayList<Feature>(0);
154    }
155  }
156}