FreeLing  4.0
semgraph.h
Go to the documentation of this file.
00001 
00002 //
00003 //    FreeLing - Open Source Language Analyzers
00004 //
00005 //    Copyright (C) 2004   TALP Research Center
00006 //                         Universitat Politecnica de Catalunya
00007 //
00008 //    This library is free software; you can redistribute it and/or
00009 //    modify it under the terms of the GNU General Public
00010 //    License as published by the Free Software Foundation; either
00011 //    version 3 of the License, or (at your option) any later version.
00012 //
00013 //    This library is distributed in the hope that it will be useful,
00014 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //    General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU General Public
00019 //    License along with this library; if not, write to the Free Software
00020 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021 //
00022 //    contact: Lluis Padro (padro@lsi.upc.es)
00023 //             TALP Research Center
00024 //             despatx C6.212 - Campus Nord UPC
00025 //             08034 Barcelona.  SPAIN
00026 //
00028 
00029 
00030 #ifndef SEMGRAPH_H
00031 #define SEMGRAPH_H
00032 
00033 #include <string>
00034 #include <vector>
00035 #include <list>
00036 #include <map>
00037 #include <set>
00038 
00039 namespace freeling {
00040 
00041 
00042   namespace semgraph {
00043 
00047 
00048     class semantic_graph;
00049 
00052   
00053     class SG_mention {
00054     private:
00055       std::wstring id;
00056       std::wstring sentenceId;
00057       std::list<std::wstring> words;
00058     public:
00059       SG_mention();
00060       SG_mention(const std::wstring &mid, const std::wstring &sid, const std::list<std::wstring> &wds);
00061       ~SG_mention();
00062       std::wstring get_id() const;
00063       std::wstring get_sentence_id() const;
00064       const std::list<std::wstring> & get_words() const;
00065     };
00066     
00069 
00070     typedef enum {ENTITY,WORD} entityType;
00071     
00072     class SG_entity {  
00073       friend class semantic_graph;
00074     private:
00076       std::wstring id;
00078       std::wstring lemma;
00080       std::wstring semclass;
00082       entityType type;
00084       std::wstring sense;
00086       std::list<std::wstring> synonyms;
00088       std::list<std::pair<std::wstring,std::wstring> > uris;
00090       std::vector<SG_mention> mentions;
00091 
00092     protected:
00093       void add_mention(const SG_mention &m);
00094       void set_id(const std::wstring &eid);
00095 
00096     public:
00097       SG_entity();
00098       SG_entity(const std::wstring &elemma,
00099                 const std::wstring &eclass, 
00100                 entityType type, 
00101                 const std::wstring &sense=L"");
00102       ~SG_entity();
00103 
00104       void set_lemma(const std::wstring &lem);
00105       std::wstring get_id() const;
00106       std::wstring get_lemma() const;
00107       std::wstring get_semclass() const;
00108       entityType get_type() const;
00109       std::wstring get_sense() const;
00110       const std::vector<SG_mention> &get_mentions() const;
00111       const std::list<std::wstring> &get_synonyms() const;
00112       const std::list<std::pair<std::wstring,std::wstring> > &get_URIs() const;
00113 
00114       void set_synonyms(const std::list<std::wstring> &syn);
00115       void add_URI(const std::wstring &kb, const std::wstring &uri);
00116     };
00117 
00120   
00121     class SG_argument {
00122     private:
00123       std::wstring role;
00124       std::wstring entity;
00125     public:
00126       SG_argument();
00127       SG_argument(const std::wstring &r, const std::wstring &e);
00128       ~SG_argument();
00129 
00130       std::wstring get_role() const;
00131       std::wstring get_entity() const;
00132     };
00133 
00136   
00137     class SG_frame {
00138       friend class semantic_graph;
00139     private:
00141       std::wstring id;
00143       std::wstring lemma;
00145       std::wstring sense;
00147       std::list<std::wstring> synonyms;
00149       std::list<std::pair<std::wstring,std::wstring> > uris;
00151       std::wstring tokenId;
00153       std::wstring sentenceId;
00155       std::vector<SG_argument> arguments;
00156 
00157     protected:
00158       // add argument to frame
00159       void add_argument(const std::wstring &role, const std::wstring &eid);
00160       void set_id(const std::wstring &fid);
00161 
00162     public:
00163       // constructor
00164       SG_frame();
00165       SG_frame(const std::wstring &lem, const std::wstring &sns, const std::wstring &tk, const std::wstring &sid);
00166       ~SG_frame();
00167 
00168       std::wstring get_id() const;
00169       std::wstring get_lemma() const;
00170       std::wstring get_sense() const;
00171       std::wstring get_token_id() const;
00172       std::wstring get_sentence_id() const;
00173       const std::vector<SG_argument> &get_arguments() const;
00174       const std::list<std::wstring> &get_synonyms() const;
00175       const std::list<std::pair<std::wstring,std::wstring> > &get_URIs() const;
00176 
00177       void set_synonyms(const std::list<std::wstring> &syn);
00178       void add_URI(const std::wstring &kb, const std::wstring &uri);
00179     };
00180 
00183   
00184     class semantic_graph {
00185     private:
00186       // list of entity nodes in the graph. indexed by id
00187       std::vector<SG_entity> entities;
00188       // list of event nodes in the graph, with relations to their arguments
00189       std::vector<SG_frame> frames;
00190     
00191       // given a mention id, get entity it belongs to
00192       std::map<std::wstring,std::wstring> mention_idx;
00193       // given a lemma, get entity with that lemma
00194       std::map<std::wstring,std::wstring> lemma_idx;
00195       // given an entity id, get its position in vector
00196       std::map<std::wstring,int> entity_idx;
00197       // given a frame id, get its position in vector
00198       std::map<std::wstring,int> frame_idx;
00199 
00200       // set to store nodes that are argument to some predicate
00201       std::set<std::wstring> is_arg;
00202       // set to store predicate nodes that have some argument
00203       std::set<std::wstring> has_args;
00204 
00205       // auxiliary counter to number graph nodes
00206       int last_id;
00207 
00208     public:
00209       semantic_graph();
00210       ~semantic_graph();
00211       
00212       std::wstring add_entity(SG_entity &ent);
00213       std::wstring add_frame(SG_frame &fr);
00214 
00215       const SG_frame & get_frame(const std::wstring &fid) const;
00216       SG_frame & get_frame(const std::wstring &fid);
00217 
00218       std::wstring get_entity_id_by_mention(const std::wstring &sid,const std::wstring &wid) const;
00219       std::wstring get_entity_id_by_lemma(const std::wstring &lemma,const std::wstring &sens=L"") const;
00220       SG_entity & get_entity(const std::wstring &eid);
00221       const SG_entity & get_entity(const std::wstring &eid) const;
00222 
00223       const std::vector<SG_entity> & get_entities() const;
00224       std::vector<SG_entity> & get_entities();
00225 
00226       const std::vector<SG_frame> & get_frames() const;
00227       std::vector<SG_frame> & get_frames();
00228 
00229       void add_mention_to_entity(const std::wstring &eid, const SG_mention &m);
00230       void add_argument_to_frame(const std::wstring &fid, const std::wstring &role, const std::wstring &eid);
00231 
00232       bool is_argument(const std::wstring &eid) const;     
00233       bool has_arguments(const std::wstring &fid) const;
00234 
00235       bool empty() const;
00236     };
00237 
00238   }
00239 }
00240 
00241 #endif