scripting vim7 spelling suggestions

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

scripting vim7 spelling suggestions

Chris Allen
Hiya,

I'm trying to write a plugin which handles suggestions for corrections
of mis-spelt words, but I have run into a problem.  It is possible to
find the start of a spelling error using the ][Ss set of commands, but
it appears to be impossible to find the end of a spelling error (I've
found no functions, commands, or expansions which do it).  I think an
expansion would be really nice, actually.

Sadly, the best I have found for picking out the error section is this
hack which splits strings at word boundaries looking for differences
with a fudge for removed words.  Doubtless this algorithm can be
improved, but it seems to me that there should be some way to find the
end of the spelling error, since Vim clearly knows it.

Any help gratefully appreciated, thanks,
Chris Allen

" Extract the changes to string 1 from string 2
function! s:extract_changes(str1, str2)
    let r_val = ""
    let chunks1 = split(a:str1, "\\>")
    let chunks2 = split(a:str2, "\\>")

    while (!empty(chunks1) && !empty(chunks2))
        let c1      = chunks1[0]
        let c2      = chunks2[0]

        if (c1 == c2 || len(chunks1) < 2 || c2 == chunks1[1])
            return r_val
        else
            let r_val = r_val . c2
        endif

        let chunks1 = chunks1[1:-1]
        let chunks2 = chunks2[1:-1]
    endwhile

    return r_val
endfunction
Reply | Threaded
Open this post in threaded view
|

Re: scripting vim7 spelling suggestions

A.J.Mechelynck
----- Original Message -----
From: "Chris Allen" <[hidden email]>
To: <[hidden email]>
Sent: Saturday, August 20, 2005 1:56 AM
Subject: scripting vim7 spelling suggestions


> Hiya,
>
> I'm trying to write a plugin which handles suggestions for corrections
> of mis-spelt words, but I have run into a problem.  It is possible to
> find the start of a spelling error using the ][Ss set of commands, but
> it appears to be impossible to find the end of a spelling error (I've
> found no functions, commands, or expansions which do it).  I think an
> expansion would be really nice, actually.
[...]

Once you are at the start of a misspelling, isn't the end of the current
word also the end of the spelling error?

Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: scripting vim7 spelling suggestions

Chris Allen
On 19/08/05, Tony Mechelynck <[hidden email]> wrote:
> Once you are at the start of a misspelling, isn't the end of the current
> word also the end of the spelling error?

If only it were so simple!  Unfortunately, no, a phrase like "the the"
is an example of a multi-word single English spelling error.  It may
not be a spelling error per se (being a grammatical error), but Vim
does detect it, which is neat.

Sadly, I can not find a way to support such things in a script, so I
have started to work along the lines of your current assumption for
the time being.  It is a whole lot easier than that horrible function
I posted earlier ;)

Thanks,
Chris Allen
Reply | Threaded
Open this post in threaded view
|

Re: scripting vim7 spelling suggestions

Benjamin Esham
In reply to this post by A.J.Mechelynck
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tony Mechelynck wrote:

> Chris Allen wrote:
>
>> I'm trying to write a plugin which handles suggestions for  
>> corrections
>> of mis-spelt words, but I have run into a problem.  It is possible to
>> find the start of a spelling error using the ][Ss set of commands,  
>> but
>> it appears to be impossible to find the end of a spelling error (I've
>> found no functions, commands, or expansions which do it).  I think an
>> expansion would be really nice, actually.
>
> Once you are at the start of a misspelling, isn't the end of the  
> current
> word also the end of the spelling error?

Not necessarily.  For example, I might have "Tony" in my dictionary, but
"Tony's" would still be a misspelling, and the apostrophe breaks the  
word
into two—in this case, there is a spelling error that spans more  
than one
word.  It makes sense when you consider that Vim's definition of a word
isn't the same as a language's definition of a word.  (And as another  
poster
mentioned, "the the" is a spelling error that spans two "real" words,  
for
lack of a better term.)

- --
Benjamin D. Esham
[hidden email]  |  http://bdesham.net  |  AIM: bdesham128
Esperanto, the international language ☆ http://www.lernu.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (Darwin)

iD8DBQFDBqv8zOC3TdZ2u5oRAgJtAJ9XiRRvCfoa4KyC7//QdTzfyftJOACg48XV
zQfrWz1BAu0LNZVcKlQAoh4=
=4W6q
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|

Re: scripting vim7 spelling suggestions

Chris Allen
On 19/08/05, Benjamin Esham <[hidden email]> wrote:
> Not necessarily.  For example, I might have "Tony" in my dictionary, but
> "Tony's" would still be a misspelling, and the apostrophe breaks the
> word into two—in this case, there is a spelling error that
> spans more than one word.

That case, however, is a WORD, which would be easy enough to handle,
since the number of superfluous characters attached to your spelling
error is rather inconsequential (for any language I know, that is).
Handling non-word and non-WORD spelling errors is a bit more
complicated, since a given language might have a multi-word "spelling"
error five or even fifteen words long, for all I know.  I would prefer
not to present a whole line as a replacement "word", however.

Thanks,
Chris Allen
Reply | Threaded
Open this post in threaded view
|

Re: scripting vim7 spelling suggestions

Bram Moolenaar
In reply to this post by Chris Allen

Chris Allen wrote:

> I'm trying to write a plugin which handles suggestions for corrections
> of mis-spelt words, but I have run into a problem.  It is possible to
> find the start of a spelling error using the ][Ss set of commands, but
> it appears to be impossible to find the end of a spelling error (I've
> found no functions, commands, or expansions which do it).  I think an
> expansion would be really nice, actually.

Why don't you use the spellbadword() function?

--
From "know your smileys":
 (:-# Said something he shouldn't have

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: scripting vim7 spelling suggestions

Chris Allen
On 20/08/05, Bram Moolenaar <[hidden email]> wrote:
> Why don't you use the spellbadword() function?

Heh, that's quite amusing.  Despite it's obvious placement I somehow
managed to completely miss it.  Thanks, Bram!

Chris Allen