1

For example, I have a keyword "abandoned" and I want to find the words that contains letters of this keyword such as "done", "abandon", band", from the arrays I stored those words. How can I search it?

I tried to write this code but not working properly. I wrote a function which takes the keyword and the word. Then i put letters of keyword to keywordletters array and the letters of the word into wordletters array.

Then i wrote a loop for matching letters. If the wordletters array match with the letter of keywordletters array, then i assigned the current wordletters element nil and then i made the keywordletters element nil. Because we can't use it second time.

After all loops, i checked the wordletters array. If it has an element which is not nil then i returned false. However, it's not working how i want. Can you help me out?

EDIT: ıI solved my problem and edited the code accordingly.

Here is my code:

  function consistLetters(keyword,word)

keywordletters={ }
    wordletters= { }
local found=false
findLetters(keyword,keywordletters)
findLetters(word,wordletters)


for i=1, #wordletters,1 do
    for j=1, #keywordletters,1 do
        if(keywordletters[j]~="") then
            if(wordletters[i]==keywordletters[j]) then
                keywordletters[j]="" 
                found=true;
                break
            end
         end
    end
    if found~=true then
        return false
    end
   found=false; 
end     

  end
Figen Güngör
  • 12,169
  • 14
  • 66
  • 108
  • Similar to http://stackoverflow.com/questions/3856630/how-to-separate-words-in-a-sentence-with-spaces/3885435#3885435 – lhf Jun 26 '12 at 12:07

3 Answers3

1

For example, I have a keyword "abandoned" and I want to find the words that contains letters of this keyword such as "done", "abandon", band", from the arrays I stored those words. How can I search it?

You can simply use the keyword as a regular expression (aka "pattern" in Lua), using it's letters as a set, for instance ('^[%s]+$'):format('abandoned'):match('done').

local words = {'done','abandon','band','bane','dane','danger','rand','bade','rand'}
local keyword = 'abandoned'

-- convert keyword to a pattern and match it against each word
local pattern = string.format('^[%s]+$', keyword)
for i,word in ipairs(words) do
    local matches = word:match(pattern)
    print(word, matches and 'matches' or 'does not match')
end

Output:

done    matches
abandon matches
band    matches
bane    matches
dane    matches
danger  does not match
rand    does not match
bade    matches
rand    does not match
Mud
  • 28,277
  • 11
  • 59
  • 92
0

Try this:

W={"done", "abandon", "band"}
for k,w in pairs(W) do
    W[w]=true
end

function findwords(s)
    for i=1,#s do
        for j=i+1,#s do
            local w=s:sub(i,j)
            if W[w] then print(w) end
        end
    end
end

findwords("abandoned")

If you don't have an array of words, you can load a dictionary:

for w in io.lines("/usr/share/dict/words") do
    W[w]=true
end
lhf
  • 70,581
  • 9
  • 108
  • 149
  • Thank you but i also want to find words consist of the letters in the keyword not necessarily should be a substring. Sorry for not giving proper examples, all examples i gave were substrings. However, it should also find "dan,dad,nad,ded" if they are in the dictionary and if they consist of letters of the keyword. – Figen Güngör Jun 26 '12 at 12:58
  • @user1463542, oh, that's a very different problem. I suggest you ask a separate question. – lhf Jun 26 '12 at 13:02
0

Run the loop on your arrays and use string.find to check against this long word.

for idx = 1, #stored_words do
   local word = stored_words[idx]
   if string.find(long_word, word, 1, true) then
      print(word .. " matches part of " .. long_word)
   end
end
Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68