Managing glossaries with the DeepL API

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Managing glossaries with the DeepL API

Post by tiska » 08 Apr 2024, 23:17

This AutoHotkey script utilizes the free DeepL API to translate selected text from German to English. @mikeyww provided significant assistance in coding it:

Code: Select all

; This script translates text.
; Save script as UTF-8 with BOM signature.
; https://www.autohotkey.com/boards/viewtopic.php?f=82&t=127623&p=564800#p564800
; https://developers.deepl.com/docs
#Requires AutoHotkey v2.0
token := token ?? 'My API token'

F8:: {
 A_Clipboard := '', Send('^c')
 If ClipWait(1) {
  If '' != str := translate(A_Clipboard, 'EN')
   SendText str
 } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 'Icon!'
}

translate(str, lang) {
 Static url    := 'https://api-free.deepl.com/v2/translate'
      , regex  := '"text":"(.+?)"'
      , ignore := '«»\'                                                   ; Special characters to be ignored
 Loop Parse ignore
  str := StrReplace(str, A_LoopField, '<ignore>' A_LoopField '</ignore>') ; Add XML tags for special chars
 str  := Trim(RegExReplace(str, '\R', ' '))                               ; Change line breaks to spaces
 str  := StrReplace(str, '\', '\\')                                       ; Fix backslash for XML
 ; MsgBox str, 'Input'
 json := '{"text":["' str '"],"target_lang":"' lang '", "tag_handling": "xml", "ignore_tags": ["ignore"]}'
 whr  := ComObject('Msxml2.XMLHTTP.6.0')
 whr.Open('POST', url, False)
 whr.SetRequestHeader('Content-Type', 'application/json')
 whr.SetRequestHeader('Authorization', 'DeepL-Auth-Key ' token)
 whr.Send(json)
 status := whr.status
 ; MsgBox whr.ResponseText, 'Output'
 If RegExMatch(whr.ResponseText, regex, &m) {
  str := RegExReplace(m[1], '<ignore>(.+?)</ignore>', '$1')               ; Strip XML tags for special chars
  Return StrReplace(str, '\\', '\')                                       ; Restore backslashes
 }
}
I need help incorporating the DeepL glossary management feature into the current script. Specifically, with adding the following functions: creating a glossary, listing existing glossaries, retrieving glossary details, retrieving glossary entries, and deleting glossaries. Here's the documentation page for managing glossaries: https://developers.deepl.com/docs/api-reference/glossaries.


Below is a rough idea of what the glossary management code should do:

Code: Select all

; Create a new glossary
^g::
    ; Make an HTTP request to create a glossary
    ; Glossary creation logic
    MsgBox, Creating a new glossary...
return

; List all created glossaries
^l::
    ; Make an HTTP request to list glossaries
    ; Glossary listing logic
    MsgBox, Listing glossaries...
return

; Get details about a specific glossary
^d::
    ; Make an HTTP request to get glossary details
    ; Glossary details retrieval logic
    MsgBox, Retrieving glossary details...
return

; List entries of a glossary
^e::
    ; Make an HTTP request to list glossary entries
    ; Glossary entries listing logic
    MsgBox, Listing glossary entries...
return

; Delete a glossary
^x::
    ; Make an HTTP request to delete a glossary
    ; Glossary deletion logic
    MsgBox, Deleting glossary...
return

User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Re: Managing glossaries with the DeepL API

Post by tiska » 10 Apr 2024, 02:55

Would it make sense to include the Node.js Client Library for the DeepL API in the script, and if so, how would one go about implementing it? https://github.com/DeepLcom/deepl-node

User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Re: Managing glossaries with the DeepL API

Post by tiska » 11 Apr 2024, 07:06

After many hours of reading and tinkering, and with a little help from OpenAI, I have come up with this script. However, it still does not work and as I am an AHK NOOB and still don't know what I don't know, I'd love some help with this 8-)

[Mod edit: Removed AI code which was a broken mix of v1 and v2 code, according to the forum rules.]

gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: Managing glossaries with the DeepL API

Post by gregster » 11 Apr 2024, 10:59

Please don't post broken AI code - which is not allowed according to the forum rules.
The code you posted contained, for example, larger parts of v1 code mixed with v2 code. Current AI systems are simply not capable of writing complex v2 scripts and are usually not a good starting point but a waste of everyone's time.

I understand that it can be frustrating to be stuck, but it seems that your script request exceeds your current abilities and our voluntary helpers don't have unlimited capacities and motivation to fulfill larger script requests at any moment.
I would recommend to tackle one problem at a time and to start debugging your script systematically with help of the v2 docs and incremental help requests in the forums. That's probably a better way to learn v2 and more sustainable than relying on the currently wonky AI systems.

User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Re: Managing glossaries with the DeepL API

Post by tiska » 12 Apr 2024, 02:31

Gregster, I value your feedback, but I feel it could have been delivered in a more constructive manner. Instead of dismissing others' efforts outright, why not offer guidance or assistance? We're all here to learn and improve, and a supportive community fosters growth for everyone involved. Regarding the code I posted, I had spent considerable time researching AHK 2 and crafting the majority of the new version myself. I only utilized AI to aid in updating it to version 2, which you promptly removed. While I appreciate moderation, this level of control feels excessive and isn't particularly motivating.

gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: Managing glossaries with the DeepL API

Post by gregster » 12 Apr 2024, 03:49

Imo, the community offers a great deal of guidance and assistance - and all helpers (including the moderators and admins) are pure volunteers who spend lots of hours to keep these forums running. And it is still there for you, if you decide to use it.
But we also have to take into account the motivation of the helpers - that's why we have a certain set of rules. The specific AI rule was at least partially introduced based on feedback of the regular helpers of this community; it became evident that current AI systems are simply not built to create good AHK code, especially (but not limited to) v2 code. (This might change at some point in the future.) In this case, I simply didn't see a justifiable reason for an exception from the rule.

As I said, I would recommend a more incremental approach. Tackle one problem at a time, and feel free to ask the community if you get stuck. When you get it working, expand the script, but don't add much more code to an already broken script.
People will be much more inclined to help with specific problems than debugging a larger script. I generally try to help myself if I find the time - but my forum activities also include certain background tasks which are not always visible for regular users (of course, in constant cooperation with the other currently active staff members).

User avatar
boiler
Posts: 17244
Joined: 21 Dec 2014, 02:44

Re: Managing glossaries with the DeepL API

Post by boiler » 12 Apr 2024, 05:07

tiska wrote: Gregster, I value your feedback, but I feel it could have been delivered in a more constructive manner.
Frankly, I am quite surprised at this comment. Where he could have simply removed the code which violates the forum rules as a standard moderation action, he took the time to add a thoughtful explanation, which he had no obligation to do.

tiska wrote: Instead of dismissing others' efforts outright, why not offer guidance or assistance?
This is unfair comment, especially given the person to whom it is addressed. There are a lot of tasks that the unpaid, volunteer moderators perform daily that the members don’t see, without which this forum would become a mess in less than a day. It would be understandable if they only did these tasks to keep the forum functioning for users like yourself. So the first responsibility of a moderator is to do that.

You may see the list of moderators at the bottom of the main forum page and think that no one moderator needs to do much, but the reality is that very few are actively performing moderation tasks on the forum on a daily basis if at all. The moderator you are addressing happens to be the most active moderator on the forum, positively affecting literally dozens of posts every day, so the effort from this individual cannot be questioned. As he mentioned, he is also a frequent helper. It cannot be expected that for every thread in which he takes a moderating action that he also takes time to “offer guidance or assistance,” which he did in your case, by the way. You are just failing to see it as such.

User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Managing glossaries with the DeepL API

Post by Seven0528 » 12 Apr 2024, 05:44

 Separate from the above, I have doubts about whether AHK is the optimal tool for this task.
While AutoHotkey can certainly do many things, it may not be as versatile as other languages when it comes to other fields besides keyboard control, as its name suggests (it's called Auto"Hotkey" after all).
Especially if one is not familiar with a specific field, it would be more advisable to choose a different code, especially if there are already example codes available.

Although I mostly use AHK for programming, if implementation is difficult or inefficient with AHK, I use C++ or C#.
When I checked the DeepL website, I found example code in Python and C#.
If you know AHK to some extent, you'll likely find it not too difficult to learn other languages.

That applies not only to coding but to everything you need to learn. There are no shortcuts in learning.
Good luck, @tiska.
Last edited by Seven0528 on 12 Apr 2024, 06:22, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: Managing glossaries with the DeepL API

Post by gregster » 12 Apr 2024, 05:59

it may not be as versatile as other languages when it comes to keyboard control, as its name suggests
I always thought that AHK is pretty good for keyboard control, but I don't see how this is related to the original request.

The request seems to be about using a specific WebAPI via https requests. This has been done before for various WebAPIs, and I don't see yet why AHK shouldn't be suited in this case.
I admit, I haven't looked closely what the API calls are exactly meant to do and how glossaries are used for translation requests, but this is probably less relevant than applying the correct syntax. Perhaps I am missing something 🤷‍♂️...
Anyway, WebAPI requests can surely be done by the vast majority of programming languages used today.

User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Managing glossaries with the DeepL API

Post by Seven0528 » 12 Apr 2024, 06:15

 @gregster
OMG, my English mistake.
My English skills aren't perfect, so I rely somewhat on a translator when writing English sentences.
I made a silly mistake though.
I have edited the sentence I wrote earlier. Thank you for correcting my English mistake.
I am aware that it can be done with AHK, but if there are no example codes written in AHK or existing libraries,
starting with another language can also be a good starting point.
Just like AHK is useful, other languages can be too.
While AutoHotkey can certainly do many things, it may not be as versatile as other languages when it comes to other fields besides keyboard control, as its name suggests (it's called Auto"Hotkey" after all).
Last edited by Seven0528 on 12 Apr 2024, 06:24, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: Managing glossaries with the DeepL API

Post by gregster » 12 Apr 2024, 06:23

I see. Well, the DeepL docs seem to provide cURL examples.
Iirc, there should be one or two libcurl wrappers for AHK v2. Perhaps this could simplify the web requests.

Post Reply

Return to “Ask for Help (v2)”