Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version
Go to top

Building an AI Chatbot using a Regular Expression Engine

, 21 Jun 2007
This article describes how to build an AI Chatterbot using a popular, Regular Expression-based open source Chatterbot engine: Verbots
verbotsdk.zip
mssccprj.scc
ResourceFiles
bin
Debug
ResourceFiles.dll
ResourceFiles.pdb
Release
ResourceFiles.dll
CHANGELOG
COPYING
mssccprj.scc
obj
Debug
buildinfo.inf
ResourceFiles.dll
ResourceFiles.pdb
ResourceFiles.projdata
temp
TempPE
Release
buildinfo.inf
ResourceFiles.dll
ResourceFiles.projdata
temp
TempPE
ResourceFiles.csproj.user
ResourceFiles.csproj.vspscc
vssver.scc
vssver2.scc
Verbot4Library
bin
Debug
Verbot4Library.dll
Verbot4Library.pdb
Release
Verbot4Library.dll
mssccprj.scc
obj
Debug
buildinfo.inf
temp
TempPE
Verbot4Library.dll
Verbot4Library.dll.incr
Verbot4Library.pdb
Verbot4Library.projdata
Release
buildinfo.inf
temp
TempPE
Verbot4Library.dll
Verbot4Library.projdata
Verbot4Library.csproj.user
Verbot4Library.csproj.vspscc
vssver.scc
vssver2.scc
VerbotConsoleApplication
App.ico
bin
Debug
Verbot4Library.dll
VerbotConsoleApplication.exe
VerbotConsoleApplication.pdb
Release
Verbot4Library.dll
VerbotConsoleApplication.exe
mssccprj.scc
obj
Debug
buildinfo.inf
temp
TempPE
VerbotConsoleApplication.exe
VerbotConsoleApplication.pdb
VerbotConsoleApplication.projdata
Release
buildinfo.inf
temp
TempPE
VerbotConsoleApplication.exe
VerbotConsoleApplication.projdata
VerbotConsoleApplication.csproj.user
VerbotConsoleApplication.csproj.vspscc
vssver.scc
VerbotSDK.vssscc
VerbotWindowsApplicationSample
App.ico
bin
Debug
Verbot4Library.dll
Verbot4Library.pdb
VerbotWindowsApplicationSample.exe
VerbotWindowsApplicationSample.pdb
Release
Verbot4Library.dll
VerbotWindowsApplicationSample.exe
mssccprj.scc
obj
Debug
buildinfo.inf
temp
TempPE
VerbotWindowsApplicationSample.exe
VerbotWindowsApplicationSample.pdb
VerbotWindowsApplicationSample.projdata
Release
buildinfo.inf
temp
TempPE
VerbotWindowsApplicationSample.exe
VerbotWindowsApplicationSample.projdata
VerbotWindowsApplicationSample.VerbotWinApp.resources
Resources
sample.ckb
sample.vkb
vssver2.scc
VerbotWindowsApplicationSample.csproj.user
VerbotWindowsApplicationSample.csproj.vspscc
vssver.scc
vssver2.scc
vssver.scc
vssver2.scc
/*
	Copyright 2004-2006 Conversive, Inc.
	http://www.conversive.com
	3806 Cross Creek Rd., Unit F
	Malibu, CA 90265
 
	This file is part of Verbot 4 Library: a natural language processing engine.

    Verbot 4 Library is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Verbot 4 Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Verbot 4 Library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
	
	Verbot 4 Library may also be available under other licenses.
*/

using System;
using System.Text;
using System.Collections;
using System.Security.Cryptography;

namespace Conversive.Verbot4
{
	/// <summary>
	/// Toolbox of common functions.
	/// </summary>
	public class ConversiveGeneralTextToolbox
	{
		public static string CleanCSVValue(string stValue, char fieldDelimiter, char textDelimiter)
		{
			stValue = stValue.Replace("\n", "");
			stValue = stValue.Replace("\r", "");
			stValue = stValue.Replace(textDelimiter.ToString(), "");
			stValue = stValue.Replace(fieldDelimiter.ToString(), "");
			return stValue;
		}

		public static string MakeCSV(ArrayList data, char fieldDelimiter, char textDelimiter)
		{
			return ConversiveGeneralTextToolbox.MakeCSV(data, fieldDelimiter, textDelimiter, true);
		}

