1. Dictionary - StewartML

Thanks for  the wordlist, it will be useful for some games.
I have a few comments about dictionary.e which may help someone.

1. for platform independence, remove both \n and \r when reading the text file

2. better to upcase the search word, and add an '*' if user forgets, otherwise
    wildcard match will fail even if the word is in the list.

3 a. if dictionary[i] = word won't work, because you will be trying to compare 
words of different lengths. equal() works, but there's a better way.

3b. find() is faster than a loop, except for very short lists of words.
      finding 'ZOO' with a loop takes 5.74 seconds,
      finding 'ZOO' with find() takes 2.17 seconds (1000 repetitions)
     
See mods below.
Regards,
Irv

include wildcard.e

global sequence Dictionary

global procedure LoadDictionary(sequence path)
    integer fn
    object temp

    fn = open(path, "r")
    if fn = -1 then
        printf(2, "Error opening the dictionary '%s'\n",{path})
        abort(1)
    end if

    Dictionary = {}

    while 1 do
        temp = gets(fn)
        if atom(temp) then
            exit
        end if

       -- if temp[length(temp)] = '\n' then
       --     temp = temp[1..length(temp) - 1]
       -- end if

        while find('\n',temp)+find('\r',temp) do   -- (1)
            temp = temp[1..length(temp) - 1]
        end while

        Dictionary = append(Dictionary, upper(temp))
    end while

    close(fn)
end procedure


global function FindMatchingWords(sequence dictionary, sequence string)
    sequence words

    words = {}

    string = upper(string)                        -- (2)
    if not find('*',string) then string &= "*"
    end if

    for i = 1 to length(dictionary) do
        if wildcard_match(string, dictionary[i]) then
            words = append(words, dictionary[i])
        end if
    end for

    return words
end function


global function IsWordInDictionary(sequence dictionary, sequence word)

--    for i = 1 to length(dictionary) do    -- (3a)
  --      if dictionary[i] = word then        -- err
--       if equal(dictionary[i],word) then -- slow
--          return i
--        end if
--    end for
--    return 0

    return find(upper(word),dictionary) -- (3b) -- faster
end function

new topic     » topic index » view message » categorize

2. Re: Dictionary - StewartML

On Monday 27 October 2003 05:35 pm, you wrote:
>
> Hi Irv,
>
> Thanks for looking at that; i hope to use this myself soon.
>
> I just have one note to add...
>
> When stripping off the end character, wouldnt it be faster
> to simply check the last character:
>
> while temp[length(temp)]=10 do
>     temp = temp[1..length(temp) - 1]
> end while
>
> Something like that?
> You wouldnt need find() that way, which looks at every
> single character in the line.

Yes, it's slightly faster (about 10%). 
Since you have to check for both 13 and 10, the fastest seems to be:
 while temp[length(temp)] < 14 do

Anybody know a better way?

Regards,
Irv

new topic     » goto parent     » topic index » view message » categorize

3. Re: Dictionary - StewartML

Thanks for the suggestions.  I should have realised about the equal 
function, but i just bashed it together in my spare time and i am not in the 
habit of having to use 'equal' (im a BASIC programmer mostly) and overlooked 
it.  If i get  some time, i'll upload the changed version.

My main project at the moment is a scheduler to help treat procedures as 
seperate processes.  its based on the langwar idea only it is more 
comprehensive.  email me (stewart[PISSOFFSPAMMERS]ml89 at msn.com - remove the 
stuff in '[]') if you like the idea and any suggestions are welcome.


>From: Irv <irvm at ellijay.com>
>Reply-To: EUforum at topica.com
>To: EUforum at topica.com
>Subject: Dictionary - StewartML
>Date: Mon, 27 Oct 2003 16:50:22 -0500
>
>
>Thanks for  the wordlist, it will be useful for some games.
>I have a few comments about dictionary.e which may help someone.
>
>1. for platform independence, remove both \n and \r when reading the text 
>file
>
>2. better to upcase the search word, and add an '*' if user forgets, 
>otherwise
>     wildcard match will fail even if the word is in the list.
>
>3 a. if dictionary[i] = word won't work, because you will be trying to 
>compare
>words of different lengths. equal() works, but there's a better way.
>
>3b. find() is faster than a loop, except for very short lists of words.
>       finding 'ZOO' with a loop takes 5.74 seconds,
>       finding 'ZOO' with find() takes 2.17 seconds (1000 repetitions)
>
>See mods below.
>Regards,
>Irv
>
>include wildcard.e
>
>global sequence Dictionary
>
>global procedure LoadDictionary(sequence path)
>     integer fn
>     object temp
>
>     fn = open(path, "r")
>     if fn = -1 then
>         printf(2, "Error opening the dictionary '%s'\n",{path})
>         abort(1)
>     end if
>
>     Dictionary = {}
>
>     while 1 do
>         temp = gets(fn)
>         if atom(temp) then
>             exit
>         end if
>
>        -- if temp[length(temp)] = '\n' then
>        --     temp = temp[1..length(temp) - 1]
>        -- end if
>
>         while find('\n',temp)+find('\r',temp) do   -- (1)
>             temp = temp[1..length(temp) - 1]
>         end while
>
>         Dictionary = append(Dictionary, upper(temp))
>     end while
>
>     close(fn)
>end procedure
>
>
>global function FindMatchingWords(sequence dictionary, sequence string)
>     sequence words
>
>     words = {}
>
>     string = upper(string)                        -- (2)
>     if not find('*',string) then string &= "*"
>     end if
>
>     for i = 1 to length(dictionary) do
>         if wildcard_match(string, dictionary[i]) then
>             words = append(words, dictionary[i])
>         end if
>     end for
>
>     return words
>end function
>
>
>global function IsWordInDictionary(sequence dictionary, sequence word)
>
>--    for i = 1 to length(dictionary) do    -- (3a)
>   --      if dictionary[i] = word then        -- err
>--       if equal(dictionary[i],word) then -- slow
>--          return i
>--        end if
>--    end for
>--    return 0
>
>     return find(upper(word),dictionary) -- (3b) -- faster
>end function
>

new topic     » goto parent     » topic index » view message » categorize

4. Re: Dictionary - StewartML

On Tuesday 28 October 2003 02:30 pm, you wrote:
>
> Thanks for the suggestions.  I should have realised about the equal
> function, but i just bashed it together in my spare time and i am not in
> the habit of having to use 'equal' (im a BASIC programmer mostly) and
> overlooked it.  If i get  some time, i'll upload the changed version.

We *all* do that - in fact, equal() is one of the most confusing 'features' 
of Euphoria, and there have been numerous suggestions to change it 
so we can use = just like every other language. Unfortunately, it's not likely 
to happen.

> My main project at the moment is a scheduler to help treat procedures as
> seperate processes.  its based on the langwar idea only it is more
> comprehensive.  email me (stewart[PISSOFFSPAMMERS]ml89 at msn.com - remove the
> stuff in '[]') if you like the idea and any suggestions are welcome.

Sounds good. More than once I have needed something like that. 

Irv

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu