PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // SymbolNode.cc 00004 // 00005 // Copyright (C) 2004 Hugo Larochelle 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: SymbolNode.cc 5388 2006-04-13 21:28:14Z lamblin $ 00037 ******************************************************* */ 00038 00039 // Authors: Hugo Larochelle 00040 00044 #include "SymbolNode.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 SymbolNode::SymbolNode() 00050 /* ### Initialize all fields to their default value */ 00051 { 00052 frequence = 0; 00053 symbol = -1; 00054 } 00055 00056 SymbolNode::SymbolNode(int symbol_) 00057 /* ### Initialize all fields to their default value */ 00058 { 00059 frequence = 0; 00060 symbol = symbol_; 00061 } 00062 00063 PLEARN_IMPLEMENT_OBJECT(SymbolNode, 00064 "Node for a NGramTree", 00065 "Everything else is obvious\n" 00066 ); 00067 00068 void SymbolNode::declareOptions(OptionList& ol) 00069 { 00070 declareOption(ol, "frequence", &SymbolNode::frequence, OptionBase::learntoption, 00071 "Frequence associated to this node"); 00072 declareOption(ol, "frequencies", &SymbolNode::frequencies, OptionBase::learntoption, 00073 "Frequencies associated to a symbol at this node"); 00074 declareOption(ol, "symbol", &SymbolNode::symbol, OptionBase::buildoption, 00075 "Symbol associated to this node"); 00076 declareOption(ol, "children", &SymbolNode::children, OptionBase::buildoption, 00077 "Children of this node"); 00078 00079 inherited::declareOptions(ol); 00080 } 00081 00082 void SymbolNode::build_(){} 00083 00084 // ### Nothing to add here, simply calls build_ 00085 void SymbolNode::build() 00086 { 00087 inherited::build(); 00088 build_(); 00089 } 00090 00091 void SymbolNode::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00092 { 00093 inherited::makeDeepCopyFromShallowCopy(copies); 00094 00095 deepCopyField(children, copies); 00096 deepCopyField(frequencies, copies); 00097 //PLERROR("SymbolNode::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00098 } 00099 00100 PP<SymbolNode> SymbolNode::add(int child_) 00101 { 00102 PP<SymbolNode> cn; 00103 map<int,PP<SymbolNode> >::iterator it = children.find(child_); 00104 if(it == children.end()) 00105 { 00106 cn = new SymbolNode(child_); 00107 children[child_] = cn; 00108 } 00109 else 00110 { 00111 cn = it->second; 00112 } 00113 return cn; 00114 } 00115 00116 PP<SymbolNode> SymbolNode::child(int child_) 00117 { 00118 if(children.find(child_) == children.end()) 00119 return 0; 00120 00121 return children.find(child_)->second; 00122 } 00123 00124 TVec<PP<SymbolNode> > SymbolNode::getChildren() 00125 { 00126 TVec<PP<SymbolNode> > ret(children.size()); 00127 int i=0; 00128 for(map<int,PP<SymbolNode> >::iterator it = children.begin(); it != children.end(); it++) 00129 ret[i++] = it->second; 00130 return ret; 00131 } 00132 00133 int SymbolNode::freq(int symbol) 00134 { 00135 map<int,int>::iterator it = frequencies.find(symbol); 00136 if(it == frequencies.end()) 00137 return 0; 00138 else 00139 return it->second; 00140 } 00141 00142 void SymbolNode::incr(int symbol) 00143 { 00144 incr(); 00145 map<int,int>::iterator it = frequencies.find(symbol); 00146 if(it == frequencies.end()) 00147 frequencies[symbol] = 1; 00148 else 00149 { 00150 int temp = it->second; 00151 frequencies[symbol] = temp+1; 00152 } 00153 } 00154 00155 00156 } // end of namespace PLearn 00157 00158 00159 /* 00160 Local Variables: 00161 mode:c++ 00162 c-basic-offset:4 00163 c-file-style:"stroustrup" 00164 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00165 indent-tabs-mode:nil 00166 fill-column:79 00167 End: 00168 */ 00169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :