- Home /
Converting Text to a String Array Format
So I have a text file with 230,000 words (Essentially every word in the English dictionary, I got it from the MOBY project) and it is set up like this:
Angry
Answer
Ants
And so on. So each word in the text file has a line break after.
I want to copy and paste the entirety of each letter section into it's own array in my script (C#).
So it would be something like (Which I think is correct but I'm not sure):
string[] Awords = new string[] {"Angry","Answer","Ants", And so on...};
But my problem is I'm dealing with huge amounts of words here, so I can't take the time to turn:
Angry
Answer
Ants
to "Angry","Answer","Ants"
by hand since I'd have to do that for thousands of words. So I'm wondering if there's some way to automate this process, which would be something like:
Remove line break from end of word.
Add " to beginning of word and ", to end.
I was hoping maybe someones already made a program for this since it would be one of the first steps in making any sort of word game. Or possibly there is already a text file online in this format?
My second, related, question is am I going about this the right way? With Arrays and such?
Anyway, thanks for your time.
Why don't you want to load this as a file? Or probably a Text resource?
Would it be faster (In terms of processing) to do it that way? I figure if I had to read through an entire file each time I checked to see if something made a word it would be slower then just splitting them into array's in a C# script by their letter then searching through those based on the first letter of the word being checked.
Answer by whydoidoit · Aug 20, 2013 at 06:44 PM
What I would do is create a text file with your words in it and then put that file into your project inside a Resources folder.
Then you can use:
var textFile = Resources.Load("NameOfFileWithoutExtension", typeof(TextAsset)) as TextAsset; //C#
To get the resource and then make an array using:
var textArray = textFile.text.Split('\n'); //C#
Or perhaps even better also turn it into a dictionary so you can look it up! You could use Linq for that
using System.Linq;
...
Dictionary<string, bool> words;
...
words = textFile.text.ToDictionary(s=>s,s=>true);
This lets you use things like ContainsKey which is very fast to find a word.
It's going to take me a while to check this out, since I've never dealt with this side of C#, but I'll look into it if you think it's faster then just filling a static array in a script, like I described in my question?
Yes, you should read the data not try to turn it into a scripted array. It won't be faster, but you'll never notice the difference as it only happens on start up.
So then there are two more things I need to deal with though if I do it this way,
So I need to separate the entire file into 26 different pieces so that I can look up words slightly faster by using their first letter as a starting point (I'm making this primarily for mobile, so speeds an issue, although if you think it won't be a problem I guess I can just use the one array with all the words).
The other thing I wanted to make sure of is will this work on mobile? I just have this funny feeling reading out a text file during run time might cause issues (I'm not sure though).
It's all fine on mobile.
Are you trying to find a real word or a partial match?
a) If it's a real complete word then nothing is faster than words.Contains$$anonymous$$ey("candidateWord") that's an O(1) operation (it doesn't matter how many words there are).
b) If you are looking for closest candidate then using a Binary Search algorithm is the way to go - that's an O(log n) operation which with 233000 words means roughly only 13 or 14 comparisons to find the closest word to your candidate.
There may be a built in one (can't spot it on google), but Binary Search is real easy to implement anyway and works on any sorted array or List.
I'm looking for complete words, so it sounds like you've given what I'm looking for, now I just need to understand what I'm doing, lol.
I think I'm getting the general gist though, basically dictionary is the reverse of an array where SomeArray[0] = SomeValue, it's SomDictionary[SomeValue] = 0, right?
so let's say the word they make is: "apple"
if(dictionary.Contains$$anonymous$$ey("apple"))
{
//The Player has found a word, give thanks.
}
would be pretty much it, yah?
I probably shouldn't even bother asking this right now, but it's giving me this error on line 5 in your dictionary example:
"The type or namespace name Dictionary
2' could not be found. Are you missing a using directive or an assembly reference?"
Answer by tw1st3d · Aug 20, 2013 at 07:44 PM
String[] lines = File.ReadAllLines("customfiles/dictionary.txt");
Pretty much just telling it to find YourGameDirectory\\customfiles\\dictionary.txt and read each line as a new array item.