COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/include/coopy/FMap.h
Go to the documentation of this file.
00001 #ifndef COOPY_FMAP
00002 #define COOPY_FMAP
00003 
00004 #include <string>
00005 
00006 #include <coopy/FVal.h>
00007 #include <coopy/Dbg.h>
00008 #include <coopy/EfficientMap.h>
00009 
00010 
00011 namespace coopy {
00012   namespace cmp {
00013     typedef std::string Feature;
00014     template <class FVal> class FPolyMap;
00015     typedef FPolyMap<FSingleVal> FMap;
00016     typedef FPolyMap<FMultiVal> FMultiMap;
00017   }
00018 }
00019 
00020 
00021 template <class FVal>
00022 class coopy::cmp::FPolyMap {
00023 public:
00024   typedef efficient_map<Feature,FVal> Cache;
00025   int ct;
00026   int xcurr, ycurr;
00027   Cache f;
00028   coopy::store::SparseFloatSheet& rowMatch;
00029   bool query;
00030   int len;
00031 
00032   FPolyMap(coopy::store::SparseFloatSheet& sheet, int len) : rowMatch(sheet) {
00033     query = false;
00034     ct = 0;
00035     this->len = len;
00036   }
00037 
00038   coopy::store::FloatSheet& getMatch();
00039 
00040   //void setSize(int w, int h) {
00041   //rowMatch.resize(w,h,0);
00042   //  }
00043 
00044   void setCurr(int x, int y) {
00045     xcurr = x;
00046     ycurr = y;
00047   }
00048 
00049   void resetCount() {
00050     ct = 0;
00051     query = -1;
00052   }
00053 
00054   void resetCache() {
00055     f.clear();
00056   }
00057 
00058   void queryBit(std::string txt) {
00059     typename Cache::iterator it = f.find(txt);
00060     if (it!=f.end()) {
00061       it->second.apply(rowMatch,ycurr);
00062     }
00063     ct++;
00064     summarize();
00065   }
00066 
00067   void addBit(std::string txt, bool alt) {
00068     if (f.find(Feature(txt))==f.end()) {
00069       if (alt) return;
00070       f[Feature(txt)] = FVal();
00071     }
00072     FVal& val = f[Feature(txt)];
00073     val.setIndex(ycurr,alt);
00074     if (!alt) {
00075       ct++;
00076       summarize();
00077     }
00078   }
00079 
00080   void applyBit(const std::string& bit, bool query, bool alt) {
00081     if (query) {
00082       queryBit(bit);
00083     } else {
00084       addBit(bit,alt);
00085     }
00086   }
00087 
00088 
00089   void add(std::string txt, bool query, bool alt, int ctrl) {
00090     //printf("add %s %d %d\n", txt.c_str(), query, ctrl);
00091     this->query = query;
00092     applyBit(txt,query,alt);
00093     if (ctrl!=0) {
00094       txt = std::string("^") + txt + "$";
00095       int len = txt.length();
00096       std::string txt_low = txt;
00097       for (size_t c=0; c<txt_low.length(); c++) {
00098         txt_low[c] = tolower(txt_low[c]);
00099       }
00100       bool need_case = (txt_low!=txt);
00101       int base = 8-ctrl*2;
00102       for (int k=base; k<10; k+=2) {
00103         for (int i=0; i<len-k; i++){
00104           std::string part = txt.substr(i,k+1);
00105           applyBit(part,query,alt);
00106           if (need_case) {
00107             std::string low = txt_low.substr(i,k+1);
00108             if (low!=part) {
00109               applyBit(low,query,alt);
00110             }
00111           }
00112         }
00113       }
00114     }
00115   }
00116 
00117   static int getCtrlMax() { return 4; }
00118 
00119   void summarize(bool force = false) {
00120     if (ct%10000==0 || force) {
00121       //dbg_printf("%s %d features\n", query?"Queried":"Added",ct);
00122     }
00123   }
00124 
00125 };
00126 
00127 
00128 #endif
00129 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines