中文词法分析的简单程序

类别:编程语言 点击:0 评论:0 推荐:

VC 6.0 下编译通过  disc 为辞典   expression 为输入的短语

 

// Code.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"
#include "string.h"
#include "windows.h"

int CountWord(char* words);  //计算有多少个字符

struct dict
{
 char syn[8],word[6];
 int scode,wcode;
} dic[19];

int main(int argc, char* argv[])
{
 strcpy(dic[0].syn,"祈使动词");
    dic[0].scode=0;
 strcpy(dic[0].word,"请");
    dic[0].wcode=0;
    strcpy(dic[1].syn,"祈使动词");
    dic[1].scode=0;
 strcpy(dic[1].word,"把");
    dic[1].wcode=1;
    strcpy(dic[2].syn,"代词");
 dic[2].scode=1;
 strcpy(dic[2].word,"你");
 dic[2].wcode=0;
 strcpy(dic[3].syn,"动词");
 dic[3].scode=2;
 strcpy(dic[3].word,"走");
 dic[3].wcode=0;
 strcpy(dic[4].syn,"动词");
 dic[4].scode=2;
 strcpy(dic[4].word,"跑");
 dic[4].wcode=1;
 strcpy(dic[5].syn,"动词");
 dic[5].scode=2;
 strcpy(dic[5].word,"跳");
 dic[5].wcode=2;
 strcpy(dic[6].syn,"动词");
 dic[6].scode=2;
 strcpy(dic[6].word,"转");
 dic[6].wcode=3;
 strcpy(dic[7].syn,"动词");
 dic[7].scode=2;
 strcpy(dic[7].word,"抬");
 dic[7].wcode=4;
 strcpy(dic[8].syn,"方向词");
 dic[8].scode=3;
 strcpy(dic[8].word,"向左");
 dic[8].wcode=0;
 strcpy(dic[9].syn,"方向词");
 dic[9].scode=3;
 strcpy(dic[9].word,"向右");
 dic[9].wcode=1;
 strcpy(dic[10].syn,"方位词");
 dic[10].scode=4;
 strcpy(dic[10].word,"向前");
 dic[10].wcode=0;
 strcpy(dic[11].syn,"方位词");
 dic[11].scode=4;
 strcpy(dic[11].word,"侧");
 dic[11].wcode=1;
 strcpy(dic[12].syn,"名词");
 dic[12].scode=5;
 strcpy(dic[12].word,"左手");
 dic[12].wcode=0;
 strcpy(dic[13].syn,"名词");
 dic[13].scode=5;
 strcpy(dic[13].word,"右手");
 dic[13].wcode=1;
 strcpy(dic[14].syn,"名词");
 dic[14].scode=5;
 strcpy(dic[14].word,"左腿");
 dic[14].wcode=2;
 strcpy(dic[15].syn,"名词");
 dic[15].scode=5;
 strcpy(dic[15].word,"右腿");
 dic[15].wcode=3;
 strcpy(dic[16].syn,"副词");
 dic[16].scode=6;
 strcpy(dic[16].word,"快");
 dic[16].wcode=0;
 strcpy(dic[17].syn,"副词");
 dic[17].scode=6;
 strcpy(dic[17].word,"慢");
 dic[17].wcode=1;
 strcpy(dic[18].syn,"其他词");
 dic[18].scode=7;
 strcpy(dic[18].word,"起来");
 dic[18].wcode=0;
//////////////////////--Init Data End
///////////////////////////////////////////

 char expression[30],phrase[40];  //输入句子的分词程序
 char *p1,*p2;      //*
 PWORD pw_exp1,pw_exp2,pw_old;   //temp  pw_old Can RollBack
 int  n_WordNub = 0,n_InputNub = 0;   //*
 
 strcpy(expression,"请你向前走快抬左手侧左腿");
 
 for(int i=0; i<40; i++)
  phrase[i] = ' ' ;
 phrase[39] = '\0';

 p2 = expression;
 p1 = phrase;
  pw_old = pw_exp1 = (PWORD)p2;  //Init

//计算输入的字数以确定查询的次数
 n_InputNub = CountWord(p2);
 
    printf("分析结果: \n");

    for(int nub=0; nub<n_InputNub; nub++) //按照输入的字数来找
 {
  for(int i=0; i<19; i++)  // 查询词表
  {
   pw_exp2 = (PWORD)dic[i].word;
   for(int j=0; j<CountWord(dic[i].word); j++) //比较单词
   {
    if (*pw_exp1 == *pw_exp2)
    {
     pw_exp1++;
     pw_exp2++;
    }
    else
    {
     pw_exp1 = pw_old;  //RollBack
    }
   }

   if ((char*)pw_exp1 != p2)  //词表找到单词
   {
    for(int x=0; x<((char*)pw_exp1 - p2); x++)  //存储识别单词
    {
     *p1 = *(p2+x);
     p1++;
    }
    *p1 = '/';  //填入分隔符
    p1++;
    p2 =(char*) pw_exp1;   //查询下一个
    pw_old = pw_exp1;      //Can RollBack
   }

   if (*p2 == '\0') //查完了!
   {
    break;
   }

  }
 }

//查询完辞典!
 if( ((char*)pw_exp1 == p2) && (*p2 != '\0') ) //词表里没有找到
 {
  cout<<"不可识别的短语!";
 }
 else{
  cout<<"识别完毕! :" ;
  cout<<phrase;}
 
 return 0;
}

int CountWord(char* words)
{
 int n = 0;
 while(*words != '\0')
 {
  words++;
  n++;
 }
 return int(n/2);
}

本文地址:http://com.8s8s.com/it/it30356.htm