		public static string MakeCSV(ArrayList data, char fieldDelimiter, char textDelimiter, bool bAddHeaderRow)
		{
			//data is an arraylist of hashtables, the keys of the first item will be the headings
			StringBuilder sb = new StringBuilder();

			for(int i = 0; i < data.Count; i++)
			{
				object oRow = data[i];
				if(oRow is Hashtable)
				{
					string stRow = "";
					Hashtable htRow = (Hashtable)oRow;
					if(bAddHeaderRow && i == 0)//add header row
					{
						foreach(object oKey in htRow.Keys)
						{
							if(stRow != "")
								stRow += fieldDelimiter;
							stRow += textDelimiter + oKey.ToString() + textDelimiter;
						}
						if(stRow != "")
						{
							sb.Append(stRow + "\r\n");
							stRow = "";
						}
					}
					foreach(object oKey in htRow.Keys)
					{
						if(stRow != "")
							stRow += fieldDelimiter;
						stRow += textDelimiter + CleanCSVValue(htRow[oKey].ToString(), fieldDelimiter, textDelimiter) + textDelimiter;
					}
					if(stRow != "")
						sb.Append(stRow + "\r\n");
				}
			}

			return sb.ToString();
		}

		public static ArrayList SplitCSV(string data, char fieldDelimiter, char textDelimiter)
		{
			//notes: fields don't need "'s around them unless they have ,'s or \n's
			//reference: http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
			//sample line => "Last, First",,27,m
			//should become => {"Last, First", "", "27", "m"}

			ArrayList lines = new ArrayList();
			ArrayList fields = new ArrayList();

			int startIndex = 0;

			while(startIndex < data.Length)
			{
				//skip white space
				while(startIndex < data.Length &&
					(data[startIndex] == ' ' && fieldDelimiter != ' ' ||
					data[startIndex] == '\t' && fieldDelimiter != '\t'))
				{
					startIndex++;
				}

				//check for end of data
				if(startIndex >= data.Length)
				{
					if(fields.Count > 0)
						lines.Add(fields);
					break;//return
				}

					//check for end of line
				else if(data[startIndex] == '\n' || data[startIndex] == '\r')
				{
					if(fields.Count > 0)
					{
						lines.Add(fields);
						fields = new ArrayList();
					}
					startIndex++;
					continue;//get the next line
				}

					//check for empty field
				else if(data[startIndex] == fieldDelimiter)
				{
					fields.Add("");
					startIndex++;
					continue;
				}

					//check for textDelimiter
				else if(data[startIndex] == textDelimiter)
				{
					//read quoted text field
					StringBuilder field = new StringBuilder();
					startIndex++;
					while(startIndex + 1 < data.Length && data[startIndex] != textDelimiter && data[startIndex] != '\n' && data[startIndex] != '\r')
					{
						field.Append(data[startIndex]);
						startIndex++;
					}
					fields.Add(field.ToString());
					startIndex += 2;//skip the textDelimiter and the fieldDelimiter
					continue;
				}
				else
				{
					//read unquoted field
					StringBuilder field = new StringBuilder();
					while(startIndex < data.Length && data[startIndex] != fieldDelimiter && data[startIndex] != '\n' && data[startIndex] != '\r')
					{
						field.Append(data[startIndex]);
						startIndex++;
					}					
					fields.Add(field.ToString().Trim());
					startIndex++;
					continue;
				}//else it's an unquoted field
			}//while not done
			if(fields.Count > 0)
				lines.Add(fields);
			return lines;

		}//SplitCSVLine(string line, char fieldDelimiter, char textDelimiter)

		public static string GetMD5String(string text)
		{
			MD5 md5 = MD5.Create();
			return byteArrayToString(md5.ComputeHash(Encoding.UTF8.GetBytes(text)));
		}

		private static string byteArrayToString(byte[] bytes)
		{
			StringBuilder sb = new StringBuilder(bytes.Length);
			for (int i = 0; i < bytes.Length; i++)
			{
				sb.Append(bytes[i].ToString("x2"));
			}
			return sb.ToString();
		}//byteArrayToString(byte[] bytes)
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

MattsterP
Web Developer
United States United States
Matt Palmerlee is a Software Engineer that has been working in the Microsoft.NET environment developing C# WebServices, Windows Applications, Web Applications, and Windows Services since 2003.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 21 Jun 2007
Article Copyright 2007 by MattsterP
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid