You can use my StringDiff module that uses my very own diffing algorithm to find the similarity metric / ratio between two sentences.
Works within the confines of Roblox without making external API (HttpService) calls.
If you have the sentences:
- “Can I have a cheeseburger?”
- “Can you give me a cheeseburger please?”
It will print a similarity ratio of ~42.655… between those sentences and optionally return the longest match that it got from that:
This algorithm has a few use cases, 1 that I can think of right now is for primitive chat bots. If your dataset is small and you have a lookup table that looks something like this:
local commands = {
['Hey.'] = "Hey!!",
['Hi.'] = "Oh hi!",
['Hello.'] = "Hello there.",
["How's it going?"] = "Hello there.",
['How are you.'] = "I am good.",
['Nice to meet you.'] = "It's very nice to meet you as well.",
}
It doesn’t matter if you write “Hey it’s so nice to meet you”, it’s going to response to you correctly with “It’s very nice to meet you as well.”.
The way this would work (I’ve left out the code for this) is to iterate over every key and calculate the similarity ratio between your original input towards each key in the lookup table. This however becomes very performance heavy with large datasets due to the complexity of O(n), so a proper trained language model neural network is advised for that.
If you don’t want to use a language model, you can alternatively use a stemmer library, tokenize the sentence and create lexemes as lookup keys with larger datasets (“Full-Text Search” in database querying works like this and I’m currently working on that as my next project within Roblox), then iterate and apply the ratio calculation on the narrower return queryset to find out a more specific response.
This could be very useful in for example Roblox cafés or restaurants if you want automated order systems if you only have say 10 to 30 orders.
The code is open sourced on github:
splinestein/splinestein-diffing-algorithm: A string similarity metric diffing algorithm invented by splinestein for chat bot use. (github.com)
Here’s a link to the module:
StringDiff - Roblox
How do you use it?
- Put the module into ReplicatedStorage.
- In your script, require it with:
local sdiff = require(game:GetService("ReplicatedStorage"):FindFirstChild("StringDiff"))
- Run it with
ratio, _ = sdiff.compare("Hey is this working?, "Hey this is working?")
- First return value is the ratio from 0 - 100, second optional return value is the longest match.
print(ratio)