[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Suggestions
. . -> [Subject]  List of spellcheck suggestions

List of spellcheck suggestions

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Worstje   Netherlands  (899 posts)  [Biography] bio
Date Sun 29 Apr 2012 09:09 AM (UTC)

Amended on Sun 29 Apr 2012 10:04 AM (UTC) by Worstje

Message
(Please please please do not let this feature be in MUSHclient already... I swear I triplechecked the help again!)

We can interact with the spellcheck engine in a number of ways. However, it is thus far impossible to programmatically request the list of possible corrections that you can see when you press Ctrl+J when there's some misspellings in the title bar.

Basically, I am looking for a:

GetSpellCheckCorrections(word)

that acts like:

tprint(GetSpellCheckCorrections("indstry"))

{ 1="industry", 2="industry's", 3="windsurf" } -- Note: most likely corrections at beginning!

Basically I need it for a right-click popup I am making in my mini-window, which will offer to add words to the dictionary and to immediately fix a spelling error. (That said... suppose one does add stuff to the user dict, where can one find what is currently in it? Or get it out again, for that matter? My users are bound to ask...)

Edit:

After a bit of struggling I figured out user stuff is in the spell.sqlite database as well. Aren't SQLite databases supposed to be openable from SQLite explorer and such tools at the same time as MUSHclient has them opened? I couldn't properly open them until I shut MUSHclient down.. which is annoying since it seems the only way to remove stuff from there at present. (Haven't tried internal MUSH SQLite bindings, but I honestly don't want to mess with SQL just to view what stuff I've put into the userdict... :/
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Sun 29 Apr 2012 08:44 PM (UTC)
Message
Quote:
Aren't SQLite databases supposed to be openable from SQLite explorer and such tools at the same time as MUSHclient has them opened
You should read

http://www.sqlite.org/faq.html#q5

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] bio
Date Reply #2 on Sun 29 Apr 2012 09:28 PM (UTC)
Message
Right.

My 'problem' is that I can open the prefs database for example just fine while MUSHclient is running. But other tools have trouble even showing the defined tables in case of the spell database, nevermind trying to add or delete a record. MUSHclient doesn't seem to notice that. Ergo, I feel like something is 'off' compared to the way the other databases behave when accessing with another applicaton.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Mon 30 Apr 2012 01:16 AM (UTC)

Amended on Mon 30 Apr 2012 08:25 PM (UTC) by Nick Gammon

Message
This will do it:


local METAPHONE_LENGTH = 4   -- how many characters of metaphone to get back
local EDIT_DISTANCE = 4      -- how close a word must be to appear in the list of suggestions
local CASE_SENSITIVE = false -- compare case? true or false

local make_upper  -- this becomes the upper-case conversion function, see below

-- sort function for sorting the suggestions into edit-distance order
local function suggestions_compare (word)
   return function (a, b)
     local diff = utils.edit_distance (make_upper (a), word) - 
                  utils.edit_distance (make_upper (b), word) 
     if diff == 0 then
       return make_upper (a) < make_upper (b)
     else
       return diff < 0
     end -- differences the same?
  end -- compareit
end -- function suggestions_compare

function GetSpellCheckCorrections (word)

  -- make a suitable function depending on whether they want case-sensitive or not
  if CASE_SENSITIVE then
    make_upper = function (s) return s end -- return original
  else
    make_upper = function (s) return s:upper () end  -- make upper case
  end -- case-sensitivity test

  uc_word = make_upper (word)  -- convert to upper-case if wanted

  -- path to the spell check dictionaries
  local directory = utils.info ().app_directory .. "spell\\"

  -- open database on disk
  db = assert (sqlite3.open( directory .. "spell.sqlite"))


  -- table of suggestions, based on the metaphone
  local keyed_suggestions = {}
  
  -- get both metaphones
  local m1, m2 = utils.metaphone (word, METAPHONE_LENGTH)
  
  local function lookup_metaphone (m)
    local found = false
    for row in db:rows(string.format ("SELECT name FROM words WHERE metaphone = '%s'", m)) do
      local word = row [1]
      if make_upper (word) == uc_word then
        found = true -- found exact match
        break
      end  -- found
      if utils.edit_distance (make_upper (word), uc_word) < EDIT_DISTANCE then
        keyed_suggestions [word] = true
      end -- close enough
    end
    return found
  end -- lookup_metaphone
  
  -- look up first metaphone
  if lookup_metaphone (m1) then
    db:close ()
    return word, "ok"
  end -- word found

  -- try 2nd metaphone
  if m2 then
    if lookup_metaphone (m2) then
      db:close ()
      return word, "ok"
    end -- word found
  end -- have alternate metaphone

  -- pull into indexed table
  local suggestions = {}
  for k in pairs (keyed_suggestions) do
    table.insert (suggestions, k)
  end -- for 
     
  table.sort (suggestions, suggestions_compare (uc_word))

  db:close ()
  return suggestions

end -- GetSpellCheckCorrections


Test it:


t = GetSpellCheckCorrections ("indstry")
tprint (t)


Output:


1="industry"
2="industry's"
3="windsurf"


This is based on the spellchecker.lua file. Note the 2nd return value is "ok" if it finds an exact match on the metaphone. You may or may not want to keep that. I can't remember what that is for.

[EDIT] Amended the two paths above that return "ok" to close the database first.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] bio
Date Reply #4 on Mon 30 Apr 2012 07:41 AM (UTC)
Message
Sweet. Thank you! I'll study and test it later today. :) I admit I had considered digging into the source to see how it was done, but I wasn't sure how viable it is as in most situations, one would consider a solution like that an implementation specific hack. :-)

Are there any plans to improve the UI for as far the custom user-added spellcheck words are concerned? I'm worried that making it easy to add new words to the dictionary will mean that mistakes are going to happen, and if people cannot easily revert it that's going to be a problem.

(Writing an entire interface just to manage such accidental clicks is a bit beyond the scope of my plugin I'd dare say.)
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,005 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]