001/** 002 * Copyright (c) 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.io.File; 027import java.io.IOException; 028import java.io.PrintWriter; 029import java.util.ArrayList; 030import java.util.List; 031 032import org.cleartk.ml.Feature; 033import org.cleartk.ml.encoder.CleartkEncoderException; 034import org.cleartk.ml.encoder.FeatureEncoderUtil; 035import org.cleartk.util.collection.CompressedStringBiMap; 036 037/** 038 * <br> 039 * Copyright (c) 2009, Regents of the University of Colorado <br> 040 * All rights reserved. 041 * 042 * @author Philip Ogren 043 */ 044 045public class NameNumberFeaturesEncoder extends 046 FeaturesEncoder_ImplBase<List<NameNumber>, NameNumber> { 047 048 private static final long serialVersionUID = 7508330794260661987L; 049 050 public static final String LOOKUP_FILE_NAME = "names-lookup.txt"; 051 052 private CompressedStringBiMap csbm; 053 054 private boolean compressFeatures; 055 056 public boolean isCompressFeatures() { 057 return compressFeatures; 058 } 059 060 private boolean allowNewFeatures = true; 061 062 private boolean sortNameLookup; 063 064 public NameNumberFeaturesEncoder(boolean compressFeatures, boolean sortNameLookup) { 065 super(); 066 this.compressFeatures = compressFeatures; 067 this.sortNameLookup = sortNameLookup; 068 if (compressFeatures) { 069 csbm = new CompressedStringBiMap(); 070 } 071 } 072 073 public NameNumberFeaturesEncoder() { 074 this(false, false); 075 } 076 077 @Override 078 public List<NameNumber> encodeAll(Iterable<Feature> features) throws CleartkEncoderException { 079 List<NameNumber> returnValues = new ArrayList<NameNumber>(); 080 081 for (Feature feature : features) { 082 for (NameNumber nameNumber : this.encode(feature)) { 083 nameNumber.name = compress(escape(nameNumber.name)); 084 if (nameNumber.name != null) { 085 returnValues.add(nameNumber); 086 } 087 } 088 } 089 return returnValues; 090 } 091 092 @Override 093 public void finalizeFeatureSet(File outputDirectory) throws IOException { 094 this.allowNewFeatures = false; 095 096 if (compressFeatures) { 097 File lookupFile = new File(outputDirectory, LOOKUP_FILE_NAME); 098 PrintWriter writer = new PrintWriter(lookupFile); 099 csbm.write(writer, sortNameLookup); 100 writer.close(); 101 } 102 } 103 104 private String escape(String string) { 105 return FeatureEncoderUtil.escape(string, new char[] { '=' }); 106 } 107 108 private String compress(String featureString) { 109 if (compressFeatures) { 110 if (allowNewFeatures) { 111 return csbm.getOrGenerateKey(featureString); 112 } else { 113 return csbm.inverse().get(featureString); 114 } 115 } 116 return featureString; 117 } 118}