/***************************************************************************** File: WnCDbExample.c Author: Keith Alcock Comment: Implements a(n) WnCDbExample ****************************************************************************** Revision control system and include for self *****************************************************************************/ char *PVCS_WNCDBEXAMPLE_c= "@(#)$Workfile: WnCDbExample.c $\n" "@(#)$Revision: 1 $\n" "@(#) $Date: 12/02/04 11:56p $\n" "@(#)---------:"; /***************************************************************************** Includes *****************************************************************************/ #if !defined(WNCDBPUBLIC_INCLUDED) # include "WnCDbPublic.h" #endif #if !defined(WNCDBSUMMARY_INCLUDED) # include "WnCDbSummary.h" #endif #if !defined(WINDOWS_INCLUDED) # include // LoadLibrary, GetProcAddress, FreeLibrary # define WINDOWS_INCLUDED 1 #endif #if !defined(STDIO_INCLUDED) # include // printf # define STDIO_INCLUDED 1 #endif /***************************************************************************** DLL Interface Utilities *****************************************************************************/ int (*pGetWnCDbVersion)(int *major,int *minor); int (*pGetWnCDbSynsetCount)(); int (*pGetWnCDbSenseFanout)(); int (*pEnumWnCDbLemmas)(SYNSET_INDEX_COLLECTOR,int *,int,int,int,LEMMA_COLLECTOR, int,char *,int,LEMMA_COLLECTOR,int,int,int,LEMMA_COLLECTOR); int (*pEnumWnCDbSynsetWords)(SYNSET_WORD_COLLECTOR,int); char *(*pGetWnCDbSynsetGloss)(int); int (*pCalcWnCDbDistances)(int,int *,int *); int (*pCalcWnCDbDepth)(int,int *,int *,int *); int (*pEnumWnCDbSynsetChildren)(SYNSET_CHILD_COLLECTOR,int,int *,int *); int (*pCalcWnCDbSearchInfos)(int,int *,int *,SEARCH_INFO *,int *,int,int,int); HINSTANCE library; void LoadDll() { library=LoadLibrary("WnCDb.dll"); pGetWnCDbVersion=(int (*)(int *,int *)) GetProcAddress(library,"getWnCDbVersion"); pGetWnCDbSynsetCount=(int (*)()) GetProcAddress(library,"getWnCDbSynsetCount"); pGetWnCDbSenseFanout=(int (*)()) GetProcAddress(library,"getWnCDbSenseFanout"); pEnumWnCDbLemmas=(int (*)(SYNSET_INDEX_COLLECTOR,int *,int,int,int,LEMMA_COLLECTOR, int,char *,int,LEMMA_COLLECTOR,int,int,int,LEMMA_COLLECTOR)) GetProcAddress(library,"enumWnCDbLemmas"); pEnumWnCDbSynsetWords=(int (*)(SYNSET_WORD_COLLECTOR,int)) GetProcAddress(library,"enumWnCDbSynsetWords"); pGetWnCDbSynsetGloss=(char *(*)(int)) GetProcAddress(library,"getWnCDbSynsetGloss"); pCalcWnCDbDistances=(int (*)(int,int *,int *)) GetProcAddress(library,"calcWnCDbDistances"); pCalcWnCDbDepth=(int (*)(int,int *,int *,int *)) GetProcAddress(library,"calcWnCDbDepth"); pEnumWnCDbSynsetChildren=(int (*)(SYNSET_CHILD_COLLECTOR,int,int *,int *)) GetProcAddress(library,"enumWnCDbSynsetChildren"); pCalcWnCDbSearchInfos=(int (*)(int,int *,int *,SEARCH_INFO *,int *,int,int,int)) GetProcAddress(library,"calcWnCDbSearchInfos"); } void UnloadDll() { FreeLibrary(library); } /***************************************************************************** WnCDDbExample *****************************************************************************/ int relativeDistances[WN_RELATIONSHIP_COUNT]= { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }; int *pSynsetIndex; int synsetIndexCollector(int synsetIndex) { *pSynsetIndex=synsetIndex; return 0; } int synsetWordCollector(int wnPos,char *word,int lexId) { printf("\t%s\n",word); return 0; } int childSynsetWordCollector(int wnPos,char *word,int lexId) { printf("\t\t%s\n",word); return 0; } int synsetChildCollector(int synsetIndex,int childIndex,int relationship) { printf("\tSynset index: %d\n",synsetIndex); pEnumWnCDbSynsetWords(childSynsetWordCollector,synsetIndex); return 0; } void showSearchInfos(SEARCH_INFO *searchInfos,int currentDepth,int *distances) { int i; for (i=0;i<=currentDepth;i++) { int synsetIndex=searchInfos[i].synsetIndex; printf("\tDepth: %d Synset index: %d\n",distances[synsetIndex],synsetIndex); pEnumWnCDbSynsetWords(childSynsetWordCollector,synsetIndex); } } int main() { char *sourceString="first aid"; char *targetString="help"; int major,minor; int synsetCount; int synsetFanout; int sourceSynsetIndex; int targetSynsetIndex; char *gloss; int *distances; int depth; SEARCH_INFO *searchInfos; int currentDepth; LoadDll(); // Check the version number. pGetWnCDbVersion(&major,&minor); printf("Version: %d.%d\n",major,minor); // Find out how many synsets there are in the database. synsetCount=pGetWnCDbSynsetCount(); printf("Synset count: %d\n",synsetCount); // Find out how many senses a synset might have. synsetFanout=pGetWnCDbSenseFanout(); printf("Synset fanout: %d\n",synsetFanout); // Look for the noun form of the source sourceSynsetIndex=-1; pSynsetIndex=&sourceSynsetIndex; pEnumWnCDbLemmas(synsetIndexCollector,NULL, 1,WN_NOUN,0,NULL, 1,sourceString,1,NULL, 0,0,2,NULL); printf("One synset index of noun '%s': %d\n",sourceString,sourceSynsetIndex); // List the words in the synset. printf("Words in the synset:\n"); pEnumWnCDbSynsetWords(synsetWordCollector,sourceSynsetIndex); // Check the gloss. gloss=pGetWnCDbSynsetGloss(sourceSynsetIndex); printf("Gloss: %s\n",gloss); // Calculate distances from break. distances=(int *) malloc(synsetCount*sizeof(int)); printf("Calculating all distances, which may take some time...\n"); pCalcWnCDbDistances(sourceSynsetIndex,distances,relativeDistances); printf("Calculation complete.\n"); // Find depth (maximum path length). pCalcWnCDbDepth(sourceSynsetIndex,distances,relativeDistances,&depth); printf("There are lemmas up to %d relationships away.\n",depth); // Find the lemmas closest to the source printf("The nearest synsets are:\n"); pEnumWnCDbSynsetChildren(synsetChildCollector,sourceSynsetIndex, distances,relativeDistances); // Look for the target in noun form targetSynsetIndex=-1; pSynsetIndex=&targetSynsetIndex; pEnumWnCDbLemmas(synsetIndexCollector,NULL, 1,WN_NOUN,0,NULL, 1,targetString,1,NULL, 0,0,2,NULL); printf("One synset index of noun '%s': %d\n",targetString,targetSynsetIndex); printf("Distance from '%s': %d\n",sourceString,distances[targetSynsetIndex]); // Find the best path printf("One shortest path from '%s' to '%s':\n",sourceString,targetString); searchInfos=(SEARCH_INFO *) malloc((depth+1)*sizeof(SEARCH_INFO)); searchInfos[0].synsetIndex=sourceSynsetIndex; currentDepth=0; pCalcWnCDbSearchInfos(sourceSynsetIndex,distances,relativeDistances, searchInfos,¤tDepth,targetSynsetIndex,depth,0); showSearchInfos(searchInfos,currentDepth,distances); free(searchInfos); free(distances); UnloadDll(); return 0; } /****************************************************************************/