Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tocaro · Aug 20, 2013 at 06:39 PM · arraytextstring

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:

  1. Remove line break from end of word.

  2. 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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Aug 20, 2013 at 06:41 PM 0
Share

Why don't you want to load this as a file? Or probably a Text resource?

avatar image Tocaro · Aug 20, 2013 at 06:45 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tocaro · Aug 20, 2013 at 06:49 PM 0
Share

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?

avatar image whydoidoit · Aug 20, 2013 at 06:51 PM 0
Share

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.

avatar image Tocaro · Aug 20, 2013 at 07:01 PM 0
Share

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).

avatar image whydoidoit · Aug 20, 2013 at 07:06 PM 0
Share

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.

avatar image Tocaro · Aug 20, 2013 at 07:17 PM 0
Share

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 Dictionary2' could not be found. Are you missing a using directive or an assembly reference?"

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I detecting whether certain characters appear in any strings in my array of strings? 1 Answer

Creating a String Array from a text file,Creating a StringArray from a TextAsset 1 Answer

Split string in C#? 1 Answer

Coloring separate text in string array 1 Answer

Iterating through multidimensional arrays 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges