Dictionary - StewartML
- Posted by Irv <irvm at ellijay.com> Oct 27, 2003
- 361 views
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