#Absolute Beginner - Checking an input field against a large list of words?

1 messages · Page 1 of 1 (latest)

static crescent
#

First off, it would be helpful to know more about the specific rules and mechanics of your game. That way, I could give more precise advice for your game specifically. But, for now, here what I can tell you.

I'd like to be able to check an input field against a large list of words, specifically a Google Sheet of all words in the Oxford Dictionary.
I'd start by exporting your Google Sheet into a CSV file because as far as I know, working with google sheet directly from HTML and JS isn't possible. The problem is that the Oxford Dictionary contains over 270k words. Loading this many words at once might consume a significant amount of memory and could slow down your application. That's, if you're able to make it load (in a reasonable amount of time). To optimize this, consider reducing the word list to only what you need for your game, which would help improve performance. If reducing the list isn’t possible, you might want to use a server-side database to store the words and query them as needed. Alternatively, check if the Oxford Dictionary offers a free API that you can use to look up words dynamically instead of handling it yourself

I'd also like to know how to implement a regex 'filter' to catch words that will be deemed correct, or incorrect.
For that, learning regex isn't difficult, there's tones of resources online. You can also use a tool like https://regexr.com/ to help you write your expressions. That said, regex might not be necessary for a simple word comparison. If your goal is just to check if a word is valid based on your list, a straightforward comparison will do the job without the need for regex.

stable maple
#

I was aware that I might need to put the words into a separate file, just couldn't remember which one it was 😄

The regex itself isn't precisely the issue, I've done a fair bit of it for a load of Minecraft skills a long time ago, so I know my way around it, it's just figuring out where to put it in a script. I can't quite word my Google searches all that well, with zero knowledge.

  if (isNaN(x) || x < 1 || x > 10) {
    text = "Input not valid";
  } else {
    text = "Input OK";
  }```

I've grabbed this from the W3Schools website, I reckoned that I could insert the regex check instead of the maths there and get a similar result, but it simply doesn't register.
static crescent
# stable maple I was aware that I might need to put the words into a separate file, just couldn...

I was aware that I might need to put the words into a separate file, just couldn't remember which one it was 😄
The main concern isn't that much the format but the number of words. 270k words is a lot and specially if you have to go through the list often it's going to take a while and/or require a lot of optimization.

The regex itself isn't precisely the issue, I've done a fair bit of it for a load of Minecraft skills a long time ago, so I know my way around it, it's just figuring out where to put it in a script. I can't quite word my Google searches all that well, with zero knowledge.
Hard to say because I don't have more context or even the code but the placement doesn't matter. that much. In general, in javascript, it's done with a event (like typing or submitting) and will run in a function (at least that's how I would do).

As for the comparison of text, there's various ways to do it but the simple way to compare 2 strings is like that

if (input == "blabla") {
  // do whathever when true
} else {
  // do whathever when false
}

// or if you have a list of words
if (words.includes(input)) {
  // do whathever when true
} else
  // do whathever when false
}

You can do a bit more things if you want to cover cases like uppercase, downcase, special characters, etc but none of that really require regex.

#

But, again, that would be better if you could explain in details what is your game idea because, with the info I have it's hard to give more precise advice.

#

Okay okay, so creating the regex for that wouldn't be too difficult. You can use string formatting and use the basic javascript's regex function to validate the words, that's not difficult.

const word = "one"; // get this from wherever
const regex = new RegExp(word, "i");

// Testing the regex
const testString = "nonexplosive";
const match = testString.match(regex);

// you can use the result and other regex method for your logic
console.log(match); // Output: ["one"]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions

I would still be concern about the word list part, because again, it's a huge list and going through the list will be slow. I think it's worth looking into a free dictionary API (maybe the Oxford dictionary offers one) or consider a server-side database instead of loading the words from a CSV and go through the list but you can try the CSV route too and change later if you want too, it shouldn't matter too much as long as you separate the source from your game logic correctly.

It's important to understand that each possible solution has consideration, other challenges and requirements.