人工智能--识别句子

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
//冠词1,形容词4,名词2,动词3, 
//主谓 
//统一用冠词+(形容词)+名词+动词结构 
//string sen1="The man studys"; 
//string sen2= “A dog sings”;
//string sen3="A lovely cat dances";
//string sen4="An ugly snake sings"; 
//主谓宾
//统一用冠词+(形容词 )+动词+冠词+(形容词)+名词;
//string sen1="The ugly dog hits the old postman";
//string sen2="The beautiful girl likes the tall tree";
//string sen3= The man became a famous doctor.
//string sen4="The Green like the lovely dog";
//string sen5="The dog hits the old man“; 
//string sen6=“The dog hits the man”; 
//主谓+间接宾语+直接宾语
//统一用冠词+(形容词)+名词+动词+名词+冠词+名词 
//string sen1="The beautiful girl bought me a computer";
//string sen2=" The old man passes him the salt";
//string sen3="The boy gives her a gift"
//string sen4="The young man gives you a gift;
//string sen5="The man gives you a beautiful gift"
//string sen5="The young man gives you a beautiful gift"
//string sen6="He gives you a gift"
//主谓宾宾补
//统一用冠词+(形容词)+名词+动词+冠词+名词+形容词 
//string sen1=“The student  keep the school clean.”
//string sen2=“The ugly dog makes the man angry” 
//string sen3="The ugly dog makes the old man angry" 
//string sne4="The dog makes the old man angry" 
string mingci[]={"dog","postman","man","cat","snake","girl","doctor","Green","computer","salt","boy","gift","her","s","me","tree","him","school","student","you","I","He"};
string dongci[]={"hits","studys","sings","dances","sings","likes","became","like","bought","passes","gives","keep","makes","am"};
string guanci[]={"The","the","a","A","an","An"};
string xingrongci[]={"lovely","ugly","old","beautiful","tall","famous","clean","angry","young"};
string juzi[]={"123","1423","1423142","12321","123241","1423212","123212","123124","1423124","123142","12312","14231424","1231424","1232142","14232142","23212"};//116条语法分析 
char panduan1(string a)
{
	for(int i=0;i<sizeof(mingci)/sizeof(string);i++)
	{
		if(a==mingci[i])
		return '2';
	}
	return '0';
 } 
char panduan2(string a)
{
	for(int i=0;i<sizeof(dongci)/sizeof(string);i++)
	{
		if(a==dongci[i])
		return '3';
	}
	return '0';
 } 
char panduan3(string a)
{
	for(int i=0;i<sizeof(guanci)/sizeof(string);i++)
	{
		if(a==guanci[i])
		return '1';
	}
	return '0';
 }  
char panduan4(string a)
{
	for(int i=0;i<sizeof(xingrongci)/sizeof(string);i++)
	{
		if(a==xingrongci[i])
		return '4';
	}
	return '0';
 }   
 int main()
 {
 	system("color F0");    /*adjust the blackground color*/
 	string sentence;
 	char a,b,c,d;
 	cout<<"欢迎来到测试句子区!"<<endl;
	cout<<"按1表示开始测试,按0表示退出,注意每输入一个句子结尾以0表示该句子结束"<<endl;
	int a1;
	cin>>a1;
	while(a1!=0)
	{
		string temp="";	
 	cin>>sentence;
    while(sentence!="0")
    {
    	a=panduan1(sentence);
    	if(a!='0')
    	temp+=a;
    	else
    	{
		b=panduan2(sentence);
		if(b!='0')
    	temp+=b;
    	else
		{ 
		c=panduan3(sentence);
		if(c!='0')
    	temp+=c;
    	else
    	{
    		d=panduan4(sentence);
    		if(d!='0')
			temp+=d;
		}
		} 
	    }
	    cin>>sentence;
	}
	int i; 
 	for(i=0;i<sizeof(juzi)/sizeof(string);i++)
	 {
	 	if(temp==juzi[i])
		 {
		 	cout<<"This is a legal sentence!"<<endl; 
		 	break; 
		 }
	  } 
	  if(i==sizeof(juzi)/sizeof(string))
	  cout<<"This is an illegal sentence!"<<endl;
	cout<<"按1表示继续测试,按0表示退出,注意每输入一个句子结尾以0表示该句子结束"<<endl;
	cin>>a1;
     }  
	  return 0;
  } 

  

posted @ 2016-11-19 19:38  PirateLHX  阅读(1079)  评论(0编辑  收藏  举报