PLearn 0.1
WordNetFeatureSet.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // WordNetFeatureSet.cc
00004 //
00005 // Copyright (C) 2006 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 // Authors: Hugo Larochelle
00036 
00040 #include "WordNetFeatureSet.h"
00041 #include <plearn/dict/WordNetSenseDictionary.h>
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 PLEARN_IMPLEMENT_OBJECT(
00047     WordNetFeatureSet,
00048     "FeatureSet with features from WordNet.",
00049     "The feature of a token (word) corresponds to the set of its possible\n"
00050     "WordNet senses or synsets. The hierarchy of WordNet can also be\n"
00051     "used to obtain the synset ancestors of these senses and use\n"
00052     "them as additional features. The input token can also be stemmed before\n"
00053     "doing the WordNet searches.\n"
00054     );
00055 
00056 WordNetFeatureSet::WordNetFeatureSet()
00057     : stem_token(true), use_wordnet_hierarchy(true)
00058 {
00059     if(wninit()<0)
00060         PLERROR("In WordNetFeatureSet(): could not open WordNet database files");
00061 }
00062 
00063 // ### Nothing to add here, simply calls build_
00064 void WordNetFeatureSet::build()
00065 {
00066     inherited::build();
00067     build_();
00068 }
00069 
00070 void WordNetFeatureSet::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00071 {
00072     inherited::makeDeepCopyFromShallowCopy(copies);
00073     //PLERROR("WordNetFeatureSet::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00074 }
00075 
00076 void WordNetFeatureSet::declareOptions(OptionList& ol)
00077 {
00078     declareOption(ol, "stem_token", &WordNetFeatureSet::stem_token, 
00079                   OptionBase::buildoption, 
00080                   "Indication that the input token should be stemmed");
00081     declareOption(ol, "use_wordnet_hierarchy", 
00082                   &WordNetFeatureSet::use_wordnet_hierarchy, 
00083                   OptionBase::buildoption, 
00084                   "Indication that features should include information from the\n"
00085                   "WordNet hierarchy, not just the token's possible senses.\n");
00086     // Now call the parent class' declareOptions
00087     inherited::declareOptions(ol);
00088 }
00089 
00090 void WordNetFeatureSet::build_()
00091 {}
00092 
00093 void WordNetFeatureSet::getNewFeaturesString(string token, TVec<string>& feats_str)
00094 {
00095     feats_str.resize(0);
00096     s_str.resize(0);
00097     anc_str.resize(0);
00098     // Extract senses for all possible POS
00099 
00100     // NOUN
00101     extractSenses(token,NOUN,"synset_key",s_str, tagcnts, anc_str, 
00102                   use_wordnet_hierarchy);
00103 
00104     if(stem_token) 
00105     {
00106         stemsOfWord(token,NOUN,stems);
00107         for(int i=0; i<stems.length(); i++)
00108         {
00109             if(token != stems[i])
00110                 extractSenses(stems[i],NOUN,"synset_key",s_str, tagcnts, 
00111                               anc_str, use_wordnet_hierarchy);
00112         }
00113     }
00114 
00115     // VERB
00116     extractSenses(token,VERB,"synset_key",s_str, tagcnts, anc_str, 
00117                   use_wordnet_hierarchy);
00118 
00119     if(stem_token) 
00120     {
00121         stemsOfWord(token,VERB,stems);
00122         for(int i=0; i<stems.length(); i++)
00123             if(token != stems[i])
00124                 extractSenses(stems[i],VERB,"synset_key",s_str, tagcnts, 
00125                               anc_str, use_wordnet_hierarchy);
00126     }
00127        
00128     // ADJ
00129     extractSenses(token,ADJ,"synset_key",s_str, tagcnts, anc_str, 
00130                   use_wordnet_hierarchy);
00131     
00132     if(stem_token) 
00133     {
00134         stemsOfWord(token,ADJ,stems);
00135         for(int i=0; i<stems.length(); i++)
00136             if(token != stems[i])
00137                 extractSenses(stems[i],ADJ,"synset_key",s_str, tagcnts, 
00138                               anc_str, use_wordnet_hierarchy);
00139     }
00140        
00141     // ADV
00142     extractSenses(token,ADV,"synset_key",s_str, tagcnts, anc_str, 
00143                   use_wordnet_hierarchy);
00144 
00145     if(stem_token) 
00146     {
00147         stemsOfWord(token,ADV,stems);
00148         for(int i=0; i<stems.length(); i++)
00149             if(token != stems[i])
00150                 extractSenses(stems[i],ADV,"synset_key",s_str, tagcnts, 
00151                               anc_str, use_wordnet_hierarchy);
00152     }
00153 
00154     if(use_wordnet_hierarchy)
00155         for(int t=0; t<anc_str.length(); t++)
00156             for(int s=0; s<anc_str[t].length(); s++)
00157                 feats_str.appendIfNotThereAlready(anc_str[t][s]);
00158     else
00159         for(int t=0; t<s_str.length(); t++)
00160             feats_str.appendIfNotThereAlready(s_str[t]);
00161 }
00162 
00163 } // end of namespace PLearn
00164 
00165 
00166 /*
00167   Local Variables:
00168   mode:c++
00169   c-basic-offset:4
00170   c-file-style:"stroustrup"
00171   c-file-offsets:((innamespace . 0)(inline-open . 0))
00172   indent-tabs-mode:nil
00173   fill-column:79
00174   End:
00175 */
00176 // 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