AI设计---Robot(v1.0),简单对话的实现

|********************|
|robot version 1.0
|********************|


Function: 

1、the robot can read some of the basic sentences and give out a right respond. For example, I

ask it "what are you doing?", it will respond with "I'm working.", and I say "i gonna go.", it will answer

 "see you, sir!". The key is that the robot can understand your sentences even you spell in a wrong

way but similar to the original one.

2、Another important idea is that the robot can raise up a topic at any time it prefer, there are so

many interesting topics it knows that it seems like a real speaker.

3、the last function is decided for myself. Because i am bad at controlling myself to study hard, i say

"make agenda", it will help me to build up a plan for tomorrow. Next day 11 o'clock at night it will inform

me to check the homework automatically.
 

Program structure:

 AI---------------AI.cpp-----------------agender.h------------------agenda.txt

        |                    |                                     |

        |                    ---algorithm.h              ---agenda_score.txt

        |                    |

        |                    ---temp.txt

        |                    |

        |                    ---topic.txt

        |

        |

        ---AI.exe

        |

        ---agender.h.gch

        |

        ---README.txt

     
Robot maker:

 Lin Chuting, Sun Yat-sen University (Department of Electronic Engineering, School of Data and

Computer Science, Guangzhou 510006,China)



A letter for all readers:

There are still many problem that needed to be solved, such as how to learn from user while talking and

how to build up the emotion for itself. It is such a long way to go, but i am not quiting untill dead. Thanks for

all crazy AI-Lover reading my program and if you have any good idea to make the robot more advanced,

please contact me with e-mail: [email protected]


Exe:

AI设计---Robot(v1.0),简单对话的实现_第1张图片

Note: We can find that the robot knows "what do u do" is the same with "what do you do";


AI设计---Robot(v1.0),简单对话的实现_第2张图片

Note: Command "make agenda" will let the robot help you build up a plan for tomorrow.


AI设计---Robot(v1.0),简单对话的实现_第3张图片

Note: Topic communication, very automatically.


Major code:

AI.cpp:

void auto_talk() {
	srand(time(0));
	int n = 4;	//	update_with_change
	int choice = rand()%n;
	int line = 0;
	float dis;
	float sim;
	char str[1024][1024];
	char tmpA[1024];
	char tmpB[1024];
	char ans[1024];
	char offset_tmp[1024];
	memset(tmpA, '\0', sizeof(tmpA));
	memset(tmpB, '\0', sizeof(tmpB));
	FILE *f = fopen("topic.txt", "r+");
	fgets(str[line], 1024, f);
	while (str[line][3]-'0' != choice) fgets(str[++line], 1024, f);
	line++;
	fgets(tmpA, 1024, f);
	while (tmpA[3]-'0' != choice+1 && tmpA[0] != '\0') {
		if (tmpA[0] == 'A') {
			for (int i = 2; i < sizeof(tmpA); i++) offset_tmp[i-2] += tmpA[i];
			printf("robot: %sme: ", &offset_tmp);
			memset(tmpA, '\0', sizeof(tmpA));
			fgets(tmpA, 1024, f);
			continue;
		} else if (tmpA[0] == 'B') {
			memset(tmpB, '\0', sizeof(tmpB));
			for (int i = 0; i < sizeof(tmpA); i++) tmpB[i] = tmpA[i];
			memset(ans, '\0', sizeof(ans));
			gets(ans);
			if (sort(ans) == 0) break;
			dis = ldistance(ans, tmpB);
			sim = float(dis)/float(sizeof(ans));
			if (sim < 0.95) {
				memset(offset_tmp, '\0', sizeof(offset_tmp));
				memset(tmpA, '\0', sizeof(tmpA));
				fgets(tmpA, 1024, f);
				continue;
			} else {
				printf("robot: %sme: ", &offset_tmp);
				continue;
			}
		}
	}
	fclose(f);
}

Agenda.h:

void agenda_check() {
	int pos = 0;
	int month = 0;
	int day = 0;
	int year = 0;
	int total = 0;
	int finish = 0;
	float sim = 0.0;
	float score = 0.0;
	SYSTEMTIME sys;
	GetLocalTime( &sys );
	int m = sys.wMonth;
	int d = sys.wDay;
	int y = sys.wYear;
	char data[1024][1024];
	char ans[1024];
	string yes = "yes";
	string no = "no";
	string ok = "ok";
	string okay = "okay";
	string sure = "sure";
	string time_str;
	string score_output;
	stringstream score_s;
	stringstream year_str;
	stringstream month_str;
	stringstream day_str;
	char* time_ch;
	year_str << y;
	month_str << m;
	day_str << d;
	FILE *f = fopen("agenda.txt", "a+");
	FILE *fs = fopen("agenda_score.txt", "a+");
	time_str = year_str.str()+"/"+month_str.str()+"/"+day_str.str()+"\n";
	time_ch = string_to_char(time_str);
	fgets(data[pos], 1024, f);
	int count = 0;
	bool is_check = false;
	while (true) {
		year =  month = day = 0;
		if (data[pos][0] >= '0' && data[pos][0] <= '9') {
			get_time(data[pos], year, month, day);
		}
		if ((year == y && month == m && day == d-1)
		|(year == y && month == m-1 && day == 1)
		|(year == y-1 && month == 1 && day == 1)) {
			printf("robot: i gonna check your work, sir\nme: ");
			while (true) {
				memset(ans, '\0', 1024);
				gets(ans);
				if ((float(ldistance(yes, ans))/5.0 < 0.1)
				|(float(ldistance(ok, ans))/5.0 < 0.1)
				|(float(ldistance(okay, ans))/5.0 < 0.1)
				|(float(ldistance(sure, ans))/5.0 < 0.1)) {
					fgets(data[++pos], 1024, f);
					while (data[pos][0] != '\0') {
						if (data[pos][0] >= '0' && data[pos][0] <= '9') {
							is_check = true;
							break;
						}
						total++;
						memset(ans, '\0', 1024);
						printf("robot: have you finish -> %sme: ", data[pos]);
						gets(ans);
						if ((float(ldistance(yes, ans))/5.0 < 0.1)
						|(float(ldistance(sure, ans))/5.0 < 0.1)) {
							finish++;
						}
						fgets(data[++pos], 1024, f);
					}
					score = float(finish)/float(total);
					printf("robot: all checked, sir\n");
					if (score == 1) {
						printf("robot: %.0lf%% are finished, good job\n", score*100);
					} else if (score >= 0.8 && score < 1) {
						printf("robot: %.0lf%% are finished, not well actually\n", score*100);
					} else if (score >= 0.5 && score < 0.8) {
						printf("robot: %.0lf%% are finished, a little bad\n", score*100);
					} else {
						printf("robot: %.0lf%% are finished, you totally ruin today\n", score*100);
					}
					score_s << score*100;
					score_output = time_str+"score: "+score_s.str()+"\n";
					fputs(string_to_char(score_output), fs);
					fclose(fs);
					fclose(f);
					return;
				} else if (float(ldistance(no, ans))/float(sizeof(ans)) < 0.6) {
					printf("robot: ok, sir\n");
					fclose(fs);
					fclose(f);
					return;
				} else {
					printf("robot: sorry, i don't understand you\n");
					continue;
				}
			}
		}
		fgets(data[++pos], 1024, f);
	}
	fclose(fs);
	fclose(f);
}

algorithm.h:

int ldistance(const string src, const string tar)
{
	string source = src;
	string target = tar;
	transform(source.begin(), source.end(), source.begin(), ::tolower);
	transform(target.begin(), target.end(), target.begin(), ::tolower);
	
    int n=source.length();
    int m=target.length();
    if (m==0) return n;
    if (n==0) return m;

	typedef vector< vector >  Tmatrix;
    Tmatrix matrix(n+1);
    for(int i=0; i<=n; i++)  matrix[i].resize(m+1);

    for(int i=1;i<=n;i++) matrix[i][0] = i;
    for(int i=1;i<=m;i++) matrix[0][i] = i;

     for(int i=1;i<=n;i++)
     {
        const char si=source[i-1];
        for(int j=1;j<=m;j++)
        {
            const char dj=target[j-1];
            int cost;
            if(si == dj) {
                cost = 0;
            }
            else {
                cost = 1;
            }
            const int above = matrix[i-1][j]+1;
            const int left = matrix[i][j-1]+1;
            const int diag = matrix[i-1][j-1]+cost;
            matrix[i][j] = min(above,min(left,diag));
        }
    }
    return matrix[n][m];
}

Note: ldistance() is a function which can justify how different it is between two sentences.


你可能感兴趣的:(AI)