00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifdef HAVE_CONFIG_H
00011 #include <config.h>
00012 #endif
00013
00014 #ifdef HAVE_UNISTD_H
00015 #include <unistd.h>
00016 #endif
00017
00018 #include <clib.h>
00019 #include <WordKey.h>
00020
00021 #include <WordKeySemantic.h>
00022
00023 WordKeySemantic::WordKeySemantic(WordContext *ncontext)
00024 {
00025 context = ncontext;
00026 int nfields = context->GetKeyInfo().nfields;
00027 document = new int[nfields];
00028 document_length = 0;
00029 location = -1;
00030 verbose = 0;
00031 }
00032
00033 WordKeySemantic::~WordKeySemantic()
00034 {
00035 if(document) delete [] document;
00036 }
00037
00038 int WordKeySemantic::Initialize(int* document_arg, int document_length_arg, int location_arg, int uniq_arg)
00039 {
00040 memcpy((char*)document, (char*)document_arg, document_length_arg * sizeof(int));
00041 document_length = document_length_arg;
00042 location = location_arg;
00043 uniq = uniq_arg;
00044
00045 for(int i = 0; i < document_length; i++) {
00046 if(i + 1 < document_length) {
00047 if(document[i] >= document[i+1]) {
00048 fprintf(stderr, "WordKeySemantic::Initialize: the document list must be a list of strictly increasing numbers (%d >= %d)\n", document[i], document[i+1]);
00049 return NOTOK;
00050 }
00051 }
00052 }
00053 return OK;
00054 }
00055
00056 void WordKeySemantic::RealmSet(const WordKey& from, WordKey& to)
00057 {
00058 to.Clear();
00059 RealmCopy(from, to);
00060 }
00061
00062 void WordKeySemantic::RealmCopy(const WordKey& from, WordKey& to)
00063 {
00064 for(int i = WORD_KEY_WORD + 1; i < document[i]; i++)
00065 to.Set(i, from.Get(i));
00066 }
00067
00068 void WordKeySemantic::RealmUndefined(WordKey& key)
00069 {
00070 for(int i = WORD_KEY_WORD + 1; i < document[i]; i++)
00071 key.Undefined(i);
00072 }
00073
00074 void WordKeySemantic::RealmClear(WordKey& key)
00075 {
00076 key.Clear();
00077 for(int i = WORD_KEY_WORD + 1; i < document[i]; i++)
00078 key.Set(i, 0);
00079 }
00080
00081 void WordKeySemantic::DocumentSet(const WordKey& from, WordKey& to)
00082 {
00083 to.Clear();
00084 DocumentCopy(from, to);
00085 }
00086
00087 void WordKeySemantic::DocumentCopy(const WordKey& from, WordKey& to)
00088 {
00089 for(int i = 0; i < document_length; i++)
00090 to.Set(document[i], from.Get(document[i]));
00091 }
00092
00093 int WordKeySemantic::DocumentCompare(const WordKey& a, const WordKey& b)
00094 {
00095 int ret = 1;
00096 for(int i = 0; i < document_length; i++) {
00097 int idx = document[i];
00098 if((a.IsDefined(idx) && b.IsDefined(idx)) &&
00099 (ret = a.Get(idx) - b.Get(idx)) != 0) return ret;
00100 }
00101 return ret;
00102 }
00103
00104 int WordKeySemantic::DocumentClear(WordKey& key)
00105 {
00106 for(int i = 0; i < document_length; i++)
00107 key.Set(document[i], 0);
00108 return 0;
00109 }
00110
00111 int WordKeySemantic::DocumentUndefined(WordKey& key)
00112 {
00113 for(int i = 0; i < document_length; i++)
00114 key.Undefined(document[i]);
00115 return 0;
00116 }
00117
00118 void WordKeySemantic::DocumentNext(WordKey& key, int use_uniq)
00119 {
00120 if(use_uniq)
00121 key.SetToFollowing(uniq);
00122 else
00123 key.SetToFollowing(document[document_length-1]);
00124 }
00125
00126
00127 void WordKeySemantic::LocationSet(const WordKey& from, WordKey& to)
00128 {
00129 to = from;
00130 to.Undefined(WORD_KEY_WORD);
00131
00132 for(int i = location + 1; i < to.NFields(); i++) {
00133 to.Undefined(i);
00134 }
00135 }
00136
00137 int WordKeySemantic::LocationCompare(const WordKey& expected, const WordKey& actual, int proximity )
00138 {
00139 int ret = 1;
00140
00141 for(int i = 1; i < document[0]; i++) {
00142 if((expected.IsDefined(i) && actual.IsDefined(i)) &&
00143 (ret = expected.Get(i) - actual.Get(i)) != 0) return ret;
00144 }
00145
00146 if((ret = DocumentCompare(expected, actual)) != 0) return ret;
00147
00148
00149
00150 if((expected.IsDefined(location) && actual.IsDefined(location)) &&
00151 (ret = expected.Get(location) - actual.Get(location))) {
00152 if(proximity < 0) {
00153
00154
00155
00156 proximity *= 2;
00157 if(ret < 0 && ret >= proximity)
00158 ret = 0;
00159 } else {
00160
00161
00162
00163 if(ret < 0 && ret >= -proximity)
00164 ret = 0;
00165 }
00166 }
00167 return ret;
00168 }
00169
00170 void WordKeySemantic::LocationNext(WordKey& key)
00171 {
00172 key.SetToFollowing(location);
00173 }
00174
00175 void WordKeySemantic::LocationNearLowest(WordKey& key, int proximity)
00176 {
00177 if(proximity < 0) {
00178 if(key.Underflow(location, proximity))
00179 key.Get(location) = 0;
00180 else
00181 key.Get(location) += proximity;
00182 }
00183 }
00184
00185 void WordKeySemantic::Location2Document(WordKey& key)
00186 {
00187 key.Undefined(location);
00188 }
00189