WordNet使用方法

来源:互联网 发布:矢量图软件coreldraw 编辑:程序博客网 时间:2024/05/20 15:10

在WordNet中有词语的各种信息,定义、同义、上位、下位、实例、包含等等关系的词集,通过调用相应方法可一一获取,然后进行同义词计算、类别划分等操作。源码的main()函数在wn.c文件中。注意:若在windows环境下调用,则需要在wn.h和wnutil.c文件首行添加 #define _WINDOWS 123 宏定义,否则编译报错。

//print the synset and hypernym of the input wordvoid printHype(char *word,int dbase){SynsetPtr SynPtr, next,HypePtr,hypeNext;int cntSense;char *wordIn,str[WORDBUF];//得到word参数的原型if(morphstr(word,dbase)){wordIn = morphstr(word,dbase);}else{strcpy(str,word);ToLowerCase(str);wordIn = strsubst(str,' ','_');}//printf("target wordIn: %s\n",wordIn);//同义词//findtheinfo_ds()函数第二个参数表示词性所在的数据库,ADJ为形容词;第三个参数表示所在词集,SYNS表示同义词集,ANTPTR表示反义词集。SynPtr = findtheinfo_ds(wordIn, dbase, SYNS, ALLSENSES);if(SynPtr == NULL){printf("word error: %s\n",word);return -1;}next = SynPtr;cntSense = 1;printf("同义词:\n");while (next != NULL){int j;printf("Sense %d: \n",cntSense);for (j = 0;j < next->wcount;j++){printf(next->words[j]);printf("  ");}printf("\n");//语义//printf(next->defn);  //print current sense definiton;//printf("\n");next = next->nextss; //go to next sense;cntSense++;}//上位词//traceptrs_ds()函数第二个参数表示所在词集,HYPERPTR表示上位词集,INSTANCE表示实例词集;第三个参数表示词性所在数据库,NOUN表示名词。HypePtr = traceptrs_ds(SynPtr,HYPERPTR,NOUN,1);hypeNext = HypePtr;if (hypeNext != NULL){printf("上位词:\n");while(hypeNext != NULL){int i;for (i = 0;i < hypeNext->wcount;i++){if (hypeNext->words[i] != ""){printf(hypeNext->words[i]);printf("  ");}}hypeNext = hypeNext->ptrlist;}printf("\n");}//实例HypePtr = traceptrs_ds(SynPtr,INSTANCE,NOUN,1);hypeNext = HypePtr;if (hypeNext != NULL){printf("是..的实例:\n");while(hypeNext != NULL){int i;for (i = 0;i < hypeNext->wcount;i++){printf(hypeNext->words[i]);printf("  ");}printf("\n");hypeNext = hypeNext->ptrlist;}}HypePtr = traceptrs_ds(SynPtr,INSTANCES,NOUN,1);hypeNext = HypePtr;if (hypeNext != NULL){printf("包含..实例:\n");while(hypeNext != NULL){int i;for (i = 0;i < hypeNext->wcount;i++){printf(hypeNext->words[i]);printf("  ");}printf("\n");hypeNext = hypeNext->ptrlist;}}//成员HypePtr = traceptrs_ds(SynPtr,HASMEMBERPTR,NOUN,1);hypeNext = HypePtr;if (hypeNext != NULL){printf("有..成员:\n");while(hypeNext != NULL){int i;for (i = 0;i < hypeNext->wcount;i++){printf(hypeNext->words[i]);printf("  ");}printf("\n");hypeNext = hypeNext->ptrlist;}}HypePtr = traceptrs_ds(SynPtr,ISMEMBERPTR,NOUN,1);hypeNext = HypePtr;if (hypeNext != NULL){printf("是..的成员:\n");while(hypeNext != NULL){int i;for (i = 0;i < hypeNext->wcount;i++){printf(hypeNext->words[i]);printf("  ");}printf("\n");hypeNext = hypeNext->ptrlist;}}//组成HypePtr = traceptrs_ds(SynPtr,HASSTUFFPTR,NOUN,1);hypeNext = HypePtr;if (hypeNext != NULL){printf("有..组成物质:\n");while(hypeNext != NULL){int i;for (i = 0;i < hypeNext->wcount;i++){printf(hypeNext->words[i]);printf("  ");}printf("\n");hypeNext = hypeNext->ptrlist;}}//反义词SynPtr = findtheinfo_ds(wordIn, ADJ, ANTPTR, ALLSENSES);HypePtr = traceptrs_ds(SynPtr,ANTPTR,ADJ,1);hypeNext = HypePtr;if (hypeNext != NULL){printf("反义词:\n");while(hypeNext != NULL){int i;for (i = 0;i < hypeNext->wcount;i++){printf(hypeNext->words[i]);printf("  ");}printf("\n");hypeNext = hypeNext->ptrlist;}}//一定要释放分配的空间free_syns(SynPtr);  // free linked list.next = NULL;SynPtr = NULL;}main(int argc,char *argv[]){char *temp,*temp1;if (wninit()) {// open database display_message("wn: Fatal error - cannot open WordNet database\n");exit (-1);}/**********************输出同义词、上位词等**********************/printHype("God",NOUN);}
若在.cpp文件中调用这些函数,则需要将string类型的字符串转换成char数组类型,再传入,main()函数如下

main(int argc,char *argv[]){char *temp,*temp1;if (wninit()) {// open database display_message("wn: Fatal error - cannot open WordNet database\n");exit (-1);}/**********************输出同义词、上位词等**********************/char conv[WORDBUF];string str = "God";char *word = (char *)str.c_str();sprintf(conv,"%s",word);printHype(conv,NOUN);}

原创粉丝点击