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.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 the 043 * count. If the count is zero, the value is not included in the output. 044 * 045 * @author Philipp Wetzler 046 */ 047public class MultiBagEncoder implements FeatureEncoder<NameNumber> { 048 049 private static final long serialVersionUID = -5280514188425612793L; 050 051 public MultiBagEncoder(String identifier, NameNumberNormalizer normalizer) { 052 this.identifier = identifier; 053 this.normalizer = normalizer; 054 } 055 056 public MultiBagEncoder(String identifier) { 057 this(identifier, new NoOpNormalizer()); 058 } 059 060 public MultiBagEncoder(NameNumberNormalizer normalizer) { 061 this(null, normalizer); 062 } 063 064 public MultiBagEncoder() { 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 String name = Feature.createName(prefix, key.toString()); 075 NameNumber fve = new NameNumber(name, frequencies.getCount(key)); 076 fves.add(fve); 077 } 078 079 normalizer.normalize(fves); 080 081 return fves; 082 } 083 084 public boolean encodes(Feature feature) { 085 if (!(feature.getValue() instanceof Counts)) 086 return false; 087 088 Counts counts = (Counts) feature.getValue(); 089 090 if (identifier == null) 091 return true; 092 093 if (identifier.equals(counts.getIdentifier())) 094 return true; 095 096 return false; 097 } 098 099 private String identifier; 100 101 private NameNumberNormalizer normalizer; 102 103}