PLearn 0.1
ConditionalDictionary.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ConditionalDictionary.cc
00004 //
00005 // Copyright (C) 2004 Hugo Larochelle, Christopher Kermorvant
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: ConditionalDictionary.cc 4184 2005-10-04 14:02:12Z larocheh $ 
00037  ******************************************************* */
00038 
00039 // Authors: Hugo Larochelle
00040 
00044 #include "ConditionalDictionary.h"
00045 #include <plearn/base/stringutils.h>
00046 #include "plearn/io/openFile.h"
00047 #include "plearn/io/fileutils.h"
00048 
00049 namespace PLearn {
00050 using namespace std;
00051 
00052 string ConditionalDictionary::get_option_string(TVec<string> options)
00053 {
00054     string ret = options[0];
00055     for(int i=1; i<options.length(); i++)
00056         ret = ret + " " + options[i];
00057     if(options_to_lower_case)
00058         return lowerstring(ret);
00059     else
00060         return ret;
00061 }
00062 
00063 ConditionalDictionary::ConditionalDictionary()
00064     : options_to_lower_case(false)
00065 {}
00066   
00067 PLEARN_IMPLEMENT_OBJECT(ConditionalDictionary,
00068                         "Dictionary where possible symbols depend on the option fields.",
00069                         "The mapping between the options and possible symbols is defined\n"
00070                         "by a mapping file, where each row has the form:\n"
00071                         "\n"
00072                         "OPTION_1 ... OPTION_M[\tab]TAG_1 TAG_2 TAG)3 ...\n"
00073                         "\n"
00074                         "with [\tab] being the tabulation character. For\n"
00075                         "instance, a Part Of Speech Dictionary could be\n"
00076                         "defined using lines like:\n"
00077                         "\n"
00078                         "pet[\tab]NN VB VBD\n"
00079                         "\n"
00080                         "When the option fields are not found in the mapping,\n"
00081                         "then the possible values are simply the set of\n"
00082                         "all possible symbols (TAG fields) of the Dictionary.\n");
00083   
00084 void ConditionalDictionary::declareOptions(OptionList& ol)
00085 {
00086     declareOption(ol, "options_to_lower_case", &ConditionalDictionary::options_to_lower_case, OptionBase::buildoption, "Indication that the given options should be put to lower case");
00087     declareOption(ol, "mapping_file_path", &ConditionalDictionary::mapping_file_path, OptionBase::buildoption, "Path to the mapping file");
00088     inherited::declareOptions(ol);
00089 }
00090   
00091 void ConditionalDictionary::build_()
00092 {
00093     if(options_to_symbols.size() == 0)
00094     {
00095         int last_update_mode = update_mode;
00096         update_mode = UPDATE;
00097         
00098         TVec<string> tokens;
00099         TVec<string> options;
00100         TVec<string> symbols;
00101         int id;
00102         string line; 
00103         PStream input_stream = openFile(mapping_file_path, PStream::raw_ascii);
00104         while (!input_stream.eof()){
00105             getNextNonBlankLine(input_stream, line);
00106             tokens = split(line, "\t");
00107             if(tokens.length() != 2)
00108                 PLERROR("In ConditionalDictionary::build_(): line \"%s\" is in bad format",line.c_str());
00109             options = split(tokens[0]," ");
00110             symbols = split(tokens[1]," ");
00111             
00112             Vec symbols_id = Vec(0); 
00113 
00114             // Insert symbols in dictionaries
00115             for(int i=0; i<symbols.size(); i++)
00116             {
00117                 id = getId(symbols[i]);
00118                 if(symbols_id.find(id) < 0)
00119                     symbols_id.append(id);
00120             }
00121             // Insert symbols in map
00122             tmp_str = get_option_string(options);
00123             if(options_to_symbols.find(tmp_str) == options_to_symbols.end())
00124                 PLERROR("In ConditionalDictionary::build_(): there is more than one mapping for option \"%s\"",tmp_str.c_str());
00125             options_to_symbols[tmp_str] = symbols_id;
00126         }
00127         
00128         update_mode = last_update_mode;
00129     }
00130 }
00131 
00132 // ### Nothing to add here, simply calls build_
00133 void ConditionalDictionary::build()
00134 {
00135     inherited::build();
00136     build_();
00137 }
00138 
00139 void ConditionalDictionary::getValues(TVec<string> options, Vec& values)
00140 { 
00141     if(options.length() == 0)
00142         inherited::getValues(options, values);
00143 
00144     string ret = get_option_string(options);
00145     if(options_to_symbols.find(ret) != options_to_symbols.end())
00146         values << options_to_symbols[ret];
00147     else
00148         inherited::getValues(options, values);
00149 }
00150 
00151 int ConditionalDictionary::size(TVec<string> options){
00152     if(options.length() == 0)
00153         return inherited::size();
00154     else         
00155     {
00156         string ret = get_option_string(options);
00157         if(options_to_symbols.find(ret) != options_to_symbols.end())
00158             return options_to_symbols[ret].length();
00159         else
00160             return inherited::size();
00161     }
00162 }
00163 
00164 void ConditionalDictionary::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00165 {
00166     inherited::makeDeepCopyFromShallowCopy(copies);    
00167     deepCopyField(options_to_symbols, copies);
00168     deepCopyField(tmp_sym, copies);
00169 }
00170 
00171 } // end of namespace PLearn
00172 
00173 
00174 /*
00175   Local Variables:
00176   mode:c++
00177   c-basic-offset:4
00178   c-file-style:"stroustrup"
00179   c-file-offsets:((innamespace . 0)(inline-open . 0))
00180   indent-tabs-mode:nil
00181   fill-column:79
00182   End:
00183 */
00184 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines