在 AHK 中循环选择一个词的同义词

Cycle through synonyms of a selected word in AHK

这是我想要做的:

假设我在任意文本编辑器中有一个句子:

Tim is a happy guy.

如果我的光标刚好放在单词的末尾 - e。 G。 "happy",然后我按下某个热键,脚本应该查看是否有包含单词 "happy"[=35 的同义词池=],如果有,将其替换为列表中的下一个同义词。

因此您可以通过重复按热键来循环显示所有同义词。

同义词列表应该在脚本中,例如:

(
happy
cheerful
jolly
merry
lively
)

我发现 this post 有类似的做法。 (在那里,一个特定的词总是被一个随机的同义词替换)

问题:

  • 是否有已经执行此操作的脚本?
  • 如果没有 - 你会怎么做?

(我还应该提一下,我对 AHK 还很陌生。)

我会使用这样的东西:

#NoEnv
#SingleInstance Force

synonyms=
(
happy,cheerful,jolly,merry,lively
unhappy,sad,down,depressed
calm,quiet,peaceful,still
; ...
)

$F1::
synonyms_found := "" ; empty this variable (erase its content)
Menu, Replace Synonym, Add
Menu, Replace Synonym, deleteAll ; empty this menu
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
Send ^+{Left} ; select the word left to cursor
Sleep, 50
Send ^c ; copy the selected word
ClipWait 0.5 ; wait 0.5 seconds for the clipboard to contain data
if (ErrorLevel) ; If ErrorLevel, clipwait found no data on the clipboard within 0.5 seconds
{
    MsgBox, No word selected
    clipboard := ClipSaved ; restore original clipboard
    return ; don't go any further
}
; otherwise:
Loop, Parse, synonyms, `n,`r ; retrieve each line from the synonyms, one at a time
{   
    If InStr(A_LoopField, clipboard) ; if the retrieved line contains the word copied
    {       
        synonyms_found .= A_LoopField . "," ; concatenate (join) the retrieved lines into a single variable
        Loop, Parse, synonyms_found, `, ; retrieve each word from the retrieved lines
            Menu, Replace Synonym, Add, %A_LoopField%, Replace_Synonym  ; create a menu of the synonym words
    }
}
If (synonyms_found != "") ; if this variable isn't empty
    Menu, Replace Synonym, Show
else
    MsgBox, No synonyms found for "%clipboard%"
Sleep, 300
clipboard := ClipSaved ; restore original clipboard
return

; select a menu item to replace the selected word:
Replace_Synonym:
SendInput, %A_ThisMenuItem%
return