Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to check the word exists in text document

Discussion in 'Scripting' started by alexchandriyaa, Feb 16, 2018.

  1. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    am creating word cookie type of game i have to check the entered word is correct or not so am creating text document with all possible words now i dont kno how to access that through script can anyone help me out
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Load the word document as a text asset and search for the string.
    Although, if it contains a lot of characters the search will be very slow. Pick a way to index the document.
    Maybe write your own word tree where every letter is a branch or leaf.
     
  3. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    any examples??
     
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Text asset and string searching:
    https://docs.unity3d.com/ScriptReference/TextAsset.html
    https://docs.microsoft.com/en-us/do...eatures/strings/how-to-search-within-a-string

    Some tree logic:
    https://www.geeksforgeeks.org/print-valid-words-possible-using-characters-array/

    And a simple word tree would be something like
    Code (CSharp):
    1. //This serves as tree root
    2. public class wordTree {
    3.     public List<wordTreeNode> children;
    4.  
    5. }
    6. //serves as branch and leaf
    7. public class wordTreeNode {
    8.     public char value;
    9.     public List<wordTreeNode> children;
    10. }
     
  5. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    but i cant give all possible words in the code right??
     
  6. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Why not? I hope you don't intend to do a file read for every word verification...
    You use text asset to get a string from file and go from there.
    How many words are we talking about?
     
    Last edited: Feb 16, 2018
  7. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    plenty because for jumbled letters am calling it randomly so i dont know which word can be formed i have to check list of words in text document
     

    Attached Files:

  8. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Yeah just pack it all into a word tree then.
    It may not be the most efficient solution but checking if the word is valid can then be accomplished in (worst case) O(n*52) where n is the word's length.
     
  9. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    A quick example of a small word tree:
    upload_2018-2-19_7-36-2.png
    C is located in root, red circles represent leaves and every other letter is a branch.
    So number of valid words will in the end be represented by a number of leaves.