- Home /
Making a search engine in Unity
Hello,
I've been working for some time on an app that contains a lot of written text. This information was seperated into over 200 categories with each one having at least 3 blocks of text. I would like to give the user the possibility to search for some keywords to find one of the text blocks since that is much easier to do than navigate over that many categories.
I have all those text blocks saved in a string[] element. Here is an example :
string[0] = "This is the tutorial. Press A to jump and B to attack."
string[1] = "This area is known as the city of Dhismir, do not enter it without preparations."
string[2] = "This is the legendary sword of Thasburg, it doesn't belong to you. Let me tell you its story"
I could easily make a simple script that checks if the string contains the keywords, but I want my search engine to accept some errors by the user: 1. Some words might be spelled differently by some users. The word "Thasburg" might be replaced by "Thusborg" or "thusburg". This is the main problem since my app is full of words that are often spelled differently depending on the user. 2. Some small words like "the" or "to" might be omitted but I don't know how much I can omit while keeping the search accurate enough.
I've done some research but I couldn't find a simple search engine that is integrated in unity. I would like my app to be offline so I can't use web based searches. I have found only a few tutorials about Linq in unity so I don't really understand how to make a search engine with it. I have found an adaptation of Lucene for Unity on github but I've never used Lucene before and the unity version has got no example and almost no documentation.
If someone knows a working example or tutorial with unity that would be really nice. I would also appreciate some ideas about the functions that I can use to search the strings and on how to account for the user errors please.
I don't really see how this topic is in any way related to Unity. You are asking about an algorithm that's entirely about string processing. Perhaps you'd have more luck on StackOverflow?
Thanks for replying I'm using unity for this project and was hoping that there is maybe a tutorial/asset/example available for unity. I was also hoping that maybe someone got Lucene to work on Unity and could tell me how he did it or knows another similiar library.
Is that a .net or c++ library? Edit: Yes there seems to be a .net port. In that case, get / build a DLL of the library with the correct .net version and copy it to the assets folder somewhere. It will be imported and available to your scripting
Have anyone found anything about this please ?,Have anyone found something about this please ?
Answer by gigos22 · Apr 21, 2021 at 03:14 PM
Try something like that:
You need an InputField for start.
Create a C# script on that InputField.
Inside the Update of that code you check:
for (i = 0; i < yourTextsArray.Length; i++) { if (yourTextsArray[i].Contains(yourInputField.text)) { // add it to some List<string> itemsList } } // also note that it should be better for you to make that string[] of yours a // public static string[]. //And before the part above you write something like: if (yourInputField.onValueChanged()) { // reset the search -> itemsList = new List<string>(), i=0; // so when the user type another letter it resets the search. // you should also take a deeper look at InputField.onValueChanged(). // Also note that: if you use TMP_InputField, the function is onValidateInput(). } }
This part is up to you how you want to show the results.
Also note that this technique is for real-time search, if you think this is too much for you or your project, and you want to make a "google search style", so there the user needs to press enter to search, all you gotta do is to put that for loop in a function and call it when the user hits enter and you send to it the youInputField.text
as the argument and then you don't need that part when you reset the search.
There are a few ways to make the search faster, but first let's see if you handle all of this and that it works for you.
Keep me updated on the subject.
Hello, I meant something that is a bit more forgiving, as mentioned in the original post:
"I could easily make a simple script that checks if the string contains the keywords, but I want my search engine to accept some errors by the user: 1. Some words might be spelled differently by some users. The word "Thasburg" might be replaced by "Thusborg" or "thusburg". This is the main problem since my app is full of words that are often spelled differently depending on the user. 2. Some small words like "the" or "to" might be omitted but I don't know how much I can omit while keeping the search accurate enough."
Dang.. So everything I wrote was a waste uh..
Well making the search engine "forgiving" means you need to create a script that manages that forgiveness. Since it looks like you know your fair share of program$$anonymous$$g I'm just gonna give you guide lines.
In that kind of script you can break down a string and check if some letters are in it, that indicates to a desired word.
It is basically pre-programmed for a specific chunk of words.
Really it shouldn't be much if it's only a few words.
Yeah I found a solution using the Lucene library.
Follow this Question
Related Questions
searching a text file for a player-input string 0 Answers
How Expensive is Find function? 3 Answers
Search for the index in an array of a customised class 1 Answer
Select texture list 2021 0 Answers