From: Anders R. <ma...@us...> - 2005-12-08 19:22:56
|
Update of /cvsroot/planeshift/planeshift/src/server/bulkobjects In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5834/src/server/bulkobjects Modified Files: dictionary.cpp dictionary.h psnpcdialog.cpp Log Message: - Rewrote NpcTerms to use pointers instead of looking up strings. Index: dictionary.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/server/bulkobjects/dictionary.cpp,v retrieving revision 1.86 retrieving revision 1.87 diff -C2 -d -r1.86 -r1.87 *** dictionary.cpp 8 Dec 2005 07:51:20 -0000 1.86 --- dictionary.cpp 8 Dec 2005 19:22:14 -0000 1.87 *************** *** 112,139 **** } ! void NPCDialogDict::AddTerm(const char *term, const char *synonym, const char *moreGeneral) { ! if (FindTerm(term) != NULL) ! return; ! NpcTerm *newphrase = new NpcTerm; newphrase->term = term; - newphrase->synonym = synonym; - newphrase->moreGeneral = moreGeneral; phrases.Insert(newphrase,TREE_OWNS_DATA); // TRUE means tree owns data - } - - void NPCDialogDict::FixMissingReferences() - { - BinaryRBIterator<NpcTerm> iter(&phrases); - NpcTerm * term; ! for (term = iter.First(); term; term = ++iter) ! { ! if (term->synonym.Length()>0 && !FindTerm(term->synonym)) ! AddTerm(term->synonym, "", ""); ! if (term->moreGeneral.Length()>0 && !FindTerm(term->moreGeneral)) ! AddTerm(term->moreGeneral, "", ""); ! } } --- 112,125 ---- } ! NpcTerm* NPCDialogDict::AddTerm(const char *term) { ! NpcTerm * npc_term = FindTerm(term); ! if (npc_term) return npc_term; ! NpcTerm *newphrase = new NpcTerm; newphrase->term = term; phrases.Insert(newphrase,TREE_OWNS_DATA); // TRUE means tree owns data ! return newphrase; } *************** *** 152,158 **** for (unsigned int i=0; i<result.Count(); i++) ! AddTerm(result[i]["word"], result[i]["synonym_of"], result[i]["more_general"]); - FixMissingReferences(); return true; } --- 138,163 ---- for (unsigned int i=0; i<result.Count(); i++) ! { ! NpcTerm* term = AddTerm(result[i]["word"]); ! ! csString synonym_of(result[i]["synonym_of"]); ! csString more_general(result[i]["more_general"]); ! ! if (synonym_of.Length()) ! { ! term->synonym = AddTerm(synonym_of); ! } ! if (more_general.Length()) ! { ! if (term->synonym) ! { ! Error4("NPC Synonym %s have more general = %s that will not be used because of synonym %s", ! term->term.GetDataSafe(),more_general.GetDataSafe(), ! term->synonym->term.GetDataSafe()); ! } ! term->moreGeneral = AddTerm(more_general); ! } ! } return true; } *************** *** 193,202 **** if (found) { ! if (found->synonym.Length() ) { CPrintf(CON_WARNING, "Warning: Word %s in trigger '%s' is already a synonym for '%s'.\n", (const char *)found->term, (const char *)trigger, ! (const char *)found->synonym); } } --- 198,207 ---- if (found) { ! if (found->synonym ) { CPrintf(CON_WARNING, "Warning: Word %s in trigger '%s' is already a synonym for '%s'.\n", (const char *)found->term, (const char *)trigger, ! (const char *)found->synonym->term); } } *************** *** 352,357 **** if (termRec == NULL) return NULL; ! if (termRec->synonym.Length() > 0) ! return FindTerm(termRec->synonym); else return termRec; --- 357,362 ---- if (termRec == NULL) return NULL; ! if (termRec->synonym) ! return termRec->synonym; else return termRec; Index: dictionary.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/server/bulkobjects/dictionary.h,v retrieving revision 1.48 retrieving revision 1.49 diff -C2 -d -r1.48 -r1.49 *** dictionary.h 8 Dec 2005 07:51:20 -0000 1.48 --- dictionary.h 8 Dec 2005 19:22:14 -0000 1.49 *************** *** 57,70 **** bool LoadDisallowedWords(iDataConnection *db); - /** If some phrases are mentioned in 'synonym' or 'moreGeneral' but do not have their own - NpcTerm records, then the records are added */ - void FixMissingReferences(); - /** All unknown words from 'trigger' that are not disallowed are added to 'phrases'. All disallowed words from 'trigger' are removed from 'trigger' */ void AddWords(csStringBase& trigger); ! /** Adds a new record to 'terms' */ ! void AddTerm(const char *term, const char* synonym, const char *moreGeneral); public: --- 57,70 ---- bool LoadDisallowedWords(iDataConnection *db); /** All unknown words from 'trigger' that are not disallowed are added to 'phrases'. All disallowed words from 'trigger' are removed from 'trigger' */ void AddWords(csStringBase& trigger); ! /** ! * Adds a new record to 'terms' ! * ! * @return A new term or and existing term if allready added. ! */ ! NpcTerm* AddTerm(const char *term); public: *************** *** 119,137 **** }; ! /** A phrase recognized by the dialog system */ class NpcTerm { public: csString term; - csString synonym; // If nonempty, phrase is translated to 'synonym' - csString moreGeneral; // If nonempty, it specifies more general phrase than this - // e.g. "apple" ---> "fruit". Phrases that have nonempty 'synonym' should - // have empty 'moreGeneral', because 'moreGeneral' of their 'synonym' - // is used instead of it bool operator==(NpcTerm& other) const { return term==other.term; }; bool operator<(NpcTerm& other) const { --- 119,175 ---- }; ! /** ! * A phrase recognized by the dialog system. Each term can hold a pointer to a ! * synonym e.g. hello = greetings or a more general term e.g. "apple" --> "fruit". ! */ class NpcTerm { public: + /** + * The recognized phrase/term + */ csString term; + /** + * Pointer to a synonym term. + * + * If nonzero, phrase is translated to 'synonym' + * + * Phrases that have 'synonym' should have no 'moreGeneral' term, + * because 'moreGeneral' of their 'synonym' is used instead of it + */ + NpcTerm* synonym; + + /** + * Pointer to a more general term. + * + * If nonzero, it specifies more general phrase than this + * e.g. "apple" ---> "fruit". + * + * Phrases that have 'synonym' should have no 'moreGeneral' term, + * because 'moreGeneral' of their 'synonym' is used instead of it + */ + NpcTerm* moreGeneral; + + /** + * Constructor for the term + */ + NpcTerm() + { + synonym = NULL; + moreGeneral = NULL; + } + + /** + * Compare if two terms are equal + */ bool operator==(NpcTerm& other) const { return term==other.term; }; + + /** + * Compare if one term is less than the other. + */ bool operator<(NpcTerm& other) const { Index: psnpcdialog.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/server/bulkobjects/psnpcdialog.cpp,v retrieving revision 1.65 retrieving revision 1.66 diff -C2 -d -r1.65 -r1.66 *** psnpcdialog.cpp 8 Dec 2005 07:51:20 -0000 1.65 --- psnpcdialog.cpp 8 Dec 2005 19:22:14 -0000 1.66 *************** *** 94,112 **** return false; ! if (terms[level]->moreGeneral.Length()) { ! NpcTerm *up = dict->FindTerm(terms[level]->moreGeneral); ! if (up) ! { ! terms[level] = up; ! str = ""; ! return true; ! } ! else ! { ! Error3("NPC Dialog term <%s> specifies a more general term of <%s> which is not found.", ! terms[level]->term.GetData(), terms[level]->moreGeneral.GetData() ); ! } } return false; } --- 94,104 ---- return false; ! if (terms[level]->moreGeneral) { ! terms[level] = terms[level]->moreGeneral; ! str = ""; ! return true; } + return false; } *************** *** 574,578 **** for (int i = 0; i < (int)generalized.TermLength(); i++) { ! if (generalized.Term(i)->moreGeneral.Length()) { gen_terms.Push(i); --- 566,570 ---- for (int i = 0; i < (int)generalized.TermLength(); i++) { ! if (generalized.Term(i)->moreGeneral) { gen_terms.Push(i); |