I have developed a VERY simple, lightweight program to take inputs, match them with patterns in a pre-defined network, and then predict which category the input may belong to. The net is not trainable, nor is it the most reliable. However, I believe that this may be the first step to creating a true system that would have realistic human-like predictions based on text inputs.
The whole system is based on a Python project found on YouTube here.
The algorithm script, placed in ServerScriptService:
-- Script
local Net = require(game.ServerScriptService.WordNet)
local IGNORE_LETTERS = {"!", "?", ".", "'"}
local function ProcessInput(Input: string): string
-- Manage the formatting of the input string
Input = string.lower(Input)
-- Set a undefined, accessable variable
local Matches = {}
-- Find matches for different patterns
for i, Category in pairs(Net) do
for _, Pattern in pairs(Category["Patterns"]) do
if string.match(Input, Pattern) ~= nil then
table.insert(Matches, i)
end
end
end
table.sort(Matches)
local ChosenCategory = nil
local HighestNumber = 0
local CurrentChosen = nil
local CurrentNumber = 0
-- Find the percentage match based on the results of the match finding
for i, Category in pairs(Matches) do
if CurrentChosen == Net[Category] and CurrentNumber > 0 then
CurrentNumber += 1
if HighestNumber < CurrentNumber then
HighestNumber = CurrentNumber
ChosenCategory = Net[Category]
end
elseif CurrentChosen == Net[Category] and CurrentNumber < 1 then
CurrentNumber = 1
if HighestNumber < CurrentNumber then
HighestNumber = CurrentNumber
ChosenCategory = Net[Category]
end
elseif CurrentChosen ~= Net[Category] and ChosenCategory == nil and HighestNumber < 1 then
CurrentNumber = 1
CurrentChosen = Net[Category]
ChosenCategory = Net[Category]
HighestNumber = 1
elseif CurrentChosen ~= Net[Category] then
CurrentNumber = 1
CurrentChosen = Net[Category]
end
end
if ChosenCategory ~= nil then
local OutcomeSelection = math.random(1, #ChosenCategory["Outcomes"])
return ChosenCategory["Outcomes"][OutcomeSelection]
else
return "Hmm... I don't think I know that one yet. I am still being trained and can't understand everything quite yet."
end
end
local ChatEvent = game.ReplicatedStorage.Chat
ChatEvent.OnServerEvent:Connect(function(Player: Player, Message: string)
task.spawn(function()
print(Player, Message)
ChatEvent:FireClient(Player, ProcessInput(Message))
end)
end)
The Network itself, currently only containing 3 categories:
-- ModuleScript
local Net = {
["Greetings"] = {
["Patterns"] = {"hello", "hi", "hey", "im back"},
["Outcomes"] = {"Hello!", "Hi! How are you?", "Hey!", "Hey! Nice to see you!", "Good to see you!", "Hey! How have you been?", "What's up!"}
},
["Goodbyes"] = {
["Patterns"] = {"bye", "cya", "see ya", "goodbye", "im leaving"},
["Outcomes"] = {"Goodbye!", "Have a good one!", "Sorry to see you leave me...", "Oh, that's okay I guess :("}
},
["Feel Good"] = {
["Patterns"] = {"feel great", "feel good", "feel happy", "feel delighted"},
["Outcomes"] = {"That's great to hear!", "Wonderful!", "Great!", "Sweet!", "Happy day!"}
},
}
return Net
Here is the test place if you would like to try out the current system.
This system is incredibly basic and is not in any way a finished productâŚ