⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wncn.cpp

📁 用中文Wordnet开发的一个智能搜索引擎
💻 CPP
字号:
//***************************************************
//   
//    中英文WordNet在线词典     
//
//    本软件遵从LGPL协议,完全开放所以源代码及设计文档。
//
//    宁夏大学智能网格系统实验室  张冬 康彩   2005.10
//
//****************************************************/

#include <time.h>
#include <process.h>
#include <conio.h>
#include <windows.h>
//#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>


#define USER_PORT 6060


char *NotFind = "Null NXU AGL!";

long DictNum;

// 数据缓冲区 
char MwordNwt[210500][1024];          
     
char wordIndex1[50];
long wordIndex2;
                  
char MW1[210500][50];              
long MW2[99999999]; 

// 搜索索引  单词
long IWM1[259][259];        
       
unsigned __stdcall HttpThread(void *  p);
unsigned long hThreadHandle;
unsigned uThreadID;

// 查找数据 
long GetIt(char *word);                          

// 读取WordNetCN词典
int LoadDictLine(char *filename,long& num);    

// 得到请求数据   查找数据  发送数据
//void GetNewWordNetCN(BufferSocket BufSock);       

// 初始化
void startcom();       

// 数据结构分解
void strX(long num);

// 排列队列  建立索引
void SearchIndexMake(); 


/***************************************************************************************************************
   初始化
***************************************************************************************************************/
void startcom()       
{
   printf("Init WordNetCN ...\n");

    //初始化索引
    memset(MW2,0,sizeof(MW2));  

    //读取词典数据
   LoadDictLine("main.data",DictNum);

    //排列队列: 统计排序  建立索引
   SearchIndexMake();  

   printf("Initialization OK !   Grid Dict  COPYLEFT 2005    NXU AGL\n");
   printf("Start Service ......");

}


/***************************************************************************************************************
     查找数据       得到一个单词的基本信息   查找类型 0  根据序号 1 根据名称   数据值
***************************************************************************************************************/
long GetIt(char *word)                    
{

   long i,vi,t,l2,l,s1,s2,t1,t2,Ls;      
  
    //kcbaby  
   long nword;

   int w;
   w=word[0];

    //判断查找方式
   vi=-999;                               

   Ls=strlen(word); 

    //判断查询的是单词还是根据标示查询 
   if (word[1]<'A' && word[1]>0  && Ls==8)                     
   {  
	  nword = atof(word);
      if (MW2[nword]>0){return MW2[nword];}else {return vi;}
   }
   else                                  
   {   // 1 根据名称 
 
	
	  if(Ls==0)
	  {
		  return vi;
	  }

	  if(Ls==1)
	  {  
       	  if(word[0]>0)	
		  {
			  t1=word[0];
   		      s1=IWM1[t1][0];
              s2=s1+1;
		  }
		  else
		  { 
			  t1=word[0]+256;
		      s1=IWM1[t1][0];
              s2=s1+1;
		  }
	  }
	  else
	  {
	      if(word[0]>0)
		  {
			  t1=word[0];
		  }
		  else
		  {
			  t1=word[0]+256;
		  }

		  if(word[1]>0)
		  {
			  t2=word[1];
		  }
		  else
		  {
			  t2=word[1]+256;
		  }
		     
		  s1=IWM1[t1][t2];
          s2=IWM1[t1][t2+1];

	  }	  

      for (i=s1;i<s2; i++)
	  {
          l2= strlen(MW1[i]);
		
		  if (Ls==l2)
		  {
		       t=strncmp(MW1[i],word,Ls);	   
		   
			   if(t==0) 
     		   { 
			    	return i;
			   }	  
		  }
   	  }
  }

   return vi;

}


/***************************************************************************************************************
     读取WordNetCN词典
***************************************************************************************************************/
int LoadDictLine(char *filename,long& num) 
{
	char tword[1024];
    num=0;
	FILE *fp;
	
	if((fp=fopen(filename,"r"))==NULL)
	{
		printf("Open the DB failed\n");
		return 0;
	}
	else
	{
		while(fgets(tword,1024,fp)!=NULL)
		{
			num++;
            memcpy(MwordNwt[num],tword,sizeof(MwordNwt[num]));

            if(strlen(MwordNwt[num])>10)         //建立索引
		    { 
			    strX(num);   //拆解WordNetCN  数据  		   	                     
				strcpy(MW1[num],wordIndex1);//  单词压入索引表
                MW2[wordIndex2]=num;           //标示压入索引表
		    }

		    if(num%30000==9300)
		    { 			
				printf("Init One :  ");
                printf("%d%%\n",num/2100);
   		    }
          
		     // if (num>=10000){goto SS;}

	    }

		rewind(fp);

	}

SS:;

	printf("Init One :  ");
    printf("%d%%\n",100);
	
	fclose(fp);

	return 1;

}

