001/** 
002 * Copyright (c) 2007-2012, 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.ml.feature.function;
025
026import java.util.Collections;
027import java.util.List;
028
029import org.cleartk.ml.Feature;
030import org.cleartk.ml.feature.util.CaseUtil;
031
032/**
033 * <br>
034 * Copyright (c) 2007-2012, Regents of the University of Colorado <br>
035 * All rights reserved.
036 * 
037 * 
038 * @author Philip Ogren
039 * 
040 */
041public class CapitalTypeFeatureFunction implements FeatureFunction {
042
043  public static final String DEFAULT_NAME = "CapitalType";
044
045  public enum CapitalType {
046    ALL_UPPERCASE, ALL_LOWERCASE, INITIAL_UPPERCASE, MIXED_CASE
047  }
048
049  /**
050   * If the value of the feature is a StringValue and is determined to be one of ALL_UPPERCASE,
051   * ALL_LOWERCASE, INITIAL_UPPERCASE, or MIXED_CASE, then a new feature containing one of those
052   * four values is returned. If the value of the feature cannot be characterized by one of these
053   * four values, then the empty list is returned (e.g. the value is an empty string, contains only
054   * white space, or contains only digits, etc.)
055   * 
056   * <P>
057   * This method was inspired by CapitalizationTypeTagger.py written by Steven Bethard.
058   * 
059   * @return a feature that has a value that is one of ALL_UPPERCASE, ALL_LOWERCASE,
060   *         INITIAL_UPPERCASE, or MIXED_CASE. Otherwise the empty list is returned.
061   */
062  public List<Feature> apply(Feature feature) {
063    String featureName = Feature.createName(DEFAULT_NAME, feature.getName());
064    Object featureValue = feature.getValue();
065    if (featureValue == null)
066      return Collections.emptyList();
067    else if (featureValue instanceof String) {
068      String value = featureValue.toString();
069      if (value == null || value.length() == 0)
070        return Collections.emptyList();
071
072      String lowerCaseValue = value.toLowerCase();
073      String upperCaseValue = value.toUpperCase();
074      if (lowerCaseValue.equals(upperCaseValue))
075        return Collections.emptyList();
076
077      if (value.equals(value.toLowerCase())) {
078        return Collections.singletonList(new Feature(
079            featureName,
080            CapitalType.ALL_LOWERCASE.toString()));
081      } else if (value.equals(value.toUpperCase())) {
082        return Collections.singletonList(new Feature(
083            featureName,
084            CapitalType.ALL_UPPERCASE.toString()));
085      }
086
087      if (CaseUtil.isInitialUppercase(value)) {
088        return Collections.singletonList(new Feature(
089            featureName,
090            CapitalType.INITIAL_UPPERCASE.toString()));
091      }
092
093      return Collections.singletonList(new Feature(featureName, CapitalType.MIXED_CASE.toString()));
094    } else
095      return Collections.emptyList();
096  }
097
098}