Timmy is a Pascal unit for creating chat bots. It provides the TTimmy
object, which is a data type that you can assign your variables to to make bots. Once that is done, you can start adding keywords for messages, and get the bot answers to the end user's messages.
Creating bots with Timmy is as easy as 1 2 3.
- Declare
timmy
unit - Declare a
TTimmy
variable - Initialize your
TTimmy
- Add some keywords for your messages, and possible replies to those messages
- Start answering
Program MyFirstBot;
Uses timmy;
Var MyBot: TTimmy;
BEGIN
MyBot.Init(70, 'I gave up', False);
MyBot.Add(StrSplit('Hello', ' '), StrSplit('Greetings!|Hello to you!|Hi!', '|'));
MyBot.Add('How are you', 'I am fine!;Never better!;I''m doing great.');
MyBot.Add('What 2 + 2', 'The answer is 4', ' ', '@');
Writeln(MyBot.Answer('Hello!'));
Writeln(MyBot.Answer('How are you?'));
Writeln(MyBot.Answer('What is 2 + 2?'));
Writeln(MyBot.Answer('What is the meaning of life?'));
END.
If we compile and execute the program, we should get something like this as the output:
Hi!
I'm doing great
The answer is 4
I gave up.
For more examples, check out the examples
folder.
- Source: Line 143 (reference)
- Parameters:
Percent
[Integer]: Desired initial value forTTimmy.TPercent
DefaultRep
[String]: Initial value forTTimmy.NoUdstdRep
DpCheck
[Boolean]: Initial value forTTimmy.DupesCheck
- Availability: v1.2.0
- Description: Constructor of the
TTimmy
instance, which prepares the instance for being used. In this constructor,TTimmy.TPercent
,TTimmy.NoUdstdRep
, andTTimmy.DupesCheck
get assigned to the values of the argumentsPercent
,DefaultRep
andDpCheck
, respectively.TTimmy.Enabled
is set to true, and the bot starts with nothing in its metadata. You must run this constructor before performing any other operation with your bot instance, or else the bot won't work properly.
- Source:
- Parameters: None
- Return: Integer
- 101: The instance is already initialized
- 100: The operation is successful
- Availability: v1.0.0 to v1.1.0
- Description: Acts like v1.2.0's
TTimmy.Init()
constructor, but this one is a function.TTimmy.TPercent
,TTimmy.NoUdstdRep
,TTimmy.DupesCheck
get assigned to 70, "Sorry, I didn't get that", andTrue
, respectively. You must run this function before performing any other operation with your bot instance, or else the bot won't work properly.
- Type: Boolean variable
- Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.Enabled
tells other functions ofTTimmy
whether if the bot instance is ready to work. IfTTimmy.Enabled
is false, all major functions ofTTimmy
won't perform their operations and will exit right away, usually with the return code 102. The value of this boolean variable can be set by usingTTimmy.Enable()
orTTimmy.Disable()
.
- Source: Line 154 (reference)
- Parameters: None
- Visibility: Public
- Availability: v1.2.0
- Description: Procedure that sets
TTimmy.Enabled
to true. In other words, it enables the bot.
- Source: Line 158 (reference)
- Parameters: None
- Visibility: Public
- Availability: v1.2.0
- Description: Procedure that sets
TTimmy.Enabled
to false, as oppose ofTTimmy.Enable()
. You may disable the bot if you somehow want it to stop working temporarily.
- Type: Dynamic array of
TStrArray
- Visibility: Private
- Availability: v1.0.0 to v1.2.0 (In v1.0.0, it's
QKeywordsList
) - Description: This is an array of arrays. Each array in this array holds strings, which are keywords for a message. Keywords help the bot (or more specifically,
TTimmy.Answer()
) to understand the end-user's messages and have replies for the messages.
- Type: Dynamic array of
TStrArray
- Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description: Just like
TTimmy.MKeywordsList
,TTimmy.ReplyList
is an array of arrays. Each array inTTimmy.ReplyList
holds strings, which are possible replies for a message. If an array inTTimmy.ReplyList
has more than two strings, the bot will pick one, randomly. Arrays inTTimmy.ReplyList
are correspond to arrays inTTimmy.MKeywordsList
in terms of position. For example, the replies at offset 2 ofTTimmy.ReplyList
are replies for the message with the keywords at offset 2 ofTTimmy.MKeywordsList
.
- Type: Integer
- Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.NOfEntries
is the number of elements inTTimmy.MKeywordsList
, orTTimmy.ReplyList
(the length ofTTimmy.MKeywordsList
is the same as the length ofTTimmy.ReplyList
at all times anyway). We implement this instead of doingLength(MKeywordsList)
(orLength(ReplyList)
) because it's more convenient.
- Source:
- Parameters: None
- Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description: Procedure that sets the lengths of
TTimmy.MKeywordsList
andTTimmy.ReplyList
toTTimmy.NOfEntries
. This procedure is called whenever the bot takes or remove data within its metadata (either byTTimmy.Add()
orTTimmy.Remove()
), in which the length ofTTimmy.MKeywordsList
(andTTimmy.ReplyList
as well) is changed.
- Type: Integer
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.TPercent
specifies the minimum percentage of the number of matching keywords over the total number of words in the message that the bot needs in order to "understand" the message.
- Type: String
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.NoUdstdRep
is the default reply of the bot. It is returned toTTimmy.Answer()
whenever the bot does not "understand" the user's message.
- Source:
- Parameters:
MKeywords
(QKeywords
in v1.0.0) [TStrArray
]: New keywords clue for a messageReplies
[TStrArray
]: Possible replies to the message that contains the keywords clue inMKeywords
- Return: Integer
- 102: The instance is not enabled (or initialized)
- 202: Duplication check is enabled (
TTimmy.DupesCheck
= true) and a match ofMKeywords
is presented inTTimmy.MKeywordsList
- 200: The operation is successful
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.Add()
adds new data to the bot's metadata, which means it addsMKeywords
toTTimmy.MKeywordsList
andReplies
toTTimmy.ReplyList
. It takes two arguments, one is keywords clue for a message, and two is possible responses to that message.TTimmy.Add()
is overloaded and this implementation of it is considered the original implementation.
- Source:
- Parameters:
KeywordsStr
[String]: New keywords clue for a message, joined by the space characterRepStr
[String]: Possible replies to the message that contains the keywords presented inKeywordsStr
, joined by the semicolon
- Return: The same as the original implementation of
TTimmy.Add()
. - Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: Yet another function to add new data, but this one takes string arguments instead of
TStrArray
s. These strings will then be delimited using a delimiter with the help of theStrSplit()
function to formTStrArray
s, and theseTStrArray
s will be passed over to the original implementation ofTTimmy.Add()
.
- Source:
- Parameters:
KeywordsStr
[String]: New keywords clue for a message, joined byKStrDeli
RepStr
[String]: Possible replies to the message that contains the keywords presented inKeywordsStr
, joined byMStrDeli
KStrDeli
[Character]: Delimiter forKeywordsStr
MStrDeli
(QStrDeli
in v1.0.0) [Character]: Delimiter forRepStr
- Return: The same as the original implementation of
TTimmy.Add()
. - Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: This implementation of
TTimmy.Add()
is the same as the above one (which takes string arguments). However, the difference is, this one allows you to use custom delimiters for the strings.
- Type: Boolean
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.DupesCheck
specifies whetherTTimmy.Add()
should check for duplicate before adding new data or not. If the new data matches any of those already persisted inTTimmy.ReplyList
,TTimmy.Add()
stops its operation and returns 202, indicating that the operation is not successful.
- Source:
- Parameters:
MKeywords
(QKeywords
in v1.0.0) [TStrArray
]: Keywords clue to delete from the bot's metadata. - Return: Integer
- 102: The instance is not enabled (or initialized)
- 308: The operation is successful
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.Remove()
removes data from the bot's metadata, and as of version 1.2.0, there are 4 overloadedTTimmy.Remove()
. This one takes aTStrArray
, find matching arrays (arrays with the same elements in the same order) inTTimmy.MKeywordsList
, and delete those matching arrays. It also deletes the array(s) inReplyList
that is/are bond to those matching array(s) of keywords.
- Source:
- Parameters:
KeywordsStr
[String]: Keywords clue to delete from the bot's metadata, joined by the space character. - Return: The same as the above implementation of
TTimmy.Remove()
. - Visibility: Public
- Availability: v1.1.0 to v1.2.0
- Description: Another overloaded
TTimmy.Remove()
, this one takes one and only string argument. The string is then delimited using the space character to get aTStrArray
output, and thisTStrArray
is processed by the aboveTTimmy.Remove(MKeywords: TStrArray)
.
- Source:
- Parameters:
KeywordsStr
[String]: Keywords clue to delete from the bot's metadata, joined byKStrDeli
KStrDeli
[Character]: Delimiter forKeywordsStr
- Return: The same as the top implementation of
TTimmy.Remove()
(TTimmy.Remove(MKeywords: TStrArray)
). - Visibility: Public
- Availability: v1.1.0 to v1.2.0
- Description: Yet another overloaded
TTimmy.Remove()
which is quite similar to the above one, but this one allows you to use any delimiter you like instead of the space character.
- Source:
- Parameters:
AIndex
[Integer]: Offset of elements inTTimmy.MKeywordsList
andTTimmy.ReplyList
that need to be removed - Return: Integer
- 305:
AIndex
is an invalid offset inTTimmy.MKeywordsList
(orTTimmy.ReplyList
) - 300: The operation is successful
- 305:
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: This is the major implementation of
TTimmy.Remove()
due to the fact that other overloadedTTimmy.Remove()
rely on this one, whether directly or indirectly. This one removes the array at offsetAIndex
inTTimmy.MKeywordsList
and inTTimmy.ReplyList
. In other words, it removesMKeywordsList[AIndex]
andReplyList[AIndex]
.
- Source:
- Parameters:
TMessage
(TQuestion
in v1.0.0) [String]: End-user's message to the bot - Return: String. Bot's response to
TMessage
. - Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: Given the end-user's message, returns the bot instance's response to that message. The message is first pre-processed (like removing extra white-spaces or punctuations like ! or ?). Then, it is splitted into many words using the space character. The function will then iterate through
TTimmy.MKeywordsList
, and compute the percentage of the keywords in each of the array inTTimmy.MKeywordsList
to the user message's splitted words. If the percentage is larger thenTTimmy.TPercent
, a random reply in the corresponding array inTTimmy.ReplyList
will be returned toTTimmy.Answer()
. In this case, we say that the bot has "understood" the end-user's message. In the case that it could not understand,TTimmy.NoUdstdRep
is returned.
TStrArray
is Array of Array of String
. In Timmy, it is used instead of Array of Array of String
to avoid type incompatible compile error.
- Source:
- Parameters:
S
[String]: String to be processed. - Return: String. The processed string.
- Availability: v1.0.0 to v1.2.0 (In v1.0.0, it's
StrProcessor()
) - Description:
StrTrim()
deletes extra white spaces in the stringS
.
- Source:
- Parameters:
S
[String]: String to be splitteddelimiter
[Character]: Delimiter forS
- Return:
TStrArray
, contains the strings ofS
after being splitted. - Availability: v1.0.0 to v1.2.0
- Description:
StrSplit()
splits the stringS
usingdelimiter
as delimiter, and returns aTStrArray
holding the delimited strings.
- Source:
- Parameters: Two
TStrArray
s to be compared,ArrayA
andArrayB
. - Return: Boolean. True if
ArrayA
is the same asArrayB
, false otherwise. - Availability: v1.0.0 to v1.2.0
- Description: This function compares two
TStrArray
s to see if they have the exact same elements. The order of elements in the arrays does matter.
Timmy is licensed under the GNU LGPL v3.