How do I populate the dictionary object with a long list of words from a text file?

0 favourites
  • 4 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • I'd like to populate the dictionary from a list of about 19,000 words. The words are in a text file where each word is on a newline. I'd like to only store words with word length of seven or less in the dictionary.

    I've created both AJAX and dictionary objects in my project file along with the text file. AJAX appears to read only about 200 words then stops and I've zero entries in the dictionary.

  • You need to parse the json entries in a loop and set the values in the dictionary.

    In json load completed parse the last data object from json and use a loop to iterate through the json values. You can use inner loops as well, but it's bot your case. Then set or add values in the dictionary.

    The second method you can try is to define the dictionary and edit the json file associated with it and put your values there.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Post how you’re doing it now and maybe someone can spot what’s amiss.

    Generally the logic would be, request the file with Ajax, use tokenat to get a word at a time, then you could compare the length of the word before adding it to a dictionary.

    Var data=“”
    Var word=“”
    
    Start of layout
    — Ajax: request file “dictionary.txt”
    
    Ajax: on request complete
    — set data to Ajax.lastdata
    — repeat tokencount(data, newline) times
    — — set word to tokenat(data, loopindex, newline)
    — — compare: len(word) <= 7
    — — — dictionary: add key word

    For thousands of words this will be slow but at least it’s only done once. You could then save the dictionary.asJson and just load that so you don’t have to parse the file every time. Or if it’s essential to just parse fast, don’t use tokenat, use find and mid instead.

  • System: On start of layout -> AJAX: Request wordlist.txt (tag "LoadWordlist")

    AJAX: On "LoadWordlist" completed -> const textData = AJAX.LastData

    const words = textData.split(/\r?\n/);

    const validWordsDictionary =

    runtime.objects.ValidWordsDictionary.getFirstInstance();

    words.forEach(word => {

    if (word.length <= 7) {

    validWordsDictionary.add(word, true);

    }

    });

    Above is my code using c3 and part of it in JS. I've also created an AJAX object as well as a dictionary object named ValidWordsDictionary. The wordlist.txt is also included as a file within the project.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)