Search word in a dictionary

Hello. I have a problem with the search for a word in a dictionary. The research work when I try the application in the corona simulator but on the device (android) does not work. The code is as follows:

local function BuildDictionary(  )

    local path=system.pathForFile(“parole.txt”)

    local file=io.open(path,“r”)

    if file then

        for line in file:lines( ) do

            dictionary[line]=true

        end

        io.close( file )

    end

end

local function CheckWord()

    return dictionary[word]

end

Any idea?

Make sure your file is actually named “parole.txt” – upper/lowercase included. The simulator won’t care if there’s a difference, but the device will.

Jay

File name is parole.txt all lowercase.

Okay, that was my best guess. :slight_smile:

In your code you have this:

local function CheckWord() return dictionary[word] end

Do you mean to pass the variable “word” into that function or is “word” a global variable that’s defined somewhere else?

One more thing, in order to narrow down where the problem lies, make sure the dictionary is actually being filled After you fill it (outside of the BuildDictionatary function) do something like 

print(dictionary[“foo”])

…where foo is a word you know to be in the list – make sure that displays.

Word is local variable defined for all scene. In createScene of storyboard template i’ve added this:

    BuildDictionary()

    print(dictionary[“ciao”])

    print(dictionary[“gdgdg”])

    if dictionary[“ciao”] then

        wordText:setText( “CIAO” )

    end

wordText is textcandy object… In corona simulator i see CIAO but in phone nothing happen…

What I’d do is change that code to this:

BuildDictionary() print(dictionary["ciao"]) print(dictionary["gdgdg"]) if dictionary["ciao"] then print("wordText is next...") wordText:setText( "CIAO" ) end

That will tell you whether there’s a problem with your dictionary or with TextCandy – based on what you said in the previous post (ciao showing up in the terminal) I’m betting it’s a TextCandy problem.

 Jay

Sorry I may have created some confusion but textcandy is not a problem here. The object wordtext clearly displays all the words that i compose with the application. The code i added was just to get a response to the test of the presence of the word in the dictionary that was visible on the phone.

Anyone have an idea?  :unsure:

After several attempts I finally found the solution. The problem seems to be due to the newline character. By performing a trim during insertion of the words in the dictionary table, it does work well on the device!

[lua]

local function BuildDictionary(  )

    local path=system.pathForFile(“parole.txt”)

    local file=io.open(path,“r”)

    if file then

        for line in file:lines( ) do

            line = (line:gsub("^%s*(.-)%s*$", “%1”))

            dictionary[line:upper()]=true

        end

        io.close( file )

    end

end

[/lua]

Make sure your file is actually named “parole.txt” – upper/lowercase included. The simulator won’t care if there’s a difference, but the device will.

Jay

File name is parole.txt all lowercase.

Okay, that was my best guess. :slight_smile:

In your code you have this:

local function CheckWord() return dictionary[word] end

Do you mean to pass the variable “word” into that function or is “word” a global variable that’s defined somewhere else?

One more thing, in order to narrow down where the problem lies, make sure the dictionary is actually being filled After you fill it (outside of the BuildDictionatary function) do something like 

print(dictionary[“foo”])

…where foo is a word you know to be in the list – make sure that displays.

Word is local variable defined for all scene. In createScene of storyboard template i’ve added this:

    BuildDictionary()

    print(dictionary[“ciao”])

    print(dictionary[“gdgdg”])

    if dictionary[“ciao”] then

        wordText:setText( “CIAO” )

    end

wordText is textcandy object… In corona simulator i see CIAO but in phone nothing happen…

What I’d do is change that code to this:

BuildDictionary() print(dictionary["ciao"]) print(dictionary["gdgdg"]) if dictionary["ciao"] then print("wordText is next...") wordText:setText( "CIAO" ) end

That will tell you whether there’s a problem with your dictionary or with TextCandy – based on what you said in the previous post (ciao showing up in the terminal) I’m betting it’s a TextCandy problem.

 Jay

Sorry I may have created some confusion but textcandy is not a problem here. The object wordtext clearly displays all the words that i compose with the application. The code i added was just to get a response to the test of the presence of the word in the dictionary that was visible on the phone.

Anyone have an idea?  :unsure:

After several attempts I finally found the solution. The problem seems to be due to the newline character. By performing a trim during insertion of the words in the dictionary table, it does work well on the device!

[lua]

local function BuildDictionary(  )

    local path=system.pathForFile(“parole.txt”)

    local file=io.open(path,“r”)

    if file then

        for line in file:lines( ) do

            line = (line:gsub("^%s*(.-)%s*$", “%1”))

            dictionary[line:upper()]=true

        end

        io.close( file )

    end

end

[/lua]