/***************************************************************************************************************
     数据结构分解
***************************************************************************************************************/
void strX(long num)
{
    long j=0,i,s1=0,s2=0,sl; 

	char d1[9];
	char d2[50];

    sl=strlen(MwordNwt[num]);

	for(i=0;i<=sl;i++)
	{
		if(sl>1020)
		{
			MessageBox (NULL, TEXT (MwordNwt[num]), TEXT ("HelloMsg"), 0);
		}

		if(MwordNwt[num][i]==9 || MwordNwt[num][i]==124)
		{
			j=j+1;
			i=i+1;
		}

	    if(j==0)          //遇到标号
	    {  
            if(MwordNwt[num][i]>47 && MwordNwt[num][i]<58 && s1<8)          
			{ 
				d1[s1] = MwordNwt[num][i]; 
		        s1=s1+1;
			}
	    }

	    if (j==1)         //第一行加末尾
	    {
          if (s1==8)
		  {
			  d1[s1] = '\0';
		  }
	    }

        if (j==4)         //开始单词
	    {   
		   if (MwordNwt[num][i]!=124 && s2<29 && MwordNwt[num][i]!=9 && MwordNwt[num][i]!=10 && MwordNwt[num][i]!=11 && MwordNwt[num][i]!=12 && MwordNwt[num][i]!=13) 
		   { 
			   d2[s2] = MwordNwt[num][i];
		       s2=s2+1;
		   }
	    }

	    if (j==5)         //第一行加末尾
	    {
	      d2[s2]= '\0';
		  break;
	    }
	}

    wordIndex2=0;

    strcpy(wordIndex1,"\0\0\0\0");

    strcpy(wordIndex1,d2);

    //d1[8]="\0";
	wordIndex2= atof(d1);

}


/***************************************************************************************************************
    排列队列: 统计排序  建立索引
***************************************************************************************************************/
void SearchIndexMake()  
{

   long i,j,h1,h2,o,L;
   o=DictNum;

    //初始化
   memset(IWM1,0,sizeof(IWM1));  
  
   for (i=1;i<=o;i++) 	  
   { 
	   h1=-999;
   	   h2=-999;
       
	   L=strlen(MW1[i]);
	  
        //长度为0
	   if (L==0)                     
	   {
		   goto bakcIt2; 
	   }
      
        //只有1个字目
	   if (L==1 && MW1[i][0]>0)      
      {
		h1=MW1[i][0];
	    h2=0;

		if(IWM1[h1][h2]==0)
		{
			IWM1[h1][h2]=i;
		}

		goto bakcIt2;

	  }
	 	  
       //只有1个字节  特殊情况
	  if(L==1 && MW1[i][0]<0)      
      {
		 h1=MW1[i][0]+256; 
		 h2=0; 
	
		 if(IWM1[h1][h2]==0)
		 {
			 IWM1[h1][h2]=i;
		 }

		 goto bakcIt2;

	  }

       //多个字母  或  汉字
      if(L>=2 )      
	  {
		  if(MW1[i][0]>0)
		  { 
			  h1=MW1[i][0];
		  }
		  else
		  {
			  h1=MW1[i][0]+256;
		  }

          if(MW1[i][1]>0)
		  {
			  h2=MW1[i][1];
		  }
		  else
		  {
			  h2=MW1[i][1]+256;
		  }

		  if(IWM1[h1][h2]==0)
		  {
			  IWM1[h1][h2]=i;
		  }

		  goto bakcIt2; 

	  }      	
      
bakcIt2:;

     if (i%60000==39200) 
	 { 
		 printf("Init Two :  ");
		 printf("%d%%\n",i*100/o);
	 }  

  } 
  
    IWM1[256][256]=o;
  
    for (i=256;i>=0;i--)   //填充数据   前<--后  首尾相应
    { 

        for (j=256;j>=1;j--) 
		{
			if(IWM1[i][j-1]==0 && IWM1[i][j]!=0 )
			{	
				IWM1[i][j-1]=IWM1[i][j];
			}
		}
		for (j=0;j>=256;j++) 
		{
            if(IWM1[i-1][256]==0 && IWM1[i][j]!=0)
            {   
		        IWM1[i-1][256]=IWM1[i][j];
		    }	
		}
	}

    printf("Init Two :  ");
	printf("%d%%\n",100);

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -