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.ml.encoder.features; 025 026import java.util.ArrayList; 027import java.util.List; 028 029import org.cleartk.ml.Feature; 030import org.cleartk.ml.encoder.features.normalizer.NameNumberNormalizer; 031import org.cleartk.ml.encoder.features.normalizer.NoOpNormalizer; 032import org.cleartk.ml.feature.Counts; 033 034/** 035 * <br> 036 * Copyright (c) 2009, Regents of the University of Colorado <br> 037 * All rights reserved. 038 * 039 * Encodes a Counts feature into a bag-of-words type feature. 040 * 041 * This works on arbitrary types of objects, not just words. If an object's count is larger than 042 * zero, the corresponding value (named using the .toString method of the object) is set to 1. If 043 * the count is zero, the value is not included in the output. 044 * 045 * @author Philipp Wetzler 046 */ 047public class BagEncoder implements FeatureEncoder<NameNumber> { 048 049 private static final long serialVersionUID = -5280514188425612793L; 050 051 public BagEncoder(String identifier, NameNumberNormalizer normalizer) { 052 this.identifier = identifier; 053 this.normalizer = normalizer; 054 } 055 056 public BagEncoder(String identifier) { 057 this(identifier, new NoOpNormalizer()); 058 } 059 060 public BagEncoder(NameNumberNormalizer normalizer) { 061 this(null, normalizer); 062 } 063 064 public BagEncoder() { 065 this(null, new NoOpNormalizer()); 066 } 067 068 public List<NameNumber> encode(Feature feature) { 069 List<NameNumber> fves = new ArrayList<NameNumber>(); 070 Counts frequencies = (Counts) feature.getValue(); 071 072 String prefix = frequencies.getFeatureName(); 073 for (Object key : frequencies.getValues()) { 074 if (frequencies.getCount(key) > 0) { 075 String name = Feature.createName(prefix, key.toString()); 076 NameNumber fve = new NameNumber(name, 1); 077 fves.add(fve); 078 } 079 } 080 081 normalizer.normalize(fves); 082 083 return fves; 084 } 085 086 public boolean encodes(Feature feature) { 087 if (!(feature.getValue() instanceof Counts)) 088 return false; 089 090 Counts counts = (Counts) feature.getValue(); 091 092 if (identifier == null) 093 return true; 094 095 if (identifier.equals(counts.getIdentifier())) 096 return true; 097 098 return false; 099 } 100 101 private String identifier; 102 103 private NameNumberNormalizer normalizer; 104 105}