-4

I have an array of strings generated randomly. Now, how am I going to check if a string is correctly spelled or not, based on US English dictionary. This way, I can remove non-English words from the list.

What I did right now is to loop through the list and have it queried to a database of dictionary words. Unfortunately, it is not efficient especially if my list contains hundred of words.

I have read about Aspell but unfortunately, I have to install it, and I am restricted because I am hosting the program in a shared web hosting.

Anyway, here's what I have so far:

//  generate random strings using the method I coded
//  returns a string array of generated strings
//  no duplicates generated here
//  just plain permutations
$generated_list = generate();

Since I have read an article that instead of looping and do query for each string, I just did a single query, like this Performing A Query In A Loop :

$only_english_list = [];
if (count($generated_list)) {
    $result = $connection->query("SELECT `word` FROM `us_eng` WHERE `word` IN (" . implode(',', $generated_list));
    while ($row = $result->fetch_row()) {
        $only_english_list[] = $row['word'];
    }
}

However, is there more efficient in checking if a string is in English dictionary? Something like a method that will return true or false?

Lynnell Neri
  • 473
  • 1
  • 9
  • 21
  • 1
    @NiranjanNRaju: He asked same question earlier (sometimes before) regarding this issue only. It was closed and get deleted by USER only. Again, he asked the same question with no code. – Nana Partykar Dec 07 '15 at 14:52
  • I just loop through the array and each string is being queried to a database table containing English words. – Lynnell Neri Dec 07 '15 at 14:52
  • 1
    @NanaPartykar did knew that... anyhow, question will be closed again... – Niranjan N Raju Dec 07 '15 at 14:53
  • 1
    @LynnellEmmanuelNeri, we are here to help you out in your code, not to code... – Niranjan N Raju Dec 07 '15 at 14:54
  • huhuhu... I don't feel welcomed here in StackOverflow. – Lynnell Neri Dec 07 '15 at 14:54
  • Yes, I will show portions of my code. – Lynnell Neri Dec 07 '15 at 14:54
  • @LynnellEmmanuelNeri, sure.. add that... in any mistake, we will defenitely help you.. – Niranjan N Raju Dec 07 '15 at 14:56
  • Some things you can do: Remove duplicates, create *one* query (the `IN` operator would be useful for that), and let the database do the work. Using a relational database would be slow pretty much no matter what you do. You would need an in-memory representation of the dictionary (a tree structure of some kind, or a hash table, for example) in order to make it fast. – Sverri M. Olsen Dec 07 '15 at 14:56
  • 1
    @LynnellEmmanuelNeri No one wants you to feel unwelcome - however showing your code is a requirement, as it is not uncommon for people to simply expect SO users to do their work for them, Showing code proves you are genuinely trying to solve the problem but need help. – Steve Dec 07 '15 at 14:57
  • Hello. I've already added a code snippet. – Lynnell Neri Dec 07 '15 at 15:07
  • Ok, adding code is a start. How does the sql query relate to your `$generated_list`? It seems to be unrelated. – Steve Dec 07 '15 at 15:32
  • I've updated the query – Lynnell Neri Dec 08 '15 at 16:19

1 Answers1

1

I now have an answer to this problem. Here's what I did. Instead of generating permutations, which takes a MASSIVE amount of time and resources, I just utilize IMMEDIATELY the capability of MySQL. That is, I used REGEXP or LIKE against a table of English words of a certain length.

So, for English words that can be formed from vleoly, I used this query to a table of English words of length 6, noted by us_6.

    SELECT word FROM us_6 WHERE
    word REGEXP 'v' AND
    word REGEXP 'l.*l' AND
    word REGEXP 'e' AND
    word REGEXP 'o' AND
    word REGEXP 'y'

And results generated are lovely and volley.

For more information, check MySQL, REGEXP - Find Words Which Contain Only The Following Exact Letters

Community
  • 1
  • 1
Lynnell Neri
  • 473
  • 1
  • 9
  • 21